Search in sources :

Example 16 with AfterMatchFiredEvent

use of org.kie.api.event.rule.AfterMatchFiredEvent 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)

Example 17 with AfterMatchFiredEvent

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

the class DeclarativeAgendaTest method testCancelActivation.

@Test(timeout = 10000)
public void testCancelActivation() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule1 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    kcontext.cancelMatch( $i ); \n";
    str += "end \n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    final List cancelled = new ArrayList();
    ksession.addEventListener(new AgendaEventListener() {

        public void beforeMatchFired(BeforeMatchFiredEvent event) {
        }

        public void agendaGroupPushed(AgendaGroupPushedEvent event) {
        }

        public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
        }

        public void matchCreated(MatchCreatedEvent event) {
        }

        public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
        }

        public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
        }

        public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
        }

        public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event);
        }
    });
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("go1");
    FactHandle go2 = ksession.insert("go2");
    ksession.fireAllRules();
    assertEquals(0, list.size());
    assertEquals(1, cancelled.size());
    assertEquals("rule1", ((MatchCancelledEvent) cancelled.get(0)).getMatch().getRule().getName());
    ksession.dispose();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) RuleFlowGroupDeactivatedEvent(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) RuleFlowGroupActivatedEvent(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaGroupPushedEvent(org.kie.api.event.rule.AgendaGroupPushedEvent) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Test(org.junit.Test)

Example 18 with AfterMatchFiredEvent

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

the class DynamicRulesTest method testDynamicRulesWithTypeDeclarations.

@Test(timeout = 10000)
public void testDynamicRulesWithTypeDeclarations() {
    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";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(type.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.fireAllRules();
    verify(ael, never()).afterMatchFired(any(AfterMatchFiredEvent.class));
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbase);
    kbuilder.add(ResourceFactory.newByteArrayResource(r1.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    ksession.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> capt = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(capt.capture());
    assertThat("R1", is(capt.getValue().getMatch().getRule().getName()));
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbase);
    kbuilder.add(ResourceFactory.newByteArrayResource(r2.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    ksession.fireAllRules();
    verify(ael, times(2)).afterMatchFired(capt.capture());
    assertThat("R2", is(capt.getAllValues().get(2).getMatch().getRule().getName()));
    ksession.dispose();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 19 with AfterMatchFiredEvent

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

the class CepEspTest method testMultipleSlidingWindows.

@Test(timeout = 10000)
public void testMultipleSlidingWindows() throws IOException, ClassNotFoundException {
    String str = "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";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSession ksession = createKnowledgeSession(kbase);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael, times(3)).afterMatchFired(captor.capture());
    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));
}
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) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 20 with AfterMatchFiredEvent

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

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace3_usingSingleQuote.

@Test
public void testKJarUpgradeDRLWithSpace3_usingSingleQuote() throws Exception {
    // DROOLS-1399 ter (using single quote)
    String drl_1 = "package org.drools.compiler\n" + "rule Rs when $s : String() then System.out.println($s); end\n" + "rule Rx when\n" + "   $m : Message( message == 'Hello World' )\n" + "then\n" + "  System.out.println($m); \n" + "end\n";
    String drl_2 = "package org.drools.compiler\n" + // <<- notice rule changed
    "rule Rs when $s : String( this == 'x') then System.out.println($s); end\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE is the an ADDITIONAL change in this other version.
    "   $m : Message( message == 'Hello World'  )\n" + "then\n" + "  System.out.println($m); \n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl_1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    final List<String> fired = new ArrayList<>();
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ksession.insert(new Message("Hello World"));
    ksession.insert("x");
    assertEquals(2, ksession.fireAllRules());
    assertTrue(fired.contains("Rs"));
    assertTrue(fired.contains("Rx"));
    fired.clear();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl_2);
    kc.updateToVersion(releaseId2);
    // rule Rx is UNchanged and should NOT fire again
    // rule Rs is changed and should match again, and fire again.
    assertEquals(1, ksession.fireAllRules());
    assertTrue(fired.contains("Rs"));
    assertFalse(fired.contains("Rx"));
}
Also used : Message(org.drools.compiler.Message) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)29 KieSession (org.kie.api.runtime.KieSession)26 Test (org.junit.Test)25 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)18 KieBase (org.kie.api.KieBase)16 ArrayList (java.util.ArrayList)8 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)8 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)8 List (java.util.List)6 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)6 StockTick (org.drools.compiler.StockTick)5 KieServices (org.kie.api.KieServices)5 ReleaseId (org.kie.api.builder.ReleaseId)5 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)5 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)5 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)5 KieContainer (org.kie.api.runtime.KieContainer)5 EntryPoint (org.kie.api.runtime.rule.EntryPoint)5 Match (org.kie.api.runtime.rule.Match)5 Person (org.drools.compiler.Person)4