use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWithSubSubPath.
@Test
public void testEagerEvaluationWithSubSubPath() {
final String drl = "package org.simple \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " exists( Boolean() and not(not(Integer()) and not(Double())) )\n" + "then \n" + "end \n";
final KieSession ksession = getSessionWithEagerActivation(drl);
try {
final List<String> list = new ArrayList<>();
final AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(final org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert(Boolean.TRUE);
assertEquals(0, list.size());
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(1);
assertEquals(1, list.size());
} finally {
ksession.dispose();
}
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testOrPropagatesThroughSubnetwork.
@Test
public void testOrPropagatesThroughSubnetwork() {
final String drl = "package org.simple \n" + "global java.util.List list; \n" + "rule yyy \n" + "when \n" + " Integer(this==1)\n" + " String()\n" + " exists( ( Integer(this == 3) and eval(list.add(\"e1\"))) or (Long(this == 4) and eval(list.add(\"e2\"))) )\n" + "then \n" + "end \n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("activate-delete-test", kieBaseTestConfiguration, drl);
final KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(new ForceEagerActivationOption.FILTERED(rule -> rule.getName().equals("yyy")));
final List<String> list = new ArrayList<>();
final AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(final org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("add:" + event.getMatch().getRule().getName());
}
public void matchCancelled(final org.kie.api.event.rule.MatchCancelledEvent event) {
list.add("rem:" + event.getMatch().getRule().getName());
}
};
KieSession ksession = null;
// scenario 1 - Add Integer, then Long (with no change), then delete Integer and check not holds. Then delete Long
ksession = kbase.newKieSession(conf, null);
try {
ksession.addEventListener(agendaEventListener);
list.clear();
ksession.setGlobal("list", list);
ksession.insert("test");
ksession.insert(1);
assertEquals(0, list.size());
FactHandle fhInt3 = ksession.insert(3);
assertEquals("[e1, add:yyy]", list.toString());
// No change as the int 1 blocks a token propagating from the not node needed for the long join
list.clear();
FactHandle fhLong4 = ksession.insert(Long.valueOf(4));
assertEquals(0, list.size());
ksession.delete(fhInt3);
assertEquals("[e2]", list.toString());
list.clear();
ksession.delete(fhLong4);
assertEquals("[rem:yyy]", list.toString());
} finally {
ksession.dispose();
}
// Scenario 2 - Add Long, then Integer (with no change), then delete Long and check not holds. Then delete Integer
ksession = kbase.newKieSession(conf, null);
try {
ksession.addEventListener(agendaEventListener);
list.clear();
ksession.setGlobal("list", list);
ksession.insert("test");
ksession.insert(1);
assertEquals(0, list.size());
FactHandle fhLong4 = ksession.insert(Long.valueOf(4));
assertEquals("[e2, add:yyy]", list.toString());
// Unlike scenario 1, e1 eval is not blocked so it will still eval, but the outer not still holds so no over all change.
list.clear();
FactHandle fhInt3 = ksession.insert(3);
assertEquals("[e1]", list.toString());
list.clear();
ksession.delete(fhLong4);
assertEquals(0, list.size());
list.clear();
ksession.delete(fhInt3);
assertEquals("[rem:yyy]", list.toString());
} finally {
ksession.dispose();
}
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testOneLazyAndOneImmediateSubPathAfterLia.
@Test
public void testOneLazyAndOneImmediateSubPathAfterLia() {
final String drl = "package org.simple \n" + "global java.util.List list; \n" + "rule xxx \n" + "when \n" + " Integer(this == 0)\n" + " $s : String()\n" + " exists( ( Integer(this == 3) and eval(list.add(\"e1\"))) or (Long(this == 1) and eval(list.add(\"e2\"))) )\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " Integer(this == 0)\n" + " $s : String()\n" + " exists( ( Integer(this == 3) and eval(list.add(\"e1\"))) or (Long(this == 1) and eval(list.add(\"e2\"))) )\n" + "then \n" + "end \n" + "rule zzz \n" + "when \n" + " Integer(this == 0)\n" + " $s : String()\n" + " eval(1==1)\n" + "then \n" + "end \n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("activate-delete-test", kieBaseTestConfiguration, drl);
final KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(new ForceEagerActivationOption.FILTERED(rule -> rule.getName().equals("yyy")));
final List<String> list = new ArrayList<>();
final AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(final org.kie.api.event.rule.MatchCreatedEvent event) {
list.add(event.getMatch().getRule().getName());
}
};
KieSession ksession = null;
// scenario 1 - Only insert the Integer side of 'or'
ksession = kbase.newKieSession(conf, null);
try {
list.clear();
ksession.setGlobal("list", list);
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(0);
ksession.insert(3);
assertEquals("[e1, yyy]", list.toString());
list.clear();
ksession.fireAllRules();
assertEquals("[xxx, zzz]", list.toString());
} finally {
ksession.dispose();
}
// Scenario 2 - Only insert the Long side of 'or'
ksession = kbase.newKieSession(conf, null);
try {
list.clear();
ksession.setGlobal("list", list);
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(0);
ksession.insert(Long.valueOf(1));
assertEquals("[e2, yyy]", list.toString());
list.clear();
ksession.fireAllRules();
assertEquals("[xxx, zzz]", list.toString());
} finally {
ksession.dispose();
}
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class AccumulateTest method execTestAccumulateMultipleFunctions.
private void execTestAccumulateMultipleFunctions(final String fileName) {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, fileName);
final KieSession ksession = kbase.newKieSession();
try {
final AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 17), new Cheese("provolone", 8) };
final Person bob = new Person("Bob", "stilton");
final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
for (int i = 0; i < cheese.length; i++) {
cheeseHandles[i] = ksession.insert(cheese[i]);
}
final FactHandle bobHandle = ksession.insert(bob);
// ---------------- 1st scenario
ksession.fireAllRules();
final ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
Mockito.verify(ael).afterMatchFired(cap.capture());
Match activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
Mockito.reset(ael);
// ---------------- 2nd scenario
final int index = 1;
cheese[index].setPrice(9);
ksession.update(cheeseHandles[index], cheese[index]);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(24));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(5));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(8));
Mockito.reset(ael);
// ---------------- 3rd scenario
bob.setLikes("brie");
ksession.update(bobHandle, bob);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(32));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(15));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(16));
Mockito.reset(ael);
// ---------------- 4th scenario
ksession.delete(cheeseHandles[3]);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(17));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(17));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(17));
} finally {
ksession.dispose();
}
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class AccumulateTest method testAccumulateMultipleFunctionsConstraint.
@Test(timeout = 10000)
public void testAccumulateMultipleFunctionsConstraint() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_AccumulateMultipleFunctionsConstraint.drl");
final KieSession ksession = kbase.newKieSession();
try {
final AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 3), new Cheese("brie", 17), new Cheese("provolone", 8) };
final Person bob = new Person("Bob", "stilton");
final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
for (int i = 0; i < cheese.length; i++) {
cheeseHandles[i] = ksession.insert(cheese[i]);
}
final FactHandle bobHandle = ksession.insert(bob);
// ---------------- 1st scenario
ksession.fireAllRules();
final ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
Mockito.verify(ael).afterMatchFired(cap.capture());
Match activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
Mockito.reset(ael);
// ---------------- 2nd scenario
final int index = 1;
cheese[index].setPrice(9);
ksession.update(cheeseHandles[index], cheese[index]);
ksession.fireAllRules();
Mockito.verify(ael, Mockito.never()).afterMatchFired(Mockito.any(AfterMatchFiredEvent.class));
Mockito.reset(ael);
// ---------------- 3rd scenario
bob.setLikes("brie");
ksession.update(bobHandle, bob);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(20));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(10));
} finally {
ksession.dispose();
}
}
Aggregations