Search in sources :

Example 26 with AgendaEventListener

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

the class AccumulateTest method testImportAccumulateFunction.

@Test
public void testImportAccumulateFunction() throws Exception {
    String drl = "package org.foo.bar\n" + "import accumulate " + TestFunction.class.getCanonicalName() + " f\n" + "rule X when\n" + "    accumulate( $s : String(),\n" + "                $v : f( $s ) )\n" + "then\n" + "end\n";
    ReleaseId releaseId = new ReleaseIdImpl("foo", "bar", "1.0");
    KieServices ks = KieServices.Factory.get();
    createAndDeployJar(ks, releaseId, drl);
    KieContainer kc = ks.newKieContainer(releaseId);
    KieSession ksession = kc.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.insert("x");
    ksession.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> ac = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael).afterMatchFired(ac.capture());
    assertThat((Integer) ac.getValue().getMatch().getDeclarationValue("$v"), is(Integer.valueOf(1)));
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 27 with AgendaEventListener

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

the class ExecutionFlowControlTest method testUpdateActivationCreationNoLoop.

@Test
public void testUpdateActivationCreationNoLoop() throws Exception {
    // JBRULES-787, no-loop blocks all dependant tuples for update
    KieBase kbase = loadKnowledgeBase("test_UpdateActivationCreationNoLoop.drl");
    KieSession ksession = kbase.newKieSession();
    final List created = new ArrayList();
    final List cancelled = new ArrayList();
    final AgendaEventListener l = new DefaultAgendaEventListener() {

        @Override
        public void matchCreated(MatchCreatedEvent event) {
            created.add(event);
        }

        @Override
        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event);
        }
    };
    ksession.addEventListener(l);
    final Cheese stilton = new Cheese("stilton", 15);
    final FactHandle stiltonHandle = ksession.insert(stilton);
    final Person p1 = new Person("p1");
    p1.setCheese(stilton);
    ksession.insert(p1);
    final Person p2 = new Person("p2");
    p2.setCheese(stilton);
    ksession.insert(p2);
    final Person p3 = new Person("p3");
    p3.setCheese(stilton);
    ksession.insert(p3);
    ksession.fireAllRules();
    assertEquals(3, created.size());
    assertEquals(0, cancelled.size());
    // simulate a modify inside a consequence
    ksession.update(stiltonHandle, stilton);
    // with true modify, no reactivations should be triggered
    assertEquals(3, created.size());
    assertEquals(0, cancelled.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 28 with AgendaEventListener

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

the class FirstOrderLogicTest method testOrWithVariableResolution.

// JBRULES-2482
@Test
public void testOrWithVariableResolution() throws Exception {
    // KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl");
    // KieSession workingMemory = createKnowledgeSession(kbase);
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_OrCEFollowedByMultipleEval.drl", FirstOrderLogicTest.class), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = createKnowledgeSession(kbase);
    final AgendaEventListener al = mock(AgendaEventListener.class);
    ksession.addEventListener(al);
    ksession.insert(new FactA("a"));
    ksession.insert(new FactB("b"));
    ksession.insert(new FactC("c"));
    ksession.fireAllRules();
    verify(al, times(6)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) FactA(org.drools.compiler.FactA) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactB(org.drools.compiler.FactB) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) FactC(org.drools.compiler.FactC) Test(org.junit.Test)

Example 29 with AgendaEventListener

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

the class CepEspTest method testSalienceWithEventsRealtimeClock.

@Test(timeout = 10000)
public void testSalienceWithEventsRealtimeClock() throws IOException, ClassNotFoundException, InterruptedException {
    String str = "package org.drools.compiler\n" + "import " + StockTick.class.getName() + "\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule R1 salience 1000\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT' )\n" + "        $s2 : StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + "    then\n" + "end\n" + "rule R2 salience 1000\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT' )\n" + "        not StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + "    then\n" + "end\n" + "rule R3 salience 100\n" + "    when\n" + "        $s2 : StockTick( company == 'ACME' )\n" + "    then\n" + "end\n";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.REALTIME_CLOCK.getId()));
    KieSession ksession = kbase.newKieSession(ksconf, null);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.insert(new StockTick(1, "RHT", 10, 1000));
    ksession.insert(new StockTick(2, "RHT", 10, 1000));
    ksession.insert(new StockTick(3, "RHT", 10, 1000));
    // sleep for 2 secs
    Thread.currentThread().sleep(2000);
    ksession.insert(new StockTick(4, "ACME", 10, 1000));
    // sleep for 1 sec
    Thread.currentThread().sleep(1000);
    int rulesFired = ksession.fireAllRules();
    assertEquals(4, rulesFired);
    ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael, times(4)).afterMatchFired(captor.capture());
    List<AfterMatchFiredEvent> aafe = captor.getAllValues();
    assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(1).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(2).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(3).getMatch().getRule().getName(), is("R3"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) 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) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 30 with AgendaEventListener

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

