Search in sources :

Example 56 with AgendaEventListener

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

the class ActivationIteratorTest method testEagerEvaluation.

@Test(timeout = 10000)
public void testEagerEvaluation() throws Exception {
    String str = "package org.simple \n" + "rule xxx @Propagation(EAGER) \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n" + "rule yyy @Propagation(EAGER) \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n";
    KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession(conf, null);
    final List list = new ArrayList();
    AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {

        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add("activated");
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.insert("test");
    assertEquals(2, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) 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) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 57 with AgendaEventListener

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

the class ActivationIteratorTest method testFilteredEagerEvaluation.

@Test(timeout = 10000)
public void testFilteredEagerEvaluation() throws Exception {
    String str = "package org.simple \n" + "rule xxx @Propagation(EAGER) \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n" + "rule yyy @Propagation(EAGER) \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n";
    KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(new ForceEagerActivationOption.FILTERED(new ForceEagerActivationFilter() {

        @Override
        public boolean accept(Rule rule) {
            return rule.getName().equals("xxx");
        }
    }));
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession(conf, null);
    final List list = new ArrayList();
    AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {

        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add("activated");
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.insert("test");
    ((InternalWorkingMemory) ksession).flushPropagations();
    assertEquals(1, list.size());
}
Also used : ForceEagerActivationOption(org.kie.internal.runtime.conf.ForceEagerActivationOption) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ForceEagerActivationFilter(org.kie.internal.runtime.conf.ForceEagerActivationFilter) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 58 with AgendaEventListener

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

the class PseudoClockEventsTest method testEvenFirePseudoClockRuleB.

@Test(timeout = 10000)
public void testEvenFirePseudoClockRuleB() throws Exception {
    AgendaEventListener ael = mock(AgendaEventListener.class);
    processStocks(evalFirePseudoClockStockCount, ael, evalFirePseudoClockDeclaration + evalFirePseudoClockRuleB);
    verify(ael, times(evalFirePseudoClockStockCount - 1)).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 59 with AgendaEventListener

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

the class PseudoClockEventsTest method testEvenFirePseudoClockRuleA.

@Test(timeout = 10000)
public void testEvenFirePseudoClockRuleA() throws Exception {
    AgendaEventListener ael = mock(AgendaEventListener.class);
    processStocks(evalFirePseudoClockStockCount, ael, evalFirePseudoClockDeclaration + evalFirePseudoClockRuleA);
    verify(ael, times(evalFirePseudoClockStockCount * (evalFirePseudoClockStockCount - 1) / 2)).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 60 with AgendaEventListener

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

the class EnabledTest method testEnabledExpression2.

@Test
public void testEnabledExpression2() {
    final String drl = "import " + MyFact.class.getName() + ";\n" + "rule R1\n" + "    enabled( rule.name == $f.name )" + "when\n" + "   $f : MyFact()\n" + "then end\n" + "rule R2\n" + "when\n" + "   MyFact( name == \"R2\" )\n" + "then end\n";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-enabled", "1.0.0");
    final KieModule km = KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl);
    // Create a session and fire rules
    final KieContainer kc = ks.newKieContainer(km.getReleaseId());
    AgendaEventListener ael = mock(AgendaEventListener.class);
    KieSession ksession = kc.newKieSession();
    try {
        ksession.addEventListener(ael);
        ksession.insert(new MyFact("R1", null));
        assertEquals(1, ksession.fireAllRules());
        ksession.dispose();
        ArgumentCaptor<AfterMatchFiredEvent> event = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        verify(ael).afterMatchFired(event.capture());
        assertEquals("R1", event.getValue().getMatch().getRule().getName());
        ael = mock(AgendaEventListener.class);
        ksession.dispose();
        ksession = kc.newKieSession();
        ksession.addEventListener(ael);
        ksession.insert(new MyFact("R2", null));
        assertEquals(1, ksession.fireAllRules());
        ksession.dispose();
        event = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        verify(ael).afterMatchFired(event.capture());
        assertEquals("R2", event.getValue().getMatch().getRule().getName());
    } finally {
        ksession.dispose();
    }
}
Also used : MyFact(org.drools.testcoverage.common.model.MyFact) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieContainer(org.kie.api.runtime.KieContainer) 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