需要帮助,了解口水中的PHREAK算法和规则激活7

时间:2019-04-30 17:17:35

标签: drools

我们正在将Drools实现从5.4迁移到7.20。我创建了一个基本的单元测试,以尝试使用新的PHREAK算法确定drools 7中的决策。

除了显着性,无循环性之外,我已经验证了当多个规则与LHS上的条件匹配时,文件中的后一个规则将具有优先权。但是,在这种情况下,我不能遵循更通用的规则,也找不到能清楚说明正在发生什么情况的文档。

此测试在具有junit 4.11和drools 7.15.0.Final的Java 8中运行。我尝试了7.20,结果相同。

有问题的2条规则将简单地将一个姓氏取为一个人,并为名字设置一个新值。如下面的规则所示,最终值是将firstName设置为“ NEW”。

rule "rule1"
    ruleflow-group "rfg1"
    when
        $person : BFEnrollmentPerson(firstName != null)
        $name : TransitoryElement(value == "Orig") from $person.getFirstName()
    then
        System.out.println("1 Fired: " + $person.getFirstName().getValue());
        $person.getFirstName().setValue("New");
    end

rule "rule2"
    ruleflow-group "rfg1"
    when
        $person : BFEnrollmentPerson()
        $name : TransitoryElement(value == "Orig") from $person.getFirstName()
    then
         $person.getFirstName().setValue("New2");
         System.out.println("2 Fired: " + $person.getFirstName().getValue());
    end
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:1:571928572:571928572:1:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData:com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData@2216effc], getObject()=com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData@2216effc, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:1:571928572:571928572:1:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData:com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData@2216effc], originOffset=-1, propagationNumber=2, rule=null, type=INSERTION]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:2:1058921154:1058921154:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@3f1ddac2], getObject()=com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@3f1ddac2, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:2:1058921154:1058921154:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@3f1ddac2], originOffset=-1, propagationNumber=3, rule=null, type=INSERTION]]
==>[RuleFlowGroupActivated(name=rfg1)]
==>[RuleFlowGroupActivated(name=rfg1)]
==>[AgendaGroupPushedEvent: getAgendaGroup()=AgendaGroup 'rfg1', getKnowledgeRuntime()=KieSession[0]]
==>[ActivationCreatedEvent: getActivation()=[[ rule1 active=false ] [ [fact 0:3:2138765681:2138765681:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@7f7af971]
[fact 0:2:1058921154:1058921154:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@3f1ddac2] ] ], getKnowledgeRuntime()=KieSession[0]]
==>[BeforeActivationFiredEvent:  getActivation()=[[ rule1 active=false ] [ [fact 0:3:2138765681:2138765681:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@7f7af971]
[fact 0:2:1058921154:1058921154:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@3f1ddac2] ] ], getKnowledgeRuntime()=KieSession[0]]
1 Fired: Orig
==>[AfterActivationFiredEvent: getActivation()=[[ rule1 active=false ] [ [fact 0:3:2138765681:2138765681:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@7f7af971]
[fact 0:2:1058921154:1058921154:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@3f1ddac2] ] ], getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupDeactivated(name=rfg1)]
==>[RuleFlowGroupDeactivated(name=rfg1)]
==>[AgendaGroupPoppedEvent: getAgendaGroup()=AgendaGroup 'rfg1', getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupActivated(name=rfg2)]
==>[RuleFlowGroupActivated(name=rfg2)]
==>[AgendaGroupPushedEvent: getAgendaGroup()=AgendaGroup 'rfg2', getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupDeactivated(name=rfg2)]
==>[RuleFlowGroupDeactivated(name=rfg2)]
==>[AgendaGroupPoppedEvent: getAgendaGroup()=AgendaGroup 'rfg2', getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupActivated(name=rfg3)]
==>[RuleFlowGroupActivated(name=rfg3)]
==>[AgendaGroupPushedEvent: getAgendaGroup()=AgendaGroup 'rfg3', getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupDeactivated(name=rfg3)]
==>[RuleFlowGroupDeactivated(name=rfg3)]
==>[AgendaGroupPoppedEvent: getAgendaGroup()=AgendaGroup 'rfg3', getKnowledgeRuntime()=KieSession[0]]
Final person first name value is: New

我可以通过听众看到第二条规则从未激活过。但是,如果我将“ rule2”更改为更具体,例如查找“ rule1”设置的值或使该规则等效于“ rule1”,则第二个规则将被激活并优先。我,其中任何一个都会激活并享有优先权:

rule "rule2"
    ruleflow-group "rfg1"
    when
        $person : BFEnrollmentPerson(firstName != null)
        $name : TransitoryElement(value == "Orig") from $person.getFirstName()
    then
         $person.getFirstName().setValue("New2");
         System.out.println("2 Fired: " + $person.getFirstName().getValue());
    end

