diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java index 4fa7de4ac..ae91ff5c2 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java @@ -59,6 +59,8 @@ public class LiteFlowChainELBuilder { EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.ELIF, Object.class, new ElifOperator()); EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.TO, Object.class, new ToOperator()); EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.TO.toLowerCase(), Object.class, new ToOperator()); + EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.DEFAULT, Object.class, new DefaultOperator()); + EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.DEFAULT.toLowerCase(), Object.class, new DefaultOperator()); EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.TAG, Object.class, new TagOperator()); EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.ANY, Object.class, new AnyOperator()); EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.ID, Object.class, new IdOperator()); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/DefaultOperator.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/DefaultOperator.java new file mode 100644 index 000000000..dc1493c9b --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/DefaultOperator.java @@ -0,0 +1,27 @@ +package com.yomahub.liteflow.builder.el.operator; + +import com.yomahub.liteflow.builder.el.operator.base.BaseOperator; +import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper; +import com.yomahub.liteflow.flow.element.Executable; +import com.yomahub.liteflow.flow.element.condition.SwitchCondition; + +/** + * EL规则中的default的操作符,用法须和SWITCH联合使用 + * + * @author Tingliang Wang + * @since 2.9.5 + */ +public class DefaultOperator extends BaseOperator { + + @Override + public SwitchCondition build(Object[] objects) throws Exception { + OperatorHelper.checkObjectSizeEqTwo(objects); + + SwitchCondition switchCondition = OperatorHelper.convert(objects[0], SwitchCondition.class); + + Executable target = OperatorHelper.convert(objects[1], Executable.class); + switchCondition.setDefaultExecutor(target); + + return switchCondition; + } +} diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java b/liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java index 6510e9abd..baced9a62 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java @@ -66,4 +66,6 @@ public interface ChainConstant { String MONITOR_BUS = "monitorBus"; String CURR_CHAIN_ID = "currChainId"; + + String DEFAULT = "DEFAULT"; } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/SwitchCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/SwitchCondition.java index 082880070..9f7a7c83d 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/SwitchCondition.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/SwitchCondition.java @@ -3,7 +3,6 @@ package com.yomahub.liteflow.flow.element.condition; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.yomahub.liteflow.core.proxy.ComponentProxy; import com.yomahub.liteflow.enums.ConditionTypeEnum; import com.yomahub.liteflow.enums.NodeTypeEnum; import com.yomahub.liteflow.exception.NoSwitchTargetNodeException; @@ -16,11 +15,7 @@ import com.yomahub.liteflow.slot.Slot; import com.yomahub.liteflow.util.LiteFlowProxyUtil; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.function.BiConsumer; -import java.util.function.Predicate; /** * 选择Condition @@ -35,6 +30,8 @@ public class SwitchCondition extends Condition{ private final String TAG_PREFIX = "tag"; private final String TAG_FLAG = ":"; + private Executable defaultExecutor; + @Override public void execute(Integer slotIndex) throws Exception { @@ -70,6 +67,11 @@ public class SwitchCondition extends Condition{ ).findFirst().orElse(null); } + if (ObjectUtil.isNull(targetExecutor)) { + //没有匹配到执行节点,则走默认的执行节点 + targetExecutor = defaultExecutor; + } + if (ObjectUtil.isNotNull(targetExecutor)) { //switch的目标不能是Pre节点或者Finally节点 if (targetExecutor instanceof PreCondition || targetExecutor instanceof FinallyCondition){ @@ -110,4 +112,12 @@ public class SwitchCondition extends Condition{ public Node getSwitchNode(){ return (Node) this.getExecutableList().get(0); } + + public Executable getDefaultExecutor() { + return defaultExecutor; + } + + public void setDefaultExecutor(Executable defaultExecutor) { + this.defaultExecutor = defaultExecutor; + } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/SwitchELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/SwitchELSpringbootTest.java index e5d2db284..c94bb45ee 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/SwitchELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/SwitchELSpringbootTest.java @@ -75,4 +75,13 @@ public class SwitchELSpringbootTest extends BaseTest { Assert.assertTrue(response.isSuccess()); Assert.assertEquals("a==>h==>b",response.getExecuteStepStr()); } + + //switch增加default选项 + @Test + public void testSwitch7() throws Exception{ + LiteflowResponse response = flowExecutor.execute2Resp("chain7", "arg"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("a==>i==>d",response.getExecuteStepStr()); + } + } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/cmp/ISwitchCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/cmp/ISwitchCmp.java new file mode 100644 index 000000000..34e58318a --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/cmp/ISwitchCmp.java @@ -0,0 +1,20 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @author Tingliang Wang + * @email bytlwang@126.com + * @Date 2022/12/09 + */ +package com.yomahub.liteflow.test.switchcase.cmp; + +import com.yomahub.liteflow.core.NodeSwitchComponent; +import org.springframework.stereotype.Component; + +@Component("i") +public class ISwitchCmp extends NodeSwitchComponent { + + @Override + public String processSwitch() throws Exception { + return "a"; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/switchcase/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/switchcase/flow.el.xml index 4a5148de8..295b845e6 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/switchcase/flow.el.xml +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/switchcase/flow.el.xml @@ -42,4 +42,12 @@ SWITCH(h).to(b.tag("td"), b.tag("xx")) ); + + + THEN( + a, + SWITCH(i).to(b, c).default(d) + ); + + \ No newline at end of file