Parcourir la source

enhancement #I6LWYM 深层次优化Condition维度的代码

everywhere.z il y a 2 ans
Parent
commit
d9ac06f832

+ 6 - 5
liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java

@@ -59,6 +59,12 @@ public class LiteFlowChainELBuilder {
         EXPRESS_RUNNER.addFunction(ChainConstant.PRE, new PreOperator());
         EXPRESS_RUNNER.addFunction(ChainConstant.FINALLY, new FinallyOperator());
         EXPRESS_RUNNER.addFunction(ChainConstant.IF, new IfOperator());
+        EXPRESS_RUNNER.addFunction(ChainConstant.NODE.toUpperCase(), new NodeOperator());
+        EXPRESS_RUNNER.addFunction(ChainConstant.NODE, new NodeOperator());
+        EXPRESS_RUNNER.addFunction(ChainConstant.FOR, new ForOperator());
+        EXPRESS_RUNNER.addFunction(ChainConstant.WHILE, new WhileOperator());
+        EXPRESS_RUNNER.addFunction(ChainConstant.ITERATOR, new IteratorOperator());
+        EXPRESS_RUNNER.addFunction(ChainConstant.CATCH, new CatchOperator());
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.ELSE, Object.class, new ElseOperator());
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.ELIF, Object.class, new ElifOperator());
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.TO, Object.class, new ToOperator());
@@ -69,11 +75,6 @@ public class LiteFlowChainELBuilder {
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.ID, Object.class, new IdOperator());
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.IGNORE_ERROR, Object.class, new IgnoreErrorOperator());
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.THREAD_POOL, Object.class, new ThreadPoolOperator());
-        EXPRESS_RUNNER.addFunction(ChainConstant.NODE.toUpperCase(), new NodeOperator());
-        EXPRESS_RUNNER.addFunction(ChainConstant.NODE, new NodeOperator());
-        EXPRESS_RUNNER.addFunction(ChainConstant.FOR, new ForOperator());
-        EXPRESS_RUNNER.addFunction(ChainConstant.WHILE, new WhileOperator());
-        EXPRESS_RUNNER.addFunction(ChainConstant.ITERATOR, new IteratorOperator());
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.DO, Object.class, new DoOperator());
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.BREAK, Object.class, new BreakOperator());
         EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.DATA, Object.class, new DataOperator());

+ 26 - 13
liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/DoOperator.java

@@ -1,33 +1,46 @@
 package com.yomahub.liteflow.builder.el.operator;
 
-import cn.hutool.core.collection.ListUtil;
+import com.ql.util.express.exception.QLException;
 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.CatchCondition;
+import com.yomahub.liteflow.flow.element.condition.Condition;
 import com.yomahub.liteflow.flow.element.condition.LoopCondition;
 
 /**
  * EL规则中的DO的操作符
- * 有种用法
+ * 有种用法
  * FOR...DO...BREAK
  * WHILE...DO...BREAK
+ * CATCH...DO
  *
  * @author Bryan.Zhang
  * @since 2.9.0
  */
