浏览代码

bug #I4FYKA jsonparser好像缺少脚本条件组件的解析

bryan31 3 年之前
父节点
当前提交
7ac63c21fc
共有 19 个文件被更改,包括 323 次插入16 次删除
  1. 23 5
      liteflow-core/src/main/java/com/yomahub/liteflow/parser/JsonFlowParser.java
  2. 70 0
      liteflow-testcase-script-groovy/src/test/java/com/yomahub/liteflow/test/script/groovy/LiteflowJsonScriptGroovyTest.java
  3. 5 5
      liteflow-testcase-script-groovy/src/test/java/com/yomahub/liteflow/test/script/groovy/LiteflowXmlScriptGroovyTest.java
  4. 1 0
      liteflow-testcase-script-groovy/src/test/resources/json-script/application.properties
  5. 34 0
      liteflow-testcase-script-groovy/src/test/resources/json-script/flow.json
  6. 40 0
      liteflow-testcase-script-groovy/src/test/resources/json-script/flow_update.json
  7. 0 1
      liteflow-testcase-script-groovy/src/test/resources/script/application.properties
  8. 1 0
      liteflow-testcase-script-groovy/src/test/resources/xml-script/application.properties
  9. 0 0
      liteflow-testcase-script-groovy/src/test/resources/xml-script/flow.xml
  10. 0 0
      liteflow-testcase-script-groovy/src/test/resources/xml-script/flow_update.xml
  11. 69 0
      liteflow-testcase-script-qlexpress/src/test/java/com/yomahub/liteflow/test/script/qlexpress/LiteflowJsonScriptQLExpressTest.java
  12. 4 4
      liteflow-testcase-script-qlexpress/src/test/java/com/yomahub/liteflow/test/script/qlexpress/LiteflowXmlScriptQLExpressTest.java
  13. 1 0
      liteflow-testcase-script-qlexpress/src/test/resources/json-script/application.properties
  14. 34 0
      liteflow-testcase-script-qlexpress/src/test/resources/json-script/flow.json
  15. 40 0
      liteflow-testcase-script-qlexpress/src/test/resources/json-script/flow_update.json
  16. 0 1
      liteflow-testcase-script-qlexpress/src/test/resources/script/application.properties
  17. 1 0
      liteflow-testcase-script-qlexpress/src/test/resources/xml-script/application.properties
  18. 0 0
      liteflow-testcase-script-qlexpress/src/test/resources/xml-script/flow.xml
  19. 0 0
      liteflow-testcase-script-qlexpress/src/test/resources/xml-script/flow_update.xml

+ 23 - 5
liteflow-core/src/main/java/com/yomahub/liteflow/parser/JsonFlowParser.java

@@ -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.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
@@ -9,7 +10,9 @@ import com.alibaba.fastjson.parser.Feature;
 import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
 import com.yomahub.liteflow.core.NodeComponent;
 import com.yomahub.liteflow.entity.flow.*;
+import com.yomahub.liteflow.enums.NodeTypeEnum;
 import com.yomahub.liteflow.exception.ExecutableItemNotFoundException;
+import com.yomahub.liteflow.exception.NodeTypeNotSupportException;
 import com.yomahub.liteflow.flow.FlowBus;
 import com.yomahub.liteflow.spring.ComponentScanner;
 import org.slf4j.Logger;
@@ -60,22 +63,37 @@ 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;
+                    String id, name, clazz, script, type;
                     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");
 
