using Groovy as fact for rules

440 views
Skip to first unread message

Jochen Theodorou

unread,
Dec 23, 2015, 5:27:25 AM12/23/15
to Drools Usage
Hi,

I was trying out the following simple program to do some tests with drools 6.3.0.Final:

// the rule
def drl = '''
dialect "mvel"

rule "Person is over 18"
    when
        $person : Person(age > 18)
    then
        System.out.println("Person is "+$person.name);
end
'''
// setup for rule
KieServices kieServices = KieServices.Factory.get()
KieFileSystem kfs = kieServices.newKieFileSystem()
kfs.write( "src/main/resources/simple.drl",
    kieServices.getResources().newReaderResource( new StringReader(drl) ) )
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll()

// check there have been no errors for rule setup
Results results = kieBuilder.getResults();
if( results.hasMessages( Message.Level.ERROR ) ){
    println( results.getMessages() )
    throw new IllegalStateException( "### errors ###" )
}
KieContainer kieContainer =
    kieServices.newKieContainer( kieBuilder.getKieModule().getReleaseId() )
KieSession kieSession = kieContainer.newKieSession()

// insert facts and fire rules
kieSession.insert(new Person("Jon Doe", 21))
kieSession.insert(new Person("Jon Darcy", 1))
kieSession.fireAllRules()

kieSession.dispose()

@Immutable
class Person {
    String name
    int age
}
I managed to get a rule like this firing:
rule hello
when then 
  System.out.println( "Hello!" );
end

But the rule "Person is over 18" above never fires. I can see by using eventlistener and logger, that adding the Person objects asserts them. I can ensure the rule is actually seen, by changing it to something invalid.But I cannot get the rule to fire. I also found out, that if I use "normal" java classes, that this works. My testing also shows that the Person class is found by Drools and the properties are seen. Still it does not work. Can it be, that there is something in the layout of the Person class that gives drools a problem? For example the metaClass property Groovy adds to every class? Or any of the synthethic methods Groovy adds? Is anybody aware of such problems? Or is this a bug in Drools 6.3.0.FINAL and a different version would work? If yes which?

Thanks for helping,
Jochen


Jochen Theodorou

unread,
Jan 4, 2016, 4:49:54 AM1/4/16
to Drools Usage
Since I did not get an answer I have to assume nobody here knows... is there a better list to ask this question? Is that supposed to work even? Should I do a bug report?

Mark Proctor

unread,
Jan 4, 2016, 6:55:14 AM1/4/16
to drools...@googlegroups.com
As long as the classes groovy produces are pojos with static properties, it will work. If they are using dynamic properties it will not.

Mark
-- 
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/4252d0c6-9939-410d-890b-25860d1370fb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jochen Theodorou

unread,
Jan 4, 2016, 8:32:22 AM1/4/16
to Drools Usage
Well, the class Person in the example I gave has a name (String) and an age (int) property (Groovy generates the getter and setter). There is actually a metaClass property added through Groovy as well, not sure if that plays a role. There seems to be no hint as to why this is not working. 

Am Montag, 4. Januar 2016 12:55:14 UTC+1 schrieb Mark Proctor:
As long as the classes groovy produces are pojos with static properties, it will work. If they are using dynamic properties it will not.

Mark
[...]

Mark Proctor

unread,
Jan 4, 2016, 9:13:09 AM1/4/16
to drools...@googlegroups.com
If it’s a JDK complain bean and works with the Introspector, it should work.

If it’s not working, maybe a classpath problem?

Mark
--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.

Jochen Theodorou

unread,
Jan 4, 2016, 11:20:00 AM1/4/16
to Drools Usage
classpath problem? You mean another Person class? Well, I ensured that is not the problem by changing the property name and then getting an error from Drools. The only other possibility I could then imagine is class duplication.. I mean two defining classloaders for the same class name. In my case above I know that the classloader knowing Person is the same class loader, that executes the drools controlling code. The Person class does in this case not exist as class on the file system, it only exists in the class loader. Now I am a beginner in Drools (and failing here already so badly ;) ), I have no idea how Drools is doing class resolving or if this could be a problem here. Nor do I have any idea how to exclude this as a problem. Is there a way?

Mike Rodriguez

unread,
Jan 5, 2016, 12:12:44 AM1/5/16
to Drools Usage
Have you tried using the Janino compiler instead of the default eclipse one or whatever it is. I know Janino at least used to be the one that worked for loading classes that were not from files. However I thought that caused errors for me not just rules not matches on type.

Jochen Theodorou

unread,
Jan 5, 2016, 8:12:27 AM1/5/16
to Drools Usage
Since it is a Groovy script we are talking about it is neither Eclipse nor Janino that play a role here. You can basically take my code, put it in file and execute it on the command line using Groovy (after adding Drools to the classpath of course). 

Mike Rodriguez

unread,
Jan 5, 2016, 8:58:54 AM1/5/16
to Drools Usage
Eclipse vs Janino compilers are the compilers used within Drools when compiling the knowedge base. So I don't think that has anything to do with how you're running your script etc.

Georgios Papageorgiou

unread,
Mar 9, 2016, 10:14:31 AM3/9/16
to Drools Usage
Hello Jochen,

I had the same problem and i resolved it after deleting the @Immutable reference from my Groovy Fact. I also use Drools 6.3.0 final.

George
Reply all
Reply to author
Forward
0 new messages