mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 12:12:08 +08:00
feature #I4GYV2 script节点支持从文件中获取脚本
This commit is contained in:
@@ -2,6 +2,7 @@ package com.yomahub.liteflow.parser;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
@@ -63,13 +64,14 @@ public abstract class JsonFlowParser extends FlowParser {
|
||||
// 当存在<nodes>节点定义时,解析node节点
|
||||
if (flowJsonObject.getJSONObject("flow").containsKey("nodes")){
|
||||
JSONArray nodeArrayList = flowJsonObject.getJSONObject("flow").getJSONObject("nodes").getJSONArray("node");
|
||||
String id, name, clazz, script, type;
|
||||
String id, name, clazz, script, type, file;
|
||||
for (int i = 0; i < nodeArrayList.size(); i++) {
|
||||
JSONObject nodeObject = nodeArrayList.getJSONObject(i);
|
||||
id = nodeObject.getString("id");
|
||||
name = nodeObject.getString("name");
|
||||
clazz = nodeObject.getString("class");
|
||||
type = nodeObject.getString("type");
|
||||
file = nodeObject.getString("file");
|
||||
|
||||
//初始化type
|
||||
if (StrUtil.isBlank(type)){
|
||||
@@ -86,12 +88,19 @@ public abstract class JsonFlowParser extends FlowParser {
|
||||
if (!FlowBus.containNode(id)){
|
||||
FlowBus.addCommonNode(id, name, clazz);
|
||||
}
|
||||
}else{
|
||||
}else if(nodeTypeEnum.equals(NodeTypeEnum.SCRIPT) || nodeTypeEnum.equals(NodeTypeEnum.COND_SCRIPT)){
|
||||
if (!FlowBus.containNode(id)){
|
||||
script = nodeObject.getString("value");
|
||||
//如果file字段不为空,则优先从file里面读取脚本文本
|
||||
if (StrUtil.isNotBlank(file)){
|
||||
script = ResourceUtil.readUtf8Str(StrUtil.format("classpath: {}", file));
|
||||
}else{
|
||||
script = nodeObject.getString("value");
|
||||
}
|
||||
|
||||
//根据节点类型把脚本添加到元数据里
|
||||
if (nodeTypeEnum.equals(NodeTypeEnum.SCRIPT)){
|
||||
FlowBus.addCommonScriptNode(id, name, script);
|
||||
}else if(nodeTypeEnum.equals(NodeTypeEnum.COND_SCRIPT)){
|
||||
}else {
|
||||
FlowBus.addCondScriptNode(id, name, script);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.yomahub.liteflow.parser;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
|
||||
@@ -68,12 +69,13 @@ public abstract class XmlFlowParser extends FlowParser {
|
||||
// 当存在<nodes>节点定义时,解析node节点
|
||||
if (ObjectUtil.isNotNull(nodesElement)){
|
||||
List<Element> nodeList = nodesElement.elements("node");
|
||||
String id, name, clazz, type, script;
|
||||
String id, name, clazz, type, script, file;
|
||||
for (Element e : nodeList) {
|
||||
id = e.attributeValue("id");
|
||||
name = e.attributeValue("name");
|
||||
clazz = e.attributeValue("class");
|
||||
type = e.attributeValue("type");
|
||||
file = e.attributeValue("file");
|
||||
|
||||
//初始化type
|
||||
if (StrUtil.isBlank(type)){
|
||||
@@ -90,12 +92,19 @@ public abstract class XmlFlowParser extends FlowParser {
|
||||
if (!FlowBus.containNode(id)){
|
||||
FlowBus.addCommonNode(id, name, clazz);
|
||||
}
|
||||
}else{
|
||||
}else if(nodeTypeEnum.equals(NodeTypeEnum.SCRIPT) || nodeTypeEnum.equals(NodeTypeEnum.COND_SCRIPT)){
|
||||
if (!FlowBus.containNode(id)){
|
||||
script = e.getTextTrim();
|
||||
//如果file字段不为空,则优先从file里面读取脚本文本
|
||||
if (StrUtil.isNotBlank(file)){
|
||||
script = ResourceUtil.readUtf8Str(StrUtil.format("classpath: {}", file));
|
||||
}else{
|
||||
script = e.getTextTrim();
|
||||
}
|
||||
|
||||
//根据节点类型把脚本添加到元数据里
|
||||
if (nodeTypeEnum.equals(NodeTypeEnum.SCRIPT)){
|
||||
FlowBus.addCommonScriptNode(id, name, script);
|
||||
}else if(nodeTypeEnum.equals(NodeTypeEnum.COND_SCRIPT)){
|
||||
}else {
|
||||
FlowBus.addCondScriptNode(id, name, script);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.yomahub.liteflow.test.script.groovy;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.entity.data.DefaultSlot;
|
||||
import com.yomahub.liteflow.entity.data.LiteflowResponse;
|
||||
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的groovy脚本组件,基于json配置
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.4
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/json-script-file/application.properties")
|
||||
@SpringBootTest(classes = LiteflowJsonScriptFileGroovyTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.groovy.cmp"})
|
||||
public class LiteflowJsonScriptFileGroovyTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
//测试普通脚本节点
|
||||
@Test
|
||||
public void testScript1() {
|
||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals(Integer.valueOf(6), response.getSlot().getData("s1"));
|
||||
}
|
||||
|
||||
//测试条件脚本节点
|
||||
@Test
|
||||
public void testScript2() {
|
||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本]==>a", response.getSlot().printStep());
|
||||
}
|
||||
|
||||
//测试脚本的热重载
|
||||
@Test
|
||||
public void testScript3() throws Exception{
|
||||
//根据配置,加载的应该是flow.xml,执行原来的规则
|
||||
LiteflowResponse<DefaultSlot> responseOld = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(responseOld.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本]==>a", responseOld.getSlot().printStep());
|
||||
//更改规则,重新加载,更改的规则内容从flow_update.xml里读取,这里只是为了模拟下获取新的内容。不一定是从文件中读取
|
||||
String newContent = ResourceUtil.readUtf8Str("classpath: /json-script-file/flow_update.json");
|
||||
//进行刷新
|
||||
FlowBus.refreshFlowMetaData(FlowParserTypeEnum.TYPE_JSON, newContent);
|
||||
|
||||
//重新执行chain2这个链路,结果会变
|
||||
LiteflowResponse<DefaultSlot> responseNew = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(responseNew.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本_改]==>b==>s3[普通脚本_新增]", responseNew.getSlot().printStep());
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的脚本组件,基于json配置
|
||||
* 测试springboot下的groovy脚本组件,基于json配置
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.4
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.yomahub.liteflow.test.script.groovy;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.entity.data.DefaultSlot;
|
||||
import com.yomahub.liteflow.entity.data.LiteflowResponse;
|
||||
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的groovy脚本组件,基于xml配置,file脚本
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.4
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/xml-script-file/application.properties")
|
||||
@SpringBootTest(classes = LiteflowXmlScriptFileGroovyTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.groovy.cmp"})
|
||||
public class LiteflowXmlScriptFileGroovyTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
//测试普通脚本节点
|
||||
@Test
|
||||
public void testScript1() {
|
||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals(Integer.valueOf(6), response.getSlot().getData("s1"));
|
||||
}
|
||||
|
||||
//测试条件脚本节点
|
||||
@Test
|
||||
public void testScript2() {
|
||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本]==>a", response.getSlot().printStep());
|
||||
}
|
||||
|
||||
//测试脚本的热重载
|
||||
@Test
|
||||
public void testScript3() throws Exception{
|
||||
//根据配置,加载的应该是flow.xml,执行原来的规则
|
||||
LiteflowResponse<DefaultSlot> responseOld = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(responseOld.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本]==>a", responseOld.getSlot().printStep());
|
||||
//更改规则,重新加载,更改的规则内容从flow_update.xml里读取,这里只是为了模拟下获取新的内容。不一定是从文件中读取
|
||||
String newContent = ResourceUtil.readUtf8Str("classpath: /xml-script-file/flow_update.xml");
|
||||
//进行刷新
|
||||
FlowBus.refreshFlowMetaData(FlowParserTypeEnum.TYPE_XML, newContent);
|
||||
|
||||
//重新执行chain2这个链路,结果会变
|
||||
LiteflowResponse<DefaultSlot> responseNew = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(responseNew.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本_改]==>b==>s3[普通脚本_新增]", responseNew.getSlot().printStep());
|
||||
}
|
||||
}
|
||||
@@ -20,12 +20,12 @@ import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的脚本组件,基于xml配置
|
||||
* 测试springboot下的groovy脚本组件,基于xml配置
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/json-script/application.properties")
|
||||
@TestPropertySource(value = "classpath:/xml-script/application.properties")
|
||||
@SpringBootTest(classes = LiteflowXmlScriptGroovyTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.groovy.cmp"})
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
liteflow.rule-source=json-script-file/flow.json
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"flow": {
|
||||
"nodes": {
|
||||
"node": [
|
||||
{
|
||||
"id": "s1",
|
||||
"name": "普通脚本",
|
||||
"type": "script",
|
||||
"file": "json-script-file/s1.groovy"
|
||||
},
|
||||
{
|
||||
"id": "s2",
|
||||
"name": "条件脚本",
|
||||
"type": "cond_script",
|
||||
"file": "json-script-file/s2.groovy"
|
||||
}
|
||||
]
|
||||
},
|
||||
"chain": [
|
||||
{
|
||||
"name": "chain1",
|
||||
"condition": [
|
||||
{"type": "then", "value": "a,b,c,s1"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "chain2",
|
||||
"condition": [
|
||||
{"type": "then", "value": "d,s2(a|b)"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"flow": {
|
||||
"nodes": {
|
||||
"node": [
|
||||
{
|
||||
"id": "s1",
|
||||
"name": "普通脚本",
|
||||
"type": "script",
|
||||
"file": "json-script-file/s1.groovy"
|
||||
},
|
||||
{
|
||||
"id": "s2",
|
||||
"name": "条件脚本_改",
|
||||
"type": "cond_script",
|
||||
"file": "json-script-file/s2_update.groovy"
|
||||
},
|
||||
{
|
||||
"id": "s3",
|
||||
"name": "普通脚本_新增",
|
||||
"type": "script",
|
||||
"file": "json-script-file/s2_update.groovy"
|
||||
}
|
||||
]
|
||||
},
|
||||
"chain": [
|
||||
{
|
||||
"name": "chain1",
|
||||
"condition": [
|
||||
{"type": "then", "value": "a,b,c,s1"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "chain2",
|
||||
"condition": [
|
||||
{"type": "then", "value": "d,s2(a|b),s3"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
Integer a=3
|
||||
Integer b=2
|
||||
slot.setData("s1",a*b)
|
||||
@@ -0,0 +1,6 @@
|
||||
Integer count = slot.getData("count")
|
||||
if(count > 100){
|
||||
return "a"
|
||||
}else{
|
||||
return "b"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
Integer count = slot.getData("count")
|
||||
if(count > 150){
|
||||
return "b"
|
||||
}else{
|
||||
return "a"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
Integer a=3
|
||||
Integer b=2
|
||||
Integer c=10
|
||||
slot.setData("s1",a*b+c)
|
||||
@@ -0,0 +1 @@
|
||||
liteflow.rule-source=xml-script-file/flow.xml
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<flow>
|
||||
<nodes>
|
||||
<node id="s1" name="普通脚本" type="script" file="xml-script-file/s1.groovy"/>
|
||||
<node id="s2" name="条件脚本" type="cond_script" file="xml-script-file/s2.groovy"/>
|
||||
</nodes>
|
||||
|
||||
<chain name="chain1">
|
||||
<then value="a,b,c,s1"/>
|
||||
</chain>
|
||||
|
||||
<chain name="chain2">
|
||||
<then value="d,s2(a|b)"/>
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<flow>
|
||||
<nodes>
|
||||
<node id="s1" name="普通脚本" type="script" file="xml-script-file/s1.groovy"/>
|
||||
<node id="s2" name="条件脚本_改" type="cond_script" file="xml-script-file/s2_update.groovy"/>
|
||||
<node id="s3" name="普通脚本_新增" type="script" file="xml-script-file/s3.groovy"/>
|
||||
</nodes>
|
||||
|
||||
<chain name="chain1">
|
||||
<then value="a,b,c,s1"/>
|
||||
</chain>
|
||||
|
||||
<chain name="chain2">
|
||||
<then value="d,s2(a|b),s3"/>
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -0,0 +1,3 @@
|
||||
Integer a=3
|
||||
Integer b=2
|
||||
slot.setData("s1",a*b)
|
||||
@@ -0,0 +1,6 @@
|
||||
Integer count = slot.getData("count")
|
||||
if(count > 100){
|
||||
return "a"
|
||||
}else{
|
||||
return "b"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
Integer count = slot.getData("count")
|
||||
if(count > 150){
|
||||
return "b"
|
||||
}else{
|
||||
return "a"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
Integer a=3
|
||||
Integer b=2
|
||||
Integer c=10
|
||||
slot.setData("s1",a*b+c);
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.yomahub.liteflow.test.script.qlexpress;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.entity.data.DefaultSlot;
|
||||
import com.yomahub.liteflow.entity.data.LiteflowResponse;
|
||||
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的脚本组件
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/json-script-file/application.properties")
|
||||
@SpringBootTest(classes = LiteflowJsonScriptFileQLExpressTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.qlexpress.cmp"})
|
||||
public class LiteflowJsonScriptFileQLExpressTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
//测试普通脚本节点
|
||||
@Test
|
||||
public void testScript1() {
|
||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals(Integer.valueOf(6), response.getSlot().getData("s1"));
|
||||
}
|
||||
|
||||
//测试条件脚本节点
|
||||
@Test
|
||||
public void testScript2() {
|
||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本]==>b", response.getSlot().printStep());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScript3() throws Exception{
|
||||
//根据配置,加载的应该是flow.xml,执行原来的规则
|
||||
LiteflowResponse<DefaultSlot> responseOld = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(responseOld.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本]==>b", responseOld.getSlot().printStep());
|
||||
//更改规则,重新加载,更改的规则内容从flow_update.xml里读取,这里只是为了模拟下获取新的内容。不一定是从文件中读取
|
||||
String newContent = ResourceUtil.readUtf8Str("classpath: /json-script-file/flow_update.json");
|
||||
//进行刷新
|
||||
FlowBus.refreshFlowMetaData(FlowParserTypeEnum.TYPE_JSON, newContent);
|
||||
|
||||
//重新执行chain2这个链路,结果会变
|
||||
LiteflowResponse<DefaultSlot> responseNew = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(responseNew.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本_改]==>a==>s3[普通脚本_新增]", responseNew.getSlot().printStep());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.yomahub.liteflow.test.script.qlexpress;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.entity.data.DefaultSlot;
|
||||
import com.yomahub.liteflow.entity.data.LiteflowResponse;
|
||||
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的脚本组件
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/xml-script-file/application.properties")
|
||||
@SpringBootTest(classes = LiteflowXmlScriptFileQLExpressTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.qlexpress.cmp"})
|
||||
public class LiteflowXmlScriptFileQLExpressTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
//测试普通脚本节点
|
||||
@Test
|
||||
public void testScript1() {
|
||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals(Integer.valueOf(6), response.getSlot().getData("s1"));
|
||||
}
|
||||
|
||||
//测试条件脚本节点
|
||||
@Test
|
||||
public void testScript2() {
|
||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本]==>b", response.getSlot().printStep());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScript3() throws Exception{
|
||||
//根据配置,加载的应该是flow.xml,执行原来的规则
|
||||
LiteflowResponse<DefaultSlot> responseOld = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(responseOld.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本]==>b", responseOld.getSlot().printStep());
|
||||
//更改规则,重新加载,更改的规则内容从flow_update.xml里读取,这里只是为了模拟下获取新的内容。不一定是从文件中读取
|
||||
String newContent = ResourceUtil.readUtf8Str("classpath: /xml-script-file/flow_update.xml");
|
||||
//进行刷新
|
||||
FlowBus.refreshFlowMetaData(FlowParserTypeEnum.TYPE_XML, newContent);
|
||||
|
||||
//重新执行chain2这个链路,结果会变
|
||||
LiteflowResponse<DefaultSlot> responseNew = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assert.assertTrue(responseNew.isSuccess());
|
||||
Assert.assertEquals("d==>s2[条件脚本_改]==>a==>s3[普通脚本_新增]", responseNew.getSlot().printStep());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
liteflow.rule-source=json-script-file/flow.json
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"flow": {
|
||||
"nodes": {
|
||||
"node": [
|
||||
{
|
||||
"id": "s1",
|
||||
"name": "普通脚本",
|
||||
"type": "script",
|
||||
"file": "json-script-file/s1.ql"
|
||||
},
|
||||
{
|
||||
"id": "s2",
|
||||
"name": "条件脚本",
|
||||
"type": "cond_script",
|
||||
"file": "json-script-file/s2.ql"
|
||||
}
|
||||
]
|
||||
},
|
||||
"chain": [
|
||||
{
|
||||
"name": "chain1",
|
||||
"condition": [
|
||||
{"type": "then", "value": "a,b,c,s1"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "chain2",
|
||||
"condition": [
|
||||
{"type": "then", "value": "d,s2(a|b)"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"flow": {
|
||||
"nodes": {
|
||||
"node": [
|
||||
{
|
||||
"id": "s1",
|
||||
"name": "普通脚本",
|
||||
"type": "script",
|
||||
"file": "json-script-file/s1.ql"
|
||||
},
|
||||
{
|
||||
"id": "s2",
|
||||
"name": "条件脚本_改",
|
||||
"type": "cond_script",
|
||||
"file": "json-script-file/s2_update.ql"
|
||||
},
|
||||
{
|
||||
"id": "s3",
|
||||
"name": "普通脚本_新增",
|
||||
"type": "script",
|
||||
"file": "json-script-file/s3.ql"
|
||||
}
|
||||
]
|
||||
},
|
||||
"chain": [
|
||||
{
|
||||
"name": "chain1",
|
||||
"condition": [
|
||||
{"type": "then", "value": "a,b,c,s1"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "chain2",
|
||||
"condition": [
|
||||
{"type": "then", "value": "d,s2(a|b),s3"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
a=3;
|
||||
b=2;
|
||||
slot.setData("s1",a*b);
|
||||
@@ -0,0 +1,6 @@
|
||||
count = slot.getData("count");
|
||||
if(count > 100){
|
||||
return "a";
|
||||
}else{
|
||||
return "b";
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
count = slot.getData("count");
|
||||
if(count > 150){
|
||||
return "b";
|
||||
}else{
|
||||
return "a";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
a=3;
|
||||
b=2;
|
||||
c=10;
|
||||
slot.setData("s1",a*b+c);
|
||||
@@ -0,0 +1 @@
|
||||
liteflow.rule-source=xml-script-file/flow.xml
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<flow>
|
||||
<nodes>
|
||||
<node id="s1" name="普通脚本" type="script" file="xml-script-file/s1.ql"/>
|
||||
<node id="s2" name="条件脚本" type="cond_script" file="xml-script-file/s2.ql"/>
|
||||
</nodes>
|
||||
|
||||
<chain name="chain1">
|
||||
<then value="a,b,c,s1"/>
|
||||
</chain>
|
||||
|
||||
<chain name="chain2">
|
||||
<then value="d,s2(a|b)"/>
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<flow>
|
||||
<nodes>
|
||||
<node id="s1" name="普通脚本" type="script" file="xml-script-file/s1.ql"/>
|
||||
<node id="s2" name="条件脚本_改" type="cond_script" file="xml-script-file/s2_update.ql"/>
|
||||
<node id="s3" name="普通脚本_新增" type="script" file="xml-script-file/s3.ql"/>
|
||||
</nodes>
|
||||
|
||||
<chain name="chain1">
|
||||
<then value="a,b,c,s1"/>
|
||||
</chain>
|
||||
|
||||
<chain name="chain2">
|
||||
<then value="d,s2(a|b),s3"/>
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -0,0 +1,3 @@
|
||||
a=3;
|
||||
b=2;
|
||||
slot.setData("s1",a*b);
|
||||
@@ -0,0 +1,6 @@
|
||||
count = slot.getData("count");
|
||||
if(count > 100){
|
||||
return "a";
|
||||
}else{
|
||||
return "b";
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
count = slot.getData("count");
|
||||
if(count > 150){
|
||||
return "b";
|
||||
}else{
|
||||
return "a";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
a=3;
|
||||
b=2;
|
||||
c=10;
|
||||
slot.setData("s1",a*b+c);
|
||||
Reference in New Issue
Block a user