-                        //如果有class的定义,则表明是java组件,无class的定义,则表明是脚本组件
-                        if (StrUtil.isNotBlank(clazz)){
+                        //初始化type
+                        if (StrUtil.isBlank(type)){
+                            type = NodeTypeEnum.COMMON.getCode();
+                        }
+                        NodeTypeEnum nodeTypeEnum = NodeTypeEnum.getEnumByCode(type);
+                        if (ObjectUtil.isNull(nodeTypeEnum)){
+                            throw new NodeTypeNotSupportException(StrUtil.format("type [{}] is not support", type));
+                        }
+
+                        //这里区分是普通java节点还是脚本节点
+                        //如果是脚本节点,又区分是普通脚本节点,还是条件脚本节点
+                        if (nodeTypeEnum.equals(NodeTypeEnum.COMMON) && StrUtil.isNotBlank(clazz)){
                             if (!FlowBus.containNode(id)){
                                 FlowBus.addCommonNode(id, name, clazz);
                             }
                         }else{
                             if (!FlowBus.containNode(id)){
-                                script = nodeObject.getString("script");
-                                FlowBus.addCommonScriptNode(id, name, script);
+                                script = nodeObject.getString("value");
+                                if (nodeTypeEnum.equals(NodeTypeEnum.SCRIPT)){
+                                    FlowBus.addCommonScriptNode(id, name, script);
+                                }else if(nodeTypeEnum.equals(NodeTypeEnum.COND_SCRIPT)){
+                                    FlowBus.addCondScriptNode(id, name, script);
+                                }
                             }
                         }
                     }

+ 70 - 0
liteflow-testcase-script-groovy/src/test/java/com/yomahub/liteflow/test/script/groovy/LiteflowJsonScriptGroovyTest.java

@@ -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下的脚本组件,基于json配置
+ * @author Bryan.Zhang
+ * @since 2.6.4
+ */
+@RunWith(SpringRunner.class)
+@TestPropertySource(value = "classpath:/json-script/application.properties")
+@SpringBootTest(classes = LiteflowJsonScriptGroovyTest.class)
+@EnableAutoConfiguration
+@ComponentScan({"com.yomahub.liteflow.test.script.groovy.cmp"})
+public class LiteflowJsonScriptGroovyTest 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/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());
+    }
+}

+ 5 - 5
liteflow-testcase-script-groovy/src/test/java/com/yomahub/liteflow/test/script/groovy/LiteflowScriptGroovyTest.java → liteflow-testcase-script-groovy/src/test/java/com/yomahub/liteflow/test/script/groovy/LiteflowXmlScriptGroovyTest.java

@@ -20,16 +20,16 @@ import javax.annotation.Resource;
 
 
 /**
- * 测试springboot下的脚本组件
+ * 测试springboot下的脚本组件,基于xml配置
  * @author Bryan.Zhang
  * @since 2.6.0
  */
 @RunWith(SpringRunner.class)
-@TestPropertySource(value = "classpath:/script/application.properties")
-@SpringBootTest(classes = LiteflowScriptGroovyTest.class)
+@TestPropertySource(value = "classpath:/json-script/application.properties")
+@SpringBootTest(classes = LiteflowXmlScriptGroovyTest.class)
 @EnableAutoConfiguration
 @ComponentScan({"com.yomahub.liteflow.test.script.groovy.cmp"})
