feature #I64PDW 增加Python脚本支持

This commit is contained in:
everywhere.z 2022-12-05 17:45:19 +08:00
parent 52eda8fe47
commit cd3118cf19
18 changed files with 413 additions and 179 deletions

View File

@ -0,0 +1,111 @@
package com.yomahub.liteflow.script.jsr223;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.exception.LiteFlowException;
import com.yomahub.liteflow.script.ScriptBeanManager;
import com.yomahub.liteflow.script.ScriptExecuteWrap;
import com.yomahub.liteflow.script.ScriptExecutor;
import com.yomahub.liteflow.script.exception.ScriptLoadException;
import com.yomahub.liteflow.slot.DataBus;
import com.yomahub.liteflow.slot.Slot;
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.*;
import java.util.Map;
/**
* JSR223 script engine的统一实现抽象类
* @author Bryan.Zhang
* @since 2.9.5
*/
public abstract class JSR223ScriptExecutor implements ScriptExecutor {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
private ScriptEngine scriptEngine;
private final Map<String, CompiledScript> compiledScriptMap = new CopyOnWriteHashMap<>();
@Override
public ScriptExecutor init() {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
scriptEngine = scriptEngineManager.getEngineByName(scriptEngineName());
return this;
}
protected abstract String scriptEngineName();
protected String convertScript(String script){
return script;
}
@Override
public void load(String nodeId, String script) {
try{
CompiledScript compiledScript = ((Compilable) scriptEngine).compile(convertScript(script));
compiledScriptMap.put(nodeId, compiledScript);
}catch (Exception e){
String errorMsg = StrUtil.format("script loading error for node[{}], error msg:{}", nodeId, e.getMessage());
throw new ScriptLoadException(errorMsg);
}
}
@Override
public Object execute(ScriptExecuteWrap wrap) throws Exception{
try{
if (!compiledScriptMap.containsKey(wrap.getNodeId())){
String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId());
throw new ScriptLoadException(errorMsg);
}
CompiledScript compiledScript = compiledScriptMap.get(wrap.getNodeId());
Bindings bindings = new SimpleBindings();
//往脚本语言绑定表里循环增加绑定上下文的key
//key的规则为自定义上下文的simpleName
//比如你的自定义上下文为AbcContext那么key就为:abcContext
//这里不统一放一个map的原因是考虑到有些用户会调用上下文里的方法而不是参数所以脚本语言的绑定表里也是放多个上下文
DataBus.getContextBeanList(wrap.getSlotIndex()).forEach(o -> {
String key = StrUtil.lowerFirst(o.getClass().getSimpleName());
bindings.put(key, o);
});
//把wrap对象转换成元数据map
Map<String, Object> metaMap = BeanUtil.beanToMap(wrap);
//在元数据里放入主Chain的流程参数
Slot slot = DataBus.getSlot(wrap.getSlotIndex());
metaMap.put("requestData", slot.getRequestData());
//如果有隐式流程则放入隐式流程的流程参数
Object subRequestData = slot.getChainReqData(wrap.getCurrChainId());
if (ObjectUtil.isNotNull(subRequestData)){
metaMap.put("subRequestData", subRequestData);
}
//往脚本上下文里放入元数据
bindings.put("_meta", metaMap);
//放入用户自己定义的bean
ScriptBeanManager.getScriptBeanMap().forEach(bindings::putIfAbsent);
return compiledScript.eval(bindings);
}catch (Exception e){
if (ObjectUtil.isNotNull(e.getCause()) && e.getCause() instanceof LiteFlowException){
throw (LiteFlowException)e.getCause();
}else{
throw e;
}
}
}
@Override
public void cleanCache() {
compiledScriptMap.clear();
}
}

View File

