Discussion:
Challenge! Using javassist and drools presents an issue with drl
markricard
2010-04-28 16:40:29 UTC
Permalink
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.
--
View this message in context: http://drools-java-rules-engine.46999.n3.nabble.com/Challenge-Using-javassist-and-drools-presents-an-issue-with-drl-tp763086p763086.html
Sent from the Drools - User mailing list archive at Nabble.com.
markricard
2010-04-28 16:53:12 UTC
Permalink
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");
...
--
View this message in context: http://drools-java-rules-engine.46999.n3.nabble.com/Challenge-Using-javassist-and-drools-presents-an-issue-with-drl-tp763086p763118.html
Sent from the Drools - User mailing list archive at Nabble.com.
Ayush
2012-05-15 11:25:34 UTC
Permalink
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 function() but the moment I'm splitting the
rules 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

--
View this message in context: http://drools.46999.n3.nabble.com/Challenge-Using-javassist-and-drools-presents-an-issue-with-drl-tp763086p3987828.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
Ayush
2012-05-16 03:20:42 UTC
Permalink
Folks,

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

--
View this message in context: http://drools.46999.n3.nabble.com/Challenge-Using-javassist-and-drools-presents-an-issue-with-drl-tp763086p3994285.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
Dave Cracauer
2010-04-28 18:34:30 UTC
Permalink
It sounds like the stated goal is to make things easier for non-developers to write rules. What about a dsl?

What about using a templating language? I have used velocity in the past for dynamically generating rules based on templates, and I think that there is built in support for templates using mvel.

-----Original Message-----
Sent: Apr 28, 2010 11:53 AM
Subject: Re: [rules-users] Challenge! Using javassist and drools presents an issue with drl
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?
import com.foo.Instance;
rule "Walgreens Test"
....
rule "Walgreens Test"
....
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.addImport("com.foo.Instance");
...
--
View this message in context: http://drools-java-rules-engine.46999.n3.nabble.com/Challenge-Using-javassist-and-drools-presents-an-issue-with-drl-tp763086p763118.html
Sent from the Drools - User mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
https://lists.jboss.org/mailman/listinfo/rules-users
Swindells, Thomas
2010-04-29 08:38:27 UTC
Permalink
Isn't the simplest solution just to add the imports at the start of the imported drl file?
That is the users don't need to worry about writing the imports but before you feed the files into your code simply prepends the required imports.

You can do this easily in memory with something like the following:
StringWriter drlContents = new StringWriter();
drlContents.write("import ...\n");
FileInputStream stream = new FileInputStream(drlFile);
InputStreamReader inR = new InputStreamReader(stream, "UTF-8");
IOUtils.copy(inR, drlContents);
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drlContents.toString())), ResourceType.DRL);

Thomas
Post by Dave Cracauer
-----Original Message-----
Sent: 28 April 2010 19:35
Subject: Re: [rules-users] Challenge! Using javassist and drools presents an issue with drl
It sounds like the stated goal is to make things easier for non-developers to
write rules. What about a dsl?
What about using a templating language? I have used velocity in the past for
dynamically generating rules based on templates, and I think that there is
built in support for templates using mvel.
-----Original Message-----
Sent: Apr 28, 2010 11:53 AM
Subject: Re: [rules-users] Challenge! Using javassist and drools presents an
issue with drl
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?
import com.foo.Instance;
rule "Walgreens Test"
....
rule "Walgreens Test"
....
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.addImport("com.foo.Instance");
...
--
View this message in context: http://drools-java-rules-
engine.46999.n3.nabble.com/Challenge-Using-javassist-and-drools-presents-an-
issue-with-drl-tp763086p763118.html
Sent from the Drools - User mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
https://lists.jboss.org/mailman/listinfo/rules-users
_______________________________________________
rules-users mailing list
https://lists.jboss.org/mailman/listinfo/rules-users
**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the ***@nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************

This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the ***@nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes.
To protect the environment please do not print this e-mail unless necessary.

An NDS Group Limited company. www.nds.com
Mark Proctor
2010-09-28 15:26:22 UTC
Permalink
Post by Swindells, Thomas
Isn't the simplest solution just to add the imports at the start of the imported drl file?
That is the users don't need to worry about writing the imports but before you feed the files into your code simply prepends the required imports.
StringWriter drlContents = new StringWriter();
drlContents.write("import ...\n");
FileInputStream stream = new FileInputStream(drlFile);
InputStreamReader inR = new InputStreamReader(stream, "UTF-8");
IOUtils.copy(inR, drlContents);
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drlContents.toString())), ResourceType.DRL);
Make sure you generate the classes first and put them into a
classloader. Then specify that ClassLoader as the root classload to the
kbuilder and kbase configurations.

Mark
Post by Swindells, Thomas
Thomas
Post by Dave Cracauer
-----Original Message-----
Sent: 28 April 2010 19:35
Subject: Re: [rules-users] Challenge! Using javassist and drools presents an
issue with drl
It sounds like the stated goal is to make things easier for non-developers to
write rules. What about a dsl?
What about using a templating language? I have used velocity in the past for
dynamically generating rules based on templates, and I think that there is
built in support for templates using mvel.
-----Original Message-----
Sent: Apr 28, 2010 11:53 AM
Subject: Re: [rules-users] Challenge! Using javassist and drools presents an
issue with drl
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?
import com.foo.Instance;
rule "Walgreens Test"
....
rule "Walgreens Test"
....
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.addImport("com.foo.Instance");
...
--
View this message in context: http://drools-java-rules-
engine.46999.n3.nabble.com/Challenge-Using-javassist-and-drools-presents-an-
issue-with-drl-tp763086p763118.html
Sent from the Drools - User mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
https://lists.jboss.org/mailman/listinfo/rules-users
_______________________________________________
rules-users mailing list
https://lists.jboss.org/mailman/listinfo/rules-users
**************************************************************************************
NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************
To protect the environment please do not print this e-mail unless necessary.
An NDS Group Limited company. www.nds.com
_______________________________________________
rules-users mailing list
https://lists.jboss.org/mailman/listinfo/rules-users
Loading...