-public class LiteflowScriptGroovyTest extends BaseTest {
+public class LiteflowXmlScriptGroovyTest extends BaseTest {
 
     @Resource
     private FlowExecutor flowExecutor;
@@ -58,7 +58,7 @@ public class LiteflowScriptGroovyTest extends BaseTest {
         Assert.assertTrue(responseOld.isSuccess());
         Assert.assertEquals("d==>s2[条件脚本]==>a", responseOld.getSlot().printStep());
         //更改规则,重新加载,更改的规则内容从flow_update.xml里读取,这里只是为了模拟下获取新的内容。不一定是从文件中读取
-        String newContent = ResourceUtil.readUtf8Str("classpath: /script/flow_update.xml");
+        String newContent = ResourceUtil.readUtf8Str("classpath: /xml-script/flow_update.xml");
         //进行刷新
         FlowBus.refreshFlowMetaData(FlowParserTypeEnum.TYPE_XML, newContent);
 

+ 1 - 0
liteflow-testcase-script-groovy/src/test/resources/json-script/application.properties

@@ -0,0 +1 @@
+liteflow.rule-source=json-script/flow.json

+ 34 - 0
liteflow-testcase-script-groovy/src/test/resources/json-script/flow.json

@@ -0,0 +1,34 @@
+{
+  "flow": {
+    "nodes": {
+      "node": [
+        {
+          "id": "s1",
+          "name": "普通脚本",
+          "type": "script",
+          "value": "def a=3;def b=2;slot.setData(\"s1\",a*b);"
+        },
+        {
+          "id": "s2",
+          "name": "条件脚本",
+          "type": "cond_script",
+          "value": "count = slot.getData(\"count\");if(count > 100){return \"a\";}else{return \"b\";}"
+        }
+      ]
+    },
+    "chain": [
+      {
+        "name": "chain1",
+        "condition": [
+          {"type": "then", "value": "a,b,c,s1"}
+        ]
+      },
+      {
+        "name": "chain2",
+        "condition": [
+          {"type": "then", "value": "d,s2(a|b)"}
+        ]
+      }
+    ]
+  }
+}

+ 40 - 0
liteflow-testcase-script-groovy/src/test/resources/json-script/flow_update.json

@@ -0,0 +1,40 @@
+{
+  "flow": {
+    "nodes": {
+      "node": [
+        {
+          "id": "s1",
+          "name": "普通脚本",
+          "type": "script",
+          "value": "def a=3;def b=2;slot.setData(\"s1\",a*b);"
+        },
+        {
+          "id": "s2",
+          "name": "条件脚本_改",
+          "type": "cond_script",
+          "value": "count = slot.getData(\"count\");if(count > 150){return \"b\";}else{return \"a\";}"
+        },
+        {
+          "id": "s3",
+          "name": "普通脚本_新增",
+          "type": "script",
+          "value": "def a=3;def b=2;def c=10;slot.setData(\"s1\",a*b+c);"
+        }
+      ]
+    },
+    "chain": [
+      {
+        "name": "chain1",
+        "condition": [
+          {"type": "then", "value": "a,b,c,s1"}
+        ]
+      },
+      {
+        "name": "chain2",
+        "condition": [
+          {"type": "then", "value": "d,s2(a|b),s3"}
+        ]
+      }
+    ]
+  }
+}

+ 0 - 1
liteflow-testcase-script-groovy/src/test/resources/script/application.properties

@@ -1 +0,0 @@
-liteflow.rule-source=script/flow.xml

+ 1 - 0
liteflow-testcase-script-groovy/src/test/resources/xml-script/application.properties

@@ -0,0 +1 @@
+liteflow.rule-source=xml-script/flow.xml

+ 0 - 0
liteflow-testcase-script-groovy/src/test/resources/script/flow.xml → liteflow-testcase-script-groovy/src/test/resources/xml-script/flow.xml


+ 0 - 0
liteflow-testcase-script-groovy/src/test/resources/script/flow_update.xml → liteflow-testcase-script-groovy/src/test/resources/xml-script/flow_update.xml


+ 69 - 0
liteflow-testcase-script-qlexpress/src/test/java/com/yomahub/liteflow/test/script/qlexpress/LiteflowJsonScriptQLExpressTest.java

@@ -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/application.properties")
+@SpringBootTest(classes = LiteflowJsonScriptQLExpressTest.class)
+@EnableAutoConfiguration
+@ComponentScan({"com.yomahub.liteflow.test.script.qlexpress.cmp"})
+public class LiteflowJsonScriptQLExpressTest 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/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());
+    }
+}

+ 4 - 4
liteflow-testcase-script-qlexpress/src/test/java/com/yomahub/liteflow/test/script/qlexpress/LiteflowScriptQLExpressTest.java → liteflow-testcase-script-qlexpress/src/test/java/com/yomahub/liteflow/test/script/qlexpress/LiteflowXmlScriptQLExpressTest.java

@@ -25,11 +25,11 @@ import javax.annotation.Resource;
  * @since 2.6.0
  */
 @RunWith(SpringRunner.class)
-@TestPropertySource(value = "classpath:/script/application.properties")
-@SpringBootTest(classes = LiteflowScriptQLExpressTest.class)
+@TestPropertySource(value = "classpath:/xml-script/application.properties")
+@SpringBootTest(classes = LiteflowXmlScriptQLExpressTest.class)
 @EnableAutoConfiguration
 @ComponentScan({"com.yomahub.liteflow.test.script.qlexpress.cmp"})
