diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/LiteFlowNodeBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/LiteFlowNodeBuilder.java index f010c1021..bb24fd700 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/LiteFlowNodeBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/LiteFlowNodeBuilder.java @@ -23,27 +23,6 @@ public class LiteFlowNodeBuilder { private final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 用于维护不同类型 node 的处理逻辑 - */ - private static final Map> NodeBuildConsumerMap = new HashMap>() {{ - // 用于处理普通 node - put(NodeTypeEnum.COMMON, (_node, nodeType) -> FlowBus.addNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.SWITCH, (_node, nodeType) -> FlowBus.addNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.IF, (_node, nodeType) -> FlowBus.addNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.FOR, (_node, nodeType) -> FlowBus.addNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.WHILE, (_node, nodeType) -> FlowBus.addNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.BREAK, (_node, nodeType) -> FlowBus.addNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - - // 用于处理脚本 node - put(NodeTypeEnum.SCRIPT, (_node, nodeType) -> FlowBus.addScriptNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.SWITCH_SCRIPT, (_node, nodeType) -> FlowBus.addScriptNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.IF_SCRIPT, (_node, nodeType) -> FlowBus.addScriptNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.FOR_SCRIPT, (_node, nodeType) -> FlowBus.addScriptNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.WHILE_SCRIPT, (_node, nodeType) -> FlowBus.addScriptNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - put(NodeTypeEnum.BREAK_SCRIPT, (_node, nodeType) -> FlowBus.addScriptNode(_node.getId(), _node.getName(), nodeType, _node.getClazz())); - }}; - private final Node node; public static LiteFlowNodeBuilder createNode() { @@ -158,13 +137,14 @@ public class LiteFlowNodeBuilder { public void build() { checkBuild(); try { - for (Map.Entry> entry : NodeBuildConsumerMap.entrySet()) { - NodeTypeEnum nodeType = entry.getKey(); - if (nodeType == this.node.getType()) { - entry.getValue().accept(this.node, nodeType); - return; - } - } + // 用于处理脚本 node + if (this.node.getType().isScript()){ + FlowBus.addScriptNode(this.node.getId(), this.node.getName(), this.node.getType(), this.node.getClazz()); + } + // 用于处理普通 node + else{ + FlowBus.addNode(this.node.getId(), this.node.getName(), this.node.getType(), this.node.getClazz()); + } } catch (Exception e) { String errMsg = StrUtil.format("An exception occurred while building the node[{}],{}", this.node.getId(), e.getMessage()); LOG.error(errMsg, e); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/ScriptComponent.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/ScriptComponent.java index 6584e1897..8a95c8840 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/ScriptComponent.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/ScriptComponent.java @@ -1,11 +1,28 @@ package com.yomahub.liteflow.core; +import com.yomahub.liteflow.enums.NodeTypeEnum; + +import java.util.HashMap; +import java.util.Map; + /** * 脚本接口 * @author Bryan.Zhang * @since 2.9.0 */ public interface ScriptComponent { + /** + * 用于维护脚本类型和脚本 cmp 的映射关系 + */ + Map> ScriptComponentClassMap = new HashMap>() {{ + put(NodeTypeEnum.SCRIPT, ScriptCommonComponent.class); + put(NodeTypeEnum.SWITCH_SCRIPT, ScriptSwitchComponent.class); + put(NodeTypeEnum.IF_SCRIPT, ScriptIfComponent.class); + put(NodeTypeEnum.FOR_SCRIPT, ScriptForComponent.class); + put(NodeTypeEnum.WHILE_SCRIPT, ScriptWhileComponent.class); + put(NodeTypeEnum.BREAK_SCRIPT, ScriptBreakComponent.class); + }}; + /** * 加载脚本 diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java index c1f3f00c5..2b505bdf6 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java @@ -55,18 +55,6 @@ public class FlowBus { private static final Map nodeMap = new CopyOnWriteHashMap<>(); - /** - * 用于维护脚本类型和脚本 cmp 的映射关系 - */ - private static final Map> ScriptComponentClassMap = new HashMap>() {{ - put(NodeTypeEnum.SCRIPT, ScriptCommonComponent.class); - put(NodeTypeEnum.SWITCH_SCRIPT, ScriptSwitchComponent.class); - put(NodeTypeEnum.IF_SCRIPT, ScriptIfComponent.class); - put(NodeTypeEnum.FOR_SCRIPT, ScriptForComponent.class); - put(NodeTypeEnum.WHILE_SCRIPT, ScriptWhileComponent.class); - put(NodeTypeEnum.BREAK_SCRIPT, ScriptBreakComponent.class); - }}; - private FlowBus() { } @@ -148,7 +136,7 @@ public class FlowBus { * @param script 脚本 */ public static void addScriptNode(String nodeId, String name, NodeTypeEnum nodeType, String script) { - addNode(nodeId, name, nodeType, ScriptComponentClassMap.get(nodeType), script); + addNode(nodeId, name, nodeType, ScriptComponent.ScriptComponentClassMap.get(nodeType), script); } private static void addNode(String nodeId, String name, NodeTypeEnum type, Class cmpClazz, String script) {