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();
}
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();
}
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));
}
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));
}
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));
}
Aggregations