1.所需jar包
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>5.22.0</version>
</dependency>
2.解析类
package cn.com.agree.rule.engine.activiti;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.CallActivity;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.FlowNode;
import org.activiti.bpmn.model.InclusiveGateway;
import org.activiti.bpmn.model.ParallelGateway;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.engine.impl.util.io.InputStreamSource;
import org.mvel2.MVEL;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
public class BPMNService {
private Map<String, BpmnModel> bpmnModelMap = new HashMap<>();
private Map<String, Process> processMap = new HashMap<>();
public void init(List<String> bpmnFilePaths) throws Exception {
for (String filePath : bpmnFilePaths) {
loadBpmnModel(filePath);
}
}
private void loadBpmnModel(String bpmnFilePath) throws Exception {
InputStream bpmnStream = new FileInputStream(bpmnFilePath);
BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
InputStreamSource inputStreamSource = new InputStreamSource(bpmnStream);
BpmnModel bpmnModel = bpmnXMLConverter.convertToBpmnModel(inputStreamSource, false, false);
bpmnStream.close();
for (Process process : bpmnModel.getProcesses()) {
String definitionKey = process.getId();
bpmnModelMap.put(definitionKey, bpmnModel);
processMap.put(definitionKey, process);
}
}
public Process getProcessByDefinitionKey(String definitionKey) {
return processMap.get(definitionKey);
}
public FlowElement getNextFlowElement(String definitionKey, String currentElementId, Map<String, Object> variables) {
Process process = getProcessByDefinitionKey(definitionKey);
if (process == null) {
return null;
}
FlowElement currentElement = process.getFlowElement(currentElementId);
if (currentElement == null) {
return null;
}
if (currentElement instanceof FlowNode) {
List<SequenceFlow> outgoingFlows = ((FlowNode) currentElement).getOutgoingFlows();
if (outgoingFlows.isEmpty()) {
return null;
}
if (currentElement instanceof ExclusiveGateway) {
for (SequenceFlow outgoingFlow : outgoingFlows) {
if (evaluateCondition(outgoingFlow.getConditionExpression(), variables)) {
return process.getFlowElement(outgoingFlow.getTargetRef());
}
}
} else if (currentElement instanceof ParallelGateway || currentElement instanceof InclusiveGateway) {
for (SequenceFlow outgoingFlow : outgoingFlows) {
return process.getFlowElement(outgoingFlow.getTargetRef());
}
} else if (currentElement instanceof CallActivity) {
String calledElement = ((CallActivity) currentElement).getCalledElement();
Process calledProcess = getProcessByDefinitionKey(calledElement);
if (calledProcess != null) {
for (FlowElement element : calledProcess.getFlowElements()) {
if (element instanceof StartEvent) {
return element;
}
}
}
} else {
return process.getFlowElement(outgoingFlows.get(0).getTargetRef());
}
}
return null;
}
private boolean evaluateCondition(String conditionExpression, Map<String, Object> variables) {
if (conditionExpression == null || conditionExpression.trim().isEmpty()) {
return true;
}
return MVEL.evalToBoolean(conditionExpression, variables);
}
public static void main(String[] args) {
try {
BPMNService bpmnService = new BPMNService();
bpmnService.init(Arrays.asList("bpmn\\index.flow.bpmn"));
String definitionKey = "trade/test";
String startNodeId = "task2";
Map<String, Object> variables = new HashMap<>();
variables.put("someVariable", true);
FlowElement nextElement = bpmnService.getNextFlowElement(definitionKey, startNodeId, variables);
if (nextElement != null) {
System.out.println("Next Element ID: " + nextElement.getId());
System.out.println("Next Element Name: " + nextElement.getName());
} else {
System.out.println("No next element found for the given node ID.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}