3 Replies Latest reply on Jun 21, 2012 11:17 AM by saig0

    Issue with timer and calendar

    srikant vege Newbie

      Hi,

       

      Somewhere in the web, I got the below code and modified it to execute the process for every 3 hours, daily (weekdays). But I am getting the exception "Unable to resolve the object Alarm()". It seems like Alarm() class is not there in drools 5.3.0-Final version. I could see this class in test drools-compiler test folder but it is not there in the source folder. Please let me know the alternative way to execute this rule.

       

       

      rule "week days rule"

          ruleflow-group "os-cron-service"

          dialect "java"

          calendars "weekDaysCalendar"

          timer (int:0 3h)

          when

              $a : Alarm()

          then

              insert(kcontext.getKnowledgeRuntime().startProcess("com.subProcess.Id", parameters));

      end

       

      Thanks

      Srikant.

        • 1. Re: Issue with timer and calendar
          saig0 Newbie

          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
            srikant vege Newbie

            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 Newbie

              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.