From 1bbc09e720fad5df07f38363331883fa38eb7ff5 Mon Sep 17 00:00:00 2001 From: "everywhere.z" Date: Wed, 5 Feb 2025 17:06:07 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#IBJO4X=20=E5=BB=BA=E7=AB=8B?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=85=83=E6=95=B0=E6=8D=AE=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E6=89=80=E6=9C=89=E5=85=83=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9A=84=E6=93=8D=E4=BD=9C=E7=9A=84=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yomahub/liteflow/flow/FlowBus.java | 8 ---- .../liteflow/flow/element/Condition.java | 4 +- .../yomahub/liteflow/flow/element/Node.java | 11 +++++ .../flow/element/condition/LoopCondition.java | 40 ++++--------------- .../liteflow/meta/LiteflowMetaOperator.java | 38 ++++++++++++++---- .../LiteFlowScriptGetNodesGraaljsELTest.java | 11 ++--- .../LiteFlowScriptGetNodesJsELTest.java | 11 ++--- .../getnodes/GetNodesELSpringbootTest.java | 15 +++---- 8 files changed, 72 insertions(+), 66 deletions(-) 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 fd416815b..e8caff312 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 @@ -292,14 +292,6 @@ public class FlowBus { return nodeMap.get(nodeId); } - // 获取某一个 chainId 下的所有 nodeId - public static List getNodesByChainId(String chainId) { - Chain chain = getChain(chainId); - return chain.getConditionList().stream().flatMap( - (Function>) condition -> condition.getAllNodeInCondition().stream() - ).collect(Collectors.toList()); - } - public static Map getNodeMap() { return nodeMap; } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Condition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Condition.java index 5c61738fc..2e126bad0 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Condition.java +++ b/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 -> condition.getAllNodeInCondition().stream() + ).collect(Collectors.toList())); }else if(executable instanceof Node){ resultList.add((Node)executable); } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java index ba6091fd8..a56381e35 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java +++ b/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 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 getBindDataMap() { + return bindDataMap; + } + + public void setBindDataMap(Map bindDataMap) { + this.bindDataMap = bindDataMap; + } + @Override public Node clone() throws CloneNotSupportedException { Node node = (Node)super.clone(); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/LoopCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/LoopCondition.java index 4484b1c51..53de0e0ac 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/LoopCondition.java +++ b/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() { diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/meta/LiteflowMetaOperator.java b/liteflow-core/src/main/java/com/yomahub/liteflow/meta/LiteflowMetaOperator.java index d30d617cc..348c3bba9 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/meta/LiteflowMetaOperator.java +++ b/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 getNodes(Executable executable){ + if (executable instanceof Chain){ + Chain chain = (Chain) executable; + return chain.getConditionList().stream().flatMap( + (Function>) condition -> getNodes(condition).stream() + ).collect(Collectors.toList()); + }else if(executable instanceof Condition){ + Condition condition = (Condition) executable; + return condition.getExecutableGroup().entrySet().stream().flatMap( + (Function>, Stream>) entry -> entry.getValue().stream() + ).flatMap( + (Function>) 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 getNodes(String chainId){ - return FlowBus.getNodesByChainId(chainId); + return getNodes(getChain(chainId)); } /** @@ -108,13 +136,7 @@ public class LiteflowMetaOperator { * @return Node对象列表,一个节点在Chain里有可能出现多次 */ public static List getNodes(String chainId, String nodeId){ - Chain chain = getChain(chainId); - if (chain == null){ - return null; - } - return chain.getConditionList().stream().flatMap( - (Function>) condition -> condition.getAllNodeInCondition().stream() - ).filter( + return getNodes(chainId).stream().filter( node -> node.getId().equals(nodeId) ).collect(Collectors.toList()); } diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-graaljs-springboot/src/test/java/com/yomahub/liteflow/test/script/graaljs/getnodes/LiteFlowScriptGetNodesGraaljsELTest.java b/liteflow-testcase-el/liteflow-testcase-el-script-graaljs-springboot/src/test/java/com/yomahub/liteflow/test/script/graaljs/getnodes/LiteFlowScriptGetNodesGraaljsELTest.java index e834f4f40..8775eced2 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-script-graaljs-springboot/src/test/java/com/yomahub/liteflow/test/script/graaljs/getnodes/LiteFlowScriptGetNodesGraaljsELTest.java +++ b/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 nodes = FlowBus.getNodesByChainId("chain1"); + List 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 nodes = FlowBus.getNodesByChainId("chain2"); + List 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 nodes = FlowBus.getNodesByChainId("chain3"); + List 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 nodes = FlowBus.getNodesByChainId("chain4"); + List 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 nodes = FlowBus.getNodesByChainId("chain5"); + List nodes = LiteflowMetaOperator.getNodes("chain5"); // 判断数量 Assertions.assertEquals(3, nodes.size()); // 判断 id diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-javascript-springboot/src/test/java/com/yomahub/liteflow/test/script/javascript/getnodes/LiteFlowScriptGetNodesJsELTest.java b/liteflow-testcase-el/liteflow-testcase-el-script-javascript-springboot/src/test/java/com/yomahub/liteflow/test/script/javascript/getnodes/LiteFlowScriptGetNodesJsELTest.java index 1bbd41bae..b57f2578d 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-script-javascript-springboot/src/test/java/com/yomahub/liteflow/test/script/javascript/getnodes/LiteFlowScriptGetNodesJsELTest.java +++ b/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 nodes = FlowBus.getNodesByChainId("chain1"); + List 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 nodes = FlowBus.getNodesByChainId("chain2"); + List 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 nodes = FlowBus.getNodesByChainId("chain3"); + List 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 nodes = FlowBus.getNodesByChainId("chain4"); + List 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 nodes = FlowBus.getNodesByChainId("chain5"); + List nodes = LiteflowMetaOperator.getNodes("chain5"); // 判断数量 Assertions.assertEquals(3, nodes.size()); // 判断 id diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/getnodes/GetNodesELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/getnodes/GetNodesELSpringbootTest.java index de2f6586d..32777f4b8 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/getnodes/GetNodesELSpringbootTest.java +++ b/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 nodeList1 = FlowBus.getNodesByChainId("chain1"); + List nodeList1 = LiteflowMetaOperator.getNodes("chain1"); Assertions.assertEquals(4, nodeList1.size()); - List nodeList2 = FlowBus.getNodesByChainId("chain2"); + List nodeList2 = LiteflowMetaOperator.getNodes("chain2"); Assertions.assertEquals(5, nodeList2.size()); - List nodeList3 = FlowBus.getNodesByChainId("chain3"); + List nodeList3 = LiteflowMetaOperator.getNodes("chain3"); Assertions.assertEquals(7, nodeList3.size()); - List nodeList4 = FlowBus.getNodesByChainId("chain4"); + List nodeList4 = LiteflowMetaOperator.getNodes("chain4"); Assertions.assertEquals(15, nodeList4.size()); } // 测试有子变量的情况 @Test public void testGetNodes2() throws Exception { - List nodeList = FlowBus.getNodesByChainId("chain5"); + List nodeList = LiteflowMetaOperator.getNodes("chain5"); Assertions.assertEquals(15, nodeList.size()); } // 测试有子chain的情况 @Test public void testGetNodes3() throws Exception { - List nodeList = FlowBus.getNodesByChainId("chain6"); + List nodeList = LiteflowMetaOperator.getNodes("chain6"); Assertions.assertEquals(6, nodeList.size()); } }