我们在用JBPM做流程的时候,很多时候会遇到需要判断的节点。类似java中的switch,根据不同的状态,跳转到不同的节点。
首先我们定义一个流程信息,jpdl流程图如下
明显的可以看到,在此种情况下我们就必须用到decision节点来控制不同的业务跳转到不同的角色上。
我们的jpdl.xml中的代码为:
<?xml version="1.0" encoding="UTF-8"?> <process key="decision" name="decision" xmlns="http://jbpm.org/4.4/jpdl"> <start name="start1" g="22,200,48,48"> <transition name="提交" to="exclusive1" g="-15,-20"/> </start> <decision name="exclusive1" expr="${node}" g="185,201,48,48"> <transition name="小于2天" to="组长" g="-49,-32"/> <transition name="大于2天,小于10" to="科长" g="-56,-28"/> <transition name="10天以上" to="主任" g="-47,-22"/> </decision> <task assignee="zuzhang" name="组长" g="338,69,92,52"> <transition name="组长批准" to="归档" g="-51,-26"/> </task> <task assignee="kezhang" name="科长" g="342,199,92,52"> <transition name="科长批准" to="归档" g="-49,-25"/> </task> <task assignee="zhuren" name="主任" g="349,325,92,52"> <transition name="主任批准" to="归档" g="-41,-25"/> </task> <task assignee="caiwu" name="归档" g="531,201,92,52"> <transition name="通过" to="end1" g="-35,-22"/> </task> <end name="end1" g="689,208,48,48"/> </process>
此种方法是定义流程变量,流程变量等于transition中的哪个name的值就跳转到哪个角色中
在代码中:
package com.tgb.node.decision; import java.util.HashMap; import java.util.Map; import org.jbpm.api.ProcessInstance; import com.tgb.video.JbpmTestCase; import com.tgb.video.JbpmUtil; public class TestDecision extends JbpmTestCase implements JbpmUtil { @Override public void deploy() { super.startUp(); repositoryService.createDeployment().addResourceFromClasspath("com/tgb/node/decision/decision.jpdl.xml").deploy(); } @Override public void createInstance() { super.startUp(); Map<String,Object> map = new HashMap<String,Object>(); map.put("node", "大于2天,小于10"); ProcessInstance processInstance = executionService.startProcessInstanceByKey("decision", map); print("流程实例ID",processInstance.getId()); } @Override public void getCurrectActivity() { super.startUp(); String name = executionService.createProcessInstanceQuery().processInstanceId("decision.130001").uniqueResult().findActiveActivityNames().toString(); print("到达节点名称",name); } @Override public void getTask() { // TODO Auto-generated method stub } @Override public void completeTask() { // TODO Auto-generated method stub } }
我们只需在代码中控制node的值即可跳转到不同的人员。
第二种:使用表达式判断
<?xml version="1.0" encoding="UTF-8"?> <process key="decision" name="decision" xmlns="http://jbpm.org/4.4/jpdl"> <start name="start1" g="22,200,48,48"> <transition name="提交" to="exclusive1" g="-15,-20"/> </start> <decision name="exclusive1" g="185,201,48,48"> <transition name="小于2天" to="组长" g="-49,-32"> <condition expr="${days lt 2}"/> </transition> <transition name="大于2天,小于10" to="科长" g="-56,-28"> <condition expr="${days ge 2 and days le 10}"/> </transition> <transition name="10天以上" to="主任" g="-47,-22"> <condition expr="${days gt 10}"/> </transition> </decision> <task assignee="zuzhang" name="组长" g="338,69,92,52"> <transition name="组长批准" to="归档" g="-51,-26"/> </task> <task assignee="kezhang" name="科长" g="342,199,92,52"> <transition name="科长批准" to="归档" g="-49,-25"/> </task> <task assignee="zhuren" name="主任" g="349,325,92,52"> <transition name="主任批准" to="归档" g="-41,-25"/> </task> <task assignee="caiwu" name="归档" g="531,201,92,52"> <transition name="通过" to="end1" g="-35,-22"/> </task> <end name="end1" g="689,208,48,48"/> </process>
此种方法即把判断放到配置文件中,只需传过一个数字来判断即可。
public void createInstance() { super.startUp(); Map<String,Object> map = new HashMap<String,Object>(); map.put("days", 15); ProcessInstance processInstance = executionService.startProcessInstanceByKey("decision", map); print("流程实例ID",processInstance.getId()); }
创建流程实例代码如上,将参数用days传过来即可判断。
第三种方法:监听判断
写一个类MyDecision,继承DecisionHandler
在jpdl文件中配置上MyDecision,通过复写decide方法,用execution将参数传过来。
<?xml version="1.0" encoding="UTF-8"?> <process key="decision" name="decision" xmlns="http://jbpm.org/4.4/jpdl"> <start name="start1" g="22,200,48,48"> <transition name="提交" to="exclusive1" g="-15,-20"/> </start> <decision name="exclusive1" g="185,201,48,48"> <handler class="com.tgb.node.decision.MyDecision"/> <transition name="到组长" to="组长" g="-49,-32"> </transition> <transition name="到科长" to="科长" g="-56,-28"> </transition> <transition name="到主任" to="主任" g="-47,-22"> </transition> </decision> <task assignee="zuzhang" name="组长" g="338,69,92,52"> <transition name="组长批准" to="归档" g="-51,-26"/> </task> <task assignee="kezhang" name="科长" g="342,199,92,52"> <transition name="科长批准" to="归档" g="-49,-25"/> </task> <task assignee="zhuren" name="主任" g="349,325,92,52"> <transition name="主任批准" to="归档" g="-41,-25"/> </task> <task assignee="caiwu" name="归档" g="531,201,92,52"> <transition name="通过" to="end1" g="-35,-22"/> </task> <end name="end1" g="689,208,48,48"/> </process>
MyDecision类中代码:
package com.tgb.node.decision; import org.jbpm.api.jpdl.DecisionHandler; import org.jbpm.api.model.OpenExecution; public class MyDecision implements DecisionHandler { /** * */ private static final long serialVersionUID = 1L; @Override public String decide(OpenExecution execution) { String to = execution.getVariable("to").toString(); if(to.equals("组长")){ return "到组长"; }else if (to.endsWith("科长")){ return "到科长"; }else{ return "到主任"; } } }
此种方法将判断逻辑交给了java代码,不用重新部署流程。