فهرست منبع

feature #I64T29 增加脚本语言Lua的支持

everywhere.z 2 سال پیش
والد
کامیت
940fbff68a
18فایلهای تغییر یافته به همراه289 افزوده شده و 5 حذف شده
  1. 28 0
      liteflow-script-plugin/liteflow-script-lua/pom.xml
  2. 39 0
      liteflow-script-plugin/liteflow-script-lua/src/main/java/com/yomahub/liteflow/script/lua/LuaScriptExecutor.java
  3. 2 0
      liteflow-script-plugin/liteflow-script-lua/src/main/resources/META-INF/services/com.yomahub.liteflow.script.ScriptExecutor
  4. 3 2
      liteflow-script-plugin/liteflow-script-python/pom.xml
  5. 1 1
      liteflow-script-plugin/liteflow-script-python/src/main/java/com/yomahub/liteflow/script/python/PythonScriptExecutor.java
  6. 1 0
      liteflow-script-plugin/pom.xml
  7. 33 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/pom.xml
  8. 22 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/BaseTest.java
  9. 42 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/ScriptLuaCommonELTest.java
  10. 20 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/ACmp.java
  11. 21 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/BCmp.java
  12. 21 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/CCmp.java
  13. 24 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/DCmp.java
  14. 1 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/application.properties
  15. 22 0
      liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/flow.xml
  16. 2 2
      liteflow-testcase-el/liteflow-testcase-el-script-python-springboot/src/test/java/com/yomahub/liteflow/test/script/python/common/ScriptPythonCommonELTest.java
  17. 1 0
      liteflow-testcase-el/pom.xml
  18. 6 0
      pom.xml

+ 28 - 0
liteflow-script-plugin/liteflow-script-lua/pom.xml

@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <artifactId>liteflow-script-plugin</artifactId>
+        <groupId>com.yomahub</groupId>
+        <version>${revision}</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>liteflow-script-lua</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.yomahub</groupId>
+            <artifactId>liteflow-core</artifactId>
+            <version>${revision}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.luaj</groupId>
+            <artifactId>luaj-jse</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>

+ 39 - 0
liteflow-script-plugin/liteflow-script-lua/src/main/java/com/yomahub/liteflow/script/lua/LuaScriptExecutor.java

@@ -0,0 +1,39 @@
+package com.yomahub.liteflow.script.lua;
+
+import cn.hutool.core.util.ReUtil;
+import cn.hutool.core.util.StrUtil;
+import com.yomahub.liteflow.script.jsr223.JSR223ScriptExecutor;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Lua脚本语言的执行器实现
+ * @author Bryan.Zhang
+ * @since 2.9.5
+ */
+public class LuaScriptExecutor extends JSR223ScriptExecutor {
+    @Override
+    protected String scriptEngineName() {
+        return "luaj";
+    }
+
+    @Override
+    protected String convertScript(String script) {
+        String[] lineArray = script.split("\\n");
+        List<String> noBlankLineList = Arrays.stream(lineArray).filter(
+                s -> !StrUtil.isBlank(s)
+        ).collect(Collectors.toList());
+
+        //用第一行的缩进的空格数作为整个代码的缩进量
+        String blankStr = ReUtil.getGroup0("^[ ]*", noBlankLineList.get(0));
+
+        //重新构建脚本
+        StringBuilder scriptSB = new StringBuilder();
+        noBlankLineList.forEach(s
+                -> scriptSB.append(StrUtil.format("{}\n", s.replaceFirst(blankStr, StrUtil.EMPTY))));
+        return scriptSB.toString();
+        //return StrUtil.format("function process()\n{}\nend\nprocess()\n",scriptSB.toString());
+    }
+}

+ 2 - 0
liteflow-script-plugin/liteflow-script-lua/src/main/resources/META-INF/services/com.yomahub.liteflow.script.ScriptExecutor

@@ -0,0 +1,2 @@
+# Lua的实现
+com.yomahub.liteflow.script.lua.LuaScriptExecutor

+ 3 - 2
liteflow-script-plugin/liteflow-script-python/pom.xml

@@ -4,9 +4,10 @@
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <modelVersion>4.0.0</modelVersion>
     <parent>
     <parent>
-        <groupId>com.yomahub</groupId>
         <artifactId>liteflow-script-plugin</artifactId>
         <artifactId>liteflow-script-plugin</artifactId>
