Drools - 取消了不同FactHandles的规则激活

时间:2015-06-03 10:20:19

标签: drools kie

规则:

rule "Accounting_11" dialect "mvel" 
//For any value of l except given in Accounting_12
salience 1 
activation-group "Group l"  
auto-focus true
lock-on-active
no-loop true 
when $iv:Invoice(x in ("D20","D21","D22", "D23", "D24","D25"),y =="E20",z =="F20",a =="G20") 
then modify($iv) { sg.setA("ALL l"), sg.setB("C20")} 
end

rule "Accounting_12"  dialect "mvel" 
//Exceptions of l
salience 2  
activation-group "Group l" 
auto-focus true
lock-on-active
no-loop true 
//Pattern is similar to Accounting_11 with additional constraint "l in (....)"
when $iv:Invoice(l in ("C20","C21","C22", "C23", "C24","C25") , x in ("D20","D21","D22", "D23", "D24","D25"),y =="E20",z =="F20",a =="G20") 
then modify($iv) { sg.setA("Sepcific l"), sg.setB("C20")} 
end

在循环中的工作记忆中插入2个事实。

  • 发票(l =“C20”,x =“D20”,y =“E20”,z =“F20”,“,a =”G20“)
  • 发票(x =“D20”,y =“E20”,z =“F20”,a =“G20”)

调用fireAllRules()。3按预期创建激活。

==>[ActivationCreatedEvent: getActivation()=[[ Accounting_11 active=false ] [ [fact 0:2:4445234:4445234:2:DEFAULT:NON_TRAIT:org.cpg.poc.drools.keloy.domain.Invoice@43d432] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@1743ccd]
    ==>[ActivationCreatedEvent: getActivation()=[[ Accounting_11 active=false ] [ [fact 0:1:2489285:2489285:1:DEFAULT:NON_TRAIT:org.cpg.poc.drools.keloy.domain.Invoice@25fbc5] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@1743ccd]
    ==>[ActivationCreatedEvent: getActivation()=[[ Accounting_12 active=false ] [ [fact 0:2:4445234:4445234:2:DEFAULT:NON_TRAIT:org.cpg.poc.drools.keloy.domain.Invoice@43d432] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@1743ccd]

其中一项预期激活被解雇

==>[BeforeActivationFiredEvent:  getActivation()=[[ Accounting_12 active=false ] [ [fact 0:2:4445234:4445234:2:DEFAULT:NON_TRAIT:org.cpg.poc.drools.keloy.domain.Invoice@43d432] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@1743ccd]

但它会取消所有其他激活。

==>[ActivationCancelledEvent: getCause()=CLEAR, getActivation()=[[ Accounting_11 active=false ] [ [fact 0:2:4445234:4445234:2:DEFAULT:NON_TRAIT:org.cpg.poc.drools.keloy.domain.Invoice@43d432] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@1743ccd]
==>[ActivationCancelledEvent: getCause()=CLEAR, getActivation()=[[ Accounting_11 active=false ] [ [fact 0:1:2489285:2489285:1:DEFAULT:NON_TRAIT:org.cpg.poc.drools.keloy.domain.Invoice@25fbc5] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@1743ccd]

它是否应该仅取消为同一事实句柄(Invoice @ 43d432)创建的激活,l = C20?

为什么取消为 Invoice @ 25fbc5 创建的激活,第二个为'l'的空值?

注意:当我在每次插入事件后触发规则时,我会得到预期的结果。

1 个答案:

答案 0 :(得分:0)

使用规则属性no-loop和lock-on-active被宣传为简化规则创作,在一定比例的用例中也是如此。但是,总是一种避免严格应用逻辑的方法,逻辑是规则条件的基础。尽管为规则条件添加完整逻辑可能需要更多工作,但它肯定会创建一个强大的逻辑,从而导致您真正需要的规则执行。

那就是说,我不知道由于插入规则会发生什么。显然,Accounting_11和Accounting_12的约束是重叠的,Accounting_12更具限制性。这总是一个可疑的情况:你想要发生后果还是只发生一次? Accounting_12具有更高的范围,因此它将是首选(无论字段l的值如何)。如果没有规则属性,我希望其他激活保持活动状态,但使用lock-on-active时,激活会被取消。

正如我所说:在基本约束中描述所需的逻辑是可取的。

例如,如果l值不在由集合`(“C20”,“C21”,“C22”,“C23”,“C24”)定义的范围内,则不应激活规则, “C25”),添加此约束:

$iv:Invoice(l not in ("C20", "C21", "C22", "C23", "C24", "C25"),...)