您的位置:首页 > 其它

Activiti工作流--流程变量的应用之二:排他网关--之八

2018-10-13 20:32 246 查看
流程的业务描述

财务报账审批流程:
a: 如果报账金额大于5000和小于10000时,财务审批以后,交由财务主管审批,然后结束流程
b: 如果报账金额大于或者等于10000,财务审批完以后交由财务经理审批,然后结束流程
c: 如果是其他情况,直接结束流程

先获取连接

private ProcessEngine processEngine;

@Before
public void initProcessEngine(){
processEngine = ProcessEngines.getDefaultProcessEngine();
}
流程定义与部署
@Test
public void tetsProcessDelopment(){
Deployment deployment = processEngine.getRepositoryService()
.createDeployment()
.addClasspathResource("diagram/exclusiveGateWay.bpmn")
.addClasspathResource("diagram/exclusiveGateWay.png")
.name("排他网关审批")
.deploy();
System.out.println("流程部署ID:"+deployment.getId());//127501
System.out.println("流程部署名称:" +deployment.getName());
}

流程图
exclusiveGateWay.bpmn代码

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="exclusiveGateWay" name="exclusiveGateWayProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="财务审批" activiti:assignee="张三"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway" default="默认连线"></exclusiveGateway>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
<userTask id="usertask2" name="财务经理审批" activiti:assignee="王五"></userTask>
<sequenceFlow id="flow3" name="大于10000" sourceRef="exclusivegateway1" targetRef="usertask2">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${money > 10000}]]></conditionExpression>
</sequenceFlow>
<userTask id="usertask3" name="财务主管审批" activiti:assignee="李四"></userTask>
<sequenceFlow id="flow4" name="大于5000小于等于10000" sourceRef="exclusivegateway1" targetRef="usertask3">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${money>5000 && money <= 10000}]]></conditionExpression>
</sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow5" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow6" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="默认连线" name="默认" sourceRef="exclusivegateway1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_exclusiveGateWay">
<bpmndi:BPMNPlane bpmnElement="exclusiveGateWay" id="BPMNPlane_exclusiveGateWay">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="160.0" y="160.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="240.0" y="150.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
<omgdc:Bounds height="40.0" width="40.0" x="390.0" y="158.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="540.0" y="228.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="540.0" y="69.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="770.0" y="160.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="195.0" y="177.0"></omgdi:waypoint>
<omgdi:waypoint x="240.0" y="177.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="345.0" y="177.0"></omgdi:waypoint>
<omgdi:waypoint x="390.0" y="178.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="410.0" y="198.0"></omgdi:waypoint>
<omgdi:waypoint x="592.0" y="228.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="54.0" x="421.0" y="216.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="410.0" y="158.0"></omgdi:waypoint>
<omgdi:waypoint x="592.0" y="124.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="42.0" width="100.0" x="409.0" y="113.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="592.0" y="124.0"></omgdi:waypoint>
<omgdi:waypoint x="787.0" y="160.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="592.0" y="228.0"></omgdi:waypoint>
<omgdi:waypoint x="787.0" y="195.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="默认连线" id="BPMNEdge_默认连线">
<omgdi:waypoint x="430.0" y="178.0"></omgdi:waypoint>
<omgdi:waypoint x="770.0" y="177.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="24.0" x="542.0" y="173.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

启动流程
@Test
public void testStartProcess(){
String processDefinitionKey = "exclusiveGateWay";
ProcessInstance processInstance = processEngine.getRuntimeService()
.startProcessInstanceByKey(processDefinitionKey);
System.out.println("流程部署ID:"+processInstance.getDeploymentId());
System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId());
System.out.println("流程实例ID:"+processInstance.getProcessInstanceId());//145001
}
查询个人,完成个人任务
/**
* 流程处理过程:查询个人任务
*/
@Test
public void testTaskQuery(){
String assignee = "张三";
String processInstanceId = "145001";
List<Task> taskList = processEngine.getTaskService()
.createTaskQuery()
.taskAssignee(assignee)
.processInstanceId(processInstanceId)
.list();
if (taskList != null && taskList.size() > 0) {
for (Task task : taskList) {
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("任务ID:"+task.getId());//145004
System.out.println("任务名称:"+task.getName());
System.out.println("任务创建时间:"+task.getCreateTime());
}
}
}

/**
* 流程处理过程:完成个人任务
*/
@Test
public void testCompleteTask(){
String taskId = "145004";
Map<String, Object> map = new HashMap<String,Object>();
map.put("money", 3000);
processEngine.getTaskService().complete(taskId,map);
System.out.println("财务主管审批完成");
}

/**
* 在流程的执行过程中,我们需要查询流程执行到了哪一个状态
* 查询的是act_ru_execution  流程实例表
* 当流程结束了  那么在act_ru_execution 在这张表中就没有了数据记录
*/
@Test
public void testQueryProinsatanceState(){
String processInstanceId = "145001";
ProcessInstance singleResult = processEngine.getRuntimeService()
.createProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
if (singleResult != null) {
System.out.println("流程执行到:"+singleResult.getActivityId());
}else{
System.out.println("流程执行完毕");
}
}
总结:

在排他网关当中,如果不符合设置了条件的连线,则会通过设置的默认连线继续执行流程
注意:执行默认连线还是需要设置流程变量

【上一篇:Activiti工作流–流程变量应用之一:选择连线–之七】
【下一篇:Activiti工作流–并行网关–之九】

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