use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class TimerAndCalendarTest method testCalendarNormalRuleSingleCalendar.
@Test(timeout = 10000)
public void testCalendarNormalRuleSingleCalendar() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " calendars \"cal1\"\n";
str += "when \n";
str += " String()\n";
str += "then \n";
str += " list.add(\"fired\"); \n";
str += "end \n";
Calendar calFalse = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
return false;
}
};
Calendar calTrue = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
return true;
}
};
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = df.parse("2009-01-01T00:00:00.000-0000");
ksession.getCalendars().set("cal1", calTrue);
timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
ksession.insert("o1");
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.insert("o2");
ksession.fireAllRules();
assertEquals(2, list.size());
ksession.getCalendars().set("cal1", calFalse);
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.insert("o3");
ksession.fireAllRules();
assertEquals(2, list.size());
ksession.getCalendars().set("cal1", calTrue);
timeService.advanceTime(30, TimeUnit.SECONDS);
ksession.insert("o4");
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class TimerAndCalendarTest method checkIntervalTimerWithStringExpressions.
private void checkIntervalTimerWithStringExpressions(boolean useExprForStart, String startTime) throws Exception {
String str = "package org.simple;\n" + "global java.util.List list;\n" + "\n" + "declare Bean\n" + " delay : String = \"30s\"\n" + " period : long = 60000\n" + " start : String = \"3-JAN-2010\"\n" + "end\n" + "\n" + "rule init \n" + "when \n" + "then \n" + " insert( new Bean() );\n" + "end \n" + "\n" + "rule xxx\n" + " salience ($d) \n" + " timer( expr: $d, $p; start=" + (useExprForStart ? "$s" : "3-JAN-2010") + " )\n" + "when\n" + " Bean( $d : delay, $p : period, $s : start )\n" + "then\n" + " list.add( \"fired\" );\n" + "end";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.setStartupTime(DateUtils.parseDate(startTime).getTime());
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(40, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
timeService.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
// simulate a pause in the use of the engine by advancing the system clock
timeService.setStartupTime(DateUtils.parseDate("3-MAR-2010").getTime());
list.clear();
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
// fires once to recover from missing activation
assertEquals(1, list.size());
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
timeService.advanceTime(40, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
timeService.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(4, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class TimerAndCalendarTest method testExprIntervalTimerRaceCondition.
@Test(timeout = 10000)
public void testExprIntervalTimerRaceCondition() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " timer (expr: $i, $i) \n";
str += "when \n";
str += " $i : Long() \n";
str += "then \n";
str += " list.add(\"fired\"); \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
FactHandle fh = (FactHandle) ksession.insert(10000l);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(17, TimeUnit.SECONDS);
ksession.update(fh, 5000l);
ksession.fireAllRules();
assertEquals(2, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class TimerAndCalendarTest method testCalendarsWithCron.
@Test(timeout = 10000)
public void testCalendarsWithCron() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " calendars \"cal1\", \"cal2\"\n";
str += " timer (cron:15 * * * * ?) ";
str += "when \n";
str += "then \n";
str += " list.add(\"fired\"); \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = df.parse("2009-01-01T00:00:00.000-0000");
timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
final Date date1 = new Date(date.getTime() + (15 * 1000));
final Date date2 = new Date(date1.getTime() + (60 * 1000));
final Date date3 = new Date(date2.getTime() + (60 * 1000));
final Date date4 = new Date(date3.getTime() + (60 * 1000));
Calendar cal1 = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
if (timestamp == date1.getTime()) {
return true;
} else if (timestamp == date4.getTime()) {
return false;
} else {
return true;
}
}
};
Calendar cal2 = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
if (timestamp == date2.getTime()) {
return false;
} else if (timestamp == date3.getTime()) {
return true;
} else {
return true;
}
}
};
ksession.getCalendars().set("cal1", cal1);
ksession.getCalendars().set("cal2", cal2);
ksession.setGlobal("list", list);
ksession.fireAllRules();
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
timeService.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
timeService.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
timeService.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(4, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration 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();
}
Aggregations