JBPM知识点------decision

decision也是一种node类型,表示判断,其判断条件可以在流程定义中进行设置,也可以由外界提供。

  下面是流程定义中的2种方式:

  • 表达式方式

 <start-state name="start">
      <transition name="" to="decision1"></transition>
   </start-state>
   <decision name="decision1"  expression="#{ (5 > 1 ? 'no' : 'no') }" >
      <transition name="yes" to="state1"></transition>
      <transition name="no" to="state2"></transition>
   </decision>
   <state name="state1">
      <transition name="" to="end1"></transition>
   </state>
   <state name="state2">
      <transition name="" to="end1"></transition>
   </state>
   <end-state name="end1"></end-state>

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

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();
   System.out.println(token.getNode());
   token.signal();
   System.out.println(token.getNode());
   jbpmContext.save(processInstance);
   }finally {
     jbpmContext.close();
   }
 } 

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

运行结果

StartState(start)
State(state2)
EndState(end1)

  • 实现DecisionHandler接口

 <start-state name="start">
      <transition name="" to="decision1"></transition>
   </start-state>
   <decision name="decision1">
      <handler class="com.jeffentest.JeffenDecisionHandler"/>
      <transition name="yes" to="state1"></transition>
      <transition name="no" to="state2"></transition>
   </decision>
   <state name="state1">
      <transition name="" to="end1"></transition>
   </state>
   <state name="state2">
      <transition name="" to="end1"></transition>
   </state>
   <end-state name="end1"></end-state>

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

package com.jeffentest;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.node.DecisionHandler;

public class JeffenDecisionHandler implements DecisionHandler {
    private static final long serialVersionUID = 1L;
    public String decide(ExecutionContext executionContext) {
      executionContext.setVariable("author", "jeffen");//设置变量,可以在前面的流程步骤中进行设置,这里为了方便放到一起。
      String author = (executionContext.getContextInstance().getVariable("author")).toString();//取出变量
      if(author=="jeffen")
       return "yes";
      else
       return "no";
    }
}

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

测试程序同上,运行结果如下:

StartState(start)
State(state1)
EndState(end1)

上面两种方式是我们在流程定义中定义好的,当然我们也可以在客户端或者其他外部系统中进行干预,只需要指定离开的变迁transitionName就好了:Token.signal(String transitionName) 或 TaskInstance.end(String transitionName)。

 

你可能感兴趣的:(JBPM知识点------decision)