use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class MarshallingTest method testScheduledActivation.
@Test
@Ignore("This test is suspicious to say the least...")
public void testScheduledActivation() {
KnowledgeBaseImpl knowledgeBase = (KnowledgeBaseImpl) KnowledgeBaseFactory.newKnowledgeBase();
KnowledgePackageImpl impl = new KnowledgePackageImpl("test");
BuildContext buildContext = new BuildContext(knowledgeBase);
// simple rule that fires after 10 seconds
final RuleImpl rule = new RuleImpl("test-rule");
new RuleTerminalNode(1, new MockTupleSource(2), rule, rule.getLhs(), 0, buildContext);
final List<String> fired = new ArrayList<String>();
rule.setConsequence(new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
fired.add("a");
}
public String getName() {
return "default";
}
});
rule.setTimer(new DurationTimer(10000));
rule.setPackage("test");
impl.addRule(rule);
knowledgeBase.addPackages(Collections.singleton(impl));
SessionConfiguration config = SessionConfiguration.newInstance();
config.setClockType(ClockType.PSEUDO_CLOCK);
KieSession ksession = knowledgeBase.newKieSession(config, KieServices.get().newEnvironment());
PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase);
ksession.insert("cheese");
assertTrue(fired.isEmpty());
// marshall, then unmarshall session
readWrite(knowledgeBase, ksession, config);
// the activations should fire after 10 seconds
assertTrue(fired.isEmpty());
scheduler.advanceTime(12, TimeUnit.SECONDS);
assertFalse(fired.isEmpty());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class TimerAndCalendarTest method testIntervalTimer.
@Test(timeout = 10000)
public void testIntervalTimer() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " timer (int:30s 10s) ";
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();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(15, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class TimerAndCalendarTest method testCronTimer.
@Test(timeout = 10000)
public void testCronTimer() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \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);
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(30, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(30, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class TimerAndCalendarTest method testIntervalTimerWithoutFire.
@Test(timeout = 10000)
public void testIntervalTimerWithoutFire() throws Exception {
String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + " timer (int:30s 10s) " + "when \n" + "then \n" + " list.add(\"fired\"); \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 list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(35, TimeUnit.SECONDS);
assertEquals(1, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
assertEquals(2, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
assertEquals(3, list.size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class TimerAndCalendarTest method testCalendarsWithCronAndStartAndEnd.
@Test(timeout = 10000)
public void testCalendarsWithCronAndStartAndEnd() throws Exception {
Locale defaultLoc = Locale.getDefault();
try {
// Because of the date strings in the DRL, fixable with JBRULES-3444
Locale.setDefault(Locale.UK);
String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + " date-effective \"2-JAN-2010\"\n" + " date-expires \"6-JAN-2010\"\n" + " calendars \"cal1\"\n" + " timer (cron: 0 0 0 * * ?) " + "when \n" + "then \n" + " list.add(\"fired\"); \n" + "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("dd-MMM-yyyy", Locale.UK);
Date date = df.parse("1-JAN-2010");
Calendar cal1 = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
return true;
}
};
long oneDay = 60 * 60 * 24;
ksession.getCalendars().set("cal1", cal1);
ksession.setGlobal("list", list);
timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
// day 3
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
// day 5
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
} finally {
Locale.setDefault(defaultLoc);
}
}
Aggregations