Search in sources :

Example 31 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener 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 32 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() {
    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 33 with AgendaEventListener

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

the class DynamicRulesTest method testDynamicRuleRemovalsSubNetworkAndNot.

@Test(timeout = 10000)
public void testDynamicRuleRemovalsSubNetworkAndNot() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase("test_DynamicRulesWithNotSubnetwork.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    final AgendaEventListener alistener = mock(AgendaEventListener.class);
    ksession.addEventListener(alistener);
    // pattern does not match, so do not activate
    ksession.insert(new Person("toni"));
    ksession.fireAllRules();
    verify(alistener, never()).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    // pattern matches, so create activation
    ksession.insert(new Person("bob"));
    ksession.fireAllRules();
    verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    // already active, so no new activation should be created
    ksession.insert(new Person("mark"));
    ksession.fireAllRules();
    verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    kbase.removeKiePackage("org.drools.compiler");
    assertEquals(0, kbase.getKiePackages().size());
    // lets re-compile and add it again
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicRulesWithNotSubnetwork.drl");
    kbase.addPackages(kpkgs);
    ksession.fireAllRules();
    // rule should be reactivated, since data is still in the session
    verify(alistener, times(2)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 34 with AgendaEventListener

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

the class CepEspTest method testEqualityAssertBehaviorOnEntryPoints.

@Test(timeout = 10000)
public void testEqualityAssertBehaviorOnEntryPoints() 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.EQUALITY);
    final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
    final KieSession ksession1 = kbase1.newKieSession();
    AgendaEventListener ael1 = mock(AgendaEventListener.class);
    ksession1.addEventListener(ael1);
    EntryPoint ep1 = ksession1.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);
    assertSame(fh1, fh2);
    assertNotSame(fh1, fh3);
    ksession1.fireAllRules();
    // must have fired 2 times, one for each event equality
    verify(ael1, times(2)).afterMatchFired(any(AfterMatchFiredEvent.class));
    ksession1.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 35 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() 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)

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