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

Matthew Erler wires8 at yahoo.com
Wed Aug 24 18:48:54 EDT 2011


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();
} 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20110824/046cc376/attachment.html 


More information about the rules-users mailing list