10 Replies Latest reply on Oct 9, 2012 2:39 AM by Thomas Setiabudi

    Accessing process variables in rule-task

    leon_ Newbie

      A simple question that has been bugging me for over a day now:

       

      I have a simple process where user input needs to be evaluated by a rule-set, i.e. my rule-task needs to access read/write the variables in my (Hash)map. I do not have a .java file as a model, so how can I access the BPMN process variables in my rule-task?

       

      I attached the related files, do not let the Dutch language scare you off :-)

        • 1. Re: Accessing process variables in rule-task
          leon_ Newbie

          I found this related thread http://drools.46999.n3.nabble.com/Rule-tasks-and-gwt-console-td1711764.html, but the answer does not make things much clearer for me...

          Where do I have to change these things, in my ProcessTest.java?

          • 2. Re: Accessing process variables in rule-task
            Esteban Aliverti Master

            I'm not sure if I understood the question, but if what you need is to access process variables inside your rules, you can use something like this:

             

             

            rule A


            ruleflow-group "ABC"

            when


            $process: WorkflowProcessInstance()

            then


            Map variable = (Map)((WorkflowProcessInstance)kcontext.getKnowledgeRuntime().getProcessInstance($process.getId())).getVariable("variableName");

            end

            • 3. Re: Accessing process variables in rule-task
              leon_ Newbie

              Thanks very much for your reply Esteban.

              I am sorry for being a bit unclear. It is my writing style, and I am working on it to define problems in a more clear way :-)

               

              So, if I have a variable "monkey" in my BPMN process (which is added to the map btw), where the user has given a value to in an .ftl form, let's say "chimp". And I want a rule to fire based on the value "chimp" I would write something like:

               

              rule A

                       

                        ruleflow-group "chimps_are_nice"

               

                        when

                              

                                            (Map)((WorkflowProcessInstance) kcontext.getKnowledgeRuntime().getProcessInstance(WorkflowProcessInstance(.getId())).getVariable("monkey") ==  "chimp";

               

                        then

                            

                                           (Map) (WorkflowProcessInstance (kcontext.getKnowledgeRuntime().getProcessInstance(WorkflowProcessInstance().getId()).setVariable("character") =  "nice";

               

               

              I realise I can store the path into a variable to shorten the code. But this is what it would come down to I guess?
              I am retrieving the variable from the map, is this neccesary or can it be ommitted?

              I assum to set variables I use setVariable, am I right?

              • 4. Re: Accessing process variables in rule-task
                Esteban Aliverti Master

                The LHS of your rule is wrong. I think you need to use an eval() if you want to invoke methods that contains parameters.

                 

                I also remembered another way to get process variables in the RHS, but I don't remember if it has some caevats:

                 

                when

                   $processInstance: WorkflowProcessInstance()

                then

                   String monkey = (String)$processInstance.getVariable("monkey"); //This will return "chimp"

                  

                   $processInstance.setVariable("character","nice");

                end

                • 5. Re: Accessing process variables in rule-task
                  leon_ Newbie

                  Thnx again Esteban!

                  It seems that the class WorkflowProcessInstance cannot use the methods get/setVariable. Googling on it brings me e.g. to the v3 of jbpm showing that only ContextInstance can use get and set.

                   

                  Which brings me to the question if "kcontext" can be used in a DRL file without imports. If not, what should be imported and must kcontext be declared in the drl file?

                   

                  I still have not found a way to access, eval and change process variables in a drl file...

                  • 6. Re: Accessing process variables in rule-task
                    leon_ Newbie

                    The following rules do not seem to fire. I have included ksession.fireUntilHalt() in my procestest. Do I miss anything else?!

                     

                    rule "regel_overheid"

                    ruleflow-group "RegelsRechtwijzer"    

                     

                         when     

                              $proces : WorkflowProcessInstance()

                              $partij : String() from (String)$proces.getVariable("conflictpartij");

                              eval($partij == "overheid");    

                        

                         then

                              ((WorkflowProcessInstance)kcontext.getKnowledgeRuntime().getProcessInstance($proces.getId())).setVariable("conflictpartij", "kantonrechter");

                    end

                     

                    rule "debug_regel"

                    ruleflow-group "RegelsRechtwijzer"    

                    #include attributes such as "salience" here...    

                         when

                              $proces : WorkflowProcessInstance();            

                        

                         then    

                              $proces.setVariable("conflictoplossing", "Kantonrechter");    

                              update($proces);

                    end

                    • 7. Re: Accessing process variables in rule-task
                      Esteban Aliverti Master

                      Sorry, I forgot to mention that the WorkflowProcessInstance object is not a fact by default. You need to insert it before the Rule Node is executed. One way to do this is to add a Script Task node that executes:

                       

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

                      • 8. Re: Accessing process variables in rule-task
                        leon_ Newbie

                        So you did, it seems :-) It is working now, I cannot thank you enough for sharing this information.

                        I think ppl with similar problems will solve it reading this thread. If not, I am happy to share the information!

                        • 9. Re: Accessing process variables in rule-task
                          Hunor-Attila Kerekes Newbie

                          Hello Esteban,

                           

                          I have the same problem what leon has. I have added to my fisrt human task at the entry action the kcontext.getKnowledgeRuntime().insert(kcontext.getProcessInstance()); script. My rule currently look like this :

                          import com.sample.User;

                          import org.drools.runtime.process.WorkflowProcessInstance

                           

                           

                           

                          rule "AgeCheck3"

                          ruleflow-group "AgeCheck"

                           

                           

                              when

                                  processInstance : WorkflowProcessInstance()

                                  age: Integer() from (Integer)((WorkflowProcessInstance)processInstance).getVariable("Age");

                                  eval(age < 18);

                              then

                                  processInstance.setVariable("Name", new String("Denied"));

                                  System.out.println(age);

                                  

                          end

                           

                           

                          If I am removing the age declaration with eval(true) the rule is fired and the name is set to Denied. The compiler accept the declaration, no errors during te running phase, but the rule is not fired.

                          Can you help me plese? I am doing my master thesis and this is also a small part of it.

                           

                          Thank you in advance.

                          Kind regards, Hunor

                          • 10. Re: Accessing process variables in rule-task
                            Thomas Setiabudi Expert

                            Hi,

                             

                            Is it possible to do this from guvnor?

                            How do you import org.drools.runtime.process.WorkflowProcessInstance in the rule file?

                             

                             

                            Note: I am using JBPM 5.3

                             

                            Regards,

                            Thomas Setiabudi