Search in sources :

Example 11 with AgendaEventListener

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();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) InternalFactHandle(org.drools.core.common.InternalFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 12 with AgendaEventListener

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]);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 13 with AgendaEventListener

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));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieServices(org.kie.api.KieServices) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 14 with AgendaEventListener

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());
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 15 with AgendaEventListener

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));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Aggregations

AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)50 Test (org.junit.Test)45 KieSession (org.kie.api.runtime.KieSession)37 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)32 ArrayList (java.util.ArrayList)21 KieBase (org.kie.api.KieBase)20 List (java.util.List)15 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)15 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)15 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)13 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)13 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)11 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)10 StockTick (org.drools.compiler.StockTick)9 KieServices (org.kie.api.KieServices)9 FactHandle (org.kie.api.runtime.rule.FactHandle)9 EntryPoint (org.kie.api.runtime.rule.EntryPoint)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 Person (org.drools.compiler.Person)6 KieHelper (org.kie.internal.utils.KieHelper)6