Getting a handle of POJOs inside my kjar

332 Views Asked by At

I just set up a kie-workbench (6.1.0 Final) on tomcat and created an example demo-project which contains a drl file and a big flat POJO created with the data modeller. I built and deployed the demo-project and managed to fire the rules from a client application using the code below:

String url = "http://yytomcat7kie.domain.com:8080/kie/maven2/gro/up/demoproject/0.0.3/demoproject-0.0.3.jar";

    ReleaseIdImpl releaseId = new ReleaseIdImpl("gro.up", "demoproject", "0.0.3");

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();

    UrlResource urlResource = (UrlResource) ResourceFactory.newUrlResource(url);

    kfs.write(urlResource);

    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();

    KieContainer kContainer = ks.newKieContainer(releaseId);

    KieSession kSession = kContainer.newKieSession();
    SessionConfiguration sConf = (SessionConfiguration)kSession.getSessionConfiguration();

    MyKiePojo kiePojo = new MyKiePojo();
    kiePojo.setField01("blah");
    kiePojo.setField02("blahblah");
    kiePojo.setField03("blahblahblah");

    kSession.insert(kiePojo);

    kSession.fireAllRules();

    System.out.println(" ALL RULES FIRED ");
    System.out.println(kiePojo.getField04());
    System.out.println(kiePojo.getField05());

It works fine but the question I have now is: Is it possible to get a handle of the MyKiePojo class which is in the demoproject.jar without having it in the client app's classpath? Ideally I would like to keep all my models in the workbench without having to mirror them in the client app and be able to instantiate them and populate them with values received from rest requests. Is this possible?

2

There are 2 best solutions below

1
On BEST ANSWER

A KieContainer when used with dynamic modules keeps all the jars it loads in an isolated ClassLoader. So you can put your models into their own jar and specify them as a maven dependency on the project being deployed. If you are using kie-ci it will resolve the transitive dependencies and build a ClassLoader from them.

Externally you can use reflection to access the pojos in that CassLoader, or you can have an initialisation rule that calls out to a static initialisation method. Where that static initializer method is any class in the jar or one of the dependant jars.

What we don't have yet is a life cycle for KieContainers and KieSession to automate certain things via callbacks. This is definitely something we need to look into, and I expect it to be in the next (after 6.2) release.

0
On

See the documentation chapter "Rule Language Reference", section "Type Declaration". A quick example taken from there:

declare Address
   number : int
   streetName : String
   city : String
end

You can create objects using new and use getters and setters etc.

You'll have to code the transformation from the request to this object.