Search in sources :

Example 46 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 47 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 48 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 49 with AgendaEventListener

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

the class PseudoClockEventsTest method testEvenFirePseudoClockRulesAB.

@Test(timeout = 60000)
public void testEvenFirePseudoClockRulesAB() throws Exception {
    AgendaEventListener ael = mock(AgendaEventListener.class);
    processStocks(evalFirePseudoClockStockCount, ael, evalFirePseudoClockDeclaration + evalFirePseudoClockRuleA + evalFirePseudoClockRuleB);
    final int expectedActivationCount = evalFirePseudoClockStockCount * (evalFirePseudoClockStockCount - 1) / 2 + evalFirePseudoClockStockCount - 1;
    verify(ael, times(expectedActivationCount)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
Also used : AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 50 with AgendaEventListener

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

the class StarImportTest method starImportedFactAlsoDeclaredInDRL.

/**
 * Tests that rule fires if given a fact that is imported using
 * "star" import, while it is also declared in DRL.
 */
@Test
public void starImportedFactAlsoDeclaredInDRL() throws Exception {
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.insert(new TestEvent("event 1"));
    ksession.fireAllRules();
    // the rule should have fired exactly once
    verify(ael, times(1)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
Also used : TestEvent(org.drools.compiler.integrationtests.facts.TestEvent) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) 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