Drools 为 StatelessKieSession 的列表输入中的每一项触发一个规则

Drools trigger one rule per item in list input for StatelessKieSession

我正在使用决策表并希望为每个输入项触发一个规则。

我正在使用 decision 设置 Sequential = true 并将所有规则定义为同一 ACTIVATION-GROUP 的一部分。

当我使用下面的方法触发 drools 规则引擎时,它只评估第一个输入项,而忽略其他项。我想要的行为是每个输入项最多评估 1 个规则(由 Salience 定义的规则顺序)。

kieStatelessSession.execute(inputList)

我可以通过一次向 kieStatelessSession 发送一个项目来使它工作,但我更愿意一次执行所有项目。

我正在使用 Drools verison 6.5.0.FINAL 和 Java 7.

Drools 中没有对您要实现的目标的开箱即用支持。如果您希望您的规则针对每个事实评估一次,您将需要自己编写代码。

一种方法可能是在处理其中一个输入时标记另一种类型的事实:

declare Marker
  fact : Object
end

//Bellow are the rules that should be coming from your decision table.
//Each rule will do whatever it needs to do, and then it will create a 
//Marker fact for the fact that was processed.
//These rules now include a "not" Conditional Element to avoid a fact to be
//evaluated more than once.

rule "Rule 1"
salience 100
when 
  $fact: Object(...)   //your conditions
  not Marker(fact == $fact)
then
  //...   Your logic
  insert(new Marker($fact)); 
end

...


rule "Rule 50"
salience 50
when 
  $fact: Object(...)   //your conditions
  not Marker(fact == $fact)
then
  //...   Your logic
  insert(new Marker($fact)); 
end

希望对您有所帮助,