Activiti6.0 (five) the use of exclusive gateway

table of Contents

I. Introduction

Two, exclusive gateway use

1. Draw a flowchart

2. Execute exclusive gateway

3. Judgment of complex conditions


I. Introduction

In the previous article, it was mentioned that when there are multiple outgoing lines, you can use process variables to control the process line. Activiti's exclusive gateway essentially sets conditions for each outgoing line after the gateway, and removes the default outgoing line and then from the top Go to the next judgment. When the condition is true or unconditional, it is considered to be satisfied, and the outgoing line is directly executed. The exclusive gateway also allows the default outgoing line to be set, that is, the default outgoing line is executed when all outgoing lines are not met.

In essence, the only advantage of the exclusive gateway is that a series of attributes can be set on the exclusive gateway to control the order of judgment, making the entire flowchart look clear.

Two, exclusive gateway use

1. Draw a flowchart

Take the reimbursement approval process as an example. When the reimbursement cost is less than 1000, go directly to the department manager to approve the exit line. I directly set the exit line as the default exit line, so the online conditions can not be set. When the cost is greater than 1000 and less than 1500, go The general manager approves the line, when it is greater than 1500, the chairman approves the line

The judgment order of exclusive gateways can also be set:

The above figure represents that after removing the approval of the default line-out department manager, first determine whether the general manager's approval conditions are met, and then transfer directly.

By the way, the xml file is as follows:

