gpt4 book ai didi

java - 流口水:限时规则

转载 作者:搜寻专家 更新时间:2023-10-31 19:47:30 27 4
gpt4 key购买 nike

流口水 documentation提到规则可以使用 date-effectivedate-expires 等属性来指定绝对规则有效期。

例如

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 点生效?


解决方案(由 Esteban Aliverti 提供):

Drools 不直接支持基于时间的关键字,但它们使用 Quartz 提供了更强大的日历机制。图书馆。由 StatelessSession 创建的 StatefulSessionWorkingMemory 具有定义这些日历的方法,这些日历可以限制触发规则的日期和时间。

示例:规则定义

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

最佳答案

更好的方法是使用 calendar 而不是 timer(cron:)。我按照以下步骤设法做了与您正在寻找的类似的事情:

创建 session 时,您必须创建和配置 Quartz 日历:

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

希望对你有帮助

关于java - 流口水:限时规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14819615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com