diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/proxy/DeclComponentProxy.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/proxy/DeclComponentProxy.java index 5345fffa6..95a65a32e 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/proxy/DeclComponentProxy.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/proxy/DeclComponentProxy.java @@ -1,5 +1,6 @@ package com.yomahub.liteflow.core.proxy; +import cn.hutool.core.collection.ListUtil; import cn.hutool.core.exceptions.InvocationTargetRuntimeException; import cn.hutool.core.lang.Tuple; import cn.hutool.core.util.ArrayUtil; @@ -36,6 +37,7 @@ import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * 声明式组件的代理核心生成类 @@ -145,7 +147,7 @@ public class DeclComponentProxy { // 首先需要保证第一个参数是NodeComponent // 其次需要针对于@LiteflowFact做处理 try { - Object[] realArgs = loadMethodParameter(proxy, currentMethodWrapBean); + Object[] realArgs = loadMethodParameter(proxy, currentMethodWrapBean, args); return ReflectUtil.invoke(declWarpBean.getRawBean(), currentMethodWrapBean.getMethod(), realArgs); }catch (InvocationTargetRuntimeException e) { InvocationTargetException targetEx = (InvocationTargetException) e.getCause(); @@ -157,52 +159,63 @@ public class DeclComponentProxy { private final ExpressRunner expressRunner = new ExpressRunner(); - private Object[] loadMethodParameter(Object proxy, MethodWrapBean methodWrapBean){ + private Object[] loadMethodParameter(Object proxy, MethodWrapBean methodWrapBean, Object[] args){ NodeComponent thisNodeComponent = (NodeComponent) proxy; - return methodWrapBean.getParameterWrapBeanList().stream().map(parameterWrapBean -> { - // 如果参数是NodeComponent,那就返回proxy本身 - if (parameterWrapBean.getParameterType().isAssignableFrom(NodeComponent.class)) { - return proxy; - } + return IntStream.range(0, methodWrapBean.getParameterWrapBeanList().size()).boxed().map(new Function() { + @Override + public Object apply(Integer index) { + ParameterWrapBean parameterWrapBean = methodWrapBean.getParameterWrapBeanList().get(index); - // 如果没有@LiteflowFact标注,那么不处理,直接赋值null - if (parameterWrapBean.getFact() == null) { - return null; - } + // 如果参数是NodeComponent,那就返回proxy本身 + if (parameterWrapBean.getParameterType().isAssignableFrom(NodeComponent.class)) { + return proxy; + } - // 把上下文数据转换成map形式的,key为别名,value为上下文 - Map contextMap = DataBus.getSlot(thisNodeComponent.getSlotIndex()).getContextBeanList().stream().collect( - Collectors.toMap(tuple -> tuple.get(0), tuple -> tuple.get(1)) - ); - - List errorList = new ArrayList<>(); - - Object result = null; - // 根据表达式去上下文里搜索相匹配的数据 - for(Map.Entry entry : contextMap.entrySet()){ - try{ - InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache(entry.getKey() + "." + parameterWrapBean.getFact().value()); - DefaultContext context = new DefaultContext<>(); - context.put(entry.getKey(), entry.getValue()); - result = expressRunner.execute(instructionSet, context, errorList, false, false); - if (result != null){ - break; + if (parameterWrapBean.getFact() == null && ArrayUtil.isNotEmpty(args)){ + if (parameterWrapBean.getParameterType().isAssignableFrom(args[index - 1].getClass())){ + return args[index -1]; } - }catch (Exception ignore){} - } + } - if (result == null){ - try{ - // 如果没有搜到,那么尝试推断表达式是指定的上下文,按照指定上下文的方式去再获取 - InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache("contextMap." + parameterWrapBean.getFact().value()); - DefaultContext context = new DefaultContext<>(); - context.put("contextMap", contextMap); - result = expressRunner.execute(instructionSet, context, errorList, false, false); - }catch (Exception ignore){} - } + // 如果没有@LiteflowFact标注,那么不处理,直接赋值null + if (parameterWrapBean.getFact() == null) { + return null; + } - return result; + // 把上下文数据转换成map形式的,key为别名,value为上下文 + Map contextMap = DataBus.getSlot(thisNodeComponent.getSlotIndex()).getContextBeanList().stream().collect( + Collectors.toMap(tuple -> tuple.get(0), tuple -> tuple.get(1)) + ); + + List errorList = new ArrayList<>(); + + Object result = null; + // 根据表达式去上下文里搜索相匹配的数据 + for(Map.Entry entry : contextMap.entrySet()){ + try{ + InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache(entry.getKey() + "." + parameterWrapBean.getFact().value()); + DefaultContext context = new DefaultContext<>(); + context.put(entry.getKey(), entry.getValue()); + result = expressRunner.execute(instructionSet, context, errorList, false, false); + if (result != null){ + break; + } + }catch (Exception ignore){} + } + + if (result == null){ + try{ + // 如果没有搜到,那么尝试推断表达式是指定的上下文,按照指定上下文的方式去再获取 + InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache("contextMap." + parameterWrapBean.getFact().value()); + DefaultContext context = new DefaultContext<>(); + context.put("contextMap", contextMap); + result = expressRunner.execute(instructionSet, context, errorList, false, false); + }catch (Exception ignore){} + } + + return result; + } }).toArray(); } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/base/BaseELDeclMultiSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/base/BaseELDeclMultiSpringbootTest.java index 1b32cc918..5bbb23205 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/base/BaseELDeclMultiSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/base/BaseELDeclMultiSpringbootTest.java @@ -32,9 +32,15 @@ public class BaseELDeclMultiSpringbootTest extends BaseTest { private FlowExecutor flowExecutor; @Test - public void testBase() throws Exception { + public void testBase1() throws Exception { LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); Assertions.assertTrue(response.isSuccess()); } + @Test + public void testBase2() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + Assertions.assertFalse(response.isSuccess()); + } + } diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/CmpConfig.java b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/CmpConfig.java index d5dab5786..3214bb959 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/CmpConfig.java +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/CmpConfig.java @@ -35,4 +35,23 @@ public class CmpConfig { System.out.println("CCmp executed!"); } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "e") + public void processE(NodeComponent bindCmp) { + System.out.println("ECmp executed!"); + } + + @LiteflowMethod(value = LiteFlowMethodEnum.BEFORE_PROCESS, nodeId = "e") + public void beforeProcessE(NodeComponent bindCmp) { + int a = 1/0; + } + + @LiteflowMethod(value = LiteFlowMethodEnum.ON_ERROR, nodeId = "e") + public void onErrorE(NodeComponent bindCmp, Exception e) { + if (e != null){ + e.printStackTrace(); + }else{ + System.out.println("no error"); + } + } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/base/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/base/flow.el.xml index edf6e579d..ab4e1dc5b 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/base/flow.el.xml +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/base/flow.el.xml @@ -3,4 +3,8 @@ THEN(a, b, WHEN(c, d)); + + + THEN(a, e); + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/BaseELDeclSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/BaseELDeclSpringbootTest.java index af10a6746..b3b89e891 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/BaseELDeclSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/BaseELDeclSpringbootTest.java @@ -32,9 +32,15 @@ public class BaseELDeclSpringbootTest extends BaseTest { private FlowExecutor flowExecutor; @Test - public void testBase() throws Exception { + public void testBase1() throws Exception { LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); Assertions.assertTrue(response.isSuccess()); } + @Test + public void testBase2() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + Assertions.assertFalse(response.isSuccess()); + } + } diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/ACmp.java index a4c0a8c47..34bc54cbc 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/ACmp.java +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/ACmp.java @@ -18,9 +18,13 @@ import org.springframework.stereotype.Component; @LiteflowCmpDefine(NodeTypeEnum.COMMON) public class ACmp { + + @LiteflowMethod(LiteFlowMethodEnum.PROCESS) public void process(NodeComponent bindCmp) { System.out.println("ACmp executed!"); } + + } diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/ECmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/ECmp.java new file mode 100644 index 000000000..9944041a2 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/base/cmp/ECmp.java @@ -0,0 +1,40 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.base.cmp; + +import com.yomahub.liteflow.annotation.LiteflowCmpDefine; +import com.yomahub.liteflow.annotation.LiteflowMethod; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.enums.LiteFlowMethodEnum; +import com.yomahub.liteflow.enums.NodeTypeEnum; +import org.springframework.stereotype.Component; + +@Component("e") +@LiteflowCmpDefine(NodeTypeEnum.COMMON) +public class ECmp { + + @LiteflowMethod(LiteFlowMethodEnum.BEFORE_PROCESS) + public void before(NodeComponent bindCmp) throws Exception{ + int a = 1/0; + } + + @LiteflowMethod(LiteFlowMethodEnum.PROCESS) + public void process(NodeComponent bindCmp) { + System.out.println("CCmp executed!"); + } + + @LiteflowMethod(LiteFlowMethodEnum.ON_ERROR) + public void onError(NodeComponent bindCmp, Exception e) { + if (e != null){ + e.printStackTrace(); + }else{ + System.out.println("no error"); + } + + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/base/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/base/flow.el.xml index edf6e579d..ab4e1dc5b 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/base/flow.el.xml +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/base/flow.el.xml @@ -3,4 +3,8 @@ THEN(a, b, WHEN(c, d)); + + + THEN(a, e); + \ No newline at end of file