-        <version>2.9.5</version>
+        <groupId>com.yomahub</groupId>
+        <version>${revision}</version>
+        <relativePath>../pom.xml</relativePath>
     </parent>
     </parent>
 
 
     <artifactId>liteflow-script-python</artifactId>
     <artifactId>liteflow-script-python</artifactId>

+ 1 - 1
liteflow-script-plugin/liteflow-script-python/src/main/java/com/yomahub/liteflow/script/python/PythonScriptExecutor.java

@@ -29,7 +29,7 @@ public class PythonScriptExecutor extends JSR223ScriptExecutor {
         //用第一行的缩进的空格数作为整个代码的缩进量
         //用第一行的缩进的空格数作为整个代码的缩进量
         String blankStr = ReUtil.getGroup0("^[ ]*", noBlankLineList.get(0));
         String blankStr = ReUtil.getGroup0("^[ ]*", noBlankLineList.get(0));
 
 
-        //重新构建python脚本
+        //重新构建脚本
         StringBuilder scriptSB = new StringBuilder();
         StringBuilder scriptSB = new StringBuilder();
         noBlankLineList.forEach(s
         noBlankLineList.forEach(s
                 -> scriptSB.append(StrUtil.format("{}\n", s.replaceFirst(blankStr, StrUtil.EMPTY))));
                 -> scriptSB.append(StrUtil.format("{}\n", s.replaceFirst(blankStr, StrUtil.EMPTY))));

+ 1 - 0
liteflow-script-plugin/pom.xml

@@ -20,6 +20,7 @@
         <module>liteflow-script-javascript</module>
         <module>liteflow-script-javascript</module>
         <module>liteflow-script-graaljs</module>
         <module>liteflow-script-graaljs</module>
         <module>liteflow-script-python</module>
         <module>liteflow-script-python</module>
+        <module>liteflow-script-lua</module>
     </modules>
     </modules>
 
 
 </project>
 </project>

+ 33 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/pom.xml

@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <artifactId>liteflow-testcase-el</artifactId>
+        <groupId>com.yomahub</groupId>
+        <version>${revision}</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>liteflow-testcase-el-script-lua-springboot</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.yomahub</groupId>
+            <artifactId>liteflow-spring-boot-starter</artifactId>
+            <version>${revision}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.yomahub</groupId>
+            <artifactId>liteflow-script-lua</artifactId>
+            <version>${revision}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>

+ 22 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/BaseTest.java

@@ -0,0 +1,22 @@
+package com.yomahub.liteflow.test;
+
+import com.yomahub.liteflow.core.FlowInitHook;
+import com.yomahub.liteflow.flow.FlowBus;
+import com.yomahub.liteflow.property.LiteflowConfigGetter;
+import com.yomahub.liteflow.spi.holder.SpiFactoryCleaner;
+import com.yomahub.liteflow.spring.ComponentScanner;
+import com.yomahub.liteflow.thread.ExecutorHelper;
+import org.junit.AfterClass;
+
+public class BaseTest {
+
+    @AfterClass
+    public static void cleanScanCache(){
+        ComponentScanner.cleanCache();
+        FlowBus.cleanCache();
+        ExecutorHelper.loadInstance().clearExecutorServiceMap();
+        SpiFactoryCleaner.clean();
+        LiteflowConfigGetter.clean();
+        FlowInitHook.cleanHook();
+    }
+}

+ 42 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/ScriptLuaCommonELTest.java

@@ -0,0 +1,42 @@
+package com.yomahub.liteflow.test.script.lua.common;
+
+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.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下的lua脚本组件,基于xml配置
+ * @author Bryan.Zhang
+ * @since 2.9.5
+ */
+@RunWith(SpringRunner.class)
+@TestPropertySource(value = "classpath:/common/application.properties")
+@SpringBootTest(classes = ScriptLuaCommonELTest.class)
+@EnableAutoConfiguration
+@ComponentScan({"com.yomahub.liteflow.test.script.lua.common.cmp"})
+public class ScriptLuaCommonELTest extends BaseTest {
+
+    @Resource
+    private FlowExecutor flowExecutor;
+
+    //测试普通脚本节点
+    @Test
+    public void testCommon1() {
+        LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
+        DefaultContext context = response.getFirstContextBean();
+        Assert.assertTrue(response.isSuccess());
+        Assert.assertEquals(Integer.valueOf(30), context.getData("s1"));
+    }
+}

+ 20 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/ACmp.java

@@ -0,0 +1,20 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.script.lua.common.cmp;
+
+import com.yomahub.liteflow.annotation.LiteflowComponent;
+import com.yomahub.liteflow.core.NodeComponent;
+
+@LiteflowComponent("a")
+public class ACmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		System.out.println("ACmp executed!");
+	}
+}

