use of org.kie.api.time.SessionClock in project drools by kiegroup.
the class TimerAndCalendarTest method testSharedTimers.
@Test
public void testSharedTimers() throws Exception {
// DROOLS-451
String str = "package org.simple \n" + "global java.util.List list \n" + "rule R1\n" + " timer (int:30s 10s) " + "when \n" + " $i: Integer()\n" + "then \n" + " System.out.println(\"1\");\n" + " list.add(\"1\"); \n" + "end \n" + "rule R2\n" + " timer (int:30s 10s) " + "when \n" + " $i: Integer()\n" + "then \n" + " System.out.println(\"2\");\n" + " list.add(\"2\"); \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);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
SessionClock clock = ksession.getSessionClock();
SessionPseudoClock pseudoClock = (SessionPseudoClock) clock;
ksession.insert(1);
ksession.fireAllRules();
pseudoClock.advanceTime(35, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("1", "2")));
}
use of org.kie.api.time.SessionClock 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.time.SessionClock in project drools by kiegroup.
the class TimerAndCalendarTest method testTimerRuleAfterCronReloadSession.
@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterCronReloadSession() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = createSession(kbase);
// must advance time or it won't save.
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
// if we do not call 'ksession.fireAllRules()', this test will run successfully.
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
// build timer rule, if the rule is fired, the list size will increase every 300ms
String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + " timer (cron: * * * * * ?) \n" + "when \n" + "then \n" + " list.add(list.size()); \n" + " end";
Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
kbase.addPackages(kpackages);
List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
ksession.setGlobal("list", list);
ksession.setGlobal("list", list);
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
ksession.setGlobal("list", list);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
Assert.assertEquals(1, list.size());
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(3, TimeUnit.SECONDS);
Assert.assertEquals(4, list.size());
ksession = disposeAndReloadSession(ksession, kbase);
ksession.setGlobal("list", list);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(2, TimeUnit.SECONDS);
// if the rule is fired, the list size will greater than one.
Assert.assertEquals(6, list.size());
}
use of org.kie.api.time.SessionClock in project drools by kiegroup.
the class TimerAndCalendarTest method testTimerRuleAfterIntReloadSession.
@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterIntReloadSession() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = createSession(kbase);
// must advance time or it won't save.
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
// if we do not call 'ksession.fireAllRules()', this test will run successfully.
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
// build timer rule, if the rule is fired, the list size will increase every 500ms
String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + " timer (int:1000 500) \n" + "when \n" + "then \n" + " list.add(list.size()); \n" + " end";
Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
kbase.addPackages(kpackages);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
ksession.setGlobal("list", list);
Assert.assertEquals(0, list.size());
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(1700, TimeUnit.MILLISECONDS);
Assert.assertEquals(2, list.size());
ksession = disposeAndReloadSession(ksession, kbase);
ksession.setGlobal("list", list);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(1000, TimeUnit.MILLISECONDS);
// if the rule is fired, the list size will greater than one.
Assert.assertEquals(4, list.size());
}
use of org.kie.api.time.SessionClock in project drools by kiegroup.
the class MarshallingTest method testMarshallEntryPointsWithSlidingTimeWindow.
@Test
@Ignore("beta4 phreak")
public void testMarshallEntryPointsWithSlidingTimeWindow() throws Exception {
String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "import java.util.List\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + " $l : List() from collect( A() over window:time(30s) from entry-point 'a-ep') \n" + "then\n" + " list.add( $l );" + "end\n";
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(conf, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get("pseudo"));
ksconf.setOption(TimerJobFactoryOption.get("trackable"));
KieSession ksession = createKnowledgeSession(kbase, ksconf);
List list = new ArrayList();
ksession.setGlobal("list", list);
EntryPoint aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
list.clear();
ksession.fireAllRules();
ksession = marsallStatefulKnowledgeSession(ksession);
assertEquals(2, ((List) list.get(0)).size());
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(15, TimeUnit.SECONDS);
ksession = marsallStatefulKnowledgeSession(ksession);
aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
list.clear();
ksession.fireAllRules();
ksession = marsallStatefulKnowledgeSession(ksession);
assertEquals(4, ((List) list.get(0)).size());
timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession = marsallStatefulKnowledgeSession(ksession);
list.clear();
ksession.fireAllRules();
assertEquals(2, ((List) list.get(0)).size());
}
Aggregations