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

This commit is contained in:
everywhere.z
2022-12-06 11:14:42 +08:00
parent 579d05a7c3
commit 940fbff68a
18 changed files with 289 additions and 5 deletions

View File

@@ -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>

View File

@@ -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());
}
}

View File

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

View File

@@ -4,9 +4,10 @@
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>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-plugin</artifactId>
<version>2.9.5</version>
<groupId>com.yomahub</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>liteflow-script-python</artifactId>

View File

@@ -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))));

View File

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

View File

@@ -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>

View File

@@ -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();
}
}

View File

@@ -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"));
}
}

View File

@@ -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!");
}
}

View File

@@ -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!");
}
}

View File

@@ -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!");
}
}

View File

@@ -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!");
}
}

View File

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

View File

@@ -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>

View File

@@ -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")

View File

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

View File

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