Challenge! Using javassist and drools presents an issue with drl

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Challenge! Using javassist and drools presents an issue with drl

markricard
In order to make writing drl files easy and not requiring writers to be developers, I have written some JavaAssist code that dynamically creates a subclass and new methods in memory.  

If for example there is a REAL java class called 'com.foo.Instance'.  Via javasssist, I create a new in-memory subclass called 'com.foo.InstanceEx'.   The new class dynamically creates a new getter called getFoo().    The drl writer would like to directly reference 'foo' in the drl file like so:

when
     i : Instance(foo == 'fee')

The problem is, in order to reference foo, I would need the drl file to do this:

import com.rrd.xspace.drools.InstanceEx;

But I cannot do that because InstanceEx does not exist at runtime.   The error I get, which is an obvious one, is "Unable to create Field Extractor for 'foo' of '[ClassObjectType class=com.foo.Instance]' in rule 'Test' : [Rule name='Test']"

Is there a way to include a dynamic import of a JavaAssist class while I am constructing the KnowledgeBase instances to run the rules so that I can reference that new method?

I would hope to have something similar to:

KnowledgeBuilder kbuilder = ....
kbuilder.addImport("com.foo.InstanceEx")

or

kbuilder.addClass(Class.forName("com.foo.InstanceEx"))


Any help GREATLY appreciated.
Reply | Threaded
Open this post in threaded view
|

Re: Challenge! Using javassist and drools presents an issue with drl

markricard
Perhaps there is an easier way to ask this question....

Rather than having to do an 'import' of a class in a drl text file, is there a way in the Java code that constructs the KnowledgeBase to via java dynamically import any class file required by any drl file that is added?

Instead of the drl having:

import com.foo.Instance;
rule "Walgreens Test"
    ....

It just has:

rule "Walgreens Test"
    ....


And in the KnowledgeBuilder does something like this:

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.addImport("com.foo.Instance");
...


Reply | Threaded
Open this post in threaded view
|

Re: Challenge! Using javassist and drools presents an issue with drl

Ayush
This post was updated on .
Folks,

I'm also facing this same issue. I tried many things such as creation session object only after I created dynamic classes using javassist CtClass, changing rules, etc. but none of the solution is working.

As per my observation the rules are accepting the dynamic class as long as I'm writing the rule as a single rule but the moment I'm splitting the rule in a function, I'm getting the following errors:
1) Unable to resolve ObjectType 'Transac_EE_TDR'
2) Error importing : 'defaultpkg.ProcessEE_TPS.processEE_TPS' 

Here Transac_EE_TDR is my dynamic class and 'processEE_TPS' is the function name.

I will appreciate if someone can help me resolve it. Thanks!

BR
Ayush
Reply | Threaded
Open this post in threaded view
|

Re: Challenge! Using javassist and drools presents an issue with drl

Ayush
Folks,

It's a burning issue for me, I would appreciate if someone can help me solving it.