4 Replies Latest reply on Feb 6, 2012 10:18 AM by Miloud Haimoune

    fireAllRules() in a loop issue

    Miloud Haimoune Newbie

      Hi guys,

       

      In my process I need to check if a rule is still valide each three second, the rule is activated and fired oanly the first time

       

      My process  :

       

       

       

      my rule :

       

      rule 'toujours'

      no-loop true

      ruleflow-group 'supplier'

          when

          then

           System.out.println('Rules : ----------- test -------------  ');

      end

       

      to fire the rules :

       

              for (int i = 0; i < 20; i++) {

                  ksession.fireAllRules();

                  Thread.sleep(2000);

              }

       

       

      The execution log :

       

      Hello World

      step 2

      Rules : ----------- test ------------- 

      step 2

      step 2

      step 2

      step 2

      step 2

      step 2

      step 2

      step 2

      step 2

       

      Any idea how to activate the rule each time in the loop:

       

      many thanks.

        • 1. Re: fireAllRules() in a loop issue
          Mauricio Salatino Master

          what are you trying to achieve in the first place? that rule is not checking for nothing.. so the first time it fires.. but nothing changes.. so it will not fire again..

          Try to explain to us what are you trying to achieve, because that doesn't seems like a good solution.

          Cheers

          • 2. Re: fireAllRules() in a loop issue
            Miloud Haimoune Newbie

            Many thanks for your reply,

             

            I have this issue in a more complex process, so I simplified the process for test purposes,

            Basically what I want to do is each time I create a human task, a rule check if a max number has been reached, if this is the case the rule do some actions.

             

            my first test was to chek if the some rule can be activated many times for the some process instance, and how to do it.

             

            have a good weekend,

             

            Cheers,

            • 3. Re: fireAllRules() in a loop issue
              Maciej Swiderski Master

              Rule must be activated (on agenda) to be eligible for invocation by fireAllRules, so multiple invocation of fireAllRules without modification to the session will not have any consequence. Activation happens on insert/update/retract operations on session. So, in your case you should insert or update a fact on session whenever human task is created (for instance with onEntry script) and fire activated rules. To trigger fireAllRules automatically you could use AgendaEventListener registered on the session, example you can find here

               

              HTH

              • 4. Re: fireAllRules() in a loop issue
                Miloud Haimoune Newbie

                I am already using the AgendaEventListener to automatically fireAllRules, I added a ProcessEventListner to update the working memory, and now it's working fine.

                 

                the method to override (more here : https://community.jboss.org/message/608017):

                 

                public void afterVariableChanged(ProcessVariableChangedEvent event) {

                                 org.drools.runtime.rule.FactHandle handle = event.getKnowledgeRuntime().getFactHandle( event.getProcessInstance() );

                                 if(handle != null){

                                                event.getKnowledgeRuntime().update( handle, event.getProcessInstance() );

                                 }

                }

                 

                Many thanks