Skip to content

Commit

Permalink
优化完善包容分支
Browse files Browse the repository at this point in the history
  • Loading branch information
qmdx committed Aug 21, 2024
1 parent 2f7321a commit 5706fed
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.*;

/**
* 流程执行过程中所传递的执行对象,其中包含流程定义、流程模型、流程实例对象、执行参数、返回的任务列表
Expand Down Expand Up @@ -149,13 +144,15 @@ public boolean executeNodeModel(FlowLongContext flowLongContext, String nodeKey)
ProcessModel processModel = this.getProcessModel();
Assert.isNull(processModel, "Process model content cannot be empty");
NodeModel nodeModel = processModel.getNode(nodeKey);
Assert.isNull(nodeModel, "Not found in the process model, process nodeKey=" + nodeKey);

// 获取当前任务列表,检查并行分支执行情况
List<String> nodeKeys = new LinkedList<>();
flowLongContext.getQueryService().getActiveTasksByInstanceId(flwTask.getInstanceId()).ifPresent(flwTasks -> {
for (FlwTask ft : flwTasks) {
nodeKeys.add(ft.getTaskKey());
}
});
Assert.isNull(nodeModel, "Not found in the process model, process nodeKey=" + nodeKey);
Optional<NodeModel> executeNodeOptional = nodeModel.nextNode(nodeKeys);
if (executeNodeOptional.isPresent()) {
// 执行流程节点
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,21 @@ public static NodeModel findNextNode(NodeModel nodeModel, List<String> currentTa
}
}

// 判断当前节点是不是并行分支,需要判断当前并行是否走完
if (parentNode.parallelNode()) {
// 找到另外的分支,看是否列表有执行,有就不能返回childNode
// 判断当前节点为并行分支或包容分支,需要判断当前并行是否走完
if (parentNode.parallelNode() || parentNode.inclusiveNode()) {
// 找到另外的分支,看是否列表有执行,有就不能返回 childNode
if (null != currentTask && Collections.disjoint(currentTask, getAllNextConditionNodeKeys(parentNode))) {
// 都执行完了
return parentNode.getChildNode();
NodeModel childNode = parentNode.getChildNode();
if (null != childNode && Objects.equals(childNode.getNodeKey(), nodeModel.getNodeKey())) {
// 父节点的子节点是当前节点,执行结束
return null;
}

// 分支执行结束,执行子节点
return childNode;
}

// 有没执行完的
// 分支未执行完
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,47 @@ public NodeModel getNode(String nodeKey) {
* @param rootNode 根节点
*/
public void buildParentNode(NodeModel rootNode) {
List<ConditionNode> conditionNodes = rootNode.getConditionNodes();
if (null != conditionNodes) {
for (ConditionNode conditionNode : conditionNodes) {
NodeModel conditionChildNode = conditionNode.getChildNode();
if (null != conditionChildNode) {
conditionChildNode.setParentNode(rootNode);
this.buildParentNode(conditionChildNode);
}
}
}
// 条件分支
this.buildParentConditionNodes(rootNode, rootNode.getConditionNodes());

// 并行分支
List<NodeModel> parallelNodes = rootNode.getParallelNodes();
if (null != parallelNodes) {
for (NodeModel nodeModel : parallelNodes) {
nodeModel.setParentNode(rootNode);
this.buildParentNode(nodeModel);
}
}

// 包容分支
this.buildParentConditionNodes(rootNode, rootNode.getInclusiveNodes());

// 子节点
NodeModel childNode = rootNode.getChildNode();
if (null != childNode) {
childNode.setParentNode(rootNode);
this.buildParentNode(childNode);
}
}

/**
* 构建条件节点的父节点
*
* @param rootNode 根节点
* @param conditionNodes 条件节点
*/
private void buildParentConditionNodes(NodeModel rootNode, List<ConditionNode> conditionNodes) {
if (null != conditionNodes) {
for (ConditionNode conditionNode : conditionNodes) {
NodeModel conditionChildNode = conditionNode.getChildNode();
if (null != conditionChildNode) {
conditionChildNode.setParentNode(rootNode);
this.buildParentNode(conditionChildNode);
}
}
}
}