<?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/processdef">
  <process id="exclusive" name="排他网关测试" isExecutable="true">
    <startEvent id="startEvent1"></startEvent>
    <userTask id="sid-F713E5EC-5EB6-4923-823A-0D490B996316" name="费用报销申请" activiti:candidateUsers="test">
      <extensionElements>
        <modeler:user-info-firstname-test xmlns:modeler="http://activiti.com/modeler"><![CDATA[test]]></modeler:user-info-firstname-test>
        <modeler:activiti-idm-candidate-user xmlns:modeler="http://activiti.com/modeler"><![CDATA[true]]></modeler:activiti-idm-candidate-user>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <exclusiveGateway id="sid-B2A37F07-424D-4797-A7C6-65E15AB760FD" default="sid-084F6369-265A-4D96-BB4D-59E84CD807CD"></exclusiveGateway>
    <userTask id="sid-E11FAB26-51D9-458F-82F8-1AE58674382B" name="部分经理审批" activiti:candidateUsers="admin">
      <extensionElements>
        <modeler:user-info-email-admin xmlns:modeler="http://activiti.com/modeler"><![CDATA[admin]]></modeler:user-info-email-admin>
        <modeler:user-info-lastname-admin xmlns:modeler="http://activiti.com/modeler"><![CDATA[Administrator]]></modeler:user-info-lastname-admin>
        <modeler:activiti-idm-candidate-user xmlns:modeler="http://activiti.com/modeler"><![CDATA[true]]></modeler:activiti-idm-candidate-user>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <userTask id="sid-BEEF4475-13D5-4A8E-9E04-3A452589BBC3" name="总经理审批" activiti:candidateUsers="test">
      <extensionElements>
        <modeler:user-info-firstname-test xmlns:modeler="http://activiti.com/modeler"><![CDATA[test]]></modeler:user-info-firstname-test>
        <modeler:activiti-idm-candidate-user xmlns:modeler="http://activiti.com/modeler"><![CDATA[true]]></modeler:activiti-idm-candidate-user>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <userTask id="sid-B9246DF5-40EB-4D9A-BF58-07E5391C6C58" name="董事长审批" activiti:candidateUsers="test">
      <extensionElements>
        <modeler:user-info-firstname-test xmlns:modeler="http://activiti.com/modeler"><![CDATA[test]]></modeler:user-info-firstname-test>
        <modeler:activiti-idm-candidate-user xmlns:modeler="http://activiti.com/modeler"><![CDATA[true]]></modeler:activiti-idm-candidate-user>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <endEvent id="sid-153A62CA-FAE5-4123-85C3-875FAE7361E7"></endEvent>
    <sequenceFlow id="sid-A29C31A7-AF17-4B31-972F-53ACC7C0DAE3" sourceRef="startEvent1" targetRef="sid-F713E5EC-5EB6-4923-823A-0D490B996316"></sequenceFlow>
    <sequenceFlow id="sid-A9D7094B-636A-49C9-877D-0A456A21A3A5" sourceRef="sid-F713E5EC-5EB6-4923-823A-0D490B996316" targetRef="sid-B2A37F07-424D-4797-A7C6-65E15AB760FD"></sequenceFlow>
    <sequenceFlow id="sid-7E8068C4-BDAA-4596-8465-6674E2A1B8AD" sourceRef="sid-BEEF4475-13D5-4A8E-9E04-3A452589BBC3" targetRef="sid-153A62CA-FAE5-4123-85C3-875FAE7361E7"></sequenceFlow>
    <sequenceFlow id="sid-BD130ADC-D94C-4332-9FBB-EC05554EF9EA" sourceRef="sid-B9246DF5-40EB-4D9A-BF58-07E5391C6C58" targetRef="sid-153A62CA-FAE5-4123-85C3-875FAE7361E7"></sequenceFlow>
    <sequenceFlow id="sid-24D74804-6EB7-426A-88D4-B622CC042178" sourceRef="sid-E11FAB26-51D9-458F-82F8-1AE58674382B" targetRef="sid-153A62CA-FAE5-4123-85C3-875FAE7361E7"></sequenceFlow>
    <sequenceFlow id="sid-084F6369-265A-4D96-BB4D-59E84CD807CD" name="默认出线" sourceRef="sid-B2A37F07-424D-4797-A7C6-65E15AB760FD" targetRef="sid-E11FAB26-51D9-458F-82F8-1AE58674382B"></sequenceFlow>
    <sequenceFlow id="sid-AA4D4312-5345-40CB-98D6-E28B700D49AA" name="费用大于1000小于1500" sourceRef="sid-B2A37F07-424D-4797-A7C6-65E15AB760FD" targetRef="sid-BEEF4475-13D5-4A8E-9E04-3A452589BBC3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${money >= 1000 && money <= 1500}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-A910EEF7-72DD-4371-9E1A-5AE5C82D90B6" name="费用大于1500" sourceRef="sid-B2A37F07-424D-4797-A7C6-65E15AB760FD" targetRef="sid-B9246DF5-40EB-4D9A-BF58-07E5391C6C58">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${money > 1500}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_exclusive">
    <bpmndi:BPMNPlane bpmnElement="exclusive" id="BPMNPlane_exclusive">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="100.0" y="163.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-F713E5EC-5EB6-4923-823A-0D490B996316" id="BPMNShape_sid-F713E5EC-5EB6-4923-823A-0D490B996316">
        <omgdc:Bounds height="80.0" width="100.0" x="178.0" y="135.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-B2A37F07-424D-4797-A7C6-65E15AB760FD" id="BPMNShape_sid-B2A37F07-424D-4797-A7C6-65E15AB760FD">
        <omgdc:Bounds height="40.0" width="40.0" x="389.0" y="155.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-E11FAB26-51D9-458F-82F8-1AE58674382B" id="BPMNShape_sid-E11FAB26-51D9-458F-82F8-1AE58674382B">
        <omgdc:Bounds height="80.0" width="100.0" x="509.0" y="15.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-BEEF4475-13D5-4A8E-9E04-3A452589BBC3" id="BPMNShape_sid-BEEF4475-13D5-4A8E-9E04-3A452589BBC3">
        <omgdc:Bounds height="80.0" width="100.0" x="509.0" y="138.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-B9246DF5-40EB-4D9A-BF58-07E5391C6C58" id="BPMNShape_sid-B9246DF5-40EB-4D9A-BF58-07E5391C6C58">
        <omgdc:Bounds height="80.0" width="100.0" x="510.0" y="255.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-153A62CA-FAE5-4123-85C3-875FAE7361E7" id="BPMNShape_sid-153A62CA-FAE5-4123-85C3-875FAE7361E7">
        <omgdc:Bounds height="28.0" width="28.0" x="750.0" y="164.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-A9D7094B-636A-49C9-877D-0A456A21A3A5" id="BPMNEdge_sid-A9D7094B-636A-49C9-877D-0A456A21A3A5">
        <omgdi:waypoint x="278.0" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="389.0" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-084F6369-265A-4D96-BB4D-59E84CD807CD" id="BPMNEdge_sid-084F6369-265A-4D96-BB4D-59E84CD807CD">
        <omgdi:waypoint x="420.5740740740741" y="166.57407407407408"></omgdi:waypoint>
        <omgdi:waypoint x="509.37344398340247" y="95.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-7E8068C4-BDAA-4596-8465-6674E2A1B8AD" id="BPMNEdge_sid-7E8068C4-BDAA-4596-8465-6674E2A1B8AD">
        <omgdi:waypoint x="609.0" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="750.0" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-A910EEF7-72DD-4371-9E1A-5AE5C82D90B6" id="BPMNEdge_sid-A910EEF7-72DD-4371-9E1A-5AE5C82D90B6">
        <omgdi:waypoint x="420.0907407407407" y="183.90925925925927"></omgdi:waypoint>
        <omgdi:waypoint x="510.0" y="255.29900332225913"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-A29C31A7-AF17-4B31-972F-53ACC7C0DAE3" id="BPMNEdge_sid-A29C31A7-AF17-4B31-972F-53ACC7C0DAE3">
        <omgdi:waypoint x="129.99471655268036" y="177.60191018001734"></omgdi:waypoint>
        <omgdi:waypoint x="178.0" y="176.3274336283186"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-BD130ADC-D94C-4332-9FBB-EC05554EF9EA" id="BPMNEdge_sid-BD130ADC-D94C-4332-9FBB-EC05554EF9EA">
        <omgdi:waypoint x="610.0" y="266.3235294117647"></omgdi:waypoint>
        <omgdi:waypoint x="751.855601632158" y="184.96516965214465"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-24D74804-6EB7-426A-88D4-B622CC042178" id="BPMNEdge_sid-24D74804-6EB7-426A-88D4-B622CC042178">
        <omgdi:waypoint x="609.0" y="85.0"></omgdi:waypoint>
        <omgdi:waypoint x="751.9950990400243" y="170.79705942401463"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-AA4D4312-5345-40CB-98D6-E28B700D49AA" id="BPMNEdge_sid-AA4D4312-5345-40CB-98D6-E28B700D49AA">
        <omgdi:waypoint x="428.1875" y="175.8125"></omgdi:waypoint>
        <omgdi:waypoint x="509.0" y="177.16387959866222"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

