Skip to content

Commit

Permalink
支持获取当前已使用的节点key列表
Browse files Browse the repository at this point in the history
  • Loading branch information
qmdx committed Nov 20, 2024
1 parent a323859 commit 673dc84
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
buildscript {
ext {
springBootVersion = "2.7.0"
mybatisPlusVersion = "3.5.9"
mybatisPlusVersion = "3.5.8"
solonVersion = "3.0.1"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.aizuda.bpm.engine.TaskActorProvider;
import com.aizuda.bpm.engine.assist.Assert;
import com.aizuda.bpm.engine.entity.FlwInstance;
import com.aizuda.bpm.engine.entity.FlwProcess;
import com.aizuda.bpm.engine.entity.FlwTask;
import com.aizuda.bpm.engine.entity.FlwTaskActor;
import com.aizuda.bpm.engine.model.ModelHelper;
Expand Down Expand Up @@ -124,6 +123,14 @@ public Execution(FlowLongEngine engine, ProcessModel processModel, FlowCreator f
this.args = args;
}

/**
* 构造函数,仅适用于模型条件节点查找
*/
public Execution(FlowCreator flowCreator, Map<String, Object> args) {
this.flowCreator = flowCreator;
this.args = args;
}

/**
* 根据当前执行对象execution、子流程定义process、当前节点名称产生子流程的执行对象
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.aizuda.bpm.engine.FlowConstants;
import com.aizuda.bpm.engine.FlowDataTransfer;
import com.aizuda.bpm.engine.assist.ObjectUtils;
import com.aizuda.bpm.engine.core.Execution;
import com.aizuda.bpm.engine.core.FlowLongContext;
import com.aizuda.bpm.engine.core.enums.NodeSetType;
import com.aizuda.bpm.engine.core.enums.TaskType;

Expand Down Expand Up @@ -368,4 +370,61 @@ public static void reloadProcessModel(ProcessModel processModel, Consumer<Proces
FlowDataTransfer.removeByKey(FlowConstants.processDynamicAssignee);
}
}

/**
* 获取当前已使用的节点key列表
*
* @param flowLongContext 流程上下文 {@link FlowLongContext}
* @param execution 流程执行对象 {@link Execution}
* @param rootNodeModel 模型根节点 {@link NodeModel}
* @param currentNodeKey 当前所在节点
* @return 当前已使用的节点key列表
*/
public static List<String> getAllUsedNodeKeys(FlowLongContext flowLongContext, Execution execution, NodeModel rootNodeModel, String currentNodeKey) {
List<String> currentUsedNodeKeys = new ArrayList<>();
if (null != rootNodeModel) {
String nodeKey = rootNodeModel.getNodeKey();
if (Objects.equals(currentNodeKey, nodeKey)) {
// 找到执行最后一个节点直接结束
currentUsedNodeKeys.add(nodeKey);
} else {
// 处理完成节点
if (rootNodeModel.conditionNode()) {
// 条件节点
List<ConditionNode> conditionNodes = rootNodeModel.getConditionNodes();
if (ObjectUtils.isNotEmpty(conditionNodes)) {
// 找到对应节点
flowLongContext.getFlowConditionHandler().getConditionNode(flowLongContext, execution, rootNodeModel).ifPresent(t -> {
// 添加执行条件节点
currentUsedNodeKeys.add(t.getNodeKey());

// 条件节点分支子节点
getChildAllUsedNodeKeys(currentUsedNodeKeys, flowLongContext, execution, t.getChildNode(), currentNodeKey);
});
}

// 条件节点子节点
getChildAllUsedNodeKeys(currentUsedNodeKeys, flowLongContext, execution, rootNodeModel.getChildNode(), currentNodeKey);
} else {

// 普通节点
currentUsedNodeKeys.add(nodeKey);

// 找子节点
NodeModel childNodeModel = rootNodeModel.getChildNode();
if (null != childNodeModel) {
getChildAllUsedNodeKeys(currentUsedNodeKeys, flowLongContext, execution, childNodeModel, currentNodeKey);
}
}
}
}
return currentUsedNodeKeys;
}

public static void getChildAllUsedNodeKeys(List<String> currentUsedNodeKeys, FlowLongContext flowLongContext,
Execution execution, NodeModel rootNodeModel, String currentNodeKey) {
if (!currentUsedNodeKeys.contains(currentNodeKey)) {
currentUsedNodeKeys.addAll(getAllUsedNodeKeys(flowLongContext, execution, rootNodeModel, currentNodeKey));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.aizuda.bpm.engine.FlowDataTransfer;
import com.aizuda.bpm.engine.assist.StreamUtils;
import com.aizuda.bpm.engine.core.Execution;
import com.aizuda.bpm.engine.core.FlowCreator;
import com.aizuda.bpm.engine.core.FlowLongContext;
import com.aizuda.bpm.engine.model.*;
Expand Down Expand Up @@ -193,4 +194,27 @@ public void errorModel01() {
ProcessModel errorModel01 = getProcessModel("test/errorModel01.json");
Assertions.assertFalse(ModelHelper.checkExistApprovalNode(errorModel01.getNodeConfig()));
}

/**
* 测试获取当前已使用的节点key列表
*/
@Test
public void testCurrentUsedNodeKeys() {
ProcessModel processModel = getProcessModel("test/ccToCondition.json");

Assertions.assertEquals(2, ModelHelper.getAllUsedNodeKeys(flowLongEngine.getContext(), new Execution(testCreator, null),
processModel.getNodeConfig(), "k002").size());

Assertions.assertEquals(4, ModelHelper.getAllUsedNodeKeys(flowLongEngine.getContext(), new Execution(testCreator, new HashMap<String, Object>() {{
put("day", 3);
}}), processModel.getNodeConfig(), "k007").size());

Assertions.assertEquals(4, ModelHelper.getAllUsedNodeKeys(flowLongEngine.getContext(), new Execution(testCreator, new HashMap<String, Object>() {{
put("day", 8);
}}), processModel.getNodeConfig(), "k005").size());

Assertions.assertEquals(5, ModelHelper.getAllUsedNodeKeys(flowLongEngine.getContext(), new Execution(testCreator, new HashMap<String, Object>() {{
put("day", 8);
}}), processModel.getNodeConfig(), "k008").size());
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-boot.version>2.7.0</spring-boot.version>
<mybatis.plus-version>3.5.9</mybatis.plus-version>
<mybatis.plus-version>3.5.8</mybatis.plus-version>
<mysql.version>8.0.32</mysql.version>
<solon.version>3.0.1</solon.version>
<maven-surefire-plugin.version>3.1.2</maven-surefire-plugin.version>
Expand Down

0 comments on commit 673dc84

Please sign in to comment.