3 Replies Latest reply on Jan 20, 2012 6:07 AM by Mahi M

    Process variable and rule task

    Cristiano Nicolai Newbie

      Hi all,

       

       

          I'm writing a rule that analyze a variable in my process, and this variable is taken in consideration to fire a rule. So I did something very simple using the from

      conditional element in the left hand side.

       

       

      rule "Test"
                ruleflow-group "test"
                when
                          $processInstance : WorkflowProcessInstance()
                          $str : String() from (String)$processInstance.getVariable("strValue");
                then 
                          System.out.println("str: " + $str);
      end
      
      

       

           My doubt is that when I have this variable "strValue" started as a parameter when I start the process everything works fine but when I have it initialized using a Script Task node before my Rule Task node, the rule isn't fired.

       

      kcontext.setVariable("strValue", "test value");
      

       

           I can check that the value is set if I use it in the right hand side (without from) or getting the variable value from the java code who started the process.

       

          Any guess about this behavior?

         

       


      Thanks!

        • 1. Re: Process variable and rule task
          Francesco Pietrobelli Newbie

          Hi Cristiano

          I think that it depends on when you insert the process instance in working memory.

          Have you try to insert process instance after you set the strValue? for example from a Script Task just before the Business Rule Task that perform the following action:

           

          kcontext.getKnowledgeRuntime().insert(kcontext.getProcessInstance());

           

          Best regards,

          Francesco.

          • 2. Re: Process variable and rule task
            Cristiano Nicolai Newbie

            Francesco,

             

            I'm currently using a ProcessEventListener to insert the process in the working memory just before it starts and remove it after completed. In this way I can handle processes in the working memory in a generic way. But a made your suggestion and it works, what happens is that the process needs to be updated in the working memory after I change a process variable. Now my listener looks like this and seems to be working fine.

             

                    

                      @Override
                      public void afterVariableChanged(ProcessVariableChangedEvent event) {
                                LOGGER.debug("{}", event);
                                LOGGER.debug("Updating ProcessInstance on working memory");
                                FactHandle handle = event.getKnowledgeRuntime().getFactHandle( event.getProcessInstance() );
                                if(handle != null){
                                               event.getKnowledgeRuntime().update( handle, event.getProcessInstance() );
                                }
                      }
            

             

            Many thanks!

            • 3. Re: Process variable and rule task
              Mahi M Newbie

              Hi Can you share some examples of using Process Event Listener and invoking DRL files.