-public class DoOperator extends BaseOperator<LoopCondition> {
+public class DoOperator extends BaseOperator<Condition> {
     @Override
-    public LoopCondition build(Object[] objects) throws Exception {
+    public Condition build(Object[] objects) throws Exception {
         OperatorHelper.checkObjectSizeEqTwo(objects);
 
-        //DO关键字有可能用在FOR后面,也有可能用于WHILE后面,所以这里要进行判断是不是这两种类型的超类LoopCondition
-        String errorMsg = "The caller must be ForCondition or WhileCondition item";
-        LoopCondition condition = OperatorHelper.convert(objects[0], LoopCondition.class, errorMsg);
-
-        //获得需要执行的可执行表达式
-        Executable doExecutableItem = OperatorHelper.convert(objects[1], Executable.class);
-        condition.setDoExecutor(doExecutableItem);
-
-        return condition;
+        if (objects[0] instanceof CatchCondition){
+            String errorMsg = "The caller must be CatchCondition item";
+            CatchCondition condition = OperatorHelper.convert(objects[0], CatchCondition.class, errorMsg);
+            //获得需要执行的可执行表达式
+            Executable doExecutableItem = OperatorHelper.convert(objects[1], Executable.class);
+            condition.setDoItem(doExecutableItem);
+            return condition;
+        }else if(objects[0] instanceof LoopCondition){
+            String errorMsg = "The caller must be LoopCondition item";
+            //DO关键字有可能用在FOR后面,也有可能用于WHILE后面,所以这里要进行判断是不是这两种类型的超类LoopCondition
+            LoopCondition condition = OperatorHelper.convert(objects[0], LoopCondition.class, errorMsg);
+            //获得需要执行的可执行表达式
+            Executable doExecutableItem = OperatorHelper.convert(objects[1], Executable.class);
+            condition.setDoExecutor(doExecutableItem);
+            return condition;
+        }else{
+            String errorMsg = "The caller must be LoopCondition or CatchCondition item";
+            throw new QLException(errorMsg);
+        }
     }
 }

+ 2 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java

@@ -70,4 +70,6 @@ public interface ChainConstant {
     String CURR_CHAIN_ID = "currChainId";
 
     String DEFAULT = "DEFAULT";
+
+    String CATCH = "CATCH";
 }

+ 26 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/Condition.java

@@ -11,8 +11,11 @@ import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.ListUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.yomahub.liteflow.enums.ExecuteTypeEnum;
+import com.yomahub.liteflow.exception.ChainEndException;
 import com.yomahub.liteflow.flow.element.Executable;
 import com.yomahub.liteflow.enums.ConditionTypeEnum;
+import com.yomahub.liteflow.slot.DataBus;
+import com.yomahub.liteflow.slot.Slot;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -38,6 +41,29 @@ public abstract class Condition implements Executable{
 	 */
 	private String currChainId;
 
+	@Override
+	public void execute(Integer slotIndex) throws Exception {
+		try{
+			executeCondition(slotIndex);
+		}catch (ChainEndException e){
+			//这里单独catch ChainEndException是因为ChainEndException是用户自己setIsEnd抛出的异常
+			//是属于正常逻辑,所以会在FlowExecutor中判断。这里不作为异常处理
+			throw e;
+		}catch (Exception e){
+			Slot slot = DataBus.getSlot(slotIndex);
+			String chainId = this.getCurrChainId();
+			//这里事先取到exception set到slot里,为了方便finally取到exception
+			if (slot.isSubChain(chainId)){
+				slot.setSubException(chainId, e);
+			}else{
+				slot.setException(e);
+			}
+			throw e;
+		}
+	}
+
+	protected abstract void executeCondition(Integer slotIndex) throws Exception;
+
 	@Override
 	public ExecuteTypeEnum getExecuteType() {
 		return ExecuteTypeEnum.CONDITION;

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/FinallyCondition.java

@@ -18,7 +18,7 @@ import com.yomahub.liteflow.flow.element.Executable;
 public class FinallyCondition extends Condition {
 
 	@Override
-	public void execute(Integer slotIndex) throws Exception {
+	public void executeCondition(Integer slotIndex) throws Exception {
 		for(Executable executableItem : this.getExecutableList()){
 			executableItem.setCurrChainId(this.getCurrChainId());
 			executableItem.execute(slotIndex);

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/ForCondition.java

@@ -19,7 +19,7 @@ import com.yomahub.liteflow.util.LiteFlowProxyUtil;
 public class ForCondition extends LoopCondition{
 
     @Override
-    public void execute(Integer slotIndex) throws Exception {
+    public void executeCondition(Integer slotIndex) throws Exception {
         Slot slot = DataBus.getSlot(slotIndex);
         Node forNode = this.getForNode();
         if (ObjectUtil.isNull(forNode)){

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/IfCondition.java

@@ -21,7 +21,7 @@ import com.yomahub.liteflow.util.LiteFlowProxyUtil;
 public class IfCondition extends Condition {
 
     @Override
-    public void execute(Integer slotIndex) throws Exception {
+    public void executeCondition(Integer slotIndex) throws Exception {
         if (ListUtil.toList(NodeTypeEnum.IF, NodeTypeEnum.IF_SCRIPT).contains(getIfNode().getType())){
             //先去判断isAccess方法,如果isAccess方法都返回false,整个IF表达式不执行
             if (!this.getIfNode().isAccess(slotIndex)){

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/IteratorCondition.java

@@ -15,7 +15,7 @@ import java.util.Iterator;
 public class IteratorCondition extends LoopCondition{
 
     @Override
-    public void execute(Integer slotIndex) throws Exception {
+    public void executeCondition(Integer slotIndex) throws Exception {
         Slot slot = DataBus.getSlot(slotIndex);
         Node iteratorNode = this.getIteratorNode();
 

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/PreCondition.java

@@ -18,7 +18,7 @@ import com.yomahub.liteflow.flow.element.Executable;
 public class PreCondition extends Condition {
 
 	@Override
-	public void execute(Integer slotIndex) throws Exception {
+	public void executeCondition(Integer slotIndex) throws Exception {
 		for(Executable executableItem : this.getExecutableList()){
 			executableItem.setCurrChainId(this.getCurrChainId());
 			executableItem.execute(slotIndex);

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/SwitchCondition.java

@@ -27,7 +27,7 @@ public class SwitchCondition extends Condition{
     private final String TAG_FLAG = ":";
 
     @Override
-    public void execute(Integer slotIndex) throws Exception {
+    public void executeCondition(Integer slotIndex) throws Exception {
         if (ListUtil.toList(NodeTypeEnum.SWITCH, NodeTypeEnum.SWITCH_SCRIPT).contains(this.getSwitchNode().getType())){
             //获取switch node
             Node switchNode = this.getSwitchNode();

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/ThenCondition.java

@@ -27,7 +27,7 @@ public class ThenCondition extends Condition {
 	}
 
 	@Override
-	public void execute(Integer slotIndex) throws Exception {
+	public void executeCondition(Integer slotIndex) throws Exception {
 		List<PreCondition> preConditionList = this.getPreConditionList();
 		List<FinallyCondition> finallyConditionList = this.getFinallyConditionList();
 

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/WhenCondition.java

@@ -51,7 +51,7 @@ public class WhenCondition extends Condition {
 
 
 	@Override
-	public void execute(Integer slotIndex) throws Exception {
+	public void executeCondition(Integer slotIndex) throws Exception {
 		executeAsyncCondition(slotIndex);
 	}
 

+ 1 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/WhileCondition.java

@@ -19,7 +19,7 @@ import com.yomahub.liteflow.util.LiteFlowProxyUtil;
 public class WhileCondition extends LoopCondition{
 
     @Override
-    public void execute(Integer slotIndex) throws Exception {
+    public void executeCondition(Integer slotIndex) throws Exception {
         Slot slot = DataBus.getSlot(slotIndex);
         Node whileNode = this.getWhileNode();
         if (ObjectUtil.isNull(whileNode)){

+ 4 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/slot/Slot.java

@@ -339,6 +339,10 @@ public class Slot{
 		putMetaDataMap(EXCEPTION, e);
 	}
 
+	public void removeException(){
+		metaDataMap.remove(EXCEPTION);
+	}
+
 	public Exception getSubException(String chainId) {
 		return (Exception) this.metaDataMap.get(SUB_EXCEPTION_PREFIX + chainId);
 	}