From 940fbff68a9507fa89d0ce255762557c51e0a02e Mon Sep 17 00:00:00 2001 From: "everywhere.z" Date: Tue, 6 Dec 2022 11:14:42 +0800 Subject: [PATCH] =?UTF-8?q?feature=20#I64T29=20=E5=A2=9E=E5=8A=A0=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E8=AF=AD=E8=A8=80Lua=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow-script-lua/pom.xml | 28 +++++++++++++ .../script/lua/LuaScriptExecutor.java | 39 +++++++++++++++++ ...com.yomahub.liteflow.script.ScriptExecutor | 2 + .../liteflow-script-python/pom.xml | 5 ++- .../script/python/PythonScriptExecutor.java | 2 +- liteflow-script-plugin/pom.xml | 1 + .../pom.xml | 33 +++++++++++++++ .../com/yomahub/liteflow/test/BaseTest.java | 22 ++++++++++ .../lua/common/ScriptLuaCommonELTest.java | 42 +++++++++++++++++++ .../test/script/lua/common/cmp/ACmp.java | 20 +++++++++ .../test/script/lua/common/cmp/BCmp.java | 21 ++++++++++ .../test/script/lua/common/cmp/CCmp.java | 21 ++++++++++ .../test/script/lua/common/cmp/DCmp.java | 24 +++++++++++ .../resources/common/application.properties | 1 + .../src/test/resources/common/flow.xml | 22 ++++++++++ .../common/ScriptPythonCommonELTest.java | 4 +- liteflow-testcase-el/pom.xml | 1 + pom.xml | 6 +++ 18 files changed, 289 insertions(+), 5 deletions(-) create mode 100644 liteflow-script-plugin/liteflow-script-lua/pom.xml create mode 100644 liteflow-script-plugin/liteflow-script-lua/src/main/java/com/yomahub/liteflow/script/lua/LuaScriptExecutor.java create mode 100644 liteflow-script-plugin/liteflow-script-lua/src/main/resources/META-INF/services/com.yomahub.liteflow.script.ScriptExecutor create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/pom.xml create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/BaseTest.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/ScriptLuaCommonELTest.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/ACmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/BCmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/CCmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/DCmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/application.properties create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/flow.xml diff --git a/liteflow-script-plugin/liteflow-script-lua/pom.xml b/liteflow-script-plugin/liteflow-script-lua/pom.xml new file mode 100644 index 000000000..e72feb6ef --- /dev/null +++ b/liteflow-script-plugin/liteflow-script-lua/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + liteflow-script-plugin + com.yomahub + ${revision} + ../pom.xml + + + liteflow-script-lua + + + + com.yomahub + liteflow-core + ${revision} + provided + + + org.luaj + luaj-jse + + + + \ No newline at end of file diff --git a/liteflow-script-plugin/liteflow-script-lua/src/main/java/com/yomahub/liteflow/script/lua/LuaScriptExecutor.java b/liteflow-script-plugin/liteflow-script-lua/src/main/java/com/yomahub/liteflow/script/lua/LuaScriptExecutor.java new file mode 100644 index 000000000..0f7a3cad7 --- /dev/null +++ b/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 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()); + } +} diff --git a/liteflow-script-plugin/liteflow-script-lua/src/main/resources/META-INF/services/com.yomahub.liteflow.script.ScriptExecutor b/liteflow-script-plugin/liteflow-script-lua/src/main/resources/META-INF/services/com.yomahub.liteflow.script.ScriptExecutor new file mode 100644 index 000000000..dc0173e11 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/liteflow-script-plugin/liteflow-script-python/pom.xml b/liteflow-script-plugin/liteflow-script-python/pom.xml index 87766dfe5..311ba8d96 100644 --- a/liteflow-script-plugin/liteflow-script-python/pom.xml +++ b/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"> 4.0.0 - com.yomahub liteflow-script-plugin - 2.9.5 + com.yomahub + ${revision} + ../pom.xml liteflow-script-python diff --git a/liteflow-script-plugin/liteflow-script-python/src/main/java/com/yomahub/liteflow/script/python/PythonScriptExecutor.java b/liteflow-script-plugin/liteflow-script-python/src/main/java/com/yomahub/liteflow/script/python/PythonScriptExecutor.java index 6933f25f9..151f54a5d 100644 --- a/liteflow-script-plugin/liteflow-script-python/src/main/java/com/yomahub/liteflow/script/python/PythonScriptExecutor.java +++ b/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)); - //重新构建python脚本 + //重新构建脚本 StringBuilder scriptSB = new StringBuilder(); noBlankLineList.forEach(s -> scriptSB.append(StrUtil.format("{}\n", s.replaceFirst(blankStr, StrUtil.EMPTY)))); diff --git a/liteflow-script-plugin/pom.xml b/liteflow-script-plugin/pom.xml index f7d602f27..356640d4c 100644 --- a/liteflow-script-plugin/pom.xml +++ b/liteflow-script-plugin/pom.xml @@ -20,6 +20,7 @@ liteflow-script-javascript liteflow-script-graaljs liteflow-script-python + liteflow-script-lua \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/pom.xml b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/pom.xml new file mode 100644 index 000000000..ad0729af6 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + liteflow-testcase-el + com.yomahub + ${revision} + ../pom.xml + + + liteflow-testcase-el-script-lua-springboot + + + + com.yomahub + liteflow-spring-boot-starter + ${revision} + + + com.yomahub + liteflow-script-lua + ${revision} + test + + + org.springframework.boot + spring-boot-starter-test + + + + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/BaseTest.java b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/BaseTest.java new file mode 100644 index 000000000..02e76f923 --- /dev/null +++ b/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(); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/ScriptLuaCommonELTest.java b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/ScriptLuaCommonELTest.java new file mode 100644 index 000000000..23e8d540b --- /dev/null +++ b/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")); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/ACmp.java new file mode 100644 index 000000000..87b7c114d --- /dev/null +++ b/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 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/BCmp.java new file mode 100644 index 000000000..bafcbe1cf --- /dev/null +++ b/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 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/CCmp.java new file mode 100644 index 000000000..6e2ae4400 --- /dev/null +++ b/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 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/DCmp.java b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/java/com/yomahub/liteflow/test/script/lua/common/cmp/DCmp.java new file mode 100644 index 000000000..eeae798ef --- /dev/null +++ b/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 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/application.properties b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/application.properties new file mode 100644 index 000000000..4c9c216b6 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=common/flow.xml \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/flow.xml b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/flow.xml new file mode 100644 index 000000000..d85c28353 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-script-lua-springboot/src/test/resources/common/flow.xml @@ -0,0 +1,22 @@ + + + + + 5) then + b=5 + else + b=2 + end + defaultContext:setData("s1",a*b) + defaultContext:setData("s2",_meta:get("nodeId")) + ]]> + + + + + THEN(a, b, c, s1); + + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-python-springboot/src/test/java/com/yomahub/liteflow/test/script/python/common/ScriptPythonCommonELTest.java b/liteflow-testcase-el/liteflow-testcase-el-script-python-springboot/src/test/java/com/yomahub/liteflow/test/script/python/common/ScriptPythonCommonELTest.java index 335f70e4b..deb23704f 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-script-python-springboot/src/test/java/com/yomahub/liteflow/test/script/python/common/ScriptPythonCommonELTest.java +++ b/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 - * @since 2.6.0 + * @since 2.9.5 */ @RunWith(SpringRunner.class) @TestPropertySource(value = "classpath:/common/application.properties") diff --git a/liteflow-testcase-el/pom.xml b/liteflow-testcase-el/pom.xml index dc98cf542..14052d959 100644 --- a/liteflow-testcase-el/pom.xml +++ b/liteflow-testcase-el/pom.xml @@ -30,6 +30,7 @@ liteflow-testcase-el-etcd-springboot liteflow-testcase-el-apollo-springboot liteflow-testcase-el-script-python-springboot + liteflow-testcase-el-script-lua-springboot diff --git a/pom.xml b/pom.xml index f9605de37..8b79e429c 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,7 @@ 1.9.4 1.7.0 2.7.3 + 3.0.1 @@ -271,6 +272,11 @@ jython-standalone ${jython.version} + + org.luaj + luaj-jse + ${luaj.version} +