【发布时间】:2020-10-23 14:16:36
【问题描述】:
在 Drools 中使用累积时,会针对未更新的事实评估和触发规则。
规则如下:
rule "WidgetsPerUser"
when
$user : User()
accumulate(
Widget ( checkIsUser($user) );
$widgetCount : sum(1)
)
then
System.out.println($user + " has " + $widgetCount + " widgets");
end
这个想法很简单:计算每个User 的Widget 对象的数量。 checkIsUser() 是一个简单的相等性检查,但表示不能使用索引的检查。
这是 Drools 7.0.0.Final 的示例输出:
-- Calling fire all rules --
User5 has 10 widgets
User4 has 10 widgets
User3 has 10 widgets
User2 has 10 widgets
User1 has 10 widgets
Widget moved from User3 to User1
-- Calling fire all rules --
User5 has 10 widgets
User3 has 9 widgets
User1 has 11 widgets
这里我们有 5 个User 事实插入到内存中,并且所有Widget 的起始计数都是 10。fireAllRules 的第一次调用显示正确的打印,因为所有User 事实都插入到内存中。接下来是更新,其中 Widget 从 User3 移动到 User1。存在预期的输出,但是 User5 没有理由显示为正在更新。
要直观地突出问题:
User5 has 10 widgets (User5 should NOT be triggered!)
(User4 is correctly ignored)
User3 has 9 widgets (User3 is correctly triggered)
(User2 is correctly ignored)
User1 has 11 widgets (User1 is correctly triggered)
User5 与 User2 或 User4 的唯一不同之处在于它是最后添加的用户事实,那么为什么它的行为会有所不同呢?
是否需要额外的评估?它有什么用途?
请注意,问题不在于如何避免或解决额外的触发器。
【问题讨论】:
标签: drools