Search in sources :

Example 71 with AgendaEventListener

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

the class CepEspTest method testMultipleSlidingWindows.

@Test(timeout = 10000)
public void testMultipleSlidingWindows() {
    final String drl = "declare A\n" + "    @role( event )\n" + "    id : int\n" + "end\n" + "declare B\n" + "    @role( event )\n" + "    id : int\n" + "end\n" + "rule launch\n" + "when\n" + "then\n" + "    insert( new A( 1 ) );\n" + "    insert( new A( 2 ) );\n" + "    insert( new B( 1 ) );\n" + "    insert( new A( 3 ) );\n" + "    insert( new B( 2 ) );\n" + "end\n" + "rule \"ab\" \n" + "when\n" + "    A( $a : id ) over window:length( 1 )\n" + "    B( $b : id ) over window:length( 1 )\n" + "then\n" + "    System.out.println(\"AB: ( \"+$a+\", \"+$b+\" )\");\n" + "end\n" + "rule \"ba\" salience 10\n" + "when\n" + "    B( $b : id ) over window:length( 1 )\n" + "    A( $a : id ) over window:length( 1 )\n" + "then\n" + "    System.out.println(\"BA: ( \"+$b+\", \"+$a+\" )\");\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        ksession.fireAllRules();
        final ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        verify(ael, times(3)).afterMatchFired(captor.capture());
        final List<AfterMatchFiredEvent> values = captor.getAllValues();
        // first rule
        Match act = values.get(0).getMatch();
        assertThat(act.getRule().getName(), is("launch"));
        // second rule
        act = values.get(1).getMatch();
        assertThat(act.getRule().getName(), is("ba"));
        assertThat(((Number) act.getDeclarationValue("$a")).intValue(), is(3));
        assertThat(((Number) act.getDeclarationValue("$b")).intValue(), is(2));
        // third rule
        act = values.get(2).getMatch();
        assertThat(act.getRule().getName(), is("ab"));
        assertThat(((Number) act.getDeclarationValue("$a")).intValue(), is(3));
        assertThat(((Number) act.getDeclarationValue("$b")).intValue(), is(2));
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 72 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() {
    final 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";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final EntryPoint ep = ksession.getEntryPoint("data");
        ep.insert(10d);
        ep.insert(11d);
        ep.insert(12d);
        ksession.fireAllRules();
        final ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        verify(ael, times(1)).afterMatchFired(captor.capture());
        final AfterMatchFiredEvent aafe = captor.getValue();
        assertThat(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(33));
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) 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 73 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() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + "    @role( event )\n" + "    @expires( 0 )\n" + "end\n" + "\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then \n" + "    // no-op\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
        ksession.addEventListener(wml);
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final PseudoClockScheduler clock = ksession.getSessionClock();
        final StockTick st1 = new StockTick(1, "RHT", 100, 1000);
        final StockTick 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(), CoreMatchers.hasItems(st1, st2));
        clock.advanceTime(3, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertThat(ksession.getObjects().size(), equalTo(0));
    } finally {
        ksession.dispose();
    }
}
Also used : RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StockTick(org.drools.testcoverage.common.model.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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 74 with AgendaEventListener

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

the class DynamicRulesTest method testDynamicRulesWithTypeDeclarations.

@Test(timeout = 10000)
public void testDynamicRulesWithTypeDeclarations() {
    // Note: This test originally use "kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( kbase );" which is not possible with KieBuilder
    // Probably this new test is not valid for exec-model and we can remove this test
    String type = "package com.sample\n" + "declare type Foo\n" + "  id : int\n" + "end\n";
    String r1 = "package com.sample\n" + "rule R1 when\n" + "  not Foo()\n" + "then\n" + "  insert( new Foo(1) );\n" + "end\n";
    String r2 = "package com.sample\n" + "rule R2 when\n" + "  $f : Foo()\n" + "then\n" + "  $f.setId( 2 );\n" + "end\n";
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration);
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, type).getKiePackages();
    kbase.addPackages(kpkgs);
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.fireAllRules();
    verify(ael, never()).afterMatchFired(any(AfterMatchFiredEvent.class));
    kpkgs = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, type, r1).getKiePackages();
    kbase.addPackages(kpkgs);
    ksession.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> capt = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(capt.capture());
    assertThat("R1", is(capt.getValue().getMatch().getRule().getName()));
    kpkgs = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, type, r2).getKiePackages();
    kbase.addPackages(kpkgs);
    ksession.fireAllRules();
    verify(ael, times(2)).afterMatchFired(capt.capture());
    assertThat("R2", is(capt.getAllValues().get(2).getMatch().getRule().getName()));
    ksession.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 75 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 = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "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.mvel.compiler.Cheese) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Aggregations

AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)78 Test (org.junit.Test)72 KieSession (org.kie.api.runtime.KieSession)59 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)49 KieBase (org.kie.api.KieBase)38 ArrayList (java.util.ArrayList)33 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)25 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)22 List (java.util.List)21 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)18 FactHandle (org.kie.api.runtime.rule.FactHandle)17 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)14 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)12 EntryPoint (org.kie.api.runtime.rule.EntryPoint)11 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)10 DefaultAgendaEventListener (org.drools.core.event.DefaultAgendaEventListener)9 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 StockTick (org.drools.testcoverage.common.model.StockTick)7 BeforeMatchFiredEvent (org.kie.api.event.rule.BeforeMatchFiredEvent)7 Arrays.asList (java.util.Arrays.asList)6