-
1. Re: Issue with timer and calendar
saig0 Jun 21, 2012 3:00 AM (in response to srikanthvege1)Hi Srikant,
you need to declare the class Alarm as a fact or an event and import it.
import your.package.Alarm; declare Alarm @role( fact) // or @role( event) end
The class Alarm can be a pojo or be declared in rule.
The rule "week days rule" will fire when you insert a object of class Alarm.
-
2. Re: Issue with timer and calendar
srikanthvege1 Jun 21, 2012 10:12 AM (in response to saig0)Hi,
Thanks for you help. Now I got another issue.
I have 2 rules (mentioned below). 1st rule should be executed on weekdays and the next rule should be executed on Holidays.
rule "week days rule"
ruleflow-group "os-cron-service"
calendars "weekDayCalendar"
timer (int:0 5h)
when
Alarm()
then
System.out.println("Week Days rule executed..");
end
rule "holiday rule"
ruleflow-group "os-cron-service"
calendars "holidayCalendar"
timer (int:0 2h)
when
Alarm()
then
System.out.println("Holidays rule executed..");
end
I have created two calendar objects (one is for weekdays & another for holidays) and set them to ksession. below is the code.
WeeklyCalendar weekCalendar = new WeeklyCalendar();
Calendar weekDaysCalendar= QuartzHelper.quartzCalendarAdapter(weekCalendar);
ksession.getCalendars().set("weekDayCalendar", weekDaysCalendar);
HolidayCalendar holidayCalendar = new HolidayCalendar();
Calendar holidayCal = QuartzHelper.quartzCalendarAdapter(holidayCalendar);
ksession.getCalendars().set("holidayCalendar", holidayCal);
When I execute the rules, both the rules are getting executed and it seems like it is not considering the calendar objects. I even tried with CronCalendar also by giving the cron expression. But no luck.
Please let me know the mistake that I am doing here. I am using drools 5.3 and jbpm 5.2
Thanks
Srikant.
-
3. Re: Issue with timer and calendar
saig0 Jun 21, 2012 11:17 AM (in response to srikanthvege1)Hi Srikant,
I copied your rule and wrote a test:
@Test public void testCalenderRule() throws InterruptedException { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory .newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("drools/" + "expert.drl"), ResourceType.DRL); KnowledgeBaseConfiguration config = KnowledgeBaseFactory .newKnowledgeBaseConfiguration(); config.setOption(EventProcessingOption.STREAM); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); KnowledgeSessionConfiguration conf = KnowledgeBaseFactory .newKnowledgeSessionConfiguration(); ksession = kbase.newStatefulKnowledgeSession(conf, null); ksession.getCalendars().set("weekDayCalendar", new Calendar() { public boolean isTimeIncluded(long timestamp) { Date date = new Date(timestamp); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); int dayOfWeek = cal.get(GregorianCalendar.DAY_OF_WEEK); if (dayOfWeek != GregorianCalendar.SATURDAY && dayOfWeek != GregorianCalendar.SUNDAY) { return true; } else { return false; } } }); ksession.getCalendars().set("holidayCalendar", new Calendar() { public boolean isTimeIncluded(long timestamp) { Date date = new Date(timestamp); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); int dayOfWeek = cal.get(GregorianCalendar.DAY_OF_WEEK); if (dayOfWeek == GregorianCalendar.SATURDAY || dayOfWeek == GregorianCalendar.SUNDAY) { return true; } else { return false; } } }); ksession.insert(new Alarm()); ksession.fireAllRules(); }
Only the "week days rule" is getting executed as excepted. For me it works fine.