5 Replies Latest reply on Jan 3, 2008 12:54 PM by Johan Parent

    how to find ProcessInstance current state or node

    TH YJ Newbie

      Hi,

      I have a simple process definition:

      <process-definition xmlns="urn:jbpm.org:jpdl-3.1"
       name="Process1">
       <start-state name="start-process">
       <transition name="to wait state"
       to="wait-state"/>
       </start-state>
       <state name="wait-state">
       <transition name="to run state" to="run-node"/>
       </state>
       <node name="run-node">
       <action class="abc.SomeAction"
       config-type="bean">
       </action>
       <transition name="end-process" to="end-process"/>
       </node>
       <end-state name="end-process"/>
      </process-definition>
      


      I have created a number of ProcessInstance for the above definition. Now i need to find out which ProcessInstance are in "wait-state" state and which are in "run-node" node?

      I have being popping around in the documentation for a couple of days now, i have the process instance ids. is it possible to get this information by querying the jBPM data model or through API ?

      Thanks,

      pike

        • 1. Re: how to find ProcessInstance current state or node
          Johan Parent Newbie

          Hi,

          You can get the root token for your process. From there you can find out in which node(s) it resides.

          A bit like below (this collects all the active nodes) but then again you'll need to do some instanceof work to see what type of node you actually got.

          ...
           ProcessInstance p = ctx.loadProcessInstance(id);
          
           // Second build a Set with all the active nodes in the process instance
           Token t = p.getRootToken();
           Map <String, Token> children = t.getChildren();
          
           // Put in the set
           Set<Map.Entry<String,Token>> cSet = children.entrySet();
           for (Iterator<Map.Entry<String,Token>> it = cSet.iterator(); it.hasNext();) {
           Map.Entry<String,Token> e = it.next();
           Token tok = e.getValue();
           //
           nodes.add(tok.getNode());
           }
          
          
           // Also add the node for this token (if any) to the set
           Node here = t.getNode();
           if (here != null)
           nodes.add(here);
          
          
          



          And oh yeah, since the hibernate proxies will get in the way of your instanceof logic you'll need to do something like below first (believe me ;)

           // We need to this to access the real object otherwise
           // we bump into the proxy object and instanceof won't work
           if (nodeinstanceof HibernateProxy) {
           node= ((HibernateProxy)node).getHibernateLazyInitializer().getImplementation();
           }
          



          Hope this helps,

          Johan

          • 2. Re: how to find ProcessInstance current state or node
            TH YJ Newbie

            Thanks Johan that helped.

            Just wondering if the method will be recursive i mean do i need to the process for the children node too? or first level node will be enough?



            • 3. Re: how to find ProcessInstance current state or node
              TH YJ Newbie

              Also is there a possibility that we can use HQL to get current states/nodes?

              • 4. Re: how to find ProcessInstance current state or node
                TH YJ Newbie

                Because i actually want to know all the process instance in particular state/node, and i do not have the process instance id or anything.

                • 5. Re: how to find ProcessInstance current state or node
                  Johan Parent Newbie

                  Ah ok I see. Well I saw I complicated things anyway. There seems to be a findActiveNodesByProcessInstance() in the GraphSession

                  So combining that with getGraphSession().findProcessInstances(processDefinitionId), which returns you a list of processes, you could get the list you are looking for.

                  Otherwise have a look in hibernate.queries.hbm.xml file and make your own hql with those used by the above mentioned methods.

                  Good luck,

                  Johan