All,
I have a simple .drl that is trying to fire rules in a specific activation-group, all in the same default agenda group, MAIN. There are 7 rules, named start, a, b, c, d, e. f. The start rule has an empty LHS and always fires. Rules a, b, and c are all in activation-group group1. Rules d, e, f are in activation-group group2. What I was expecting according to the drools docs is that one rule from each group would fire, not important which one. The rule that fires is supposed to drop the other rules in that activation-group from the agenda.
What I see in the log is that only rules start and d fire although there are matchCreates for all 7, but then 5 immediate matchCancels for rules a, b, c, e, f. All have the same LHS looking for an A record with no qualifiers.
I have no-loop and lock on active set on all of the rules, but it doesn't seem to work.
I also tried inserting a control fact named Selector (now commented out in the drl) and added that to rules c and f as a qualifier, and the launch code does successfully insert the Selector. In that case both rules c and f fire [log not shown]. But I am thinking that this should not be necessary if in fact the activation-group mechanism works as described.
My kmodule.xml is included, and the app runs in ksession ksessionActivation in kieBase kbaseActivation.
My understanding of activation-group could very well be incorrect .. What am I missing?
Thanks in advance for any and all assistance and pointers!
// drl source
package activation;
global Logger logger;
import org.slf4j.Logger;
declare A
name : String
end
declare B
name : String
end
declare C
name : String
end
declare D
name : String
end
declare E
name : String
end
rule "start"
no-loop
when
not A()
then
logger.info("> start: inserting A");
A a = new A();
a.setName("preferred");
insert(a);
logger.info("> start: inserted " + a );
end
rule "a"
no-loop
lock-on-active
//auto-focus
activation-group "group1"
when
a : A()
then
logger.info("> A: received " + a +" on activation group1" );
end
rule "b"
no-loop
lock-on-active
//auto-focus
activation-group "group1"
when
a : A()
then
logger.info("> B: received " + a +" on activation group1");
end
rule "c"
no-loop
lock-on-active
//auto-focus
activation-group "group1"
when
// s : Selector( name == "c" )
a : A()
then
logger.info("> C: received " + a + " on activation group1" );
end
rule "d"
no-loop
activation-group "group2"
when
a : A()
then
logger.info("> D: received " + a + " on activation group2 ");
end
rule "e"
no-loop
activation-group "group2"
when
a : A()
then
logger.info("> E: received " + a + " on activation group2" );
end
rule "f"
no-loop
activation-group "group2"
when
// s : Selector( name == "f" )
a : A()
then
logger.info("> F: received " + a + " on activation group2");
end
// code to fire rules in spring rest controller
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
knowledgeSession = kContainer.newKieSession("ksessionActivation");
// install global logger and listeners
knowledgeSession.setGlobal("logger", logger);
knowledgeSession.addEventListener( new MyDefaultAgendaListener("activation"));
logger.info("agenda event listener installed for activation" );
knowledgeSession.addEventListener(new MyRuleRuntimeEventListener("activation"));
logger.info("runtime listener installed for activation " );
knowledgeSession.insert(new Selector("c"));
//
KieHelper.kieReport(kContainer, "debug activation");
logger.info("firing activation rules in kbase=kbaseActivation ksession=ksessionActivation");
// fire rules
knowledgeSession.fireAllRules();
knowledgeSession.insert(new Selector("f"));
knowledgeSession.fireAllRules();
// go !
return "activation chain executed at " + LocalDateTime.now().toString();
-- log output --
2022-11-04 21:57:53.144 INFO 12132 --- [nio-8085-exec-5] c.h.s.controller.ActivationController : firing activation rules in kbase=kbaseActivation ksession=ksessionActivation
2022-11-04 21:57:53.144 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCreated: [Rule name=start, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.145 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.afterMatchFired: [Rule name=start, agendaGroup=MAIN, salience=0, no-loop=true]
2022-11-04 21:57:53.146 INFO 12132 --- [nio-8085-exec-5] c.h.s.controller.ActivationController : > start: inserting A
2022-11-04 21:57:53.155 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyRuleRuntimeEventListener : activation objectInserted: A( name=preferred ) event: ==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:2:603753489:603753489:2:DEFAULT:NON_TRAIT:activation.A:A( name=preferred )], getObject()=A( name=preferred ), getKnowledgeRuntime()=KieSession[1], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:2:603753489:603753489:2:DEFAULT:NON_TRAIT:activation.A:A( name=preferred )], originOffset=-1, propagationNumber=3, rule=[Rule name=start, agendaGroup=MAIN, salience=0, no-loop=true], type=INSERTION]] runntime.agenda=org.drools.core.common.DefaultAgenda@66fe1eaa rule: start fact: 0:2:603753489:603753489:2:DEFAULT:NON_TRAIT:activation.A
2022-11-04 21:57:53.156 INFO 12132 --- [nio-8085-exec-5] c.h.s.controller.ActivationController : > start: inserted A( name=preferred )
2022-11-04 21:57:53.157 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.afterMatchFired: [Rule name=start, agendaGroup=MAIN, salience=0, no-loop=true]
2022-11-04 21:57:53.157 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCreated: [Rule name=d, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.157 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCreated: [Rule name=e, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.158 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCreated: [Rule name=f, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.158 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCreated: [Rule name=a, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.158 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCancelled: [Rule name=a, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.159 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCreated: [Rule name=b, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.159 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCancelled: [Rule name=b, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.160 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCreated: [Rule name=c, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.160 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCancelled: [Rule name=c, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.160 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.afterMatchFired: [Rule name=d, agendaGroup=MAIN, salience=0, no-loop=true]
2022-11-04 21:57:53.161 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCancelled: [Rule name=e, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.163 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.matchCancelled: [Rule name=f, agendaGroup=MAIN, salience=0, no-loop=true]runtime: KieSession[1]
2022-11-04 21:57:53.164 INFO 12132 --- [nio-8085-exec-5] c.h.s.controller.ActivationController : > D: received A( name=preferred ) on activation group2
2022-11-04 21:57:53.166 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyDefaultAgendaListener : activation.afterMatchFired: [Rule name=d, agendaGroup=MAIN, salience=0, no-loop=true]
2022-11-04 21:57:53.167 INFO 12132 --- [nio-8085-exec-5] c.h.s.c.MyRuleRuntimeEventListener : activation objectInserted: Selector { name=f} event: ==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:3:2081133841:2081133841:3:DEFAULT:NON_TRAIT:com.highmark.staticdrools.controller.Selector:Selector { name=f}], getObject()=Selector { name=f}, getKnowledgeRuntime()=KieSession[1], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:3:2081133841:2081133841:3:DEFAULT:NON_TRAIT:com.highmark.staticdrools.controller.Selector:Selector { name=f}], originOffset=-1, propagationNumber=4, rule=null, type=INSERTION]] runntime.agenda=org.drools.core.common.DefaultAgenda@66fe1eaa rule: null fact: 0:3:2081133841:2081133841:3:DEFAULT:NON_TRAIT:com.highmark.staticdrools.controller.Selector
-- kmodule.xml
<kmodule xmlns="http://www.drools.org/xsd/kmodule" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<kbase name="kbaseInference" default="true" eventProcessingMode="stream" equalsBehavior="identity" packages="inference">
<ksession name="ksessionInference"/>
</kbase>
<kbase name="kbaseInferenceAgenda" eventProcessingMode="stream" equalsBehavior="identity"
packages="inference.agenda, inference.agenda.*">
<ksession name="ksessionInferenceAgenda"/>
</kbase>
<kbase name="kbaseActivation" eventProcessingMode="stream" equalsBehavior="identity" packages="activation">
<ksession name="ksessionActivation"/>
</kbase>
<kbase name="kbaseAgenda" eventProcessingMode="stream" equalsBehavior="identity" packages="agenda">
<ksession name="ksessionAgenda"/>
</kbase>
<!-- rfg -->
<kbase name="kbase" eventProcessingMode="stream" equalsBehavior="identity" packages="com.*">
<ksession name="ksession"/>
</kbase>
</kmodule>