Skip to content

Commit

Permalink
优化完善包容分支
Browse files Browse the repository at this point in the history
  • Loading branch information
qmdx committed Aug 9, 2024
1 parent 8794d04 commit 5ce7f3e
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.stream.Collectors;

/**
* EL 表达式
* 条件表达式
*
* <p>
* 尊重知识产权,不允许非法使用,后果自负
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ public FlowLongContext(FlowCache flowCache, ProcessModelParser processModelParse
}
}

/**
* 检查并返回条件表达式
*/
public Expression checkExpression() {
Assert.isNull(expression, "Interface Expression not implemented");
return this.expression;
}

/**
* 流程 JSON 处理器,默认 jackson 实现
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ public interface ConditionNodeHandler {
* @param nodeModel 节点模型
* @return true 成功 false 失败
*/
Optional<List<NodeModel>> getInclusiveNodes(FlowLongContext flowLongContext, Execution execution, NodeModel nodeModel);
Optional<List<ConditionNode>> getInclusiveNodes(FlowLongContext flowLongContext, Execution execution, NodeModel nodeModel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.aizuda.bpm.engine.model.NodeModel;

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

/**
* 默认流程执行条件处理器
Expand Down Expand Up @@ -50,17 +51,16 @@ public Optional<ConditionNode> getConditionNode(FlowLongContext flowLongContext,

// 根据正则条件节点选择
Map<String, Object> args = this.getArgs(flowLongContext, execution, nodeModel);
Expression expression = flowLongContext.getExpression();
Assert.isNull(expression, "Interface Expression not implemented");
Expression expression = flowLongContext.checkExpression();
Optional<ConditionNode> conditionNodeOptional = conditionNodes.stream()
.sorted(Comparator.comparing(ConditionNode::getPriorityLevel))
.filter(t -> expression.eval(t.getConditionList(), args)).findFirst();
if (!conditionNodeOptional.isPresent()) {
// 未发现满足条件分支,使用无条件分支
conditionNodeOptional = conditionNodes.stream().filter(t -> ObjectUtils.isEmpty(t.getConditionList())).findFirst();
Assert.isFalse(conditionNodeOptional.isPresent(), "Not found executable ConditionNode");
if (conditionNodeOptional.isPresent()) {
return conditionNodeOptional;
}
return conditionNodeOptional;

// 未发现满足条件分支,使用无条件分支
return defaultConditionNode(conditionNodes);
}

public Map<String, Object> getArgs(FlowLongContext flowLongContext, Execution execution, NodeModel nodeModel) {
Expand All @@ -69,9 +69,23 @@ public Map<String, Object> getArgs(FlowLongContext flowLongContext, Execution ex
return args;
}

public Optional<ConditionNode> defaultConditionNode(List<ConditionNode> conditionNodes) {
Optional<ConditionNode> cnOpt = conditionNodes.stream().filter(t -> ObjectUtils.isEmpty(t.getConditionList())).findFirst();
Assert.isFalse(cnOpt.isPresent(), "Not found executable ConditionNode");
return cnOpt;
}

@Override
public Optional<List<NodeModel>> getInclusiveNodes(FlowLongContext flowLongContext, Execution execution, NodeModel nodeModel) {
// TODO
return Optional.empty();
public Optional<List<ConditionNode>> getInclusiveNodes(FlowLongContext flowLongContext, Execution execution, NodeModel nodeModel) {
List<ConditionNode> inclusiveNodes = nodeModel.getInclusiveNodes();

// 根据正则条件节点选择
Expression expression = flowLongContext.checkExpression();
Map<String, Object> args = this.getArgs(flowLongContext, execution, nodeModel);
List<ConditionNode> cnsOpt = inclusiveNodes.stream().filter(t -> expression.eval(t.getConditionList(), args)).collect(Collectors.toList());
if (ObjectUtils.isEmpty(cnsOpt)) {
cnsOpt = Collections.singletonList(defaultConditionNode(inclusiveNodes).get());
}
return Optional.of(cnsOpt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ private static List<String> getAllNextConditionNodeKeys(NodeModel nodeModel) {
}
} else if (nodeModel.inclusiveNode()) {
// 包容节点
for (NodeModel node : nodeModel.getInclusiveNodes()) {
nodeKeys.addAll(getAllNextConditionNodeKeys(node));
for (ConditionNode conditionNode : nodeModel.getInclusiveNodes()) {
nodeKeys.addAll(getAllNextConditionNodeKeys(conditionNode.getChildNode()));
}
} else {
if (!nodeModel.ccNode()) {
Expand Down Expand Up @@ -204,8 +204,8 @@ public static List<NodeModel> getRootNodeAllChildNodes(NodeModel rootNodeModel)
}
} else if (rootNodeModel.inclusiveNode()) {
// 包容节点
for (NodeModel nodeModel : rootNodeModel.getInclusiveNodes()) {
nodeModels.addAll(getRootNodeAllChildNodes(nodeModel));
for (ConditionNode conditionNode : rootNodeModel.getInclusiveNodes()) {
nodeModels.addAll(getRootNodeAllChildNodes(conditionNode.getChildNode()));
}
} else {
// 普通节点
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public class NodeModel implements ModelInstance, Serializable {
* 条件节点列表
*/
private List<ConditionNode> conditionNodes;
/**
* 并行节点
* <p>相当于并行网关</p>
*/
private List<NodeModel> parallelNodes;
/**
* 包容节点
* <p>相当于包容网关</p>
*/
private List<ConditionNode> inclusiveNodes;
/**
* 审批提醒
*/
Expand Down Expand Up @@ -182,21 +192,10 @@ public class NodeModel implements ModelInstance, Serializable {
* 父节点,模型 json 不存在该属性、属于逻辑节点
*/
private NodeModel parentNode;
/**
* 并行节点
* <p>相当于并行网关</p>
*/
private List<NodeModel> parallelNodes;
/**
* 触发器类型 1,立即执行 2,延迟执行
*/
private String triggerType;
/**
* 包容节点
* <p>相当于包容网关</p>
*/
private List<NodeModel> inclusiveNodes;

/**
* 延时处理类型 1,固定时长 2,自动计算
* <p>
Expand Down Expand Up @@ -235,42 +234,20 @@ public boolean execute(FlowLongContext flowLongContext, Execution execution) {
/*
* 执行包容分支
*/
Optional<List<NodeModel>> nodeModelsOptional = flowLongContext.getFlowConditionHandler()
.getInclusiveNodes(flowLongContext, execution, this);
if (nodeModelsOptional.isPresent()) {
// TODO
}
flowLongContext.getFlowConditionHandler()
.getInclusiveNodes(flowLongContext, execution, this)
.ifPresent(t -> t.forEach(s -> this.executeConditionNode(flowLongContext, execution, s)));
return true;
}

if (ObjectUtils.isNotEmpty(conditionNodes)) {
/*
* 执行条件分支
*/
Optional<ConditionNode> conditionNodeOptional = flowLongContext.getFlowConditionHandler()
.getConditionNode(flowLongContext, execution, this);
/*
* 执行创建条件任务
*/
if (conditionNodeOptional.isPresent()) {
NodeModel childNode = conditionNodeOptional.get().getChildNode();
if (null == childNode) {
// 当前条件节点无执行节点,进入当前执行条件节点的下一个节点
childNode = this.getChildNode();
}
if (null != childNode) {
childNode.execute(flowLongContext, execution);
} else {
// 查看是否存在其他的节点 fix https://gitee.com/aizuda/flowlong/issues/I9O8GV
if (nextNode().isPresent()) {
nextNode().ifPresent(nodeModel -> nodeModel.execute(flowLongContext, execution));
} else {
// 不存在任何子节点结束流程
execution.endInstance(this);
}
return true;
}
}
flowLongContext.getFlowConditionHandler()
.getConditionNode(flowLongContext, execution, this)
.ifPresent(t -> this.executeConditionNode(flowLongContext, execution, t));
return true;
}

/*
Expand Down Expand Up @@ -301,6 +278,32 @@ else if (TaskType.end.eq(this.type)) {
return true;
}

/**
* 执行条件节点分支
*
* @param flowLongContext {@link FlowLongContext}
* @param execution {@link Execution}
* @param conditionNode {@link ConditionNode}
*/
public void executeConditionNode(FlowLongContext flowLongContext, Execution execution, ConditionNode conditionNode) {
NodeModel childNode = conditionNode.getChildNode();
if (null == childNode) {
// 当前条件节点无执行节点,进入当前执行条件节点的下一个节点
childNode = this.getChildNode();
}
if (null != childNode) {
childNode.execute(flowLongContext, execution);
} else {
// 查看是否存在其他的节点 fix https://gitee.com/aizuda/flowlong/issues/I9O8GV
if (nextNode().isPresent()) {
nextNode().ifPresent(nodeModel -> nodeModel.execute(flowLongContext, execution));
} else {
// 不存在任何子节点结束流程
execution.endInstance(this);
}
}
}

/**
* 获取process定义的指定节点key的节点模型
*
Expand All @@ -313,7 +316,7 @@ public NodeModel getNode(String nodeKey) {
}

// 条件分支
NodeModel fromConditionNode = getFromConditionNodes(nodeKey);
NodeModel fromConditionNode = this.getFromConditionNodes(nodeKey, conditionNodes);
if (fromConditionNode != null) {
return fromConditionNode;
}
Expand All @@ -325,7 +328,7 @@ public NodeModel getNode(String nodeKey) {
}

// 包容分支
NodeModel fromInclusiveNode = this.getFromNodeModels(nodeKey, inclusiveNodes);
NodeModel fromInclusiveNode = this.getFromConditionNodes(nodeKey, inclusiveNodes);
if (fromInclusiveNode != null) {
return fromInclusiveNode;
}
Expand All @@ -340,7 +343,8 @@ public NodeModel getNode(String nodeKey) {
/**
* 从节点列表中获取指定key节点信息
*
* @param nodeKey 节点 key
* @param nodeKey 节点 key
* @param nodeModels 节点模型列表
* @return 模型节点
*/
private NodeModel getFromNodeModels(String nodeKey, List<NodeModel> nodeModels) {
Expand All @@ -358,10 +362,11 @@ private NodeModel getFromNodeModels(String nodeKey, List<NodeModel> nodeModels)
/**
* 从条件节点中获取节点
*
* @param nodeKey 节点 key
* @param nodeKey 节点 key
* @param conditionNodes 条件节点模型列表
* @return 模型节点
*/
private NodeModel getFromConditionNodes(String nodeKey) {
private NodeModel getFromConditionNodes(String nodeKey, List<ConditionNode> conditionNodes) {
if (null != conditionNodes) {
for (ConditionNode conditionNode : conditionNodes) {
NodeModel conditionChildNode = conditionNode.getChildNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void test() {
processId = this.deployByResource("test/inclusiveProcess.json", testCreator);
// 启动指定流程定义ID启动流程实例
Map<String, Object> args = new HashMap<>();
args.put("day", 8);
args.put("age", 8);
flowLongEngine.startInstanceById(processId, testCreator, args).ifPresent(instance -> {

});
Expand Down
Loading

0 comments on commit 5ce7f3e

Please sign in to comment.