流程中的网关用于控制顺序流的分支和汇合,如果不需要进行顺序流的控制,则可以不使用网关
排他网关就好像一个人走到了一个岔路口,只能选择一条路前进,至于选择哪条路由顺序流的条件决定
排他网关用内部带有 X
图标的标准网关(菱形)表示,X
图标代表异或的含义。请注意内部没有图标的网关默认为排他网关。BPMN 2.0
规范不允许在同一个流程中混合使用有及没有 X
的菱形标志
案例
并行网关对执行流会有两种行为:分岔和合并
并行网关案例
对应的 xml
文件如下,其中绘制流程图时已指定下述变量
${workers}
${ProjectManager}
${TechnicalManager}
${boss}
3
天使用变量指定:${num>3}
3
天使用变量指定:${num<=3}
<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:flowable="http://flowable.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.flowable.org/processdef" exporter="Flowable Open Source Modeler"
exporterVersion="6.7.2">
<process id="Leave1" name="Leave1" isExecutable="true">
<documentation>公司请假申请流程documentation>
<startEvent id="sid-EDEC0102-C387-4F9A-B5EF-157587793A4D" flowable:formFieldValidation="true">startEvent>
<userTask id="userId" name="请假申请" flowable:assignee="${workers}" flowable:formFieldValidation="true">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler">
modeler:initiator-can-complete>
extensionElements>
userTask>
<sequenceFlow id="sid-A62E0666-ABAF-4A31-B7A6-43157D7F59B2" sourceRef="sid-EDEC0102-C387-4F9A-B5EF-157587793A4D"
targetRef="userId">sequenceFlow>
<sequenceFlow id="sid-0EED4703-83C6-4B57-9015-A75B06927C0C" sourceRef="userId"
targetRef="sid-FF0BB60C-47D5-4DB5-8C3E-C21A1DFC1F81">sequenceFlow>
<parallelGateway id="sid-FF0BB60C-47D5-4DB5-8C3E-C21A1DFC1F81">parallelGateway>
<userTask id="xmjlId" name="项目经理审批" flowable:assignee="${ProjectManager}" flowable:formFieldValidation="true">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler">
modeler:initiator-can-complete>
extensionElements>
userTask>
<userTask id="jsjlId" name="技术经理审批" flowable:assignee="${TechnicalManager}" flowable:formFieldValidation="true">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler">
modeler:initiator-can-complete>
extensionElements>
userTask>
<sequenceFlow id="sid-7A21D68A-3115-447A-B147-CDC6144D6D71" sourceRef="sid-FF0BB60C-47D5-4DB5-8C3E-C21A1DFC1F81"
targetRef="jsjlId">sequenceFlow>
<sequenceFlow id="sid-EF7EDB7C-3719-485D-85C6-EDF2DA8DBE33" sourceRef="sid-FF0BB60C-47D5-4DB5-8C3E-C21A1DFC1F81"
targetRef="xmjlId">sequenceFlow>
<sequenceFlow id="sid-25F6DB53-F695-4A0D-BBFC-6B8A45371EDF" sourceRef="jsjlId"
targetRef="sid-B961DC17-1F54-4B6B-BCA9-9EA15DEFDC29">sequenceFlow>
<parallelGateway id="sid-B961DC17-1F54-4B6B-BCA9-9EA15DEFDC29">parallelGateway>
<sequenceFlow id="sid-C7AA05DF-F958-476D-8710-F9B8DD51BE85" sourceRef="xmjlId"
targetRef="sid-B961DC17-1F54-4B6B-BCA9-9EA15DEFDC29">sequenceFlow>
<exclusiveGateway id="sid-3C064FB3-2132-4C38-88E0-F1AD3CA14A1C">exclusiveGateway>
<sequenceFlow id="sid-4C96EBB0-7EAC-4FD5-BF06-6551F53C9D0D" sourceRef="sid-B961DC17-1F54-4B6B-BCA9-9EA15DEFDC29"
targetRef="sid-3C064FB3-2132-4C38-88E0-F1AD3CA14A1C">sequenceFlow>
<userTask id="zjlId" name="总经理审批" flowable:assignee="${boss}" flowable:formFieldValidation="true">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler">
modeler:initiator-can-complete>
extensionElements>
userTask>
<endEvent id="sid-94B5337F-CEDF-4FF3-A051-C90E82D4E670">endEvent>
<sequenceFlow id="sid-3E165D65-0F3E-4BE1-A3A4-980746B9B473" sourceRef="zjlId"
targetRef="sid-94B5337F-CEDF-4FF3-A051-C90E82D4E670">sequenceFlow>
<sequenceFlow id="sid-DB491A8C-EB35-4962-99B8-0BF39C468CBB" name="请假天数大于3天"
sourceRef="sid-3C064FB3-2132-4C38-88E0-F1AD3CA14A1C" targetRef="zjlId">
<conditionExpression xsi:type="tFormalExpression">3}]]>conditionExpression>
sequenceFlow>
<sequenceFlow id="sid-39AF05C4-5F77-414F-A898-180771841241" name="请假天数小于等于3天"
sourceRef="sid-3C064FB3-2132-4C38-88E0-F1AD3CA14A1C"
targetRef="sid-94B5337F-CEDF-4FF3-A051-C90E82D4E670">
<conditionExpression xsi:type="tFormalExpression">conditionExpression>
sequenceFlow>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_Leave1">
<bpmndi:BPMNPlane bpmnElement="Leave1" id="BPMNPlane_Leave1">
<bpmndi:BPMNShape bpmnElement="sid-EDEC0102-C387-4F9A-B5EF-157587793A4D"
id="BPMNShape_sid-EDEC0102-C387-4F9A-B5EF-157587793A4D">
<omgdc:Bounds height="30.0" width="30.0" x="60.0" y="120.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="userId" id="BPMNShape_userId">
<omgdc:Bounds height="80.0" width="100.0" x="150.0" y="95.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-FF0BB60C-47D5-4DB5-8C3E-C21A1DFC1F81"
id="BPMNShape_sid-FF0BB60C-47D5-4DB5-8C3E-C21A1DFC1F81">
<omgdc:Bounds height="40.0" width="40.0" x="315.0" y="115.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="xmjlId" id="BPMNShape_xmjlId">
<omgdc:Bounds height="80.0" width="100.0" x="405.0" y="0.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="jsjlId" id="BPMNShape_jsjlId">
<omgdc:Bounds height="80.0" width="100.0" x="405.0" y="199.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-B961DC17-1F54-4B6B-BCA9-9EA15DEFDC29"
id="BPMNShape_sid-B961DC17-1F54-4B6B-BCA9-9EA15DEFDC29">
<omgdc:Bounds height="40.0" width="40.0" x="555.0" y="115.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-3C064FB3-2132-4C38-88E0-F1AD3CA14A1C"
id="BPMNShape_sid-3C064FB3-2132-4C38-88E0-F1AD3CA14A1C">
<omgdc:Bounds height="40.0" width="40.0" x="645.0" y="115.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="zjlId" id="BPMNShape_zjlId">
<omgdc:Bounds height="80.0" width="100.0" x="810.0" y="95.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-94B5337F-CEDF-4FF3-A051-C90E82D4E670"
id="BPMNShape_sid-94B5337F-CEDF-4FF3-A051-C90E82D4E670">
<omgdc:Bounds height="28.0" width="28.0" x="846.0" y="225.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-EF7EDB7C-3719-485D-85C6-EDF2DA8DBE33"
id="BPMNEdge_sid-EF7EDB7C-3719-485D-85C6-EDF2DA8DBE33" flowable:sourceDockerX="20.5"
flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
<omgdi:waypoint x="335.5" y="115.5">omgdi:waypoint>
<omgdi:waypoint x="335.5" y="40.0">omgdi:waypoint>
<omgdi:waypoint x="405.0" y="40.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-A62E0666-ABAF-4A31-B7A6-43157D7F59B2"
id="BPMNEdge_sid-A62E0666-ABAF-4A31-B7A6-43157D7F59B2" flowable:sourceDockerX="15.0"
flowable:sourceDockerY="15.0" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
<omgdi:waypoint x="89.94999883049303" y="135.0">omgdi:waypoint>
<omgdi:waypoint x="149.99999999995785" y="135.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-C7AA05DF-F958-476D-8710-F9B8DD51BE85"
id="BPMNEdge_sid-C7AA05DF-F958-476D-8710-F9B8DD51BE85" flowable:sourceDockerX="50.0"
flowable:sourceDockerY="40.0" flowable:targetDockerX="20.5" flowable:targetDockerY="20.5">
<omgdi:waypoint x="504.949999999981" y="40.0">omgdi:waypoint>
<omgdi:waypoint x="575.5" y="40.0">omgdi:waypoint>
<omgdi:waypoint x="575.5" y="115.5">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-4C96EBB0-7EAC-4FD5-BF06-6551F53C9D0D"
id="BPMNEdge_sid-4C96EBB0-7EAC-4FD5-BF06-6551F53C9D0D" flowable:sourceDockerX="20.5"
flowable:sourceDockerY="20.5" flowable:targetDockerX="20.5" flowable:targetDockerY="20.5">
<omgdi:waypoint x="594.4394839067702" y="135.5">omgdi:waypoint>
<omgdi:waypoint x="645.5" y="135.5">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-0EED4703-83C6-4B57-9015-A75B06927C0C"
id="BPMNEdge_sid-0EED4703-83C6-4B57-9015-A75B06927C0C" flowable:sourceDockerX="50.0"
flowable:sourceDockerY="40.0" flowable:targetDockerX="20.5" flowable:targetDockerY="20.5">
<omgdi:waypoint x="249.94999999999231" y="135.1843173431734">omgdi:waypoint>
<omgdi:waypoint x="315.425925925926" y="135.42592592592595">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-DB491A8C-EB35-4962-99B8-0BF39C468CBB"
id="BPMNEdge_sid-DB491A8C-EB35-4962-99B8-0BF39C468CBB" flowable:sourceDockerX="20.5"
flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
<omgdi:waypoint x="684.4939335394126" y="135.45103092783506">omgdi:waypoint>
<omgdi:waypoint x="809.999999999999" y="135.12840616966582">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-7A21D68A-3115-447A-B147-CDC6144D6D71"
id="BPMNEdge_sid-7A21D68A-3115-447A-B147-CDC6144D6D71" flowable:sourceDockerX="20.5"
flowable:sourceDockerY="20.5" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
<omgdi:waypoint x="335.5" y="154.44085424710423">omgdi:waypoint>
<omgdi:waypoint x="335.5" y="239.0">omgdi:waypoint>
<omgdi:waypoint x="405.0" y="239.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-3E165D65-0F3E-4BE1-A3A4-980746B9B473"
id="BPMNEdge_sid-3E165D65-0F3E-4BE1-A3A4-980746B9B473" flowable:sourceDockerX="50.0"
flowable:sourceDockerY="40.0" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
<omgdi:waypoint x="860.0" y="174.95">omgdi:waypoint>
<omgdi:waypoint x="860.0" y="225.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-25F6DB53-F695-4A0D-BBFC-6B8A45371EDF"
id="BPMNEdge_sid-25F6DB53-F695-4A0D-BBFC-6B8A45371EDF" flowable:sourceDockerX="50.0"
flowable:sourceDockerY="40.0" flowable:targetDockerX="20.5" flowable:targetDockerY="20.5">
<omgdi:waypoint x="504.9499999999326" y="239.0">omgdi:waypoint>
<omgdi:waypoint x="575.5" y="239.0">omgdi:waypoint>
<omgdi:waypoint x="575.5" y="154.40911508704068">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-39AF05C4-5F77-414F-A898-180771841241"
id="BPMNEdge_sid-39AF05C4-5F77-414F-A898-180771841241" flowable:sourceDockerX="20.5"
flowable:sourceDockerY="20.5" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
<omgdi:waypoint x="665.5" y="154.44085424710428">omgdi:waypoint>
<omgdi:waypoint x="665.5" y="239.0">omgdi:waypoint>
<omgdi:waypoint x="846.0" y="239.0">omgdi:waypoint>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
// 部署流程
@Test
public void test1() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("processes/Leave1.bpmn20.xml")
.name("并行网关流程")
.deploy();
System.out.println("deploy.getId() = " + deploy.getId());
System.out.println("deploy.getName() = " + deploy.getName());
}
// 启动流程
@Test
public void test2() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
// 启动流程时,给变量 workers、num 分别赋值
Map<String, Object> variables = new HashMap<>();
variables.put("workers", "张三");// 请假发起人
variables.put("num", 3); // 请几天假
variables.put("description", "工作累了,想出去玩玩"); // 请假的原因
ProcessInstance processInstance = runtimeService.startProcessInstanceById("Leave1:2:5a013b6c-574a-11ed-aaf6-005056c00001", variables);
System.out.println("流程定义的ID:" + processInstance.getProcessDefinitionId());
System.out.println("流程实例的ID:" + processInstance.getId());
}
执行完成后,在 ACT_RU_TASK
表中会有一条张三的任务,即请假申请任务
// 任务发起人(张三)完成请假申请的任务
@Test
public void test3() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
// 查询张三的任务
Task task = taskService.createTaskQuery()
.processInstanceId("aa0cea08-574b-11ed-b92e-005056c00001")
.taskAssignee("张三")
.singleResult();
// 完成任务时,指定下一个节点任务的处理人
Map<String, Object> variables = new HashMap<>();
variables.put("ProjectManager", "李项目经理");
variables.put("TechnicalManager", "王技术经理");
if (task != null) {
taskService.complete(task.getId(), variables);
}
}
我们再次查看 ACT_RU_TASK
表数据有两条记录
然后同时在 ACT_RU_EXECUTION
中有三条记录,一个任务对应的有两个执行实例
ACT_RU_TASK
表生成了两条记录,即并行网关有几个分支,就会生成几个任务记录。并且,必须要等多个分支任务都完成后汇聚到一起,才能流转到下一个节点任务// 项目经理完成任务
@Test
public void test4() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
// 直接查询李项目经理的任务
Task task = taskService.createTaskQuery()
.processInstanceId("aa0cea08-574b-11ed-b92e-005056c00001")
.taskAssignee("李项目经理")
.singleResult();
if (task != null) {
taskService.complete(task.getId());
}
}
项目经理任务完成后,ACT_RU_TASK
表中还有一条技术经理的任务,所以还需要把技术经理的任务完成
// 技术经理完成任务
@Test
public void test5() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
// 直接查询王技术经理的任务
Task task = taskService.createTaskQuery()
.processInstanceId("aa0cea08-574b-11ed-b92e-005056c00001")
.taskAssignee("王技术经理")
.singleResult();
if (task != null) {
taskService.complete(task.getId());
}
}
此时,ACT_RU_TASK
表中已经没有数据了
此时,ACT_RU_EXECUTION
表中也没有数据了
此时,ACT_RU_VARIABLE
表中也没有数据了
此时流程就结束了,因为在张三请假申请任务的那一步,请假天数 num
设置为了 3
,不需要由总经理审批
包含网关可以看做是排他网关和并行网关的结合体。 和排他网关一样,你可以在外出顺序流上定义流条件,包含网关会解析它们。 但是主要的区别是包含网关可以选择多于一条顺序流,这和并行网关一样
1
和顺序流 2
设置流条件并行
走满足条件的 所有的
顺序流(这点和排他网关有区别,排他网关是哪个顺序流的定义在流程文件中靠前,走哪个顺序流)当并行网关使用时,不能为从包含网关输出的顺序流 1
和顺序流 2
设置流条件;这样以来它就和并行网关的特性一模一样,既有分岔行为也有合并行为,且处理方式也和并行网关一致
Catching
事件来决定流程的走向1
和顺序流 2
,顺序流 1
直接连接了信号事件,顺序流 2
直接连接了定时器事件有些同学会有以下疑惑:当流程走到事件网关时,并不知道是往顺序流 1
走?还是往顺序流 2
走?因为只有走了顺序流 1
或者顺序流 2
后才能到达各自连接的事件,然后才能根据事件进行判断;这样以来就出现了走向选择的问题
实际上当流程走到事件网关时,并不会立马选择走顺序流 1
还是走顺序流 2
,而是 Activiti
会为全部的中间 Catching
事件创建相应的数据,当某个中间事件先被触发了,流程就会选择触发事件所在的顺序流走下去