Search in sources :

Example 1 with AgendaEventListener

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

the class TimerAndCalendarTest method testRaceConditionWithTimedRuleExectionOption.

@Test(timeout = 10000)
@Ignore("the listener callback holds some locks so blocking in it is not safe")
public void testRaceConditionWithTimedRuleExectionOption() throws Exception {
    // BZ-1073880
    String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx @Propagation(EAGER)\n" + "  timer (int:30s 10s) " + "when \n" + "  $s: String()\n" + "then \n" + "  list.add($s); \n" + "end  \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    conf.setOption(TimedRuleExecutionOption.YES);
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final AtomicBoolean aBool = new AtomicBoolean(true);
    AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {

        public void afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent event) {
            try {
                if (aBool.get()) {
                    barrier.await();
                    aBool.set(false);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    ksession.addEventListener(agendaEventListener);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    // Using the Pseudo Clock.
    SessionClock clock = ksession.getSessionClock();
    SessionPseudoClock pseudoClock = (SessionPseudoClock) clock;
    // Insert the event.
    String eventOne = "one";
    ksession.insert(eventOne);
    // Advance the time .... so the timer will fire.
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    // Rule doesn't fire in PHREAK. This is because you need to call 'fireAllRules' after you've inserted the fact, otherwise the timer
    // job is not created.
    ksession.fireAllRules();
    // Rule still doesn't fire, because the DefaultTimerJob is created now, and now we need to advance the timer again.
    pseudoClock.advanceTime(30000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    String eventTwo = "two";
    ksession.insert(eventTwo);
    ksession.fireAllRules();
    // 60
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    // 70
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    // From here, the second rule should fire.
    // phaser.register();
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    // Now 2 rules have fired, and those will now fire every 10 seconds.
    pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
    aBool.set(true);
    barrier.await();
    barrier.reset();
    pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
    aBool.set(true);
    barrier.await();
    barrier.reset();
    ksession.destroy();
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) SessionClock(org.kie.api.time.SessionClock) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) Arrays.asList(java.util.Arrays.asList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with AgendaEventListener

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

the class Misc2Test method testReuseAgendaAfterException.

@Test
public void testReuseAgendaAfterException() throws Exception {
    // JBRULES-3677
    String str = "import org.drools.compiler.Person;\n" + "global java.util.List results;" + "rule R1\n" + "ruleflow-group \"test\"\n" + "when\n" + "   Person( $age : age ) \n" + "then\n" + "   if ($age > 40) throw new RuntimeException(\"Too old\");\n" + "   results.add(\"OK\");" + "end";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = kbase.newKieSession();
    List<String> res = new ArrayList<String>();
    ksession.setGlobal("results", res);
    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) {
        }

        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) {
            ksession.fireAllRules();
        }

        public void beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
        }

        public void afterRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
        }
    };
    ksession.addEventListener(agendaEventListener);
    FactHandle fact1 = ksession.insert(new Person("Mario", 38));
    ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("test");
    ksession.fireAllRules();
    assertEquals(1, res.size());
    res.clear();
    ksession.delete(fact1);
    FactHandle fact2 = ksession.insert(new Person("Mario", 48));
    try {
        ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("test");
        ksession.fireAllRules();
        fail("should throw an Exception");
    } catch (Exception e) {
    }
    ksession.delete(fact2);
    assertEquals(0, res.size());
    // try to reuse the ksession after the Exception
    FactHandle fact3 = ksession.insert(new Person("Mario", 38));
    ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("test");
    ksession.fireAllRules();
    assertEquals(1, res.size());
    ksession.delete(fact3);
    ksession.dispose();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) InternalAgenda(org.drools.core.common.InternalAgenda) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) 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) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 3 with AgendaEventListener

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

the class PseudoClockEventsTest method testEvenFirePseudoClockRuleA.

@Test(timeout = 6000)
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 4 with AgendaEventListener

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

the class PseudoClockEventsTest method testEvenFirePseudoClockRuleB.

@Test(timeout = 6000)
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 5 with AgendaEventListener

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

the class FirstOrderLogicTest method testOrWithVariableResolution2.

// JBRULES-2526
@Test
public void testOrWithVariableResolution2() throws Exception {
    // KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl");
    // KieSession workingMemory = createKnowledgeSession(kbase);
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_OrCEFollowedByMultipleEval2.drl", FirstOrderLogicTest.class), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = createKnowledgeSession(kbase);
    final AgendaEventListener al = mock(AgendaEventListener.class);
    ksession.addEventListener(al);
    ksession.insert(new FactA("a"));
    ksession.insert(new FactB("b"));
    ksession.insert(new FactC("c"));
    ksession.fireAllRules();
    verify(al, times(8)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) FactA(org.drools.compiler.FactA) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactB(org.drools.compiler.FactB) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) FactC(org.drools.compiler.FactC) 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