use of org.kie.api.time.Calendar 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);
}
}
use of org.kie.api.time.Calendar in project drools by kiegroup.
the class TimerAndCalendarTest method testCalendarNormalRuleMultipleCalendars.
@Test(timeout = 10000)
public void testCalendarNormalRuleMultipleCalendars() 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 += "when \n";
str += " String()\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);
Calendar calFalse = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
return false;
}
};
Calendar calTrue = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
return true;
}
};
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);
ksession.getCalendars().set("cal2", calTrue);
timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
ksession.insert("o1");
ksession.fireAllRules();
assertEquals(1, list.size());
ksession.getCalendars().set("cal2", calFalse);
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.insert("o2");
ksession.fireAllRules();
assertEquals(1, list.size());
ksession.getCalendars().set("cal1", calFalse);
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.insert("o3");
ksession.fireAllRules();
assertEquals(1, list.size());
ksession.getCalendars().set("cal1", calTrue);
ksession.getCalendars().set("cal2", calTrue);
timeService.advanceTime(30, TimeUnit.SECONDS);
ksession.insert("o4");
ksession.fireAllRules();
assertEquals(2, list.size());
}
use of org.kie.api.time.Calendar 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.time.Calendar 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.time.Calendar in project drools by kiegroup.
the class TimerTest method testCromTimer.
@Test
public void testCromTimer() throws Exception {
final Locale defaultLoc = Locale.getDefault();
try {
// Because of the date strings in the DRL, fixable with JBRULES-3444
Locale.setDefault(Locale.UK);
final String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + " date-effective \"02-Jan-2010\"\n" + " date-expires \"06-Jan-2010\"\n" + " calendars \"cal1\"\n" + " timer (cron: 0 0 0 * * ?) " + "when \n" + "then \n" + " list.add(\"fired\"); \n" + "end \n";
KieSession ksession = getKieSession(getCepKieModuleModel(), str);
try {
final List list = new ArrayList();
final PseudoClockScheduler timeService = ksession.getSessionClock();
final Date date = convertDate("01-Jan-2010");
final Calendar cal1 = timestamp -> true;
final 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 {
ksession.dispose();
}
} finally {
Locale.setDefault(defaultLoc);
}
}
Aggregations