@ -1,104 +1,16 @@
package com.yomahub.liteflow.script.groovy;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.exception.LiteFlowException;
import com.yomahub.liteflow.script.ScriptBeanManager;
import com.yomahub.liteflow.script.ScriptExecuteWrap;
import com.yomahub.liteflow.slot.DataBus;
import com.yomahub.liteflow.slot.Slot;
import com.yomahub.liteflow.script.ScriptExecutor;
import com.yomahub.liteflow.script.exception.ScriptLoadException;
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.*;
import java.util.Map;
import com.yomahub.liteflow.script.jsr223.JSR223ScriptExecutor;
/**
* Groovy脚本语言的执行器实现
* @author Bryan.Zhang
* @since 2.6.0
*/
public class GroovyScriptExecutor implements ScriptExecutor {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private ScriptEngine scriptEngine;
private final Map<String, CompiledScript> compiledScriptMap = new CopyOnWriteHashMap<>();
public class GroovyScriptExecutor extends JSR223ScriptExecutor {
@Override
public ScriptExecutor init() {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
scriptEngine = scriptEngineManager.getEngineByName("groovy");
return this;
}
@Override
public void load(String nodeId, String script) {
try{
CompiledScript compiledScript = ((Compilable) scriptEngine).compile(script);
compiledScriptMap.put(nodeId, compiledScript);
}catch (Exception e){
String errorMsg = StrUtil.format("script loading error for node[{}], error msg:{}", nodeId, e.getMessage());
throw new ScriptLoadException(errorMsg);
}
}
@Override
public Object execute(ScriptExecuteWrap wrap) throws Exception{
try{
if (!compiledScriptMap.containsKey(wrap.getNodeId())){
String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId());
throw new ScriptLoadException(errorMsg);
}
CompiledScript compiledScript = compiledScriptMap.get(wrap.getNodeId());
Bindings bindings = new SimpleBindings();
//往脚本语言绑定表里循环增加绑定上下文的key
//key的规则为自定义上下文的simpleName
//比如你的自定义上下文为AbcContext那么key就为:abcContext
//这里不统一放一个map的原因是考虑到有些用户会调用上下文里的方法而不是参数所以脚本语言的绑定表里也是放多个上下文
DataBus.getContextBeanList(wrap.getSlotIndex()).forEach(o -> {
String key = StrUtil.lowerFirst(o.getClass().getSimpleName());
bindings.put(key, o);
});
//把wrap对象转换成元数据map
Map<String, Object> metaMap = BeanUtil.beanToMap(wrap);
//在元数据里放入主Chain的流程参数
Slot slot = DataBus.getSlot(wrap.getSlotIndex());
metaMap.put("requestData", slot.getRequestData());
//如果有隐式流程则放入隐式流程的流程参数
Object subRequestData = slot.getChainReqData(wrap.getCurrChainId());
if (ObjectUtil.isNotNull(subRequestData)){
metaMap.put("subRequestData", subRequestData);
}
//往脚本上下文里放入元数据
bindings.put("_meta", metaMap);
//放入用户自己定义的bean
ScriptBeanManager.getScriptBeanMap().forEach(bindings::putIfAbsent);
return compiledScript.eval(bindings);
}catch (Exception e){
if (ObjectUtil.isNotNull(e.getCause()) && e.getCause() instanceof LiteFlowException){
throw (LiteFlowException)e.getCause();
}else{
throw e;
}
}
}
@Override
public void cleanCache() {
compiledScriptMap.clear();
protected String scriptEngineName() {
return "groovy";
}
}

View File

@ -1,101 +1,21 @@
package com.yomahub.liteflow.script.javascript;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.script.ScriptBeanManager;
import com.yomahub.liteflow.script.ScriptExecuteWrap;
import com.yomahub.liteflow.slot.DataBus;
import com.yomahub.liteflow.slot.Slot;
import com.yomahub.liteflow.script.ScriptExecutor;
import com.yomahub.liteflow.script.exception.ScriptLoadException;
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.*;
import java.util.Map;
import com.yomahub.liteflow.script.jsr223.JSR223ScriptExecutor;
/**
* JavaScript脚本语言的执行器实现
* @author Bryan.Zhang
* @since 2.9.1
*/
public class JavaScriptExecutor implements ScriptExecutor {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private ScriptEngine scriptEngine;
private final Map<String, CompiledScript> compiledScriptMap = new CopyOnWriteHashMap<>();
public class JavaScriptExecutor extends JSR223ScriptExecutor {
@Override
public ScriptExecutor init() {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
scriptEngine = scriptEngineManager.getEngineByName("javascript");
return this;
protected String scriptEngineName() {
return "javascript";
}
@Override
public void load(String nodeId, String script) {
try{
String wrapScript = StrUtil.format("function process(){{}} process();",script);
CompiledScript compiledScript = ((Compilable) scriptEngine).compile(wrapScript);
compiledScriptMap.put(nodeId, compiledScript);
}catch (Exception e){
String errorMsg = StrUtil.format("script loading error for node[{}], error msg:{}", nodeId, e.getMessage());
throw new ScriptLoadException(errorMsg);
}
}
@Override
public Object execute(ScriptExecuteWrap wrap) throws Exception{
try{
if (!compiledScriptMap.containsKey(wrap.getNodeId())){
String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId());
throw new ScriptLoadException(errorMsg);
}
CompiledScript compiledScript = compiledScriptMap.get(wrap.getNodeId());
Bindings bindings = new SimpleBindings();
//往脚本语言绑定表里循环增加绑定上下文的key
//key的规则为自定义上下文的simpleName
//比如你的自定义上下文为AbcContext那么key就为:abcContext
//这里不统一放一个map的原因是考虑到有些用户会调用上下文里的方法而不是参数所以脚本语言的绑定表里也是放多个上下文
DataBus.getContextBeanList(wrap.getSlotIndex()).forEach(o -> {
String key = StrUtil.lowerFirst(o.getClass().getSimpleName());
bindings.put(key, o);
});
//把wrap对象转换成元数据map
Map<String, Object> metaMap = BeanUtil.beanToMap(wrap);
//在元数据里放入主Chain的流程参数
Slot slot = DataBus.getSlot(wrap.getSlotIndex());
metaMap.put("requestData", slot.getRequestData());
//如果有隐式流程则放入隐式流程的流程参数
Object subRequestData = slot.getChainReqData(wrap.getCurrChainName());
if (ObjectUtil.isNotNull(subRequestData)){
metaMap.put("subRequestData", subRequestData);
}
//往脚本上下文里放入元数据
bindings.put("_meta", metaMap);
//放入用户自己定义的bean
ScriptBeanManager.getScriptBeanMap().forEach(bindings::putIfAbsent);
return compiledScript.eval(bindings);
}catch (Exception e){
throw e;
}
}
@Override
public void cleanCache() {
compiledScriptMap.clear();
protected String convertScript(String script) {
return StrUtil.format("function process(){{}} process();",script);
}
}

View File

@ -0,0 +1,27 @@
<?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>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-plugin</artifactId>
<version>2.9.5</version>
</parent>
<artifactId>liteflow-script-python</artifactId>
<dependencies>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-core</artifactId>
<version>${revision}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,50 @@
package com.yomahub.liteflow.script.python;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.CreditCodeUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.script.jsr223.JSR223ScriptExecutor;
import org.python.antlr.PythonParser;
import org.python.util.CodegenUtils;
import org.python.util.JycompileAntTask;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Python脚本语言的执行器实现
* @author Bryan.Zhang
* @since 2.9.5
*/
public class PythonScriptExecutor extends JSR223ScriptExecutor {
@Override
protected String scriptEngineName() {
return "python";
}
@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));
//重新构建python脚本
StringBuilder scriptSB = new StringBuilder();
noBlankLineList.forEach(s
-> scriptSB.append(StrUtil.format("{}\n", s.replaceFirst(blankStr, StrUtil.EMPTY))));
return scriptSB.toString();
}
}

