mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 20:22:07 +08:00
使用文件持久化保存node实例id和chain el
This commit is contained in:
@@ -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<String> nodeList = FileUtil.readLines(nodeDir.getPath(), CharsetUtil.UTF_8);
|
||||
|
||||
Map<String, String[]> 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<String> 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;
|
||||
|
||||
@@ -37,11 +37,32 @@ public class BaseCommonELSpringTest extends BaseTest {
|
||||
|
||||
String executeStepStrWithInstanceId = response.getExecuteStepStrWithInstanceId();
|
||||
Set<String> 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<String> 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<String> set2 = extractValues(executeStepStrWithInstanceId);
|
||||
|
||||
Assertions.assertEquals(set2.size(), 4);
|
||||
Assertions.assertEquals(set1, set2);
|
||||
}
|
||||
|
||||
public static Set<String> extractValues(String input) {
|
||||
Set<String> values = new HashSet<>();
|
||||
Pattern pattern = Pattern.compile("\\[(.*?)]");
|
||||
|
||||
Reference in New Issue
Block a user