the class CepEspTest method testStreamModeNoSerialization.

@Test(timeout = 10000)
public void testStreamModeNoSerialization() throws IOException, ClassNotFoundException {
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_StreamMode.drl");
    KieBase kbase2 = (KieBase) DroolsStreamUtils.streamIn(DroolsStreamUtils.streamOut(kbase1), null);
    final KieSession ksession1 = kbase1.newKieSession();
    AgendaEventListener ael1 = mock(AgendaEventListener.class);
    ksession1.addEventListener(ael1);
    final KieSession ksession2 = kbase2.newKieSession();
    AgendaEventListener ael2 = mock(AgendaEventListener.class);
    ksession2.addEventListener(ael2);
    // -------------
    // first, check the non-serialized session
    // -------------
    ksession1.insert(new Sensor(10, 10));
    ksession1.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> aafe1 = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael1, times(1)).afterMatchFired(aafe1.capture());
    List<AfterMatchFiredEvent> events1 = aafe1.getAllValues();
    assertThat(events1.get(0).getMatch().getDeclarationValue("$avg"), is((Object) 10));
    ksession1.insert(new Sensor(20, 20));
    ksession1.fireAllRules();
    verify(ael1, times(2)).afterMatchFired(aafe1.capture());
    events1 = aafe1.getAllValues();
    assertThat(events1.get(1).getMatch().getDeclarationValue("$avg"), is((Object) 15));
    ksession1.insert(new Sensor(30, 30));
    ksession1.fireAllRules();
    verify(ael1, times(3)).afterMatchFired(aafe1.capture());
    assertThat(events1.get(2).getMatch().getDeclarationValue("$avg"), is((Object) 25));
    ksession1.dispose();
    // -------------
    // now we check the serialized session
    // -------------
    ArgumentCaptor<AfterMatchFiredEvent> aafe2 = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    ksession2.insert(new Sensor(10, 10));
    ksession2.fireAllRules();
    verify(ael2, times(1)).afterMatchFired(aafe2.capture());
    List<AfterMatchFiredEvent> events2 = aafe2.getAllValues();
    assertThat(events2.get(0).getMatch().getDeclarationValue("$avg"), is((Object) 10));
    ksession2.insert(new Sensor(20, 20));
    ksession2.fireAllRules();
    verify(ael2, times(2)).afterMatchFired(aafe2.capture());
    events2 = aafe2.getAllValues();
    assertThat(events2.get(1).getMatch().getDeclarationValue("$avg"), is((Object) 15));
    ksession2.insert(new Sensor(30, 30));
    ksession2.fireAllRules();
    verify(ael2, times(3)).afterMatchFired(aafe2.capture());
    assertThat(events2.get(2).getMatch().getDeclarationValue("$avg"), is((Object) 25));
    ksession2.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) 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) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Sensor(org.drools.compiler.Sensor) 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