我在理解 exists
的原因时遇到问题关键字是必要的。
我有以下规则:
1)
rule "normal"
when
Bus( seats > 20 )
then
System.out.println("There is a 20+ bus);
end
2)
rule "with exists"
when
exists Bus( seats > 20 )
then
System.out.println("There is a 20+ bus existing...);
end
第一条规则的 LHS 与第二条规则有何不同?
谢谢!
最佳答案
documentation为 exists
声明如下
The CE exists is first order logic's existential quantifier and checks for the existence of something in the Working Memory. Think of "exists" as meaning "there is at least one..". It is different from just having the pattern on its own, which is more like saying "for each one of...". If you use exists with a pattern, the rule will only activate at most once, regardless of how much data there is in working memory that matches the condition inside of the exists pattern. Since only the existence matters, no bindings will be established.
因此,对于您的示例,第一个规则是为每辆超过 20 个座位的巴士触发,但第二个规则仅触发一次,即使有超过 20 个座位的巴士。
关于Drools 存在与 "normal"模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32416411/