JBPM知识点-----ProcessState

许博
2023-12-01

用来定义子流程,当主流程遇到ProcessState就会停滞并生成子流程运行,子流程运行完毕重新回到主流程运行。主子流程的部署有点特殊(耗 费了我3个小时才搞定,我开始认为只要在processdefinition.xml中定义好2个流程的关系,然后分别发布就好了,但是不行,一定要程序 设置好两者的关系再发布)

主流程xml:
<?xml version="1.0" encoding="UTF-8"?>
<process-definition xmlns="" name="yytest">
 <start-state name='yyteststart' >
    <transition  name='done' to="initial_subyytest" />
 </start-state>
 <process-state name="initial_subyytest">
    <sub-process name="subyytest" />
    <!-- variable name="a" access="read,write" mapped-name="aa" /-->
    <!-- variable name="b" access="read" mapped-name="bb" /-->
    <transition  name='done' to="yytestend" />
 </process-state>
 <end-state name="yytestend" />
</process-definition>
--------------------------------------------------

子流程xml:
<?xml version="1.0" encoding="UTF-8"?>
<process-definition
  xmlns=""  name="subyytest">
   <start-state name="subyyteststart">
      <transition name="" to="subyyteststate1"></transition>
   </start-state>
   <state name="subyyteststate1">
      <transition name="" to="subyytestend"></transition>
   </state>
   <end-state name="subyytestend"></end-state>
</process-definition>

 -----------------------------------------------------

部署以及过程生成:
private static void Deploying_processstate(){
  JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
  try {
   InputStream is = new FileInputStream("processes/yytest/processdefinition.xml");
   processDefinition = ProcessDefinition.parseXmlInputStream(is);
   InputStream subis = new FileInputStream("processes/subyytest/processdefinition.xml");
   ProcessDefinition subprocessDefinition = ProcessDefinition.parseXmlInputStream(subis);
   //建立关联关系
   ProcessState processState = (ProcessState) processDefinition.getNode("initial_subyytest");
   processState.setSubProcessDefinition(subprocessDefinition);
   //一定要先部署子流程,不然hibernate会报错。
   jbpmContext.deployProcessDefinition(subprocessDefinition);
   jbpmContext.deployProcessDefinition(processDefinition);
  } catch(FileNotFoundException e){
   e.printStackTrace();
  }finally {
    jbpmContext.close();
  }
 }
private static void Starting_process(){
  JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
  try {
   String processName="yytest";
   processInstance =jbpmContext.newProcessInstance(processName);//只生成主流程就好了
   }finally {
     jbpmContext.close();
   }
 }

---------------------------------------------------

private static void run(){
  JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
  try {
   long processInstanceId =1;
   processInstance = jbpmContext.loadProcessInstance(processInstanceId);
   Token token = processInstance.getRootToken();
   System.out.println(token.getNode());
   token.signal();
   ProcessInstance subprocessInstance=token.getSubProcessInstance();
   Token subtoken = subprocessInstance.getRootToken();
   System.out.println(token.getNode());
   System.out.println(subtoken.getNode());
   subtoken.signal();
   System.out.println(subtoken.getNode());
   System.out.println(token.getNode());
   jbpmContext.save(processInstance);
   }finally {
     jbpmContext.close();
   }
 }

-----------------------------------------------------

运行结果:
StartState(yyteststart)
ProcessState(initial_subyytest)
State(subyyteststate1)
EndState(subyytestend)
EndState(yytestend)


补充:
自循环的流程:
<process-definition name="recursive process">
 <start-state>
  <transition to="first wait"/>
 </start-state>
 <state name="first wait">
  <transition to="subprocessnode"/>
  <transition name="done" to="end"/>
 </state>
 <process-state name="subprocessnode">
  <sub-process name="recursive process"/>
  <transition to="end"/>
 </process-state>
 <end-state name="end"/>
</process-definition>
-----------------------------------------------------
关联指定version的子流程
<process-definition>
 <process-state name="the sub process state">
  <sub-process name="the ultimate subprocess" version="1"/>
 </process-state>
</process-definition>

通常,当一个子流程被启动,在离开开始状态的信号被发出之前,所有拥有“read”存取属性的变量都被从超流程载入新创建的子流程;当子流程结束 时,所有拥有“write”存取属性的变量都被从子流程拷贝到超流程。variabled元素的mapped-name属性允许你指定在子流程中将使用的 变量名称。
相对于上例改动如下
<?xml version="1.0" encoding="UTF-8"?>
<process-definition xmlns="" name="yytest">
 <start-state name='yyteststart' >
    <transition  name='done' to="initial_subyytest" />
 </start-state>
 <process-state name="initial_subyytest">
    <sub-process name="subyytest" />
    <variable name="a" access="read,write" mapped-name="aa" />
    <variable name="b" access="read" mapped-name="bb" />

    <transition  name='done' to="yytestend" />
 </process-state>
 <end-state name="yytestend" />
</process-definition>

---------------------------------------
private static void run(){
  JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
  try {
   long processInstanceId =1;
   processInstance = jbpmContext.loadProcessInstance(processInstanceId);
   ContextInstance contextInstance = processInstance.getContextInstance();
      contextInstance.setVariable("a", "1");
      contextInstance.setVariable("b", "1");
      System.out.println("主流程变量a:"+contextInstance.getVariable("a"));
   System.out.println("主流程变量b:"+contextInstance.getVariable("b"));
   Token token = processInstance.getRootToken();
   System.out.println(token.getNode());
   token.signal();
   ProcessInstance subprocessInstance=token.getSubProcessInstance();
   /*取得subprocessInstance方法二
    long processInstanceId = processInstance.getId();
       long subProcessInstanceId = processInstance.getRootToken().getSubProcessInstance().getId();
       processInstance = jbpmContext.loadProcessInstance(processInstanceId);
       ProcessInstance subprocessInstance = jbpmContext.loadProcessInstance(subProcessInstanceId);
    */
   contextInstance = subprocessInstance.getContextInstance();
   System.out.println("子流程变量aa:"+contextInstance.getVariable("aa"));
   System.out.println("子流程变量bb:"+contextInstance.getVariable("bb"));
   contextInstance.setVariable("aa", "2");
      contextInstance.setVariable("bb", "2");
   Token subtoken = subprocessInstance.getRootToken();
   System.out.println(token.getNode());
   System.out.println(subtoken.getNode());
   subtoken.signal();
   contextInstance = processInstance.getContextInstance();
   System.out.println("主流程变量a:"+contextInstance.getVariable("a"));
   System.out.println("主流程变量b:"+contextInstance.getVariable("b"));
   System.out.println(subtoken.getNode());
   System.out.println(token.getNode());
   jbpmContext.save(processInstance);
   }finally {
     jbpmContext.close();
   }
 } 
------------------------------
运行结果:

主流程变量a:1
主流程变量b:1
StartState(yyteststart)
子流程变量aa:1
子流程变量bb:1
ProcessState(initial_subyytest)
State(subyyteststate1)
主流程变量a:2
主流程变量b:1
EndState(subyytestend)
EndState(yytestend)
 类似资料: