瀏覽代碼

enhancement #IBJO4X 建立统一元数据操作类,所有元数据的操作的入口

everywhere.z 3 月之前
父節點
當前提交
1bbc09e720

+ 0 - 8
liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java

@@ -292,14 +292,6 @@ public class FlowBus {
 		return nodeMap.get(nodeId);
 	}
 
-    // 获取某一个 chainId 下的所有 nodeId
-    public static List<Node> getNodesByChainId(String chainId) {
-        Chain chain = getChain(chainId);
-		return chain.getConditionList().stream().flatMap(
-				(Function<Condition, Stream<Node>>) condition -> condition.getAllNodeInCondition().stream()
-		).collect(Collectors.toList());
-    }
-
 	public static Map<String, Node> getNodeMap() {
 		return nodeMap;
 	}

+ 3 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Condition.java

@@ -118,7 +118,9 @@ public abstract class Condition implements Executable{
             if (executable instanceof Condition){
                 resultList.addAll(((Condition)executable).getAllNodeInCondition());
 			}else if(executable instanceof Chain){
-				resultList.addAll(FlowBus.getNodesByChainId(executable.getId()));
+				resultList.addAll(((Chain) executable).getConditionList().stream().flatMap(
+						(Function<Condition, Stream<Node>>) condition -> condition.getAllNodeInCondition().stream()
+				).collect(Collectors.toList()));
             }else if(executable instanceof Node){
                 resultList.add((Node)executable);
             }

+ 11 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java

@@ -24,6 +24,7 @@ import com.yomahub.liteflow.log.LFLog;
 import com.yomahub.liteflow.log.LFLoggerManager;
 import com.yomahub.liteflow.util.TupleOf2;
 
+import java.util.Map;
 import java.util.Stack;
 import java.util.concurrent.locks.ReentrantLock;
 
@@ -63,6 +64,8 @@ public class Node implements Executable, Cloneable, Rollbackable{
 
 	private String cmpData;
 
+	private Map<String, ?> bindDataMap;
+
 	private String currChainId;
 
 	private boolean isCompiled = true;
@@ -504,6 +507,14 @@ public class Node implements Executable, Cloneable, Rollbackable{
 		return getInstance().getItemResultMetaValue(slotIndex);
 	}
 
+	public Map<String, ?> getBindDataMap() {
+		return bindDataMap;
+	}
+
+	public void setBindDataMap(Map<String, ?> bindDataMap) {
+		this.bindDataMap = bindDataMap;
+	}
+
 	@Override
 	public Node clone() throws CloneNotSupportedException {
 		Node node = (Node)super.clone();

+ 8 - 32
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/LoopCondition.java

@@ -5,9 +5,11 @@ import com.yomahub.liteflow.flow.element.Condition;
 import com.yomahub.liteflow.flow.element.Executable;
 import com.yomahub.liteflow.flow.element.Node;
 import com.yomahub.liteflow.flow.parallel.LoopFutureObj;
+import com.yomahub.liteflow.meta.LiteflowMetaOperator;
 
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
+import java.util.function.Consumer;
 import java.util.function.Supplier;
 
 /**
@@ -48,47 +50,21 @@ public abstract class LoopCondition extends Condition {
     }
 
     protected void setLoopIndex(Executable executableItem, int index) {
-        if (executableItem instanceof Chain) {
-            ((Chain) executableItem).getConditionList().forEach(condition -> setLoopIndex(condition, index));
-        } else if (executableItem instanceof Condition) {
-            ((Condition) executableItem).getExecutableGroup()
-                    .forEach((key, value) -> value.forEach(executable -> setLoopIndex(executable, index)));
-        } else if (executableItem instanceof Node) {
-            ((Node) executableItem).setLoopIndex(this, index);
-        }
+        LoopCondition thisCondition = this;
+        LiteflowMetaOperator.getNodes(executableItem).forEach(node -> node.setLoopIndex(thisCondition, index));
     }
 
     protected void setCurrLoopObject(Executable executableItem, Object obj) {
-        if (executableItem instanceof Chain) {
-            ((Chain) executableItem).getConditionList().forEach(condition -> setCurrLoopObject(condition, obj));
-        } else if (executableItem instanceof Condition) {
-            ((Condition) executableItem).getExecutableGroup()
-                    .forEach((key, value) -> value.forEach(executable -> setCurrLoopObject(executable, obj)));
-        } else if (executableItem instanceof Node) {
-            ((Node) executableItem).setCurrLoopObject(this, obj);
-        }
+        LoopCondition thisCondition = this;
+        LiteflowMetaOperator.getNodes(executableItem).forEach(node -> node.setCurrLoopObject(thisCondition, obj));
     }
 
     protected void removeLoopIndex(Executable executableItem) {
-        if (executableItem instanceof Chain) {
-            ((Chain) executableItem).getConditionList().forEach(this::removeLoopIndex);
-        } else if (executableItem instanceof Condition) {
-            ((Condition) executableItem).getExecutableGroup()
-                    .forEach((key, value) -> value.forEach(this::removeLoopIndex));
-        } else if (executableItem instanceof Node) {
-            ((Node) executableItem).removeLoopIndex();
-        }
+        LiteflowMetaOperator.getNodes(executableItem).forEach(Node::removeLoopIndex);
     }
 
     protected void removeCurrLoopObject(Executable executableItem) {
-        if (executableItem instanceof Chain) {
-            ((Chain) executableItem).getConditionList().forEach(this::removeCurrLoopObject);
-        } else if (executableItem instanceof Condition) {
-            ((Condition) executableItem).getExecutableGroup()
-                    .forEach((key, value) -> value.forEach(this::removeCurrLoopObject));
-        } else if (executableItem instanceof Node) {
-            ((Node) executableItem).removeCurrLoopObject();
-        }
+        LiteflowMetaOperator.getNodes(executableItem).forEach(Node::removeCurrLoopObject);
     }
 
     public boolean isParallel() {

+ 30 - 8
liteflow-core/src/main/java/com/yomahub/liteflow/meta/LiteflowMetaOperator.java

@@ -1,9 +1,12 @@
 package com.yomahub.liteflow.meta;
 
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.collection.ListUtil;
 import com.yomahub.liteflow.core.FlowExecutorHolder;
 import com.yomahub.liteflow.flow.FlowBus;
 import com.yomahub.liteflow.flow.element.Chain;
 import com.yomahub.liteflow.flow.element.Condition;
+import com.yomahub.liteflow.flow.element.Executable;
 import com.yomahub.liteflow.flow.element.Node;
 import com.yomahub.liteflow.flow.instanceId.NodeInstanceIdManageSpiHolder;
 
@@ -92,13 +95,38 @@ public class LiteflowMetaOperator {
 
     //--------------------------------------------Node相关---------------------------------------------
 
+    /**
+     * 从任意Executable对象中取到Node列表
+     * @param executable 可执行对象,包括Chain,Condition,Node
+     * @return 节点列表
+     */
+    public static List<Node> getNodes(Executable executable){
+        if (executable instanceof Chain){
+            Chain chain = (Chain) executable;
+            return chain.getConditionList().stream().flatMap(
+                    (Function<Condition, Stream<Node>>) condition -> getNodes(condition).stream()
+            ).collect(Collectors.toList());
+        }else if(executable instanceof Condition){
+            Condition condition = (Condition) executable;
+            return condition.getExecutableGroup().entrySet().stream().flatMap(
+                    (Function<Map.Entry<String, List<Executable>>, Stream<Executable>>) entry -> entry.getValue().stream()
+            ).flatMap(
+                    (Function<Executable, Stream<Node>>) item -> getNodes(item).stream()
+            ).collect(Collectors.toList());
+        }else if(executable instanceof Node){
+            return CollectionUtil.toList((Node) executable);
+        }else{
+            return ListUtil.empty();
+        }
+    }
+
     /**
      * 通过chainId获得这个chain中所有的Node
      * @param chainId chain的Id
      * @return 指定chain中的所有Node
      */
     public static List<Node> getNodes(String chainId){
-        return FlowBus.getNodesByChainId(chainId);
+        return getNodes(getChain(chainId));
     }
 
     /**
@@ -108,13 +136,7 @@ public class LiteflowMetaOperator {
      * @return Node对象列表,一个节点在Chain里有可能出现多次
      */
     public static List<Node> getNodes(String chainId, String nodeId){
-        Chain chain = getChain(chainId);
-        if (chain == null){
-            return null;
-        }
-        return chain.getConditionList().stream().flatMap(
-                (Function<Condition, Stream<Node>>) condition -> condition.getAllNodeInCondition().stream()
-        ).filter(
+        return getNodes(chainId).stream().filter(
                 node -> node.getId().equals(nodeId)
         ).collect(Collectors.toList());
     }

+ 6 - 5
liteflow-testcase-el/liteflow-testcase-el-script-graaljs-springboot/src/test/java/com/yomahub/liteflow/test/script/graaljs/getnodes/LiteFlowScriptGetNodesGraaljsELTest.java

@@ -4,6 +4,7 @@ import com.yomahub.liteflow.core.FlowExecutor;
 import com.yomahub.liteflow.flow.FlowBus;
 import com.yomahub.liteflow.flow.LiteflowResponse;
 import com.yomahub.liteflow.flow.element.Node;
+import com.yomahub.liteflow.meta.LiteflowMetaOperator;
 import com.yomahub.liteflow.test.BaseTest;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -38,7 +39,7 @@ public class LiteFlowScriptGetNodesGraaljsELTest extends BaseTest {
     public void getNodesTest1() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain1");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain1");
         // 判断数量
         Assertions.assertEquals(5, nodes.size());
         // 判断 id
@@ -54,7 +55,7 @@ public class LiteFlowScriptGetNodesGraaljsELTest extends BaseTest {
     public void getNodesTest2() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain2");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain2");
         // 判断总数量
         Assertions.assertEquals(6, nodes.size());
         // 判断 id 与数量
@@ -79,7 +80,7 @@ public class LiteFlowScriptGetNodesGraaljsELTest extends BaseTest {
     public void getNodesTest3() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain3");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain3");
         // 判断总数量
         Assertions.assertEquals(8, nodes.size());
         // 判断 id 与数量
@@ -100,7 +101,7 @@ public class LiteFlowScriptGetNodesGraaljsELTest extends BaseTest {
     public void getNodesTest4() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain4");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain4");
         // 判断数量
         Assertions.assertEquals(5, nodes.size());
         // 判断 id
@@ -116,7 +117,7 @@ public class LiteFlowScriptGetNodesGraaljsELTest extends BaseTest {
     public void getNodesTest5() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain5");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain5");
         // 判断数量
         Assertions.assertEquals(3, nodes.size());
         // 判断 id

+ 6 - 5
liteflow-testcase-el/liteflow-testcase-el-script-javascript-springboot/src/test/java/com/yomahub/liteflow/test/script/javascript/getnodes/LiteFlowScriptGetNodesJsELTest.java

@@ -4,6 +4,7 @@ import com.yomahub.liteflow.core.FlowExecutor;
 import com.yomahub.liteflow.flow.FlowBus;
 import com.yomahub.liteflow.flow.LiteflowResponse;
 import com.yomahub.liteflow.flow.element.Node;
+import com.yomahub.liteflow.meta.LiteflowMetaOperator;
 import com.yomahub.liteflow.test.BaseTest;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -38,7 +39,7 @@ public class LiteFlowScriptGetNodesJsELTest extends BaseTest {
     public void getNodesTest1() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain1");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain1");
         // 判断数量
         Assertions.assertEquals(5, nodes.size());
         // 判断 id
@@ -54,7 +55,7 @@ public class LiteFlowScriptGetNodesJsELTest extends BaseTest {
     public void getNodesTest2() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain2");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain2");
         // 判断总数量
         Assertions.assertEquals(6, nodes.size());
         // 判断 id 与数量
@@ -79,7 +80,7 @@ public class LiteFlowScriptGetNodesJsELTest extends BaseTest {
     public void getNodesTest3() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain3");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain3");
         // 判断总数量
         Assertions.assertEquals(8, nodes.size());
         // 判断 id 与数量
@@ -100,7 +101,7 @@ public class LiteFlowScriptGetNodesJsELTest extends BaseTest {
     public void getNodesTest4() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain4");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain4");
         // 判断数量
         Assertions.assertEquals(5, nodes.size());
         // 判断 id
@@ -116,7 +117,7 @@ public class LiteFlowScriptGetNodesJsELTest extends BaseTest {
     public void getNodesTest5() {
         LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
         Assertions.assertTrue(response.isSuccess());
-        List<Node> nodes = FlowBus.getNodesByChainId("chain5");
+        List<Node> nodes = LiteflowMetaOperator.getNodes("chain5");
         // 判断数量
         Assertions.assertEquals(3, nodes.size());
         // 判断 id

+ 8 - 7
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/getnodes/GetNodesELSpringbootTest.java

@@ -4,6 +4,7 @@ import com.yomahub.liteflow.core.FlowExecutor;
 import com.yomahub.liteflow.flow.FlowBus;
 import com.yomahub.liteflow.flow.LiteflowResponse;
 import com.yomahub.liteflow.flow.element.Node;
+import com.yomahub.liteflow.meta.LiteflowMetaOperator;
 import com.yomahub.liteflow.test.BaseTest;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -16,7 +17,7 @@ import javax.annotation.Resource;
 import java.util.List;
 
 /**
- * springboot环境FlowBus.getNodesByChainId的例子测试
+ * springboot环境LiteflowMetaOperator.getNodes的例子测试
  *
  * @author Bryan.Zhang
  */
@@ -29,27 +30,27 @@ public class GetNodesELSpringbootTest extends BaseTest {
 	// 从简单到复杂
 	@Test
 	public void testGetNodes1() throws Exception {
-		List<Node> nodeList1 = FlowBus.getNodesByChainId("chain1");
+		List<Node> nodeList1 = LiteflowMetaOperator.getNodes("chain1");
 		Assertions.assertEquals(4, nodeList1.size());
-		List<Node> nodeList2 = FlowBus.getNodesByChainId("chain2");
+		List<Node> nodeList2 = LiteflowMetaOperator.getNodes("chain2");
 		Assertions.assertEquals(5, nodeList2.size());
-		List<Node> nodeList3 = FlowBus.getNodesByChainId("chain3");
+		List<Node> nodeList3 = LiteflowMetaOperator.getNodes("chain3");
 		Assertions.assertEquals(7, nodeList3.size());
-		List<Node> nodeList4 = FlowBus.getNodesByChainId("chain4");
+		List<Node> nodeList4 = LiteflowMetaOperator.getNodes("chain4");
 		Assertions.assertEquals(15, nodeList4.size());
 	}
 
 	// 测试有子变量的情况
 	@Test
 	public void testGetNodes2() throws Exception {
-		List<Node> nodeList = FlowBus.getNodesByChainId("chain5");
+		List<Node> nodeList = LiteflowMetaOperator.getNodes("chain5");
 		Assertions.assertEquals(15, nodeList.size());
 	}
 
 	// 测试有子chain的情况
 	@Test
 	public void testGetNodes3() throws Exception {
-		List<Node> nodeList = FlowBus.getNodesByChainId("chain6");
+		List<Node> nodeList = LiteflowMetaOperator.getNodes("chain6");
 		Assertions.assertEquals(6, nodeList.size());
 	}
 }