[rules-users] Easy mistake...

Willem van Asperen willem at van.asperen.org
Mon Feb 18 05:11:30 EST 2013


Hi All,

Just wanted to share with you an easy mistake. This is done running
Drools 5.5.0.Final.

Imagine a fact

|Cheese (||
||    creationTick : double||
||    preservationTicks : double||
||)|

Then the following does not do what you expect:

|rule "dump old cheese"||
||    when||
||        $c : Cheese( $sbd : creationTick + preservationTicks )||
||        CurrentTime( tick >= $sbd )||
||    then||
||        System.out.println("||we have to dump "+$c);||
||end|

You would expect that $c is dumped when current time has passed
creationTick + preservationTicks. But no. The variable $sbd is bound to
creationTick /before/ the preservationTicks are added!! I must say that
I do not quite understand how ($sbd : creationTick) + preservationTicks
resolves to "true" to make the premise succeed... Maybe because it is != 0?

I found that it should be:

|rule "dump old cheese"||
||    when||
||        $c : Cheese( $sbd : (creationTick + preservationTicks) )||
||        CurrentTime( tick >= $sbd )||
||    then||
||        System.out.println("we have to dump "+$c);||
||end|

This makes sense. Now $sbd is bound to the result of the addition. But
it is an easy trap!!

Regards,
Willem
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20130218/2c462841/attachment.html 


More information about the rules-users mailing list