Skip to content

Commit

Permalink
并行分支支持并行子流程
Browse files Browse the repository at this point in the history
  • Loading branch information
lifejwang11 committed Sep 10, 2024
1 parent e467e9b commit 22e0ff4
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,14 @@ default Optional<List<FlwTaskActor>> getActiveTaskActorsByTaskId(Long taskId) {
* @return 历史任务列表
*/
Optional<List<FlwHisTask>> getHisTasksByInstanceId(Long instanceId);

/**
* 根据实例ID获取所有子流程
*
*
* @param instanceId 实例ID
* @return 所有子流程
*/
Optional<List<FlwInstance>> getSubProcessByInstanceId(Long instanceId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 All @@ -18,6 +19,7 @@

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
* 流程执行过程中所传递的执行对象,其中包含流程定义、流程模型、流程实例对象、执行参数、返回的任务列表
Expand Down Expand Up @@ -162,12 +164,32 @@ public boolean executeNodeModel(FlowLongContext flowLongContext, String nodeKey)

// 获取当前任务列表,检查并行分支执行情况
List<String> nodeKeys = new LinkedList<>();
List<String> otherProcessKeys = new LinkedList<>();
flowLongContext.getQueryService().getActiveTasksByInstanceId(flwTask.getInstanceId()).ifPresent(flwTasks -> {
for (FlwTask ft : flwTasks) {
nodeKeys.add(ft.getTaskKey());
}
});
Optional<NodeModel> executeNodeOptional = nodeModel.nextNode(nodeKeys);

//查找流程关联的子流程
Optional<List<FlwInstance>> subProcessList = flowLongContext.getQueryService().getSubProcessByInstanceId(flwTask.getInstanceId());
subProcessList.ifPresent(subProcesses -> subProcesses.forEach(process ->{
ProcessModel otherModel = flowLongContext.getRuntimeService().getProcessModelByInstanceId(process.getId());
otherProcessKeys.addAll(new ArrayList<>(ModelHelper.getRootNodeAllChildNodes(otherModel.getNodeConfig()).stream().map(NodeModel::getNodeKey).collect(Collectors.toList())));
flowLongContext.getQueryService().getActiveTasksByInstanceId(process.getId()).ifPresent(flwTasks -> {
//其他的key
for (FlwTask ft : flwTasks) {
nodeKeys.add(ft.getTaskKey());
}
});
}));

Optional<NodeModel> executeNodeOptional = Optional.empty();
//如果有额外的流程,先判断当前的task是否在流程里面,如果不在直找下一个节点
if (!(!otherProcessKeys.isEmpty() && !nodeKeys.isEmpty() && !Collections.disjoint(nodeKeys, otherProcessKeys))){
executeNodeOptional = nodeModel.nextNode(nodeKeys);
}

if (executeNodeOptional.isPresent()) {
// 执行流程节点
NodeModel executeNode = executeNodeOptional.get();
Expand All @@ -180,7 +202,6 @@ public boolean executeNodeModel(FlowLongContext flowLongContext, String nodeKey)
if (nodeKeys.isEmpty()) {
return this.endInstance(nodeModel);
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ public Optional<List<FlwHisTask>> getHisTasksByInstanceId(Long instanceId) {
return hisTaskDao.selectListByInstanceId(instanceId);
}

@Override
public Optional<List<FlwInstance>> getSubProcessByInstanceId(Long instanceId) {
return instanceDao.selectListByParentInstanceId(instanceId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ public static NodeModel findNextNode(NodeModel nodeModel, List<String> currentTa

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

// 分支执行结束,执行子节点
return childNode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2023-2025 Licensed under the AGPL License
*/
package test.mysql;

import com.aizuda.bpm.engine.ProcessService;
import com.aizuda.bpm.engine.assist.Assert;
import com.aizuda.bpm.engine.core.FlowCreator;
import com.aizuda.bpm.engine.entity.FlwProcess;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

/**
* 测试子流程
*
* @author xdg
*/
public class TestParalleSubProcess extends MysqlTest {

@BeforeEach
public void before() {
processId = this.deployByResource("test/parallelSubProcess.json", getFlowCreator());

// 部署子流程
this.deployByResource("test/workHandover.json", getFlowCreator());
}

@Test
public void testProcess() {
ProcessService processService = flowLongEngine.processService();

// 根据流程定义ID查询
FlwProcess process = processService.getProcessById(processId);
if (null != process) {
// 根据流程定义ID和版本号查询
Assertions.assertNotNull(processService.getProcessByVersion(process.getTenantId(), process.getProcessKey(), process.getProcessVersion()));
}

// 启动指定流程定义ID启动流程实例
FlowCreator flowCreator = this.getFlowCreator();
// 发起,执行条件路由
// 启动指定流程定义ID启动流程实例
flowLongEngine.startInstanceById(processId, testCreator).ifPresent(instance -> {
this.executeTaskByKey(instance.getId(), test2Creator, "k003");
// 找到子流程并执行【接收工作任务】完成启动父流程执行结束
flowLongEngine.queryService().getHisTasksByInstanceId(instance.getId()).ifPresent(flwHisTasks -> flwHisTasks.forEach(flwHisTask -> {
if (null != flwHisTask.getCallInstanceId()) {
this.executeActiveTasks(flwHisTask.getCallInstanceId(), test3Creator);
}
}));
this.executeTaskByKey(instance.getId(), test2Creator,"k005");
});

// 卸载指定的定义流程
// Assertions.assertTrue(processService.undeploy(processId));
}


public FlowCreator getFlowCreator() {
return testCreator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"id":10,
"key":"parallelNodes",
"name":"请假审批",
"nodeConfig":{
"nodeName":"发起人",
"nodeKey":"k001",
"type":0,
"nodeAssigneeList":[ ],
"childNode":{
"nodeName":"并行路由",
"type":8,
"nodeKey":"k002",
"conditionNodes":[ ],
"parallelNodes":[
{
"nodeName":"领导审批",
"type":1,
"setType":1,
"nodeKey":"k003",
"nodeAssigneeList":[
{
"id":"test002",
"name":"测试2"
}
],
"examineLevel":1,
"directorLevel":1,
"selectMode":1,
"termAuto":false,
"term":0,
"termMode":1,
"examineMode":1,
"directorMode":0
},
{
"nodeName":"工作交接",
"nodeKey":"k007",
"type":5,
"callProcess":"workHandover"
}
],
"childNode":{
"nodeName":"最后审批",
"type":1,
"setType":1,
"nodeKey":"k005",
"nodeAssigneeList":[
{
"id":"test002",
"name":"测试2"
}
],
"examineLevel":1,
"directorLevel":1,
"selectMode":1,
"termAuto":false,
"term":0,
"termMode":1,
"examineMode":1,
"directorMode":0
}
}
}
}

0 comments on commit 22e0ff4

Please sign in to comment.