review: 代码审查,方法抽取

This commit is contained in:
smallNorthLee 2025-03-01 20:22:50 +08:00
parent 5e15a100cb
commit 8a3264cfd3
1 changed files with 46 additions and 47 deletions

View File

@ -808,53 +808,26 @@ public class BpmnModelUtils {
// 情况ExclusiveGateway 排它只有一个满足条件的如果没有就走默认的 // 情况ExclusiveGateway 排它只有一个满足条件的如果没有就走默认的
if (currentElement instanceof ExclusiveGateway) { if (currentElement instanceof ExclusiveGateway) {
// 查找满足条件的 SequenceFlow 路径 // 查找满足条件的 SequenceFlow 路径
Gateway gateway = (Gateway) currentElement; SequenceFlow matchSequenceFlow = findMatchSequenceFlow((Gateway) currentElement, variables);
// TODO @小北当一个网关节点下存在多个满足的并行节点时只查询一个节点流程流转会存在问题需要优化具体见issuehttps://github.com/YunaiV/ruoyi-vue-pro/issues/761
SequenceFlow matchSequenceFlow = CollUtil.findOne(gateway.getOutgoingFlows(),
flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId())
&& (evalConditionExpress(variables, flow.getConditionExpression())));
if (matchSequenceFlow == null) {
matchSequenceFlow = CollUtil.findOne(gateway.getOutgoingFlows(),
flow -> ObjUtil.equal(gateway.getDefaultFlow(), flow.getId()));
// 特殊没有默认的情况下并且只有 1 个条件则认为它是默认的
if (matchSequenceFlow == null && gateway.getOutgoingFlows().size() == 1) {
matchSequenceFlow = gateway.getOutgoingFlows().get(0);
}
}
// 遍历满足条件的 SequenceFlow 路径 // 遍历满足条件的 SequenceFlow 路径
if (matchSequenceFlow != null) { if (matchSequenceFlow != null) {
simulateNextFlowElements(matchSequenceFlow.getTargetFlowElement(), variables, resultElements, visitElements); simulateNextFlowElements(matchSequenceFlow.getTargetFlowElement(), variables, resultElements, visitElements);
} }
return;
} }
// 情况InclusiveGateway 包容多个满足条件的如果没有就走默认的 // 情况InclusiveGateway 包容多个满足条件的如果没有就走默认的
if (currentElement instanceof InclusiveGateway) { else if (currentElement instanceof InclusiveGateway) {
// 查找满足条件的 SequenceFlow 路径 // 查找满足条件的 SequenceFlow 路径
Gateway gateway = (Gateway) currentElement; Collection<SequenceFlow> matchSequenceFlows = findMatchSequenceFlows((Gateway) currentElement, variables);
Collection<SequenceFlow> matchSequenceFlows = CollUtil.filterNew(gateway.getOutgoingFlows(),
flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId())
&& evalConditionExpress(variables, flow.getConditionExpression()));
if (CollUtil.isEmpty(matchSequenceFlows)) {
matchSequenceFlows = CollUtil.filterNew(gateway.getOutgoingFlows(),
flow -> ObjUtil.equal(gateway.getDefaultFlow(), flow.getId()));
// 特殊没有默认的情况下并且只有 1 个条件则认为它是默认的
if (CollUtil.isEmpty(matchSequenceFlows) && gateway.getOutgoingFlows().size() == 1) {
matchSequenceFlows = gateway.getOutgoingFlows();
}
}
// 遍历满足条件的 SequenceFlow 路径 // 遍历满足条件的 SequenceFlow 路径
matchSequenceFlows.forEach( matchSequenceFlows.forEach(
flow -> simulateNextFlowElements(flow.getTargetFlowElement(), variables, resultElements, visitElements)); flow -> simulateNextFlowElements(flow.getTargetFlowElement(), variables, resultElements, visitElements));
} }
// 情况ParallelGateway 并行都满足都走 // 情况ParallelGateway 并行都满足都走
if (currentElement instanceof ParallelGateway) { else if (currentElement instanceof ParallelGateway) {
Gateway gateway = (Gateway) currentElement; Gateway gateway = (Gateway) currentElement;
// 遍历子节点 // 遍历子节点
gateway.getOutgoingFlows().forEach( gateway.getOutgoingFlows().forEach(
nextElement -> simulateNextFlowElements(nextElement.getTargetFlowElement(), variables, resultElements, visitElements)); nextElement -> simulateNextFlowElements(nextElement.getTargetFlowElement(), variables, resultElements, visitElements));
return;
} }
} }
@ -911,8 +884,25 @@ public class BpmnModelUtils {
*/ */
private static void handleExclusiveGateway(Gateway gateway, BpmnModel bpmnModel, private static void handleExclusiveGateway(Gateway gateway, BpmnModel bpmnModel,
Map<String, Object> variables, List<FlowNode> nextFlowNodes) { Map<String, Object> variables, List<FlowNode> nextFlowNodes) {
// TODO @小北 这里和 simulateNextFlowElements 中有重复代码是否重构每个网关节点拆分出方法应该比较合理化@芋艿 // 查找满足条件的 SequenceFlow 路径
// TODO @小北ok simulateNextFlowElements 里面处理网关的复用这个方法可以么 SequenceFlow matchSequenceFlow = findMatchSequenceFlow(gateway, variables);
// 遍历满足条件的 SequenceFlow 路径
if (matchSequenceFlow != null) {
FlowElement targetElement = bpmnModel.getFlowElement(matchSequenceFlow.getTargetRef());
if (targetElement instanceof FlowNode) {
nextFlowNodes.add((FlowNode) targetElement);
}
}
}
/**
* 处理排它网关Exclusive Gateway选择符合条件的路径
*
* @param gateway 排他网关
* @param variables 流程变量
* @return 符合条件的路径
*/
private static SequenceFlow findMatchSequenceFlow(Gateway gateway, Map<String, Object> variables){
SequenceFlow matchSequenceFlow = CollUtil.findOne(gateway.getOutgoingFlows(), SequenceFlow matchSequenceFlow = CollUtil.findOne(gateway.getOutgoingFlows(),
flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId()) flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId())
&& (evalConditionExpress(variables, flow.getConditionExpression()))); && (evalConditionExpress(variables, flow.getConditionExpression())));
@ -924,13 +914,7 @@ public class BpmnModelUtils {
matchSequenceFlow = gateway.getOutgoingFlows().get(0); matchSequenceFlow = gateway.getOutgoingFlows().get(0);
} }
} }
// 遍历满足条件的 SequenceFlow 路径 return matchSequenceFlow;
if (matchSequenceFlow != null) {
FlowElement targetElement = bpmnModel.getFlowElement(matchSequenceFlow.getTargetRef());
if (targetElement instanceof FlowNode) {
nextFlowNodes.add((FlowNode) targetElement);
}
}
} }
/** /**
@ -943,6 +927,26 @@ public class BpmnModelUtils {
*/ */
private static void handleInclusiveGateway(Gateway gateway, BpmnModel bpmnModel, private static void handleInclusiveGateway(Gateway gateway, BpmnModel bpmnModel,
Map<String, Object> variables, List<FlowNode> nextFlowNodes) { Map<String, Object> variables, List<FlowNode> nextFlowNodes) {
// 查找满足条件的 SequenceFlow 路径集合
Collection<SequenceFlow> matchSequenceFlows = findMatchSequenceFlows(gateway, variables);
// 遍历满足条件的 SequenceFlow 路径获取目标节点
matchSequenceFlows.forEach(flow -> {
FlowElement targetElement = bpmnModel.getFlowElement(flow.getTargetRef());
if (targetElement instanceof FlowNode) {
nextFlowNodes.add((FlowNode) targetElement);
}
});
}
/**
* 处理排它网关Inclusive Gateway选择符合条件的路径
*
* @param gateway 排他网关
* @param variables 流程变量
* @return 符合条件的路径
*/
private static Collection<SequenceFlow> findMatchSequenceFlows(Gateway gateway, Map<String, Object> variables) {
// 查找满足条件的 SequenceFlow 路径
Collection<SequenceFlow> matchSequenceFlows = CollUtil.filterNew(gateway.getOutgoingFlows(), Collection<SequenceFlow> matchSequenceFlows = CollUtil.filterNew(gateway.getOutgoingFlows(),
flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId()) flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId())
&& evalConditionExpress(variables, flow.getConditionExpression())); && evalConditionExpress(variables, flow.getConditionExpression()));
@ -954,15 +958,10 @@ public class BpmnModelUtils {
matchSequenceFlows = gateway.getOutgoingFlows(); matchSequenceFlows = gateway.getOutgoingFlows();
} }
} }
// 遍历满足条件的 SequenceFlow 路径获取目标节点 return matchSequenceFlows;
matchSequenceFlows.forEach(flow -> {
FlowElement targetElement = bpmnModel.getFlowElement(flow.getTargetRef());
if (targetElement instanceof FlowNode) {
nextFlowNodes.add((FlowNode) targetElement);
}
});
} }
/** /**
* 处理并行网关 * 处理并行网关
* *