Browse Source

!146 #I64T00 希望给SWITCH增加一个default特性
Merge pull request !146 from wangtl/feature/I64T00

铂赛东 2 years ago
parent
commit
79e64f3373

+ 2 - 0
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());

+ 27 - 0
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<SwitchCondition> {
+
+    @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;
+    }
+}

+ 2 - 0
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";
 }

+ 15 - 5
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;
+    }
 }

+ 9 - 0
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());
+    }
+
 }

+ 20 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/cmp/ISwitchCmp.java

@@ -0,0 +1,20 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @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";
+	}
+}

+ 8 - 0
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"))
         );
     </chain>
+
+    <chain name="chain7">
+        THEN(
+        a,
+        SWITCH(i).to(b, c).default(d)
+        );
+    </chain>
+
 </flow>