It appears that you MUST use the From Condition Element to reason over
sub-objects.
I have a Person class. Person::getDetails() returns a Details
instance, which has the name and age of the person. (This is a
contrived example to demonstrate the issue.)
My rules are:
rule "30 is the new 20"
when
person : Person( details.age == 30 )
then
person.getDetails().setAge(20);
System.out.println( "Now 20 : " + person );
update( person );
end
rule "Older than 20 - Good"
salience -100
when
person : Person( )
Details( age > 20 ) from person.details
then
System.out.println( "Older than 20 (good) : " + person );
end
rule "Older than 20 - Bad"
salience -100
when
person : Person( details.age > 20 )
then
System.out.println( "Older than 20 (bad) : " + person );
end
I assert Abe, Bob, Cat, Don, and Eve with ages of 10, 20, 30, 40, and
50, respectively. The output is as follows.
Now 20 : Person(Details(Cat,20))
Older than 20 (good) : Person(Details(Eve,50))
Older than 20 (bad) : Person(Details(Eve,50))
Older than 20 (good) : Person(Details(Don,40))
Older than 20 (bad) : Person(Details(Don,40))
Older than 20 (bad) : Person(Details(Cat,20))
You can see that the "Bad" rule is more concise but it does not use
the From Conditional Element and therefore it doesn't work properly
(Cat is determined to be older than 20 when she is not.)
Why does Drools allow the "Bad" rule to be written and compiled when
it does not behave properly?
Thanks,
Aaron