From 8595a20b0891e9e9ca97f68539ccdcabc3b5fa25 Mon Sep 17 00:00:00 2001 From: "everywhere.z" Date: Mon, 18 Aug 2025 12:12:52 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#ICO3IK=20=E5=9C=A8switch?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E4=B8=AD=EF=BC=8C=E5=B8=8C=E6=9C=9B=E8=83=BD?= =?UTF-8?q?=E6=8B=BF=E5=88=B0target=E7=9A=84=E5=88=97=E8=A1=A8=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/core/NodeSwitchComponent.java | 18 ++++++++++++++++++ .../element/condition/SwitchCondition.java | 8 +++----- .../switchcase/SwitchELSpringbootTest.java | 15 +++++++++++++++ .../test/switchcase/cmp/ESwitchCmp.java | 4 ++++ .../src/test/resources/switchcase/flow.el.xml | 4 ++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeSwitchComponent.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeSwitchComponent.java index c4320a4e2..b4bc05f8a 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeSwitchComponent.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeSwitchComponent.java @@ -7,9 +7,18 @@ */ package com.yomahub.liteflow.core; +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.flow.element.Condition; +import com.yomahub.liteflow.flow.element.Executable; +import com.yomahub.liteflow.flow.element.condition.SwitchCondition; import com.yomahub.liteflow.slot.DataBus; import com.yomahub.liteflow.core.proxy.LiteFlowProxyUtil; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; + /** * 条件路由节点抽象类 * @@ -32,4 +41,13 @@ public abstract class NodeSwitchComponent extends NodeComponent { return DataBus.getSlot(slotIndex).getSwitchResult(this.getMetaValueKey()); } + public List getTargetList(){ + Condition condition = this.getSlot().getCurrentCondition(); + if (condition instanceof SwitchCondition){ + return ((SwitchCondition)condition).getTargetList().stream().map(Executable::getId).collect(Collectors.toList()); + }else{ + return ListUtil.empty(); + } + } + } 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 3c575f2f4..a04fe39ad 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 @@ -55,11 +55,9 @@ public class SwitchCondition extends Condition { String[] target = targetId.split(TAG_FLAG, 2); String _targetId = target[0]; String _targetTag = target[1]; - targetExecutor = targetList.stream().filter(executable -> { - return (StrUtil.startWith(_targetId, TAG_PREFIX) && ObjectUtil.equal(_targetTag,executable.getTag())) - || ((StrUtil.isEmpty(_targetId) || _targetId.equals(executable.getId())) - && (StrUtil.isEmpty(_targetTag) || _targetTag.equals(executable.getTag()))); - }).findFirst().orElse(null); + targetExecutor = targetList.stream().filter(executable -> (StrUtil.startWith(_targetId, TAG_PREFIX) && ObjectUtil.equal(_targetTag,executable.getTag())) + || ((StrUtil.isEmpty(_targetId) || _targetId.equals(executable.getId())) + && (StrUtil.isEmpty(_targetTag) || _targetTag.equals(executable.getTag())))).findFirst().orElse(null); } else { targetExecutor = targetList.stream() 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 734cadf91..87ae8624d 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 @@ -1,7 +1,10 @@ package com.yomahub.liteflow.test.switchcase; +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.collection.ListUtil; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.slot.DefaultContext; import com.yomahub.liteflow.test.BaseTest; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -11,6 +14,7 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.test.context.TestPropertySource; import javax.annotation.Resource; +import java.util.List; /** * springboot环境EL常规的例子测试 @@ -119,4 +123,15 @@ public class SwitchELSpringbootTest extends BaseTest { Assertions.assertTrue(response.isSuccess()); } + // 获取targetList + @Test + public void testSwitch14() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain14", "arg"); + DefaultContext context = response.getFirstContextBean(); + List targetList = context.getData("targetList"); + boolean flag = CollectionUtil.isEqualList(targetList, ListUtil.toList("sub1", "d", "a", "chain10")); + Assertions.assertTrue(flag); + Assertions.assertTrue(response.isSuccess()); + } + } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/cmp/ESwitchCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/cmp/ESwitchCmp.java index 275e3c98c..5c9d05bc7 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/cmp/ESwitchCmp.java +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/switchcase/cmp/ESwitchCmp.java @@ -8,6 +8,7 @@ package com.yomahub.liteflow.test.switchcase.cmp; import com.yomahub.liteflow.core.NodeSwitchComponent; +import com.yomahub.liteflow.slot.DefaultContext; import org.springframework.stereotype.Component; @Component("e") @@ -15,6 +16,9 @@ public class ESwitchCmp extends NodeSwitchComponent { @Override public String processSwitch() throws Exception { + DefaultContext context = this.getFirstContextBean(); + context.setData("targetList", this.getTargetList()); + return "d"; } 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 de0fc97d5..62d1d1400 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 @@ -83,4 +83,8 @@ THEN(a); + + + SWITCH(e).TO(sub1, d, a, chain10); + \ No newline at end of file