Drools: Time restricted rule

Gaim picture Gaim · Feb 11, 2013 · Viewed 7.5k times · Source

Drools documentation mentions that rules can use attributes like date-effective and date-expires to specify an absolute rule validity period.

For example

rule "Date-restricted rule"
    date-effective "20.2.2013 8:00"      # 8 AM
    date-expires   "20.2.2013 16:00"     # 4 PM
    when
    then
end

Drools also supports periodically repeated rules with interval as timer(int:) and cron as timer(cron:) but it means that the rule is fired in such points.

Question:

I am interested if there is any option how to specify periodically available (not fired) rules with time restriction. For example let's image the business hours in some company - the operation can be performed only during official working period but not after hours.

I would like something like this, but it is not Drools valid rule

rule "Time-restricted rule"
    time-effective "8:00"      # 8 AM
    time-expires   "16:00"     # 4 PM
    when
    then
end

Would be possible to extend such rule to be active only Monday to Friday 8 AM to 4 PM?


Solution (by Esteban Aliverti):

Drools doesn't have a direct support for time-based keywords, but they provide much more powerful calendar mechanism using Quartz library. StatefulSession or WorkingMemory created by StatelessSession has methods to define these calendars which can restrict date and time when the rule can be fired.

Example: Rule definition

rule "Business hours only"
    calendars "business-hours"
    when
        SomeAttachedClass()
    then
        System.out.println("Rule is fired");
end

Calendar definition

import org.quartz.impl.calendar.DailyCalendar;

// stateless session and working memory or directly stateful session
StatefulKnowledgeSession memory = session.newWorkingMemory(); 
// interested time range is 8-16, also there is many Calendar implementation, not just Daily
DailyCalendar businessHours = new DailyCalendar( 8, 0, 0, 0, 16, 0, 0, 0 );
// by default, defined time is EXCLUDED, the inversion makes it INCLUDED and excludes the rest
businessHours.setInvertTimeRange( true );
//convert the calendar into a org.drools.time.Calendar
org.drools.time.Calendar businessHoursCalendar = QuartzHelper.quartzCalendarAdapter( businessHours );
//Register the calendar in the session with a name. You must use this name in your rules.
memory.getCalendars().set( "business-hours", businessHoursCalendar );

Answer

Esteban Aliverti picture Esteban Aliverti · Feb 12, 2013

A better approach is to use calendar instead of timer(cron:). I managed to do something similar to what you are looking for following these steps:

When you create the session you have to create and configure a Quartz calendar:

//in this case I'm using a DailyCalendar but you can use whatever implementation of Calendar you want
org.quartz.impl.calendar.DailyCalendar businessHours = new org.quartz.impl.calendar.DailyCalendar("business-hours", 8, 0, 0, 0, 16, 0, 0, 0);
businessHours.setInvertTimeRange(true);

//convert the calendar into a org.drools.time.Calendar
org.drools.time.Calendar businessHoursCalendar = QuartzHelper.quartzCalendarAdapter(businessHours);

//Register the calendar in the session with a name. You must use this name in your rules.
ksession.getCalendars().set( "business-hours", businessHoursCalendar );

Then in your rule you have to write something like this:

rule "Rule X"
    calendars "business-hours"
when
    ...
then
    ...
end

Hope it helps,