/**
* 清理父节点关系
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package test.mysql;

import com.aizuda.bpm.engine.entity.FlwHisInstance;
import com.aizuda.bpm.engine.entity.FlwTask;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
Expand All @@ -13,11 +16,79 @@ public class TestInclusiveNode extends MysqlTest {
@Test
public void test() {
processId = this.deployByResource("test/inclusiveProcess.json", testCreator);
// 启动指定流程定义ID启动流程实例
Map<String, Object> args = new HashMap<>();

// 走默认分支,有 1 个执行任务
args.put("age", 1);
flowLongEngine.startInstanceById(processId, testCreator, args).flatMap(i -> flowLongEngine.queryService()
.getActiveTasksByInstanceId(i.getId())).ifPresent(t -> {
Assertions.assertEquals(1, t.size());

// 李小广 审批
FlwTask flwTask = t.get(0);
flowLongEngine.executeTask(flwTask.getId(), test3Creator);

// 流程结束
FlwHisInstance fhi = flowLongEngine.queryService().getHistInstance(flwTask.getInstanceId());
Assertions.assertEquals(1, fhi.getInstanceState());
});
}

@Test
public void test2() {
processId = this.deployByResource("test/inclusiveProcess.json", testCreator);
Map<String, Object> args = new HashMap<>();

// 走 age < 8 分支,有 2 个执行任务
args.put("age", 8);
flowLongEngine.startInstanceById(processId, testCreator, args).ifPresent(instance -> {
flowLongEngine.startInstanceById(processId, testCreator, args).flatMap(i -> flowLongEngine.queryService()
.getActiveTasksByInstanceId(i.getId())).ifPresent(t -> {
Assertions.assertEquals(2, t.size());

// 王小飞 审批
FlwTask flwTask = t.get(0);
flowLongEngine.executeTask(flwTask.getId(), testCreator);

// 陈小超 审批
FlwTask flwTask2 = t.get(1);
flowLongEngine.executeTask(flwTask2.getId(), test2Creator);

// 李小广 审批
flowLongEngine.queryService().getActiveTasksByInstanceId(flwTask.getInstanceId()).ifPresent(t2 -> {
Assertions.assertEquals(1, t2.size());
flowLongEngine.executeTask(t2.get(0).getId(), test3Creator);
});

// 流程结束
FlwHisInstance fhi = flowLongEngine.queryService().getHistInstance(flwTask.getInstanceId());
Assertions.assertEquals(1, fhi.getInstanceState());
});
}

@Test
public void test3() {
processId = this.deployByResource("test/inclusiveProcess.json", testCreator);
Map<String, Object> args = new HashMap<>();

// 走 age < 6 分支,有 1 个执行任务
args.put("age", 6);
flowLongEngine.startInstanceById(processId, testCreator, args).flatMap(i -> flowLongEngine.queryService()
.getActiveTasksByInstanceId(i.getId())).ifPresent(t -> {
Assertions.assertEquals(1, t.size());

// 陈小超 审批
FlwTask flwTask = t.get(0);
flowLongEngine.executeTask(flwTask.getId(), test2Creator);

// 李小广 审批
flowLongEngine.queryService().getActiveTasksByInstanceId(flwTask.getInstanceId()).ifPresent(t2 -> {
Assertions.assertEquals(1, t2.size());
flowLongEngine.executeTask(t2.get(0).getId(), test3Creator);
});

// 流程结束
FlwHisInstance fhi = flowLongEngine.queryService().getHistInstance(flwTask.getInstanceId());
Assertions.assertEquals(1, fhi.getInstanceState());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
]
],
"childNode": {
"nodeName": "审核人",
"nodeName": "包容分支审核01",
"nodeKey": "flk1722779065762",
"type": 1,
"setType": 1,
"nodeAssigneeList": [
{
"id": "test001",
"name": "李小广"
"name": "王小飞"
}
],
"examineLevel": 1,
Expand Down Expand Up @@ -73,7 +73,7 @@
]
],
"childNode": {
"nodeName": "审核人",
"nodeName": "包容分支审核02",
"nodeKey": "flk1722779074379",
"type": 1,
"setType": 1,
Expand Down

0 comments on commit 5706fed

Please sign in to comment.