1 2 Previous Next 15 Replies Latest reply on Mar 21, 2016 8:15 AM by ammu_upadhyay

    How to get a process instance id within a sequence flow "condition expression"?

    dmitry.erkin

      Hello!

      My process has a gateway and two outgoing sequence flows. For each flow I want to set up a condition based on facts stored in a session. The problem is that I can not get a process instance id:

       

      code like this does not work:

       

      WorkflowProcessInstanceImpl(id == kcontext.processInstance.id, $request: variables["request"])

      Request($id:id) from $request

      not exists(Issue(requestid == $id))

       

      sample code from "jbpm5 developer guide":

       

      WorkflowProcessInstanceImpl( $person: variables["person"])

      Person( age< 18 ) from $person

       

      looks like it chooses a particular process instance rather by person's age than by instance id

       

      easier speaking I have zero or more Issue() facts inserted into a session for each Request(). an issue has a requestid. In case there are no issues for particular request the process must go one way.

      so rule  must look like

       

      not exists(Issue(requestid == $id))

       

      any ideas?

       

       

      Regards,

      Dmitry Erkin

        • 1. Re: How to get a process instance id within a sequence flow "condition expression"?
          swiderski.maciej

          I can see you use rules as the condition language, so you need to insert process instance into working memory (ksession) before you can access it inside the rule. So you can do it in two ways:

          - first createProcessInstance, then insert it into ksession, and start the process instance

          - employ process event listener to do it for you, take a look here for sample on how it can be done

           

          HTH

          • 2. Re: How to get a process instance id within a sequence flow "condition expression"?
            dmitry.erkin

            Hello, Maciej!

             

            yes I have my process instances inserted as facts:

             

            package defaultPackage

            dialect "mvel"

             

            import defaultPackage.Request;

            import java.util.Map;

            import java.util.HashMap;

            import org.drools.runtime.process.ProcessInstance;

            import org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl;

             

            rule "start"

                when

                    $rq:Request()

                    not(WorkflowProcessInstanceImpl($request: variables["request"]) and (Request(this == $rq) from $request))

                then

                    System.out.println(" >>> The approveRequest process will be started!");

                    Map params = new HashMap();

                    params.put("request", $rq);

                    ProcessInstance pi = kcontext.getKnowledgeRuntime().createProcessInstance("defaultPackage.approveRequest", params);

                    insert(pi);

                    kcontext.getKnowledgeRuntime().startProcessInstance(pi.getId());

            end

             

            What I do not get is how to get a certain process instance attribute's value in a sequence flow's condition expression. By certain instance I mean the instance which is created to approve certain request. I have a request pocess variable, a request.id attribute for that variable and I want to make an inference over facts which relate to that certain request. A relation between facts stored in a session and certain request is set up through a request.id value.

             

            Regards,

            Dmitry Erkin

            • 3. Re: How to get a process instance id within a sequence flow "condition expression"?
              dmitry.erkin

              if I use it in a condition expression does WorkflowProcessInstanceImpl( $customer: variables["customer"]) mean that inference will be based on an instance of customer variable which belongs to one certain process instance only? Or customer variables from all process instances which are in a session will be considered as facts?

              • 4. Re: How to get a process instance id within a sequence flow "condition expression"?
                swiderski.maciej

                Dmitry, what you mean by process instance's attributes? Once process instance is inside the session you can access their process variables and match them with other facts in the session.

                 

                as far as I know process variables are not facts themselves if only process instance is inserted into session. Only process instance is a fact then - not rule expert so ...

                 

                HTH

                • 5. Re: How to get a process instance id within a sequence flow "condition expression"?
                  dmitry.erkin

                  Hello, Maciej !

                   

                  let me imagine that there are a lot of process instances are in the same session as facts. I want one certain of those instances to change its state by firing a sequence flow condition expression. And I think I found out the answer/ The key is ProcessVariable!

                   

                  ProcessVariable(name == "request", $value: value)

                  Request($id:id) from $value

                  not exists(Issue(requestid == $id))

                   

                  thank you

                  • 6. Re: How to get a process instance id within a sequence flow "condition expression"?
                    dmitry.erkin

                    nope, I think I am wrong, the condition expression from my previous post (ProcessVariable) which is based on Salaboy sample does not allow me to get "request" from certain process instance. It just allows to make an inference over all requests inserted in a session.

                     

                    So I still can not get how to get instanceid for the current process instance in condition expression.

                     

                    Regards,

                    Dmitry Erkin

                     

                    • 7. Re: How to get a process instance id within a sequence flow "condition expression"?
                      swiderski.maciej

                      have you tried with such approach to the rule:

                       

                      $processInstance : WorkflowProcessInstance()

                      $request : Request() from $processInstance.getVariable("request")

                      not exists(Issue(requestId == $request.id)

                       

                      have not run that rule so it could have some minor issues but it should give you what you looking for I think (if I understood the need right)

                       

                      HTH

                      • 8. Re: How to get a process instance id within a sequence flow "condition expression"?
                        dmitry.erkin

                        Hello, Maciej !

                         

                        It says: Unable to resolve ObjectType 'exists' if I try to build a package which includes a process with  the expression you suggested:

                         

                        $processInstance : org.drools.runtime.process.WorkflowProcessInstance()

                        $request : Request($id:id) from $processInstance.getVariable("request")

                        not exists(Issue(requestid == $id))

                         

                        My first question is what is wrong with "exists"?

                        Secondly, are you sure that "$processInstance : org.drools.runtime.process.WorkflowProcessInstance()" would give me just one fact which represents current instance in case there are many instances are inserted in a session as facts?

                         

                        I really appreciate your efforts, thank you

                         

                         

                        Regards,

                         

                        Dmitry Erkin

                        • 9. Re: How to get a process instance id within a sequence flow "condition expression"?
                          swiderski.maciej

                          Dmitry, here is a complete rule that I just tested and looks it works and does what expected (at least based on my understanding).

                           

                          rule "Your First Rule" ruleflow-group "MyRuleFlow"

                              when

                                  $processInstance : WorkflowProcessInstance()

                                  $request : Request($id:id) from $processInstance.getVariable("request")

                                  not (exists (Issue(requestId == $id)))

                              then

                                  System.out.println("Got request with id " + $request.getId());

                           

                          end

                          • 10. Re: How to get a process instance id within a sequence flow "condition expression"?
                            swiderski.maciej

                            forgot to answer the other question - the rule will be executed for each process instance that is available in working memory that satisfy rule condition. So once you process a given process instance you could either retract the process instance from session or make sure that for it condition will not be meet any more, for example insert Issue instance with the id of request.

                             

                            HTH

                            • 11. Re: How to get a process instance id within a sequence flow "condition expression"?
                              dmitry.erkin

                              Maciej,

                               

                              In my case

                               

                                      $processInstance : WorkflowProcessInstance()

                                      $request : Request($id:id) from $processInstance.getVariable("request")

                                      not (exists (Issue(requestId == $id)))

                               

                               

                              is not a rule but a flow condition in a jbpm process.

                               

                              So does that condition return "true" for the process instance "A" (requestId==1) if there are not any issues like Issue(requestId==1) in the session but there is another process instance "B" (requestId==2) in the same session and there are some issues like Issue(requestId==2) in the session as well?

                               

                               

                              Regards,

                              Dmitry Erkin

                              • 12. Re: How to get a process instance id within a sequence flow "condition expression"?
                                swiderski.maciej

                                Dmitry Erkin wrote:

                                 

                                So does that condition return "true" for the process instance "A" (requestId==1) if there are not any issues like Issue(requestId==1) in the session but there is another process instance "B" (requestId==2) in the same session and there are some issues like Issue(requestId==2) in the session as well?

                                 

                                 

                                Regards,

                                Dmitry Erkin

                                I believe so, it should be executed in context of the process instance only but I recommend to perform additional tests and do not take my comment here as the only truth

                                 

                                HTH

                                • 13. Re: How to get a process instance id within a sequence flow "condition expression"?
                                  dmitry.erkin

                                  Hello, Maciej !

                                   

                                  I am sorry but it does not make sence because if I use a clause like "$processInstance : WorkflowProcessInstance(id < 100)" it should be executed in context of a session.

                                   

                                  Regards,

                                  Dmitry Erkin

                                  • 14. Re: How to get a process instance id within a sequence flow "condition expression"?
                                    swiderski.maciej

                                    Dmitry, I think we are going in to different directions now do you mind to provide a simple test case so we could work on it and find the best approach?

                                     

                                    Cheers

                                    1 2 Previous Next