-public class LiteflowScriptQLExpressTest extends BaseTest {
+public class LiteflowXmlScriptQLExpressTest extends BaseTest {
 
     @Resource
     private FlowExecutor flowExecutor;
@@ -57,7 +57,7 @@ public class LiteflowScriptQLExpressTest extends BaseTest {
         Assert.assertTrue(responseOld.isSuccess());
         Assert.assertEquals("d==>s2[条件脚本]==>b", responseOld.getSlot().printStep());
         //更改规则,重新加载,更改的规则内容从flow_update.xml里读取,这里只是为了模拟下获取新的内容。不一定是从文件中读取
-        String newContent = ResourceUtil.readUtf8Str("classpath: /script/flow_update.xml");
+        String newContent = ResourceUtil.readUtf8Str("classpath: /xml-script/flow_update.xml");
         //进行刷新
         FlowBus.refreshFlowMetaData(FlowParserTypeEnum.TYPE_XML, newContent);
 

+ 1 - 0
liteflow-testcase-script-qlexpress/src/test/resources/json-script/application.properties

@@ -0,0 +1 @@
+liteflow.rule-source=json-script/flow.json

+ 34 - 0
liteflow-testcase-script-qlexpress/src/test/resources/json-script/flow.json

@@ -0,0 +1,34 @@
+{
+  "flow": {
+    "nodes": {
+      "node": [
+        {
+          "id": "s1",
+          "name": "普通脚本",
+          "type": "script",
+          "value": "a=3;b=2;slot.setData(\"s1\",a*b);"
+        },
+        {
+          "id": "s2",
+          "name": "条件脚本",
+          "type": "cond_script",
+          "value": "count = slot.getData(\"count\");if(count > 100){return \"a\";}else{return \"b\";}"
+        }
+      ]
+    },
+    "chain": [
+      {
+        "name": "chain1",
+        "condition": [
+          {"type": "then", "value": "a,b,c,s1"}
+        ]
+      },
+      {
+        "name": "chain2",
+        "condition": [
+          {"type": "then", "value": "d,s2(a|b)"}
+        ]
+      }
+    ]
+  }
+}

+ 40 - 0
liteflow-testcase-script-qlexpress/src/test/resources/json-script/flow_update.json

@@ -0,0 +1,40 @@
+{
+  "flow": {
+    "nodes": {
+      "node": [
+        {
+          "id": "s1",
+          "name": "普通脚本",
+          "type": "script",
+          "value": "a=3;b=2;slot.setData(\"s1\",a*b);"
+        },
+        {
+          "id": "s2",
+          "name": "条件脚本_改",
+          "type": "cond_script",
+          "value": "count = slot.getData(\"count\");if(count > 150){return \"b\";}else{return \"a\";}"
+        },
+        {
+          "id": "s3",
+          "name": "普通脚本_新增",
+          "type": "script",
+          "value": "a=3;b=2;c=10;slot.setData(\"s1\",a*b+c);"
+        }
+      ]
+    },
+    "chain": [
+      {
+        "name": "chain1",
+        "condition": [
+          {"type": "then", "value": "a,b,c,s1"}
+        ]
+      },
+      {
+        "name": "chain2",
+        "condition": [
+          {"type": "then", "value": "d,s2(a|b),s3"}
+        ]
+      }
+    ]
+  }
+}

+ 0 - 1
liteflow-testcase-script-qlexpress/src/test/resources/script/application.properties

@@ -1 +0,0 @@
-liteflow.rule-source=script/flow.xml

+ 1 - 0
liteflow-testcase-script-qlexpress/src/test/resources/xml-script/application.properties

@@ -0,0 +1 @@
+liteflow.rule-source=xml-script/flow.xml

+ 0 - 0
liteflow-testcase-script-qlexpress/src/test/resources/script/flow.xml → liteflow-testcase-script-qlexpress/src/test/resources/xml-script/flow.xml


+ 0 - 0
liteflow-testcase-script-qlexpress/src/test/resources/script/flow_update.xml → liteflow-testcase-script-qlexpress/src/test/resources/xml-script/flow_update.xml