activiti文本解析

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);
        }
    }

    // 加载单个 BPMN 文件
    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);
        }
    }

    // 根据 definitionKey 获取 Process
    public Process getProcessByDefinitionKey(String definitionKey) {
        return processMap.get(definitionKey);
    }
 // 根据当前节点的 ID 获取下一个节点(包括处理网关和嵌套流程)
    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) {
                // 处理 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; // 无条件表达式时默认返回 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();
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值