Manually controlling execution of nodes
michaelklem May 4, 2006 6:04 PMI created the following process definition
<process-definition xmlns="urn:jbpm.org:jpdl-3.1" name="test1"> <start-state name="start"> <transition name="step1" to="step1"/> </start-state> <end-state name="end"/> <state name="step1"> <transition name="step2" to="step2"/> </state> <state name="step2"> <transition name="step3" to="step3"/> </state> <state name="step3"> <transition name="step4" to="step4"/> </state> <state name="step4"> <transition name="step5" to="step5"/> </state> <state name="step5"> <transition name="end" to="end"/> </state> </process-definition>
I am trying to manually control which state gets executed next. For example, once the process is started, I am trying to execute the states in the following order:
step1
step1
step4
step3
step3
step2
step5
I am able to do so with the following code
/** Sequence Control Test. Verify that we can specify which token to taken on a simple sequential process. TEST1_PROCESS is a sequential process definition of wait states. Once we start the process, we should be able to step to any node we want. */ public void test1() { try { // Deploy the process definition init( TEST1_PROCESS ); JbpmContext jbpmContext = sJbpmConfiguration.createJbpmContext(); GraphSession graphSession = jbpmContext.getGraphSession(); ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition( "test1" ); ProcessInstance processInstance = new ProcessInstance( processDefinition ); long processDefId = processDefinition.getId(); long processId = processInstance.getId(); Token token = processInstance.getRootToken(); assertTrue( processDefinition.getNode( "start" ).equals( token.getNode() ) ); processInstance.signal(); assertTrue( processDefinition.getNode( "step1" ).equals( token.getNode() ) ); jbpmContext.close(); Node n; // Go to step 1 jbpmContext = sJbpmConfiguration.createJbpmContext(); graphSession = jbpmContext.getGraphSession(); processInstance = graphSession.loadProcessInstance( processId ); processDefinition = graphSession.loadProcessDefinition( processDefId ); n = processDefinition.getNode( "start" ); token = processInstance.getRootToken(); token.setNode( n ); token.signal(); assertTrue( processDefinition.getNode( "step1" ).equals( token.getNode() ) ); jbpmContext.close(); // Go to step 1 jbpmContext = sJbpmConfiguration.createJbpmContext(); graphSession = jbpmContext.getGraphSession(); processInstance = graphSession.loadProcessInstance( processId ); processDefinition = graphSession.loadProcessDefinition( processDefId ); n = processDefinition.getNode( "start" ); token = processInstance.getRootToken(); token.setNode( n ); token.signal(); assertTrue( processDefinition.getNode( "step1" ).equals( token.getNode() ) ); jbpmContext.close(); // Go to step 4 jbpmContext = sJbpmConfiguration.createJbpmContext(); graphSession = jbpmContext.getGraphSession(); processInstance = graphSession.loadProcessInstance( processId ); processDefinition = graphSession.loadProcessDefinition( processDefId ); n = processDefinition.getNode( "step3" ); token = processInstance.getRootToken(); token.setNode( n ); token.signal(); assertTrue( processDefinition.getNode( "step4" ).equals( token.getNode() ) ); jbpmContext.close(); // Go to step 3 jbpmContext = sJbpmConfiguration.createJbpmContext(); graphSession = jbpmContext.getGraphSession(); processInstance = graphSession.loadProcessInstance( processId ); processDefinition = graphSession.loadProcessDefinition( processDefId ); n = processDefinition.getNode( "step2" ); token = processInstance.getRootToken(); token.setNode( n ); token.signal(); assertTrue( processDefinition.getNode( "step3" ).equals( token.getNode() ) ); jbpmContext.close(); // Go to step 3 jbpmContext = sJbpmConfiguration.createJbpmContext(); graphSession = jbpmContext.getGraphSession(); processInstance = graphSession.loadProcessInstance( processId ); processDefinition = graphSession.loadProcessDefinition( processDefId ); n = processDefinition.getNode( "step2" ); token = processInstance.getRootToken(); token.setNode( n ); token.signal(); assertTrue( processDefinition.getNode( "step3" ).equals( token.getNode() ) ); jbpmContext.close(); // Go to step 2 jbpmContext = sJbpmConfiguration.createJbpmContext(); graphSession = jbpmContext.getGraphSession(); processInstance = graphSession.loadProcessInstance( processId ); processDefinition = graphSession.loadProcessDefinition( processDefId ); n = processDefinition.getNode( "step1" ); token = processInstance.getRootToken(); token.setNode( n ); token.signal(); assertTrue( processDefinition.getNode( "step2" ).equals( token.getNode() ) ); jbpmContext.close(); // Go to step 5 jbpmContext = sJbpmConfiguration.createJbpmContext(); graphSession = jbpmContext.getGraphSession(); processInstance = graphSession.loadProcessInstance( processId ); processDefinition = graphSession.loadProcessDefinition( processDefId ); n = processDefinition.getNode( "step4" ); token = processInstance.getRootToken(); token.setNode( n ); token.signal(); assertTrue( processDefinition.getNode( "step5" ).equals( token.getNode() ) ); jbpmContext.close(); } catch( Exception ex ) { ex.printStackTrace(); fail(); } } /* test1 */
This code works but it seems like there must be a better/easier way to do this in jBPM. I read all the docs and searched the forums but was unable to find an answer that was obvious to me. Am I missing something here or is this something I should not be doing with jBPM?
Thanks