Нужна помощь в понимании алгоритма PHREAK и активации правил в drools 7 - PullRequest
0 голосов
/ 30 апреля 2019

Мы переносим нашу реализацию drools с 5.4 на 7.20. Я создал базовый модульный тест, чтобы попытаться выяснить процесс принятия решения в drools 7 с помощью нового алгоритма PHREAK.

Если оставить в стороне отличительные черты, отсутствие петель и т. Д., Я убедился, что более позднее правило в файле будет иметь приоритет, когда несколько правил соответствуют критериям на LHS. Однако у меня есть ситуация, когда более общее правило не соблюдается, и я не могу найти документацию, которая четко объясняет, что происходит.

Этот тест выполняется в java 8 с junit 4.11 и drools 7.15.0.Final. Я пробовал 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

Я могу видеть слушателем, что 2-е правило никогда не активируется. Однако, если я изменю «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» не учитывается в первом сценарии?

...