use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class CepEspTest method testIdentityAssertBehaviorOnEntryPoints.
@Test(timeout = 10000)
public void testIdentityAssertBehaviorOnEntryPoints() throws IOException, ClassNotFoundException {
StockTickInterface st1 = new StockTick(1, "RHT", 10, 10);
StockTickInterface st2 = new StockTick(1, "RHT", 10, 10);
StockTickInterface st3 = new StockTick(2, "RHT", 15, 20);
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
kbconf.setOption(EqualityBehaviorOption.IDENTITY);
final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
final KieSession ksession = kbase1.newKieSession();
AgendaEventListener ael1 = mock(AgendaEventListener.class);
ksession.addEventListener(ael1);
EntryPoint ep1 = ksession.getEntryPoint("stocktick stream");
FactHandle fh1 = ep1.insert(st1);
FactHandle fh1_2 = ep1.insert(st1);
FactHandle fh2 = ep1.insert(st2);
FactHandle fh3 = ep1.insert(st3);
assertSame(fh1, fh1_2);
assertNotSame(fh1, fh2);
assertNotSame(fh1, fh3);
assertNotSame(fh2, fh3);
ksession.fireAllRules();
// must have fired 3 times, one for each event identity
verify(ael1, times(3)).afterMatchFired(any(AfterMatchFiredEvent.class));
ksession.dispose();
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class CepEspTest method testComplexOperator.
@Test(timeout = 10000)
public void testComplexOperator() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_ComplexOperator.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, sconf);
List list = new ArrayList();
ksession.setGlobal("list", list);
final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
clock.setStartupTime(1000);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 0, 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 4, 3);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 8, 3);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 12, 5);
StockTickInterface tick5 = new StockTick(5, "ACME", 10, 12, 5);
StockTickInterface tick6 = new StockTick(6, "ACME", 10, 13, 3);
StockTickInterface tick7 = new StockTick(7, "ACME", 10, 13, 5);
StockTickInterface tick8 = new StockTick(8, "ACME", 10, 15, 3);
ksession.insert(tick1);
ksession.insert(tick2);
ksession.insert(tick3);
ksession.insert(tick4);
ksession.insert(tick5);
ksession.insert(tick6);
ksession.insert(tick7);
ksession.insert(tick8);
ksession.fireAllRules();
assertEquals(1, list.size());
StockTick[] stocks = (StockTick[]) list.get(0);
assertSame(tick4, stocks[0]);
assertSame(tick2, stocks[1]);
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWith2Paths.
@Test
public void testEagerEvaluationWith2Paths() throws Exception {
String str = "package org.simple \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " $i : Integer()\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " $i : Integer()\n" + "then \n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
@Override
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
@Override
public void matchCancelled(MatchCancelledEvent event) {
list.add("cancelled");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
FactHandle fh = ksession.insert(1);
assertEquals(2, list.size());
assertEquals("activated", list.get(0));
assertEquals("activated", list.get(1));
list.clear();
ksession.delete(fh);
assertEquals(2, list.size());
assertEquals("cancelled", list.get(0));
assertEquals("cancelled", list.get(1));
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWith2SubPaths.
@Test
public void testEagerEvaluationWith2SubPaths() throws Exception {
String str = "package org.simple \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " exists( Integer() or Long() )\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " exists( Integer() or Long() )\n" + "then \n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(1);
assertEquals(2, list.size());
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testOneLinkedAndOneUnlinkedPath.
@Test
public void testOneLinkedAndOneUnlinkedPath() throws Exception {
String str = "package org.simple \n" + "rule xxx \n" + "when \n" + " String()\n" + " Integer()\n" + " Long()\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " String()\n" + " Integer()\n" + " Boolean()\n" + "then \n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add(event.getMatch().getRule().getName());
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(Boolean.TRUE);
assertEquals(0, list.size());
ksession.insert(1);
assertEquals(1, list.size());
assertEquals("yyy", list.get(0));
}
Aggregations