In process design, we often encounter the problem of judging the process direction based on conditions. The corresponding implementation method is also given in Activiti: gateway.
Gateway (Gateway) is used to control the flow of the process (called “execution token” in the BPMN2.0 specification). According to different functions, it can be divided into the following four types of gateways:
-Exclusive Gateway
-Parallel Gateway
-Inclusive Gateway
-Event Gateway
Among them, the Exclusive Gateway is used to control the process In the direction of modeling. When the process is executed to the gateway, Activiti calculates according to the set conditions. When the result of the condition calculation is true, the output stream of the current gateway is executed.
Tips: If multiple lines are calculated as true, only the first one will be executed. If there is no true value, an exception will be thrown.
The image of the exclusive gateway in the flowchart is shown below:
The following describes how to use the exclusive gateway.
1, Drawing a flowchart
ExclusiveProcess.bpmn is as follows:
Three of the exclusive gateways Set the Condition value for each output line:
${type == 1}${type == 2}${type == 3}
When the expression evaluates to true, the following system tasks will be executed.
The three system tasks are set as shown in the figure:
The Task type of the three system tasks is set to Expression, and the Expression options are filled in separately
${resumeService.showTask1()}${resumeService.showTask2()}< span class="hljs-variable">${resumeService.showTask3()}
The XML code of the flowchart is as follows:
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"< /span> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" span> xmlns:activiti="http://activiti.org/bpmn" x mlns: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< /span>="http://www.w3.org/1999/XPath" targetNamespace=< span class="hljs-value">"http://www.activiti.org/test"> <process id="exclusiveProcess" name="ExclusiveProcess.bpmn" isExecutable="true"> <startEvent id="startevent1" name="Start">startEvent> < span class="hljs-tag"><exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway">exclusiveGateway> < sequenceFlow id="flow1" sourceRef< /span>="startevent1" targetRef="exclusivegateway1">sequenceFlow>< /span> <serviceTask id="servicetask1" name="Service Task1" activiti:expression="${resumeService.showTask1()}">serviceTask> <sequenceFlow id="flow2" sourceRef= "exclusivegateway1" targetRef="servicetask1" > <conditionExp ression xsi:type="tFormalExpression">conditionExpression> sequenceFlow> << span class="hljs-title">serviceTask id="servicetask3" name="Service Task3" activiti:expression=< span class="hljs-value">"${resumeService.showTask3()}"> serviceTask> <sequenceFlow id="flow3" sourceRef="exclusivegateway1" targetRef="servicetask3"> <conditionExpression xsi:type="tFormalExpression">conditionExpression> sequenceFlow > <serviceTask id ="servicetask2" name="Service Task2" span> activiti:expression="${resumeService.showTask2()}">< span class="hljs-tag">serviceTask> < span class="hljs-tag"><sequenceFlow id="flow4" sourceRef="exclusivegateway1" targetRef="servicetask2"> < conditionExpression xsi:type="tFormalExpression">conditionExpression> sequenceFlow> << span class="hljs-title">endEvent id="endevent1" name="End">endEvent> <sequenceFlow id="flow5" sourceRef ="servicetask2" targetRef="endevent1 ">sequenceFlow> <sequenceFlow id="flow6" sourceRef="servicetask1" targetRef= "endevent1">sequenceFlow> <sequenceFlow id="flow7" sourceRef="servicetask3" targetRef="endevent1">sequenceFlow> process> <bpmndi:BPMNDiagram id ="BPMNDiagram_exclusiveProcess"> <bpmndi: BPMNPlane bpmnElement="exclusiveProcess" id< /span>="BPMNPlane_exclusiveProcess"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">< /span> <omgdc:Bounds height="35.0" width="35.0" x="60.0" y="140.0">omgdc:Bounds> span> bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1"> < span class="hljs-tag"><omgdc:Bounds height="40.0" width="40.0" x="140.0" y="138.0">omgdc:Bounds> bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1"> <omgdc:Bounds height="55.0" width="105.0" x="260.0" y="40.0"> span>omgdc:Bounds> bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3"> <omgdc:Bounds height="55.0" width="105.0" x="269.0" y="220.0">< span class="hljs-title">omgdc:Bounds> bpmndi:BPMNShape > <bpmndi:BPMNShape bpmnElement=< span class="hljs-value">"servicetask2" id="BPMNShape_servicetask2"> <omgdc:Bounds height="55.0" width="105.0" x="260.0" y="131.0">omgdc:Bounds>< / span> bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="480.0" y="141.0"> omgdc:Bounds> bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id span>="BPMNEdge_flow1"> <omgdi:waypoint< /span> x="95.0" y="157.0">omgdi:waypoint< /span>> <omgdi:waypoint x="140.0" y="158.0" span>>omgdi:waypoint> bpmndi:BPMNEdge> <bpmndi:BPMNEdge< /span> bpmnElement ="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="160.0" y="138.0">omgdi:waypoint>< /span> <omgdi:waypoint x="160.0" y="67.0"> span>omgdi:waypoint> <omgdi:waypoint x="260.0" y="67.0">omgdi:waypoint> bpmndi:BPMNEdge> < span class="hljs-tag"><bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="160.0" y="178.0">omgdi:waypoint> <omgdi:waypoint x="160.0" y="247.0">omgdi:waypoint > <omgdi:waypoint x span>="269.0" y="247.0"< /span>>omgdi:waypoint> bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow4" id span>="BPMNEdge_flow4"> <omgdi:waypoint< /span> x="180.0" y="158.0">omgdi:waypoint> <omgdi:waypoint x="260.0" y="158.0">< /span>omgdi:waypoint> < span class="hljs-title">bpmndi:BPMNEdge> <bpmndi:BPMNEdge < span class="hljs-attribute">bpmnElement="flow5" id="BPMNEdge_flow5"> <omgdi:waypoint x="365.0" y="158.0">omgdi:waypoint> < span class="hljs-tag"><omgdi:waypoint x="480.0" y="158.0">omgdi:waypoint> bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6"> <omgdi:waypoint x="365.0" y="67.0 ">omgdi:waypoint> <omgdi:waypoint x="497.0 " y="67.0">omgdi:waypoint> <omgdi: waypoint x="497.0" y< /span>="141.0">omgdi: waypoint> bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow7 " id="BPMNEdge_flow7"> <omgdi:waypoint x="374.0" y="247.0">omgdi:waypoint> <omgdi:waypoint x="497.0" y="247.0">omgdi:waypoint> <omgdi:waypoint x="497.0" y="176.0">< span class="hljs-tag">omgdi:waypoint> bpmndi:BPMNEdge> bpmndi:BPMNPlane> bpmndi:BPMNDiagram>definitions>
2, Deployment process
Put the flowchart file in the processes folder under resources in the project, and run the program.
3, write code
Create a new spring boot project, implement a service in the project, and implement three methods in the service. The code is as follows: < br> ResumeService interface
public interface ResumeService {void showTask1(); void showTask2(); void showTask3();}
ResumeServiceImpl implementation class
@Service("resumeService")public class< /span> ResumeServiceImpl implements ResumeService {< /span> @Override public void < span class="hljs-title">showTask1() { System.out.println("############################Execute task 1" );} @Override public void< /span> showTask2() {System.out.println("############ ################Perform task 2");} @Override public void showTask3() {System.out.println("###########################Execute task 3"); }}
Using spring boot unit testing to implement the process:
ExclusiveProcessTest test class
@RunWith (SpringJUnit4ClassRunner.class)@SpringBootTest(classes = SpringBootActivitiApplication.class)public < span class="hljs-keyword">class ExclusiveProcessTest { @Autowired RuntimeService runtimeService; /** * Start the process, and pass in the parameter type to the process, and the exclusive gateway judges the process direction according to the parameter type*/ @Test public void testStartProcess () {Map map = new HashMap<>(); map.put( "type", 3); runtimeService.startProcessInstanceByKey("exclusiveProcess", map); }}
4, execution result
Run the unit test, the execution result is as follows:
Tips: One attribute of the exclusive gateway is default. When all conditions are not met, the exclusive gateway executes the process defined by the default attribute by default.
<exclusiveGateway id< /span>="exclusivegateway1" name="Exclusive Gateway " default="flow1">exclusiveGateway>