View File

@ -0,0 +1,2 @@
# Python的实现
com.yomahub.liteflow.script.python.PythonScriptExecutor

View File

@ -19,6 +19,7 @@
<module>liteflow-script-groovy</module>
<module>liteflow-script-javascript</module>
<module>liteflow-script-graaljs</module>
<module>liteflow-script-python</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-python-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-python</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.python.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下的groovy脚本组件基于xml配置
* @author Bryan.Zhang
* @since 2.6.0
*/
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/common/application.properties")
@SpringBootTest(classes = ScriptPythonCommonELTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.script.python.common.cmp"})
public class ScriptPythonCommonELTest 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.python.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.python.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.python.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.python.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,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="s1" name="普通脚本1" type="script">
<![CDATA[
a=6
b=10
if a>5:
b=5
print 'hello'
else:
print 'hi'
defaultContext.setData("s1",a*b)
]]>
</node>
</nodes>
<chain name="chain1">
THEN(a, b, c, s1);
</chain>
</flow>

View File

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

View File

@ -69,6 +69,7 @@
<httpclient.version>4.5.13</httpclient.version>
<commons-beanutils.version>1.9.4</commons-beanutils.version>
<apollo.version>1.7.0</apollo.version>
<jython.version>2.7.3</jython.version>
</properties>
<dependencyManagement>
@ -265,7 +266,11 @@
<artifactId>apollo-client</artifactId>
<version>${apollo.version}</version>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>${jython.version}</version>
</dependency>
</dependencies>
</dependencyManagement>