Search code examples
javajodatimedrools

Drools: Joda DateTime comparison


I'm setting up some Drools rules which are time-dependent, i.e. if the supplied time is between, say 8am and 5pm, then a certain result needs to be returned. A different one should be returned outside these times.

The object I'm passing in has a Joda DateTime which I'm needing to use in the rule.

At the moment, my approach consists of trying to format this Joda DateTime object into a 24-hour numeric value which I can then use an integer comparison on (i.e. > 800 and < 1700). I couldn't think of any other way of doing it since I have searched for examples but found few mentions of timestamp comparison in Drools, unfortunately.

Thanks in advance for any assistance.


Solution

  • Forget Drools temporal operators and timestamps: these are meant to be used in combination with events - CEP (Complex Event Processing). Whatever your objects describe, you need to test a simple numeric value that just happens to be derived from a time-and-date value, which is secondary to your issue.

    Best way I can think of is to write a Drools function along the lines of

    function boolean isBetween( DateTime dt, int start, int end ){
        int hhmm = dt.hourOfDay()*100 + dt.minuteOfDay();
        return start <= hhmm && hhmm < end;
    }
    

    and use it in a constraint

    GarlicBreadObject( $dt: itsDateTime, isBetween( $dt, 800, 1700 ) )