enhancement #I5IJLN 支持脚本里获取requestData

This commit is contained in:
everywhere.z 2022-07-26 21:48:19 +08:00
parent daa65d0d0e
commit f89683bfb9
7 changed files with 49 additions and 5 deletions

View File

@ -15,7 +15,7 @@ public class ScriptComponent extends NodeComponent{
@Override
public void process() throws Exception {
ScriptExecutorFactory.loadInstance().getScriptExecutor().execute(getNodeId(), getSlotIndex());
ScriptExecutorFactory.loadInstance().getScriptExecutor().execute(this.getCurrChainName(), getNodeId(), getSlotIndex());
}
public void loadScript(String script) {

View File

@ -11,7 +11,7 @@ public class ScriptSwitchComponent extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
return (String)ScriptExecutorFactory.loadInstance().getScriptExecutor().execute(getNodeId(), getSlotIndex());
return (String)ScriptExecutorFactory.loadInstance().getScriptExecutor().execute(this.getCurrChainName(), getNodeId(), getSlotIndex());
}
public void loadScript(String script) {

View File

@ -11,7 +11,7 @@ public interface ScriptExecutor {
void load(String nodeId, String script);
Object execute(String nodeId, int slotIndex);
Object execute(String currChainName, String nodeId, int slotIndex);
void cleanCache();
}

View File

@ -1,5 +1,6 @@
package com.yomahub.liteflow.script.groovy;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.slot.DataBus;
import com.yomahub.liteflow.slot.Slot;
@ -47,7 +48,7 @@ public class GroovyScriptExecutor implements ScriptExecutor {
}
@Override
public Object execute(String nodeId, int slotIndex) {
public Object execute(String currChainName, String nodeId, int slotIndex) {
try{
if (!compiledScriptMap.containsKey(nodeId)){
String errorMsg = StrUtil.format("script for node[{}] is not loaded", nodeId);
@ -65,6 +66,17 @@ public class GroovyScriptExecutor implements ScriptExecutor {
String key = StrUtil.lowerFirst(o.getClass().getSimpleName());
bindings.put(key, o);
});
//放入主Chain的流程参数
Slot slot = DataBus.getSlot(slotIndex);
bindings.put("requestData", slot.getRequestData());
//如果有隐试流程则放入隐式流程的流程参数
Object subRequestData = slot.getChainReqData(currChainName);
if (ObjectUtil.isNotNull(subRequestData)){
bindings.put("subRequestData", subRequestData);
}
return compiledScript.eval(bindings);
}catch (Exception e){
log.error(e.getMessage(), e);

View File

@ -1,5 +1,6 @@
package com.yomahub.liteflow.script.qlexpress;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.ql.util.express.DefaultContext;
@ -50,7 +51,7 @@ public class QLExpressScriptExecutor implements ScriptExecutor {
}
@Override
public Object execute(String nodeId, int slotIndex) {
public Object execute(String currChainName, String nodeId, int slotIndex) {
List<String> errorList = new ArrayList<>();
try{
if (!compiledScriptMap.containsKey(nodeId)){
@ -70,6 +71,16 @@ public class QLExpressScriptExecutor implements ScriptExecutor {
context.put(key, o);
});
//放入主Chain的流程参数
Slot slot = DataBus.getSlot(slotIndex);
context.put("requestData", slot.getRequestData());
//如果有隐试流程则放入隐式流程的流程参数
Object subRequestData = slot.getChainReqData(currChainName);
if (ObjectUtil.isNotNull(subRequestData)){
context.put("subRequestData", subRequestData);
}
return expressRunner.execute(instructionSet, context, errorList, true, false, null);
}catch (Exception e){
for (String scriptErrorMsg : errorList){

View File

@ -68,4 +68,13 @@ public class LiteflowXmlScriptGroovyELTest extends BaseTest {
Assert.assertTrue(responseNew.isSuccess());
Assert.assertEquals("d==>s2[条件脚本_改]==>b==>s3[普通脚本_新增]", responseNew.getExecuteStepStr());
}
//测试脚本中的requestData的引用
@Test
public void testScript4() {
LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");
DefaultContext context = response.getFirstContextBean();
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("s4:arg", context.getData("s4"));
}
}

View File

@ -28,6 +28,14 @@
defaultContext.setData("s1",a*b);
]]>
</node>
<node id="s4" name="普通脚本3" type="script">
<![CDATA[
def a=30;
def b=2;
defaultContext.setData("s4","s4:" + requestData);
]]>
</node>
</nodes>
<chain name="chain1">
@ -37,4 +45,8 @@
<chain name="chain2">
THEN(d, SWITCH(s2).to(a,b));
</chain>
<chain name="chain4">
THEN(a, s4);
</chain>
</flow>