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

Drools - Rule Activations for different FactHandles cancelled

规则:

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 个事实。

调用 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'?

的空值

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

使用规则属性 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"),...)