update - 修复特殊嵌套网关模型设计,没有 child 时,异常连接到结束节点的问题

This commit is contained in:
wangli 2024-04-11 21:22:26 +08:00
parent b89c4e3480
commit 91cbaee067
3 changed files with 2110 additions and 1832 deletions

View File

@ -197,7 +197,7 @@ public final class BpmnJsonConverterUtil {
mainProcess.addFlowElement(convertJsonToElement(EndEvent.class, mainProcess));
// 解析前端传入的模型设计 json
List<String> lastNodeIds = create(bpmnJsonNode, mainProcess, bpmnModel, START_EVENT_ID);
List<String> lastNodeIds = create(bpmnJsonNode, mainProcess, bpmnModel, null, START_EVENT_ID);
if (CollectionUtils.isEmpty(lastNodeIds)) {
throw new WorkflowEngineException(CONVERTOR_COMMON_ERROR, "未找到链接结束节点的节点数据");
@ -405,10 +405,115 @@ public final class BpmnJsonConverterUtil {
* @return 创建的节点的 ID
*/
private static List<String> create(BpmnJsonNode bpmnJsonNode, Process mainProcess,
BpmnModel bpmnModel, String... preNodeIds) {
BpmnModel bpmnModel, BpmnJsonNode defaultConditionConnectNode, String... preNodeIds) {
// 设置来源节点
bpmnJsonNode.setIncoming(Lists.newArrayList(preNodeIds));
Class<? extends BaseElement> clz = chooseNodeClass(bpmnJsonNode);
// 根据 BpmnJsonNode 创建节点
FlowElement flowElement = convertJsonToElement(clz, bpmnJsonNode, mainProcess);
mainProcess.addFlowElement(flowElement);
connectPreNodes(bpmnJsonNode, mainProcess, preNodeIds);
// 只有网关才会涉及到 branch
List<String> branchLastNodeIds = new ArrayList<>();
if (!CollectionUtils.isEmpty(bpmnJsonNode.getBranches())) {
Gateway gateway = (Gateway) flowElement;
for (BpmnJsonNode branch : bpmnJsonNode.getBranches()) {
// branch == node_condition
BpmnJsonNode nextJsonNode =
(Objects.isNull(branch.getChildren()) || Objects.isNull(branch.getChildren().getType())
|| Objects.equals(NODE_EMPTY, branch.getChildren().getType()))
? bpmnJsonNode.getChildren() : branch.getChildren();
if (Objects.isNull(nextJsonNode) || Objects.isNull(nextJsonNode.getId()) ||
(Objects.equals(NODE_EMPTY, nextJsonNode.getType()) &&
(Objects.isNull(nextJsonNode.getChildren()) || Objects.isNull(nextJsonNode.getChildren().getId())))) {
BpmnJsonNode tempEndNode = new BpmnJsonNode();
tempEndNode.setIncoming(Lists.newArrayList(gateway.getId()));
if (Objects.isNull(defaultConditionConnectNode)) {
tempEndNode.setId(END_EVENT_ID);
} else {
if (Objects.equals(defaultConditionConnectNode.getType(), NODE_EMPTY) && Objects.nonNull(defaultConditionConnectNode.getChildren())) {
tempEndNode.setId(defaultConditionConnectNode.getChildren().getId());
} else {
tempEndNode.setId(defaultConditionConnectNode.getId());
}
defaultConditionConnectNode = null;
}
nextJsonNode = tempEndNode;
} else {
if (Objects.equals(NODE_EMPTY, nextJsonNode.getType()) && Objects.nonNull(nextJsonNode.getChildren())) {
nextJsonNode = nextJsonNode.getChildren();
}
nextJsonNode.setIncoming(Lists.newArrayList(gateway.getId()));
// node_condition 放入计算节点的上级属性中方便顺序流转换器对条件进行处理
nextJsonNode.setPreJsonNode(branch);
}
SequenceFlow sequenceFlow = (SequenceFlow) convertJsonToElement(SequenceFlow.class, nextJsonNode,
mainProcess);
mainProcess.addFlowElement(sequenceFlow);
// 设置网关默认流
if (Objects.nonNull(branch.getProperty()) && Boolean.TRUE.equals(branch.getProperty().getDefaultBranch())) {
// 如果是默认流, 以防止前端输入错误, 强制置空
sequenceFlow.setConditionExpression(null);
gateway.setDefaultFlow(sequenceFlow.getId());
}
if (Objects.nonNull(branch.getChildren()) && StringUtils.hasLength(branch.getChildren().getId())
&& !Objects.equals(NODE_EMPTY, branch.getChildren().getType())) {
branchLastNodeIds.addAll(create(branch.getChildren(), mainProcess, bpmnModel,
Objects.isNull(defaultConditionConnectNode) ? bpmnJsonNode.getChildren() : defaultConditionConnectNode));
}
}
}
// 开始处理下级节点
BpmnJsonNode children = bpmnJsonNode.getChildren();
if (Objects.isNull(children)
|| (Objects.equals(NODE_EMPTY, children.getType()) && (Objects.isNull(children.getChildren()) || Objects.isNull(children.getChildren().getId())))
|| !StringUtils.hasLength(children.getId())) {
if (CollectionUtils.isEmpty(branchLastNodeIds)) {
return Lists.newArrayList(flowElement.getId());
} else {
return branchLastNodeIds;
}
} else {
if (Objects.equals(NODE_EMPTY, children.getType())
&& Objects.nonNull(children.getChildren())
&& Objects.nonNull(children.getChildren().getId())) {
children = children.getChildren();
}
if (CollectionUtils.isEmpty(branchLastNodeIds)) {
return create(children, mainProcess, bpmnModel, defaultConditionConnectNode, flowElement.getId());
}
return create(children, mainProcess, bpmnModel, defaultConditionConnectNode, branchLastNodeIds.toArray(new String[0]));
}
}
private static void connectPreNodes(BpmnJsonNode bpmnJsonNode, Process mainProcess, String[] preNodeIds) {
// 连接当前节点与前一个节点
if (Lists.newArrayList(preNodeIds).isEmpty()) {
// first time entrance, do nothing.
} else if (Lists.newArrayList(preNodeIds).size() == 1 && !NODE_CONDITION.equals(bpmnJsonNode.getType())) {
bpmnJsonNode.setPreJsonNode(null);
mainProcess.addFlowElement(convertJsonToElement(SequenceFlow.class, bpmnJsonNode, mainProcess));
} else {
// 将网关分支的最末级节点 ID 关联到网关的 children 节点上网关协议转换才算闭环
Arrays.stream(preNodeIds).forEach(income -> {
bpmnJsonNode.setIncoming(Lists.newArrayList(income));
bpmnJsonNode.setPreJsonNode(null);
FlowElement sequenceFlow = convertJsonToElement(SequenceFlow.class, bpmnJsonNode, mainProcess);
mainProcess.addFlowElement(sequenceFlow);
});
}
}
private static Class<? extends BaseElement> chooseNodeClass(BpmnJsonNode bpmnJsonNode) {
Class<? extends BaseElement> clz;
switch (bpmnJsonNode.getType()) {
case NODE_STARTER:
@ -445,92 +550,7 @@ public final class BpmnJsonConverterUtil {
clz = NotSupportConverter.NotSupportFlowElement.class;
break;
}
// 根据 BpmnJsonNode 创建节点
FlowElement flowElement = convertJsonToElement(clz, bpmnJsonNode, mainProcess);
mainProcess.addFlowElement(flowElement);
if (Lists.newArrayList(preNodeIds).isEmpty()) {
// first time entrance, do nothing.
} else if (Lists.newArrayList(preNodeIds).size() == 1 && !NODE_CONDITION.equals(bpmnJsonNode.getType())) {
bpmnJsonNode.setPreJsonNode(null);
mainProcess.addFlowElement(convertJsonToElement(SequenceFlow.class, bpmnJsonNode, mainProcess));
} else {
// 将网关分支的最末级节点 ID 关联到网关的 children 节点上网关协议转换才算闭环
Arrays.stream(preNodeIds).forEach(income -> {
bpmnJsonNode.setIncoming(Lists.newArrayList(income));
bpmnJsonNode.setPreJsonNode(null);
FlowElement sequenceFlow = convertJsonToElement(SequenceFlow.class, bpmnJsonNode, mainProcess);
mainProcess.addFlowElement(sequenceFlow);
});
}
// 只有网关才会涉及到 branch
List<String> branchLastNodeIds = new ArrayList<>();
if (!CollectionUtils.isEmpty(bpmnJsonNode.getBranches())) {
Gateway gateway = (Gateway) flowElement;
for (BpmnJsonNode branch : bpmnJsonNode.getBranches()) {
// branch == node_condition
BpmnJsonNode nextJsonNode =
(Objects.isNull(branch.getChildren()) || Objects.isNull(branch.getChildren().getType())
|| Objects.equals(NODE_EMPTY, branch.getChildren().getType()))
? bpmnJsonNode.getChildren() : branch.getChildren();
if (Objects.isNull(nextJsonNode) || Objects.isNull(nextJsonNode.getId()) ||
(Objects.equals(NODE_EMPTY, nextJsonNode.getType()) &&
(Objects.isNull(nextJsonNode.getChildren()) || Objects.isNull(nextJsonNode.getChildren().getId())))) {
BpmnJsonNode tempEndNode = new BpmnJsonNode();
tempEndNode.setIncoming(Lists.newArrayList(gateway.getId()));
tempEndNode.setId(END_EVENT_ID);
nextJsonNode = tempEndNode;
} else {
if (Objects.equals(NODE_EMPTY, nextJsonNode.getType()) && Objects.nonNull(nextJsonNode.getChildren())) {
nextJsonNode = nextJsonNode.getChildren();
}
nextJsonNode.setIncoming(Lists.newArrayList(gateway.getId()));
// node_condition 放入计算节点的上级属性中方便顺序流转换器对条件进行处理
nextJsonNode.setPreJsonNode(branch);
}
SequenceFlow sequenceFlow = (SequenceFlow) convertJsonToElement(SequenceFlow.class, nextJsonNode,
mainProcess);
mainProcess.addFlowElement(sequenceFlow);
// 设置网关默认流
if (Objects.nonNull(branch.getProperty()) && Boolean.TRUE.equals(branch.getProperty().getDefaultBranch())) {
// 如果是默认流, 以防止前端输入错误, 强制置空
sequenceFlow.setConditionExpression(null);
gateway.setDefaultFlow(sequenceFlow.getId());
}
if (Objects.nonNull(branch.getChildren()) && StringUtils.hasLength(branch.getChildren().getId())
&& !Objects.equals(NODE_EMPTY, branch.getChildren().getType())) {
branchLastNodeIds.addAll(create(branch.getChildren(), mainProcess, bpmnModel));
}
}
}
// 开始处理下级节点
BpmnJsonNode children = bpmnJsonNode.getChildren();
if (Objects.isNull(children)
|| (Objects.equals(NODE_EMPTY, children.getType()) && (Objects.isNull(children.getChildren()) || Objects.isNull(children.getChildren().getId())))
|| !StringUtils.hasLength(children.getId())) {
if (CollectionUtils.isEmpty(branchLastNodeIds)) {
return Lists.newArrayList(flowElement.getId());
} else {
return branchLastNodeIds;
}
} else {
if (Objects.equals(NODE_EMPTY, children.getType())
&& Objects.nonNull(children.getChildren())
&& Objects.nonNull(children.getChildren().getId())) {
children = children.getChildren();
}
if (CollectionUtils.isEmpty(branchLastNodeIds)) {
return create(children, mainProcess, bpmnModel, flowElement.getId());
}
return create(children, mainProcess, bpmnModel, branchLastNodeIds.toArray(new String[0]));
}
return clz;
}
private static FlowElement convertJsonToElement(Class<? extends BaseElement> clz, Process process) {

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff