[rules-users] How to pass a "global" reference date to rules

Wolfgang Laun wolfgang.laun at gmail.com
Tue Feb 5 09:38:30 EST 2013


On 05/02/2013, pdario <dario.piantanida at gmail.com> wrote:
> I see, I'll have a look at the classes you mention.
>
> But what if I need to calculate a time difference from today to another
> date
> (say "order expire date")?
> Would a "new Date()" in the rules give me the "pseudo" time?

java.util.Date is not the session clock. The default clock's
getCurrentTime returns System.currentTimeMillis() and the pseudo-clock
returns whatever you set it to.

You'll have to base all your calculations with dates in fact data on the
value returned by the session clock. I suppose you'll want to do this
in RHS code, so this is one approach:

global MyClock myClock;

rule "x"
when
    $d : Date() // or (... $d: orderExpireDate,... )
then
    System.out.println( $d + " diff in days: " + myClock.daysTo( $d ) );
end

This is a primitive implementation of MyClock:

public class MyClock {
    private static final long MS_PER_DAY = 24*60*60*1000;
    public static SessionClock sessionClock;
    public MyClock( SessionClock sessionClock ){
        this.sessionClock = sessionClock;
    }
    public int daysTo( Date d ){
        long dTime = d.getTime();
        int dDays = (int)((dTime - dTime % MS_PER_DAY)/MS_PER_DAY);
        long sTime = sessionClock.getCurrentTime();
        int sDays = (int)((sTime - sTime % MS_PER_DAY)/MS_PER_DAY);
	return dDays - sDays;
    }
}



>
> Thank you!
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/How-to-pass-a-global-reference-date-to-rules-tp4022057p4022066.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>


More information about the rules-users mailing list