Drools: time-limited rule

The Drools documentation mentions that rules can use attributes such as date-effectiveand date-expiresto indicate the validity period of an absolute rule.

for instance

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 repeating rules at intervals timer(int:)and cron like timer(cron:), but this means that the rule runs at such points.

Question:

I am wondering if there is any option how to specify periodically available (non-working) rules with a time limit. For example, let the image of working time in any company - the operation can be performed only during the official working period, but not after several hours.

I would like something like this, but this is not the right Drools rule

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

8:00 16:00?


( ):

Drools , , , Quartz. StatefulSession WorkingMemory, StatelessSession, , , .

:

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

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 );
+5
3

calendar timer(cron:). - , , :

:

//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 );

- :

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

, ,

+6

. , , :

Java-, , . , TimeFact, update(long time): void, getDay(): String getTime(): String. when, :

rule "Foo"
when
    TimeFact(day in ("Monday", "Thursday"), time > "16:00:00" && < "17:00:00")
    [more conditions]
then
    [do stuff]
end

TimeFact ( ). , , , , , calendars, .

, , " ", calendars.

+1

According to the documentation, you can use cron expressions as timers. So you could use something like this:

timer(cron: * 8-16 * * 1-5 *) 

Disclaimer: I have not tested this!

Hope this helps,

0
source

All Articles