From 116a8e64fd5bb88c411f1b9e1dc91f8c1d27c500 Mon Sep 17 00:00:00 2001 From: jay li <2215313286@qq.com> Date: Wed, 16 Oct 2024 19:31:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=87=E4=BB=B6=E6=8C=81?= =?UTF-8?q?=E4=B9=85=E5=8C=96=E4=BF=9D=E5=AD=98node=E5=AE=9E=E4=BE=8Bid?= =?UTF-8?q?=E5=92=8Cchain=20el?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builder/el/LiteFlowChainELBuilder.java | 65 ++++++++++++++++--- .../test/base/BaseCommonELSpringTest.java | 23 ++++++- 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java index 81bf66e23..bc3823aa6 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java @@ -1,6 +1,7 @@ package com.yomahub.liteflow.builder.el; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.*; import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.digest.Digester; @@ -28,11 +29,10 @@ import com.yomahub.liteflow.property.LiteflowConfig; import com.yomahub.liteflow.property.LiteflowConfigGetter; import com.yomahub.liteflow.util.ElRegexUtil; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.function.Consumer; +import java.io.File; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; + /** * Chain基于代码形式的组装器 EL表达式规则专属组装器 @@ -217,11 +217,7 @@ public class LiteFlowChainELBuilder { throw new QLException(StrUtil.format("parse el fail,el:[{}]", elStr)); } - condition.getExecutableGroup().forEach((s, executables) -> executables.forEach(executable -> { - if (executable instanceof Node) { - ((Node) executable).setInstanceId(generateInstanceId(executable.getId())); - } - })); + setNodesInstanceId(condition, liteflowConfig); // 把主要的condition加入 this.conditionList.add(condition); @@ -243,6 +239,55 @@ public class LiteFlowChainELBuilder { } } + private void setNodesInstanceId(Condition condition, LiteflowConfig liteflowConfig) { + File nodeDir = new File(System.getProperty("user.dir") + "/." + liteflowConfig.getRuleSource() + "/" + this.chain.getChainId()); + String elTrim = chain.getEl().trim(); + + // 如果文件不存在,或者文件内容不是当前el,则写入 + if (FileUtil.isEmpty(nodeDir) || !FileUtil.readLines(nodeDir.getPath(), CharsetUtil.UTF_8).get(0).equals(elTrim)) { + writeNodeInstanceId(nodeDir, condition); + } else { + // 文件存在,则直接读取 + List nodeList = FileUtil.readLines(nodeDir.getPath(), CharsetUtil.UTF_8); + + Map executableMap = new HashMap<>(); + for (int i = 1; i < nodeList.size(); i++) { + String info = nodeList.get(i); + int index = info.indexOf(","); + executableMap.put(info.substring(0, index), info.substring(index + 1).split(",")); + } + + condition.getExecutableGroup().forEach((key, executables) -> { + AtomicInteger index = new AtomicInteger(0); + executables.forEach(executable -> { + if (executableMap.containsKey(key)) { + if (executable instanceof Node) { + ((Node) executable).setInstanceId((executableMap.get(key)[index.getAndIncrement()])); + } + } + }); + }); + } + } + + private void writeNodeInstanceId(File nodeDir, Condition condition) { + ArrayList writeList = new ArrayList<>(); + writeList.add(chain.getEl().trim()); + + condition.getExecutableGroup().forEach((key, executables) -> { + StringBuilder instanceIds = new StringBuilder(); + executables.forEach(executable -> { + if (executable instanceof Node) { + ((Node) executable).setInstanceId(generateInstanceId(executable.getId())); + instanceIds.append(",").append(((Node) executable).getInstanceId()); + } + }); + writeList.add(key + instanceIds); + }); + + FileUtil.writeLines(writeList, nodeDir.getPath(), CharsetUtil.UTF_8); + } + public LiteFlowChainELBuilder setNamespace(String nameSpace){ if (StrUtil.isBlank(nameSpace)) { nameSpace = ChainConstant.DEFAULT_NAMESPACE; diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/base/BaseCommonELSpringTest.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/base/BaseCommonELSpringTest.java index edd94ccf4..bb4dc6fbb 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/base/BaseCommonELSpringTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/base/BaseCommonELSpringTest.java @@ -37,11 +37,32 @@ public class BaseCommonELSpringTest extends BaseTest { String executeStepStrWithInstanceId = response.getExecuteStepStrWithInstanceId(); Set strings = extractValues(executeStepStrWithInstanceId); - System.out.println(executeStepStrWithInstanceId); Assertions.assertEquals(strings.size(), 4); } + @Test + public void testBaseCommonInstanceId() { + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>a==>a==>a", response.getExecuteStepStr()); + + String executeStepStrWithInstanceId = response.getExecuteStepStrWithInstanceId(); + Set set1 = extractValues(executeStepStrWithInstanceId); + + Assertions.assertEquals(set1.size(), 4); + + response = flowExecutor.execute2Resp("chain2", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>a==>a==>a", response.getExecuteStepStr()); + + executeStepStrWithInstanceId = response.getExecuteStepStrWithInstanceId(); + Set set2 = extractValues(executeStepStrWithInstanceId); + + Assertions.assertEquals(set2.size(), 4); + Assertions.assertEquals(set1, set2); + } + public static Set extractValues(String input) { Set values = new HashSet<>(); Pattern pattern = Pattern.compile("\\[(.*?)]");