activiti文本解析

2024-06-12 17:12
文章标签 解析 文本 activiti

本文主要是介绍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 获取 Processpublic 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) {// 处理 callActivityString 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();}}
}

这篇关于activiti文本解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1054834

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s