enhancement #I5IJLN 支持脚本里获取requestData
This commit is contained in:
parent
daa65d0d0e
commit
f89683bfb9
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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){
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue