!133 新增graaljs脚本插件

Merge pull request !133 from zendwang/feature-graaljs-v0
This commit is contained in:
铂赛东
2022-11-27 08:39:45 +00:00
committed by Gitee
60 changed files with 1392 additions and 13 deletions

View File

@@ -57,17 +57,7 @@ public class EtcdXmlELParser extends ClassXmlFlowELParser {
try {
String content = etcdParserHelper.getContent();
Consumer<String> listenerConsumer = t -> {
try {
parse(t);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
etcdParserHelper.listen(listenerConsumer);
etcdParserHelper.listen();
return content;
} catch (Exception e){
throw new EtcdException(e.getMessage());

View File

@@ -145,7 +145,7 @@ public class EtcdParserHelper {
/**
* 监听 etcd 节点
*/
public void listen(Consumer<String> parseConsumer) {
public void listen() {
this.client.watchChildChange(this.etcdParserVO.getChainPath(), (updatePath, updateValue) -> {
LOG.info("starting reload flow config... update path={} value={},", updatePath, updateValue);
String chainName = updatePath.replace(this.etcdParserVO.getChainPath() + SEPARATOR, "");

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">
<parent>
<artifactId>liteflow-script-plugin</artifactId>
<groupId>com.yomahub</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>liteflow-script-graaljs</artifactId>
<dependencies>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-core</artifactId>
<version>${revision}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,105 @@
package com.yomahub.liteflow.script.graaljs;
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.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.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* GraalVM JavaScript脚本语言的执行器实现
* @author zendwang
* @since 2.9.4
*/
public class GraalJavaScriptExecutor implements ScriptExecutor {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private final Map<String, String> scriptMap = new CopyOnWriteHashMap<>();
@Override
public ScriptExecutor init() {
return this;
}
@Override
public void load(String nodeId, String script) {
try{
String wrapScript = StrUtil.format("function process(){{}} process();",script);
scriptMap.put(nodeId, wrapScript);
}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 (!scriptMap.containsKey(wrap.getNodeId())){
String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId());
throw new ScriptLoadException(errorMsg);
}
Context context = Context.newBuilder().allowAllAccess(true).build();
Value bindings = context.getBindings("js");
//往脚本语言绑定表里循环增加绑定上下文的key
//key的规则为自定义上下文的simpleName
//比如你的自定义上下文为AbcContext那么key就为:abcContext
//这里不统一放一个map的原因是考虑到有些用户会调用上下文里的方法而不是参数所以脚本语言的绑定表里也是放多个上下文
DataBus.getContextBeanList(wrap.getSlotIndex()).forEach(o -> {
String key = StrUtil.lowerFirst(o.getClass().getSimpleName());
bindings.putMember(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.putMember("_meta", metaMap);
//放入用户自己定义的bean
ScriptBeanManager.getScriptBeanMap().entrySet().stream().forEach( e ->{
bindings.putMember(e.getKey(), e.getValue());
});
Value value = context.eval("js", scriptMap.get(wrap.getNodeId()));
if (value.isBoolean()) {
return value.asBoolean();
} else if (value.isNumber()) {
return value.asInt();
} else if (value.isString()) {
return value.asString();
}
return value;
}catch (Exception e){
log.error(e.getMessage(), e);
throw e;
}
}
@Override
public void cleanCache() {
scriptMap.clear();
}
}

View File

@@ -0,0 +1,2 @@
# JavaScript的实现
com.yomahub.liteflow.script.graaljs.GraalJavaScriptExecutor

View File

@@ -1,2 +1,2 @@
# JavaScript的实现
com.yomahub.liteflow.script.javascript.JavaScriptExecutor
com.yomahub.liteflow.script.graaljs.GraalJavaScriptExecutor

View File

@@ -18,6 +18,7 @@
<module>liteflow-script-qlexpress</module>
<module>liteflow-script-groovy</module>
<module>liteflow-script-javascript</module>
<module>liteflow-script-graaljs</module>
</modules>
</project>

View File

@@ -0,0 +1,35 @@
<?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">
<parent>
<artifactId>liteflow-testcase-el</artifactId>
<groupId>com.yomahub</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>liteflow-testcase-el-script-graaljs-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-graaljs</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,20 @@
package com.yomahub.liteflow.test;
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();
}
}

View File

@@ -0,0 +1,42 @@
package com.yomahub.liteflow.test.script.graaljs.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下的graaljs脚本组件基于xml配置
* @author zendwang
* @since 2.9.4
*/
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/common/application.properties")
@SpringBootTest(classes = LiteflowXmlScriptCommonELTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.common.cmp"})
public class LiteflowXmlScriptCommonELTest 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(11), 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.graaljs.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.graaljs.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.graaljs.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,82 @@
package com.yomahub.liteflow.test.script.graaljs.ifelse;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
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;
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/ifelse/application.properties")
@SpringBootTest(classes = LiteFlowXmlScriptIfelseJsELTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.ifelse.cmp"})
public class LiteFlowXmlScriptIfelseJsELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
//IF只有2个参数
@Test
public void testIf1() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x0==>a==>b", response.getExecuteStepStrWithoutTime());
}
//IF只有3个参数
@Test
public void testIf2() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x1==>c==>d", response.getExecuteStepStrWithoutTime());
}
//IF有3个参数进行嵌套
@Test
public void testIf3() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x1==>x1==>c==>c==>b", response.getExecuteStepStrWithoutTime());
}
//IF有2个参数加上ELSE
@Test
public void testIf4() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x1==>c==>d", response.getExecuteStepStrWithoutTime());
}
//IF有2个参数ELSE里再嵌套一个IF
@Test
public void testIf5() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x1==>x1==>c==>c==>b", response.getExecuteStepStrWithoutTime());
}
//标准的IF ELIF ELSE
@Test
public void testIf6() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain6", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x1==>x0==>c==>c", response.getExecuteStepStrWithoutTime());
}
//IF ELIF... ELSE 的形式
@Test
public void testIf7() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain7", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x1==>x1==>x1==>x1==>d==>b==>a", response.getExecuteStepStrWithoutTime());
}
}

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.graaljs.ifelse.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.graaljs.ifelse.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.graaljs.ifelse.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.graaljs.ifelse.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,65 @@
package com.yomahub.liteflow.test.script.graaljs.loop;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
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;
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/loop/application.properties")
@SpringBootTest(classes = LiteFlowXmlScriptLoopJsELTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.loop.cmp"})
public class LiteFlowXmlScriptLoopJsELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
//FOR循环数字直接在el中定义
@Test
public void testLoop1() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("LOOP_2==>a==>b==>c==>a==>b==>c", response.getExecuteStepStr());
}
//FPR循环由For组件定义
@Test
public void testLoop2() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("x==>a==>b==>c==>a==>b==>c==>a==>b==>c", response.getExecuteStepStr());
}
//FOR循环中加入BREAK组件
@Test
public void testLoop3() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
Assert.assertTrue(response.isSuccess());
}
//WHILE循环
@Test
public void testLoop4() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("z==>a==>d==>z==>a==>d==>z==>a==>d==>z==>a==>d==>z==>a==>d==>z", response.getExecuteStepStr());
}
//WHILE循环加入BREAK
@Test
public void testLoop5() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("z==>a==>d==>y==>z==>a==>d==>y==>z==>a==>d==>y==>z==>a==>d==>y", response.getExecuteStepStr());
}
}

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.graaljs.loop.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("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.graaljs.loop.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("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.graaljs.loop.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
}
}

View File

@@ -0,0 +1,29 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.script.graaljs.loop.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("d")
public class DCmp extends NodeComponent {
@Override
public void process() {
DefaultContext context = this.getFirstContextBean();
String key = "test";
if (context.hasData(key)){
int count = context.getData(key);
context.setData(key, ++count);
}else{
context.setData(key, 1);
}
}
}

View File

@@ -0,0 +1,43 @@
package com.yomahub.liteflow.test.script.graaljs.meta;
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;
/**
* 测试与Java交互的元数据
* @author zendwang
* @since 2.9.4
*/
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/meta/application.properties")
@SpringBootTest(classes = LiteflowXmlScriptMetaELTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.meta.cmp"})
public class LiteflowXmlScriptMetaELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
@Test
public void testMeta() {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
DefaultContext context = response.getFirstContextBean();
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("chain1", context.getData("currChainId"));
Assert.assertEquals("arg", context.getData("requestData"));
Assert.assertEquals("s1", context.getData("nodeId"));
}
}

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.graaljs.meta.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.graaljs.meta.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.graaljs.meta.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,53 @@
package com.yomahub.liteflow.test.script.graaljs.refresh;
import cn.hutool.core.io.resource.ResourceUtil;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.LiteflowResponse;
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下的graaljs脚本组件基于xml配置
* @author zendwang
* @since 2.9.4
*/
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/refresh/application.properties")
@SpringBootTest(classes = LiteflowXmlScriptJsRefreshELTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.refresh.cmp"})
public class LiteflowXmlScriptJsRefreshELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
//测试脚本的热重载
@Test
public void testRefresh1() throws Exception{
//根据配置加载的应该是flow.xml执行原来的规则
LiteflowResponse responseOld = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(responseOld.isSuccess());
Assert.assertEquals("d==>s1[选择脚本]==>a", responseOld.getExecuteStepStr());
//更改规则重新加载更改的规则内容从flow_update.xml里读取这里只是为了模拟下获取新的内容。不一定是从文件中读取
String newContent = ResourceUtil.readUtf8Str("classpath: /refresh/flow_update.xml");
//进行刷新
FlowBus.refreshFlowMetaData(FlowParserTypeEnum.TYPE_EL_XML, newContent);
//重新执行chain2这个链路结果会变
LiteflowResponse responseNew = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(responseNew.isSuccess());
Assert.assertEquals("d==>s1[选择脚本_改]==>b==>s2[普通脚本_新增]", responseNew.getExecuteStepStr());
}
}

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.graaljs.refresh.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.graaljs.refresh.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.graaljs.refresh.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.graaljs.refresh.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,44 @@
package com.yomahub.liteflow.test.script.graaljs.scriptbean;
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;
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/scriptbean/application.properties")
@SpringBootTest(classes = LiteFlowScriptScriptbeanJsELTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.scriptbean.cmp", "com.yomahub.liteflow.test.script.graaljs.scriptbean.bean"})
public class LiteFlowScriptScriptbeanJsELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
@Test
public void testScriptBean1() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response.isSuccess());
DefaultContext context = response.getFirstContextBean();
Assert.assertEquals("hello", context.getData("demo"));
}
@Test
public void testScriptBean2() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
Assert.assertTrue(response.isSuccess());
DefaultContext context = response.getFirstContextBean();
Assert.assertEquals("hello,kobe", context.getData("demo"));
}
}

View File

@@ -0,0 +1,22 @@
package com.yomahub.liteflow.test.script.graaljs.scriptbean.bean;
import com.yomahub.liteflow.script.ScriptBean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@ScriptBean("demo")
public class DemoBean1 {
@Resource
private DemoBean2 demoBean2;
public String getDemoStr1(){
return "hello";
}
public String getDemoStr2(String name){
return demoBean2.getDemoStr2(name);
}
}

View File

@@ -0,0 +1,11 @@
package com.yomahub.liteflow.test.script.graaljs.scriptbean.bean;
import org.springframework.stereotype.Component;
@Component
public class DemoBean2 {
public String getDemoStr2(String name){
return "hello,"+name;
}
}

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.graaljs.scriptbean.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("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.graaljs.scriptbean.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("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.graaljs.scriptbean.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
}
}

View File

@@ -0,0 +1,40 @@
package com.yomahub.liteflow.test.script.graaljs.sw;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
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下的graaljs脚本组件基于xml配置
* @author zendwang
* @since 2.9.4
*/
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/sw/application.properties")
@SpringBootTest(classes = LiteflowXmlScriptJsSwitchELTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.sw.cmp"})
public class LiteflowXmlScriptJsSwitchELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
//测试选择脚本节点
@Test
public void testSw1() {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("d==>s1[选择脚本]==>a", response.getExecuteStepStr());
}
}

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.graaljs.sw.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.graaljs.sw.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.graaljs.sw.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.graaljs.sw.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,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="s1" name="普通脚本1" type="script" language="js">
<![CDATA[
var a=3;
var b=2;
var c=1;
var d=5;
function addByArray(values) {
var sum = 0;
for (var i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
}
var result = addByArray([a,b,c,d]);
defaultContext.setData("s1",parseInt(result));
]]>
</node>
</nodes>
<chain name="chain1">
THEN(a, b, c, s1);
</chain>
</flow>

View File

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

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="x0" type="if_script" language="js">
<![CDATA[
return true;
]]>
</node>
<node id="x1" type="if_script" language="js">
<![CDATA[
return false;
]]>
</node>
</nodes>
<chain name="chain1">
IF(x0, THEN(a, b));
</chain>
<chain name="chain2">
IF(x1, THEN(a, b), THEN(c, d));
</chain>
<chain name="chain3">
item = IF(x1, a, THEN(c, c, b));
IF(
x1,
a,
item
);
</chain>
<chain name="chain4">
IF(x1, THEN(a, b)).ELSE(THEN(c, d));
</chain>
<chain name="chain5">
item = IF(x1, a, THEN(c, c, b));
IF(x1, THEN(a, b)).ELSE(item);
</chain>
<chain name="chain6">
IF(x1, THEN(a, b)).ELIF(x0, THEN(c, c)).ELSE(d);
</chain>
<chain name="chain7">
IF(x1, a).ELIF(x1, b)
.ELIF(x1, c)
.ELIF(x1, d)
.ELSE(THEN(d, b, a));
</chain>
</flow>

View File

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

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="x" type="for_script" language="js">
<![CDATA[
return 3;
]]>
</node>
<node id="y" type="break_script" language="js">
<![CDATA[
var count = defaultContext.getData("test");
return count > 3;
]]>
</node>
<node id="z" type="while_script" language="js">
<![CDATA[
var key = "test";
if (defaultContext.hasData(key)){
var count = defaultContext.getData("test");
return count < 5;
}else{
return true;
}
]]>
</node>
</nodes>
<chain name="chain1">
FOR(2).DO(THEN(a,b,c));
</chain>
<chain name="chain2">
FOR(x).DO(THEN(a,b,c));
</chain>
<chain name="chain3">
FOR(10).DO(THEN(a,b,d)).BREAK(y);
</chain>
<chain name="chain4">
WHILE(z).DO(THEN(a,d));
</chain>
<chain name="chain5">
WHILE(z).DO(THEN(a,d)).BREAK(y);
</chain>
</flow>

View File

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

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="s1" name="普通脚本1" type="script" language="js">
<![CDATA[
var nodeId = _meta.nodeId
var currChainId = _meta.currChainId
var requestData = _meta.requestData
defaultContext.setData("nodeId",nodeId)
defaultContext.setData("currChainId",currChainId)
defaultContext.setData("requestData",requestData)
]]>
</node>
</nodes>
<chain id="chain1">
THEN(a, b, c, s1);
</chain>
</flow>

View File

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

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="s1" name="选择脚本" type="switch_script" language="js">
<![CDATA[
var count = defaultContext.getData("count");
if(count > 100){
return "a";
}else{
return "b";
}
]]>
</node>
</nodes>
<chain name="chain1">
THEN(d, SWITCH(s1).to(a,b));
</chain>
</flow>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="s1" name="选择脚本_改" type="switch_script" language="js">
<![CDATA[
var count = defaultContext.getData("count");
if(count > 150){
return "b";
}else{
return "a";
}
]]>
</node>
<node id="s2" name="普通脚本_新增" type="script" language="js">
<![CDATA[
var a=3;
var b=2;
var c=10;
defaultContext.setData("s1",a*b+c);
]]>
</node>
</nodes>
<chain name="chain1">
THEN(d, SWITCH(s1).to(a,b), s2);
</chain>
</flow>

View File

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

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="d" type="script" language="js">
<![CDATA[
var str = demo.getDemoStr1();
defaultContext.setData("demo", str);
]]>
</node>
<node id="e" type="script" language="js">
<![CDATA[
var str = demo.getDemoStr2("kobe");
defaultContext.setData("demo", str);
]]>
</node>
</nodes>
<chain name="chain1">
THEN(a,b,c,d);
</chain>
<chain name="chain2">
THEN(a,b,c,e);
</chain>
</flow>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="s1" name="选择脚本" type="switch_script" language="js">
<![CDATA[
var count = defaultContext.getData('count');
if(count > 100){
return 'a';
}else{
return 'b';
}
]]>
</node>
</nodes>
<chain name="chain1">
THEN(d, SWITCH(s1).to(a,b));
</chain>
</flow>

View File

@@ -23,6 +23,7 @@
<module>liteflow-testcase-el-script-groovy-springboot</module>
<module>liteflow-testcase-el-script-qlexpress-springboot</module>
<module>liteflow-testcase-el-script-javascript-springboot</module>
<module>liteflow-testcase-el-script-graaljs-springboot</module>
<module>liteflow-testcase-el-zk-springboot</module>
<module>liteflow-testcase-el-sql-springboot</module>
<module>liteflow-testcase-el-nacos-springboot</module>

View File

@@ -60,6 +60,7 @@
<nacos.version>1.4.4</nacos.version>
<qlexpress.version>3.3.0</qlexpress.version>
<groovy.version>3.0.8</groovy.version>
<graalvm.version>21.3.3.1</graalvm.version>
<bytebuddy.version>1.11.13</bytebuddy.version>
<aspectjweaver.version>1.8.13</aspectjweaver.version>
<logback-classic.version>1.2.3</logback-classic.version>
@@ -213,6 +214,11 @@
<artifactId>byte-buddy</artifactId>
<version>${bytebuddy.version}</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>${graalvm.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>