Search in sources :

Example 36 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWithSubSubPath.

@Test
public void testEagerEvaluationWithSubSubPath() throws Exception {
    String str = "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";
    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(Boolean.TRUE);
    assertEquals(0, list.size());
    ksession.insert("test");
    assertEquals(0, list.size());
    ksession.insert(1);
    assertEquals(1, 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 37 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class Misc2Test method testListnersOnStatlessKieSession.

@Test
public void testListnersOnStatlessKieSession() {
    // DROOLS-141
    // BZ-999491
    String str = "rule R when\n" + "  String()\n" + "then\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    final List<String> firings = new ArrayList<String>();
    AgendaEventListener agendaEventListener = new AgendaEventListener() {

        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
        }

        public void matchCancelled(org.kie.api.event.rule.MatchCancelledEvent event) {
        }

        public void beforeMatchFired(org.kie.api.event.rule.BeforeMatchFiredEvent event) {
        }

        public void afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent event) {
            firings.add("Fired!");
        }

        public void agendaGroupPopped(org.kie.api.event.rule.AgendaGroupPoppedEvent event) {
        }

        public void agendaGroupPushed(org.kie.api.event.rule.AgendaGroupPushedEvent event) {
        }

        public void beforeRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
        }

        public void afterRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
        }

        public void beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
        }

        public void afterRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.execute("1");
    ksession.execute("2");
    assertEquals(2, firings.size());
    ksession.removeEventListener(agendaEventListener);
    ksession.execute("3");
    assertEquals(2, firings.size());
}
Also used : ArrayList(java.util.ArrayList) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Test(org.junit.Test)

Example 38 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class StreamsTest method testWindowDeclaration.

public void testWindowDeclaration() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "declare window RedHatTicks\n" + "    StockTick( company == 'RHT' )\n" + "               over window:length(5)\n" + "               from entry-point ticks\n" + "end\n" + "rule X\n" + "when\n" + "    accumulate( $s : StockTick( price > 20 ) from window RedHatTicks,\n" + "                $sum : sum( $s.getPrice() ),\n" + "                $cnt : count( $s ) )\n" + "then\n" + "end\n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep = ksession.getEntryPoint("ticks");
    // not in the window
    ep.insert(new StockTick(1, "ACME", 20, 1000));
    // not > 20
    ep.insert(new StockTick(2, "RHT", 20, 1000));
    ep.insert(new StockTick(3, "RHT", 30, 1000));
    // not in the window
    ep.insert(new StockTick(4, "ACME", 30, 1000));
    ep.insert(new StockTick(5, "RHT", 25, 1000));
    // not in the window
    ep.insert(new StockTick(6, "ACME", 10, 1000));
    // not > 20
    ep.insert(new StockTick(7, "RHT", 10, 1000));
    ep.insert(new StockTick(8, "RHT", 40, 1000));
    ksession.fireAllRules();
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    AfterMatchFiredEvent aafe = captor.getValue();
    assertThat(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(95));
    assertThat(((Number) aafe.getMatch().getDeclarationValue("$cnt")).intValue(), is(3));
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession)

Example 39 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class StreamsTest method testWindowDeclaration2.

@Test(timeout = 10000)
public void testWindowDeclaration2() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare Double\n" + "    @role(event)\n" + "end\n" + "declare window Streem\n" + "    Double() over window:length( 10 ) from entry-point data\n" + "end\n" + "rule \"See\"\n" + "when\n" + "    $sum : Double() from accumulate (\n" + "        $d: Double()\n" + "            from window Streem,\n" + "        sum( $d )\n" + "    )\n" + "then\n" + "end";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep = ksession.getEntryPoint("data");
    ep.insert(Double.valueOf(10));
    ep.insert(Double.valueOf(11));
    ep.insert(Double.valueOf(12));
    ksession.fireAllRules();
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    AfterMatchFiredEvent aafe = captor.getValue();
    assertThat(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(33));
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 40 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class StreamsTest method testEventExpirationSetToZero.

@Test(timeout = 10000)
public void testEventExpirationSetToZero() throws Exception {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase("test_EventExpirationSetToZero.drl", kconf);
    KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kbase.newKieSession(ksessionConfig, null);
    RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
    ksession.addEventListener(wml);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    final StockTickInterface st1 = new StockTick(1, "RHT", 100, 1000);
    final StockTickInterface st2 = new StockTick(2, "RHT", 100, 1000);
    ksession.insert(st1);
    ksession.insert(st2);
    assertThat(ksession.fireAllRules(), equalTo(2));
    verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
    verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
    assertThat(ksession.getObjects().size(), equalTo(2));
    assertThat((Collection<Object>) ksession.getObjects(), hasItems((Object) st1, st2));
    clock.advanceTime(3, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertThat(ksession.getObjects().size(), equalTo(0));
}
Also used : PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) 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