Drools documentation提到规则可以使用像date-effective
和date-expires
这样的属性来指定绝对规则有效期。Drools:时间限制规则
例如
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的也支持与间隔为timer(int:)
和cron为timer(cron:)
周期性地重复规则,但它意味着,该规则被在这样的点烧制。
问:
我感兴趣,如果有任何选项如何指定可定期(不启动)与时间限制规则。例如,让我们将某个公司的营业时间形象化 - 只能在正式工作期间执行操作,而不能在下班后执行操作。
,我想这样的事情,但它不是Drools的有效规则
rule "Time-restricted rule"
time-effective "8:00" # 8 AM
time-expires "16:00" # 4 PM
when
then
end
将有可能这样的规则是活跃,只有周一至周五上午8点延长到下午4点?
溶液(埃斯特班Aliverti):
的Drools不具备基于时间的关键词的直接支持,但他们提供使用Quartz库更加强大的日历机构。由StatelessSession
创建的StatefulSession
或WorkingMemory
定义了这些日历的方法,这些日历可以限制可以触发规则的日期和时间。
例子: 规则定义
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);
来报告的。令人惊讶的是,这正是我所需要的。非常感谢你的努力! – Gaim 2013-02-13 12:10:39
'QuartzHelper'类在新版本的Drools中不可用;如果你仍然想使用它,你可以从[这里]手动下载(http://grepcode.com/file/repository.jboss.org/nexus/content/repositories/releases/org.drools/drools-api/ 5.1.1 /组织/滴料/运行/帮助/ QuartzHelper.java /)。另外,'org.drools.time.Calendar'已经在更新版本的Drools中变成'org.kie.api.time.Calendar'。 – 2016-10-03 06:57:41