gpt4 book ai didi

java - Drools 规则 - 空检查和累积条件

转载 作者:行者123 更新时间:2023-12-04 09:57:39 24 4
gpt4 key购买 nike

我目前正在对运行一组 Drools 的 java 批处理做一些修复。 (耶!)规则。

我必须解决的规则是:

rule "Insert a too old condition"
salience -1
when
$person : Person()
$tooOldInstant : DateTime() from now.minusDays(10)
DateTime( this < $tooOldInstant ) from accumulate (
LastData( $date : lastDate ) from $person.personLastDatas,
maxValue($date)
)
then
insert(new Condition("submitTooOldCondition"));
end

简化的地方 Person是一个带有 personLastDatas Set<LastData> 的简单 bean和 LastData有一个 org.joda.time.DateTime lastDate 属性。

问题:如何插入新条件 if $person.personLastDatas规则是否为空?

就像是:
rule "Insert a too old condition modified"
salience -1
when
$person : Person()
$tooOldInstant : DateTime() from now.minusDays(10)
$maxLastDate : DateTime() from accumulate (
LastData( $date : lastDate ) from $person.personLastDatas,
maxValue($date)
)
($maxLastDate == null || $maxLastDate < $tooOldInstant)
then
insert(new Condition("submitTooOldCondition"));
end

最佳答案

您应该有两个规则,一个用于 null 条件,另一个用于比较日期。

这是空条件规则;它验证 Person 存在但它没有 personLastDatas属性(property):

rule "Insert a too old condition modified - null case"
salience -1
when
$person: Person( personLastDatas == null )
then
insert(new Condition("submitTooOldCondition"));
end

您现有的规则足以进行日期比较检查。

一般而言,如果您发现自己试图在规则的任一侧执行复杂的 if-else 逻辑,则表明您应该有两个规则。由于这两个规则不可能都为真,因此您只能插入一个这种类型的条件。

话虽如此,如果您使用的是现代版本的 drools,您可以使用条件和命名空间的结果。 documentation详细介绍了这一点(我已经链接了 7.37.0.Final;大多数最近的 7.30+ 版本都具有此功能。)下面是您的规则可能是什么样子的示例:
rule "Insert a too old condition"
salience -1
when
$person : Person( $lastDatas: personLastDatas )
if ( $lastDatas == null ) break[noData]
$tooOldInstant : DateTime() from now.minusDays(10)
DateTime( this < $tooOldInstant ) from accumulate (
LastData( $date : lastDate ) from $person.personLastDatas,
maxValue($date)
)
then
insert(new Condition("submitTooOldCondition"));
then[noData]
// any special logic for the null case goes here
insert(new Condition("submitTooOldCondition"));
end

(这是伪代码;我在这台计算机上没有 drools 项目,但它应该是相似的。)

基本上这种语法虽然更难阅读,但可以让您处理这些类型的重复/部分共享案例规则。通常建议在您有两个规则的情况下使用它们,其中一个扩展另一个,因此常见条件的子集可以触发一个结果,而完整的条件集可以触发另一个结果。这不是您在这里所拥有的,但可以为您的用例设置功能。
break关键字告诉引擎一旦满足条件就停止评估左侧;还有一个 do允许继续评估的关键字。

关于java - Drools 规则 - 空检查和累积条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61890851/

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