Activit的心路歷程:獲取當前節點的下一節點【可能存在多個】的nodeId
阿新 • • 發佈:2020-11-13
上一任務節點
在我的開發任務中,突然給我提出了一個待辦任務需要獲取當前任務節點下一任務節點的表單資訊,剛開始搞得我有點措手不及,後來仔細是靠後,靈感一下,直接操作流程的bpmn資訊就可以獲取到節點資訊嘛,順著這個思路,我整理出了自己的思路:
(1)將節點大體分為兩類,一類是閘道器節點,另外一類就是使用者任務節點,使用List集合,將閘道器與使用者任務進行分類
(2)獲取上一節點,我們就需要從bpmn的連線資訊入手,這次我們獲取的是UserTask節點的出線,固定連線的sourceRef,辨別節點targtaetRef的型別,當是使用者任務時,放進 List behildNodeIdlist= new ArrayList<>();,當是GateWay節點時,將sourceRef設為閘道器的,繼續遍歷下一節點,就是跳過閘道器節點,只要使用者任務節點
@Test public void behindNode() { // String nodeId="UserTask_0mkm9h7"; String nodeId = "UserTask_0mkm9h7"; String processInstanceId="2205001"; //閘道器集合 List<Gateway> gateways = new ArrayList<>(); //使用者任務集合 List<UserTask> userTasks = new ArrayList<>(); //閘道器節點id List<String> gatewayNodelIdList = new ArrayList<>(); //使用者任務節點id List<String> usertaskNodelIdList = new ArrayList<>(); ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); String processDefinitionId = processInstance.getProcessDefinitionId(); //ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult(); BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId); List<Process> processes = bpmnModel.getProcesses(); Process process = processes.get(0); Collection<FlowElement> flowElements = process.getFlowElements(); //將閘道器資訊與使用者任務進行分類儲存(放於JVM堆中) flowElements.forEach(flowElement -> { if (flowElement instanceof Gateway) { gatewayNodelIdList.add(flowElement.getId()); gateways.add((Gateway) flowElement); } if (flowElement instanceof UserTask) { usertaskNodelIdList.add(flowElement.getId()); userTasks.add((UserTask) flowElement); } }); //存放下一節點的nodeId(可能存在多個) List<String> behildNodeIdlist = new ArrayList<>(); //變數所有的UserTask節點 for (UserTask userTask : userTasks) { //獲取UserTask節點的出線 List<SequenceFlow> outgoingFlows = userTask.getOutgoingFlows(); for (SequenceFlow outgoingFlow : outgoingFlows) { String sourceRef = outgoingFlow.getSourceRef(); String targetRef = outgoingFlow.getTargetRef(); //固定出線的(sourceRef) if (nodeId.equals(sourceRef)) { //當前節點的下一節點是閘道器,跳過 if (gatewayNodelIdList.contains(targetRef)) { for (Gateway gateway : gateways) { //獲取閘道器的出線資訊 List<SequenceFlow> outgoingFlowsGateWay = gateway.getOutgoingFlows(); for (SequenceFlow sequenceFlow : outgoingFlowsGateWay) { String sourceRefGateWay = sequenceFlow.getSourceRef(); String targetRefGateWay = sequenceFlow.getTargetRef(); //定sourceRefGateWay if (targetRef.equals(sourceRefGateWay)) { behildNodeIdlist.add(targetRefGateWay); } } } } else { behildNodeIdlist.add(targetRef); } } } } behildNodeIdlist.forEach(System.out::println); }
我使用的測試流程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://activiti.org/bpmn"> <process id="Process_1" name="1023後臺設計器測試資料HH" isExecutable="true"> <startEvent id="StartEvent_1b9bgjv" name="start"></startEvent> <userTask id="UserTask_1wba62u" name="faqi" activiti:assignee="wuniting"> <extensionElements> <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <sequenceFlow id="SequenceFlow_1fnj22n" sourceRef="StartEvent_1b9bgjv" targetRef="UserTask_1wba62u"></sequenceFlow> <parallelGateway id="ParallelGateway_0g393ev"></parallelGateway> <userTask id="UserTask_1nt87by" name="tb1" activiti:assignee="wuniting"> <extensionElements> <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <userTask id="UserTask_1xty4gu" name="tb2" activiti:assignee="zhangmiao"> <extensionElements> <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <userTask id="UserTask_0y14rmq" name="sp1" activiti:assignee="wuniting"> <extensionElements> <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <userTask id="UserTask_17jd9y1" name="sp2" activiti:assignee="zhangmiao"> <extensionElements> <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <parallelGateway id="ParallelGateway_1qs7f87"></parallelGateway> <endEvent id="EndEvent_0fo9mwb" name="交易完成"></endEvent> <sequenceFlow id="SequenceFlow_1dkxqia" sourceRef="UserTask_1wba62u" targetRef="ParallelGateway_0g393ev"></sequenceFlow> <sequenceFlow id="SequenceFlow_1pp85s1" sourceRef="ParallelGateway_0g393ev" targetRef="UserTask_1nt87by"></sequenceFlow> <sequenceFlow id="SequenceFlow_1x051ae" sourceRef="ParallelGateway_0g393ev" targetRef="UserTask_1xty4gu"></sequenceFlow> <sequenceFlow id="SequenceFlow_05zxco5" sourceRef="UserTask_1nt87by" targetRef="UserTask_0y14rmq"></sequenceFlow> <sequenceFlow id="SequenceFlow_0bx5wt0" sourceRef="UserTask_1xty4gu" targetRef="UserTask_17jd9y1"></sequenceFlow> <sequenceFlow id="SequenceFlow_0xgm2pr" sourceRef="UserTask_17jd9y1" targetRef="ParallelGateway_1qs7f87"></sequenceFlow> <sequenceFlow id="SequenceFlow_1o2rph4" sourceRef="UserTask_0y14rmq" targetRef="ParallelGateway_1qs7f87"></sequenceFlow> <sequenceFlow id="SequenceFlow_1hyky18" sourceRef="ParallelGateway_1qs7f87" targetRef="UserTask_0mkm9h7"></sequenceFlow> <userTask id="UserTask_0mkm9h7" name="spe" activiti:assignee="wuniting"> <extensionElements> <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete> </extensionElements> </userTask> <sequenceFlow id="SequenceFlow_1smv331" sourceRef="UserTask_0mkm9h7" targetRef="EndEvent_0fo9mwb"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_Process_1"> <bpmndi:BPMNPlane bpmnElement="Process_1" id="BPMNPlane_Process_1"> <bpmndi:BPMNShape bpmnElement="StartEvent_1b9bgjv" id="BPMNShape_StartEvent_1b9bgjv"> <omgdc:Bounds height="36.0" width="36.0" x="182.0" y="222.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="UserTask_1wba62u" id="BPMNShape_UserTask_1wba62u"> <omgdc:Bounds height="80.0" width="100.0" x="270.0" y="200.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="ParallelGateway_0g393ev" id="BPMNShape_ParallelGateway_0g393ev"> <omgdc:Bounds height="50.0" width="50.0" x="425.0" y="215.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="UserTask_1nt87by" id="BPMNShape_UserTask_1nt87by"> <omgdc:Bounds height="80.0" width="100.0" x="540.0" y="140.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="UserTask_1xty4gu" id="BPMNShape_UserTask_1xty4gu"> <omgdc:Bounds height="80.0" width="100.0" x="540.0" y="260.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="UserTask_0y14rmq" id="BPMNShape_UserTask_0y14rmq"> <omgdc:Bounds height="80.0" width="100.0" x="710.0" y="140.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="UserTask_17jd9y1" id="BPMNShape_UserTask_17jd9y1"> <omgdc:Bounds height="80.0" width="100.0" x="710.0" y="260.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="ParallelGateway_1qs7f87" id="BPMNShape_ParallelGateway_1qs7f87"> <omgdc:Bounds height="50.0" width="50.0" x="865.0" y="205.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="EndEvent_0fo9mwb" id="BPMNShape_EndEvent_0fo9mwb"> <omgdc:Bounds height="36.0" width="36.0" x="1162.0" y="212.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="UserTask_0mkm9h7" id="BPMNShape_UserTask_0mkm9h7"> <omgdc:Bounds height="80.0" width="100.0" x="990.0" y="190.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_1pp85s1" id="BPMNEdge_SequenceFlow_1pp85s1"> <omgdi:waypoint x="450.0" y="215.0"></omgdi:waypoint> <omgdi:waypoint x="450.0" y="180.0"></omgdi:waypoint> <omgdi:waypoint x="540.0" y="180.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_0xgm2pr" id="BPMNEdge_SequenceFlow_0xgm2pr"> <omgdi:waypoint x="810.0" y="300.0"></omgdi:waypoint> <omgdi:waypoint x="890.0" y="300.0"></omgdi:waypoint> <omgdi:waypoint x="890.0" y="255.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_1dkxqia" id="BPMNEdge_SequenceFlow_1dkxqia"> <omgdi:waypoint x="370.0" y="240.0"></omgdi:waypoint> <omgdi:waypoint x="425.0" y="240.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_05zxco5" id="BPMNEdge_SequenceFlow_05zxco5"> <omgdi:waypoint x="640.0" y="180.0"></omgdi:waypoint> <omgdi:waypoint x="710.0" y="180.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_1x051ae" id="BPMNEdge_SequenceFlow_1x051ae"> <omgdi:waypoint x="450.0" y="265.0"></omgdi:waypoint> <omgdi:waypoint x="450.0" y="300.0"></omgdi:waypoint> <omgdi:waypoint x="540.0" y="300.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_1o2rph4" id="BPMNEdge_SequenceFlow_1o2rph4"> <omgdi:waypoint x="810.0" y="180.0"></omgdi:waypoint> <omgdi:waypoint x="890.0" y="180.0"></omgdi:waypoint> <omgdi:waypoint x="890.0" y="205.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_0bx5wt0" id="BPMNEdge_SequenceFlow_0bx5wt0"> <omgdi:waypoint x="640.0" y="300.0"></omgdi:waypoint> <omgdi:waypoint x="710.0" y="300.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_1smv331" id="BPMNEdge_SequenceFlow_1smv331"> <omgdi:waypoint x="1090.0" y="230.0"></omgdi:waypoint> <omgdi:waypoint x="1162.0" y="230.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_1fnj22n" id="BPMNEdge_SequenceFlow_1fnj22n"> <omgdi:waypoint x="218.0" y="240.0"></omgdi:waypoint> <omgdi:waypoint x="270.0" y="240.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="SequenceFlow_1hyky18" id="BPMNEdge_SequenceFlow_1hyky18"> <omgdi:waypoint x="915.0" y="230.0"></omgdi:waypoint> <omgdi:waypoint x="990.0" y="230.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>