2. Execute exclusive gateway

The specific use is exactly the same as the previous one. It is also when the money variable is set to the value of the expense reimbursement approval link and then the current task is completed. Activiti will automatically retrieve the gateway exit, judge the conditions from top to bottom, and exit when it is met. Pay attention to the conditions It is considered satisfied when it is empty.

@Test
    public void exclusiveGatewayTest() {
        // 1、发布流程
        Deployment deployment = repositoryService.createDeployment().name("排他网关流程").addClasspathResource("processes/exclusiveGateway.bpmn20.xml").deploy();

        // 2、启动一个流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("exclusive");

        // 3、查询所有任务
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).list();

        Task task = tasks.get(0);
        // 4、完成费用报销审批环节任务的同时设置流程变量,来告诉Activiti该走哪条线
        Map<String, Object> variables = new HashMap<>();
        variables.put("money", 1600);
        taskService.complete(task.getId(), variables);
    }

You can test the flow of the process by modifying the value of the money variable. Of course, in actual use, the business side only needs to use the conditions of the process line, and the specific values ​​can be stored by themselves.

3. Judgment of complex conditions

In actual use, our judgment conditions may be very complicated, involving a lot of business logic, and only relying on the conditional expression in ${} may not be satisfied. At this time, you can add a custom judgment method of a custom service class in the curly braces , Such as ${conditionService.checkCondition(execution)}, execution is an interface of Activiti, it will be automatically injected when the flow is judged, and the conditionService also needs to be handed over to Spring for management, that is, the @Service annotation is added

The custom class is as follows:

@Service
@Slf4j
public class ConditionService {

    public boolean checkCondition(Execution execution) {
        ExecutionEntity executionEntity = (ExecutionEntity) execution;
        // 此处可以做业务判断
        log.info("tacheId:{}", executionEntity.getActivityId());
        log.info("tacheName:{}", executionEntity.getCurrentFlowElement().getName());
        return true;
    }
}

Then the judgment condition corresponding to the flow line is also modified to ${conditionService.checkCondition(execution)}, and then execute.

Since it returns true every time, it happens to be able to verify the execution order of the next exclusive gateway. It can be seen from the above that after the gateway removes the default outgoing line, start with the general manager's approval. Then the current task after completion should also be the general manager's approval~

Viewing the current task data of the database is also correct~

Guess you like

Origin blog.csdn.net/m0_38001814/article/details/104182756