+ 21 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/BCmp.java

@@ -0,0 +1,21 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.script.lua.common.cmp;
+
+import com.yomahub.liteflow.annotation.LiteflowComponent;
+import com.yomahub.liteflow.core.NodeComponent;
+
+@LiteflowComponent("b")
+public class BCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		System.out.println("BCmp executed!");
+	}
+
+}

+ 21 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/CCmp.java

@@ -0,0 +1,21 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.script.lua.common.cmp;
+
+import com.yomahub.liteflow.annotation.LiteflowComponent;
+import com.yomahub.liteflow.core.NodeComponent;
+
+@LiteflowComponent("c")
+public class CCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		System.out.println("CCmp executed!");
+	}
+
+}

+ 24 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/DCmp.java

@@ -0,0 +1,24 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.script.lua.common.cmp;
+
+import com.yomahub.liteflow.annotation.LiteflowComponent;
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.slot.DefaultContext;
+
+@LiteflowComponent("d")
+public class DCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		DefaultContext context = this.getFirstContextBean();
+		context.setData("count",198);
+		System.out.println("DCmp executed!");
+	}
+
+}

+ 1 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/application.properties

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

+ 22 - 0
liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/flow.xml

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<flow>
+    <nodes>
+        <node id="s1" name="普通脚本1" type="script">
+            <![CDATA[
+                local a=6
+                local b=10
+                if(a>5) then
+                    b=5
+                else
+                    b=2
+                end
+                defaultContext:setData("s1",a*b)
+                defaultContext:setData("s2",_meta:get("nodeId"))
+            ]]>
+        </node>
+    </nodes>
+
+    <chain name="chain1">
+        THEN(a, b, c, s1);
+    </chain>
+</flow>

+ 2 - 2
liteflow-testcase-el/liteflow-testcase-el-script-python-springboot/src/test/java/com/yomahub/liteflow/test/script/python/common/ScriptPythonCommonELTest.java

@@ -17,9 +17,9 @@ import javax.annotation.Resource;
 
 
 
 
 /**
 /**
- * 测试springboot下的groovy脚本组件,基于xml配置
+ * 测试springboot下的python脚本组件,基于xml配置
  * @author Bryan.Zhang
  * @author Bryan.Zhang
- * @since 2.6.0
+ * @since 2.9.5
  */
  */
 @RunWith(SpringRunner.class)
 @RunWith(SpringRunner.class)
 @TestPropertySource(value = "classpath:/common/application.properties")
 @TestPropertySource(value = "classpath:/common/application.properties")

+ 1 - 0
liteflow-testcase-el/pom.xml

@@ -30,6 +30,7 @@
         <module>liteflow-testcase-el-etcd-springboot</module>
         <module>liteflow-testcase-el-etcd-springboot</module>
         <module>liteflow-testcase-el-apollo-springboot</module>
         <module>liteflow-testcase-el-apollo-springboot</module>
         <module>liteflow-testcase-el-script-python-springboot</module>
         <module>liteflow-testcase-el-script-python-springboot</module>
+        <module>liteflow-testcase-el-script-lua-springboot</module>
     </modules>
     </modules>
 
 
     <build>
     <build>

+ 6 - 0
pom.xml

@@ -70,6 +70,7 @@
 		<commons-beanutils.version>1.9.4</commons-beanutils.version>
 		<commons-beanutils.version>1.9.4</commons-beanutils.version>
 		<apollo.version>1.7.0</apollo.version>
 		<apollo.version>1.7.0</apollo.version>
 		<jython.version>2.7.3</jython.version>
 		<jython.version>2.7.3</jython.version>
+		<luaj.version>3.0.1</luaj.version>
 	</properties>
 	</properties>
 
 
 	<dependencyManagement>
 	<dependencyManagement>
@@ -271,6 +272,11 @@
 				<artifactId>jython-standalone</artifactId>
 				<artifactId>jython-standalone</artifactId>
 				<version>${jython.version}</version>
 				<version>${jython.version}</version>
 			</dependency>
 			</dependency>
+			<dependency>
+				<groupId>org.luaj</groupId>
+				<artifactId>luaj-jse</artifactId>
+				<version>${luaj.version}</version>
+			</dependency>
 		</dependencies>
 		</dependencies>
 	</dependencyManagement>
 	</dependencyManagement>