[rules-users] How to pass global variables to a function called in a condition

Esteban Aliverti esteban.aliverti at gmail.com
Thu Aug 25 05:35:16 EDT 2011


Translation: If the LHS of your rules are going to use globals, make sure to
set them BEFORE you insert/update/retract any fact. :P

Best Regards,

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Esteban Aliverti
- Developer @ http://www.plugtree.com
- Blog @ http://ilesteban.wordpress.com


2011/8/25 Wolfgang Laun <wolfgang.laun at gmail.com>

> Conditions are evaluated when facts are inserted, modified and deleted.
> -W
>
>
> 2011/8/25 Matthew Erler <wires8 at yahoo.com>
>
>> In Drools 5 I'm passing in two global Date objects and need to find the
>> difference in days between them.  I've discovered that the function
>> parameters (fromDate and toDate in my case) are null in the condition when
>> the function is called, but when the function is called in the consequence
>> they have values.  I can see that because the output of the following rule
>> is:
>> fromDate is null
>> toDate is null
>> I'm in dateDiff and the date diff is: 14
>>
>> I'm guessing that Drools doesn't make globals t available at the point
>> that I need them.  Can anyone confirm this and offer a solution?
>>
>> Here is the rule file itself:
>>
>> import java.util.Date;
>> import org.joda.time.DateMidnight;
>> import org.joda.time.Days;
>>
>> global java.util.Date fromDate;
>> global java.util.Date toDate;
>>
>>
>> rule "dateDiff"
>> dialect "mvel"
>>      when
>>           eval(getDaysBetweenStartAndEndDate(fromDate, toDate) < 10)
>>      then
>>           System.out.println( "I'm in dateDiff and the date diff is: "
>> +        getDaysBetweenStartAndEndDate(fromDate, toDate));
>> end
>>
>>
>> function int getDaysBetweenStartAndEndDate(Date fromDate, Date toDate) {
>>     if (fromDate==null) System.out.println("fromDate is null");
>>     if (toDate==null) System.out.println("toDate is null");
>>   return Days.daysBetween(new DateMidnight(fromDate), new
>> DateMidnight(toDate)).getDays();
>> }
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20110825/1be870ab/attachment.html 


More information about the rules-users mailing list