rule "rule2"
    ruleflow-group "rfg1"
    when
        $person : BFEnrollmentPerson()
        $name : TransitoryElement(value == "New") from $person.getFirstName()
    then
         $person.getFirstName().setValue("New2");
         System.out.println("2 Fired: " + $person.getFirstName().getValue());
    end
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:1:323761934:323761934:1:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData:com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData@134c370e], getObject()=com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData@134c370e, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:1:323761934:323761934:1:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData:com.benefitfocus.transactionhistory.snapshotdata.SnapshotData$LatestRequestedData@134c370e], originOffset=-1, propagationNumber=2, rule=null, type=INSERTION]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:2:1931008760:1931008760:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8], getObject()=com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:2:1931008760:1931008760:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8], originOffset=-1, propagationNumber=3, rule=null, type=INSERTION]]
==>[RuleFlowGroupActivated(name=rfg1)]
==>[RuleFlowGroupActivated(name=rfg1)]
==>[AgendaGroupPushedEvent: getAgendaGroup()=AgendaGroup 'rfg1', getKnowledgeRuntime()=KieSession[0]]
==>[ActivationCreatedEvent: getActivation()=[[ rule1 active=false ] [ [fact 0:3:438874950:438874950:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@1a28b346]
[fact 0:2:1931008760:1931008760:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8] ] ], getKnowledgeRuntime()=KieSession[0]]
==>[BeforeActivationFiredEvent:  getActivation()=[[ rule1 active=false ] [ [fact 0:3:438874950:438874950:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@1a28b346]
[fact 0:2:1931008760:1931008760:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8] ] ], getKnowledgeRuntime()=KieSession[0]]
==>[AfterActivationFiredEvent: getActivation()=[[ rule1 active=false ] [ [fact 0:3:438874950:438874950:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@1a28b346]
[fact 0:2:1931008760:1931008760:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8] ] ], getKnowledgeRuntime()=KieSession[0]]
==>[ActivationCreatedEvent: getActivation()=[[ rule2 active=false ] [ [fact 0:3:438874950:438874950:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@1a28b346]
[fact 0:2:1931008760:1931008760:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8] ] ], getKnowledgeRuntime()=KieSession[0]]
==>[BeforeActivationFiredEvent:  getActivation()=[[ rule2 active=false ] [ [fact 0:3:438874950:438874950:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@1a28b346]
[fact 0:2:1931008760:1931008760:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8] ] ], getKnowledgeRuntime()=KieSession[0]]
==>[AfterActivationFiredEvent: getActivation()=[[ rule2 active=false ] [ [fact 0:3:438874950:438874950:3:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement:com.benefitfocus.transactionhistory.snapshotdata.TransitoryElement@1a28b346]
[fact 0:2:1931008760:1931008760:2:DEFAULT:NON_TRAIT:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson:com.benefitfocus.transactionhistory.snapshotdata.BFEnrollmentPerson@7318daf8] ] ], getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupDeactivated(name=rfg1)]
==>[RuleFlowGroupDeactivated(name=rfg1)]
==>[AgendaGroupPoppedEvent: getAgendaGroup()=AgendaGroup 'rfg1', getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupActivated(name=rfg2)]
==>[RuleFlowGroupActivated(name=rfg2)]
==>[AgendaGroupPushedEvent: getAgendaGroup()=AgendaGroup 'rfg2', getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupDeactivated(name=rfg2)]
==>[RuleFlowGroupDeactivated(name=rfg2)]
==>[AgendaGroupPoppedEvent: getAgendaGroup()=AgendaGroup 'rfg2', getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupActivated(name=rfg3)]
==>[RuleFlowGroupActivated(name=rfg3)]
==>[AgendaGroupPushedEvent: getAgendaGroup()=AgendaGroup 'rfg3', getKnowledgeRuntime()=KieSession[0]]
==>[RuleFlowGroupDeactivated(name=rfg3)]
==>[RuleFlowGroupDeactivated(name=rfg3)]
==>[AgendaGroupPoppedEvent: getAgendaGroup()=AgendaGroup 'rfg3', getKnowledgeRuntime()=KieSession[0]]
1 Fired: Orig
2 Fired: New2
Final person first name value is: New2

我希望在所有这些情况下,“ rule2”将始终处于激活状态,并且最终值将来自“ rule2”,因为它在所有情况下均符合标准。它在规则流中具有默认显着性,并且是文件中的最新显着性。为什么在第一种情况下不考虑“ rule2”?

0 个答案:

没有答案