enhancement #I61EMZ 添加验证EL规则的api

This commit is contained in:
zendwang 2022-11-30 14:05:41 +08:00
parent 2921694314
commit 88c2521b6b
3 changed files with 44 additions and 43 deletions

View File

@ -14,12 +14,12 @@ import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.element.Chain;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.condition.*;
import com.yomahub.liteflow.script.ScriptBeanManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
/**
* Chain基于代码形式的组装器
@ -30,12 +30,10 @@ import java.util.function.BiConsumer;
*/
public class LiteFlowChainELBuilder {
private Chain chain;
private static final Logger LOG = LoggerFactory.getLogger(LiteFlowChainELBuilder.class);
/**
* 是否只需要验证EL表达式
*/
private Boolean onlyValidate;
private Chain chain;
/**
* //这是主体的Condition不包含前置和后置
@ -92,7 +90,6 @@ public class LiteFlowChainELBuilder {
public LiteFlowChainELBuilder() {
chain = new Chain();
onlyValidate = Boolean.FALSE;
conditionList = new ArrayList<>();
preConditionList = new ArrayList<>();
finallyConditionList = new ArrayList<>();
@ -124,11 +121,6 @@ public class LiteFlowChainELBuilder {
return this;
}
public LiteFlowChainELBuilder setOnlyValidate() {
this.onlyValidate = Boolean.TRUE;
return this;
}
public LiteFlowChainELBuilder setEL(String elStr) {
if (StrUtil.isBlank(elStr)) {
String errMsg = StrUtil.format("no content in this chain[{}]", chain.getChainId());
@ -180,6 +172,21 @@ public class LiteFlowChainELBuilder {
}
}
/**
* EL表达式校验
* @param elStr EL表达式
* @return true 校验成功 false 校验失败
*/
public Boolean validate(String elStr) {
try {
this.setEL(elStr);
return Boolean.TRUE;
} catch (ELParseException e) {
LOG.error(e.getMessage());
}
return Boolean.FALSE;
}
public void build() {
this.chain.setConditionList(this.conditionList);
this.chain.setPreConditionList(this.preConditionList);
@ -187,10 +194,7 @@ public class LiteFlowChainELBuilder {
checkBuild();
// 仅校验EL表达式格式是否正确时当前生成的chain 不加入元数据
if (!this.onlyValidate) {
FlowBus.addChain(this.chain);
}
FlowBus.addChain(this.chain);
}
/**

View File

@ -5,6 +5,7 @@ import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.FlowExecutorHolder;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.property.LiteflowConfig;
import com.yomahub.liteflow.test.BaseTest;
@ -160,4 +161,26 @@ public class BuilderTest extends BaseTest {
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("a1[组件A1]==>c2[组件C2]==>a2[组件A2]==>c1[组件C1]", response.getExecuteStepStr());
}
@Test
public void testChainELExpressValidate() {
LiteFlowNodeBuilder.createNode().setId("a")
.setName("组件A")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.builder.cmp.ACmp")
.build();
LiteFlowNodeBuilder.createNode().setId("b")
.setName("组件B")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.builder.cmp.BCmp")
.build();
LiteFlowNodeBuilder.createNode().setId("c")
.setName("组件C")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.builder.cmp.CCmp")
.build();
Boolean res = LiteFlowChainELBuilder.createChain().validate("THEN(a, b, h)");
Assert.assertFalse(res);
Assert.assertTrue(LiteFlowChainELBuilder.createChain().validate("THEN(a, b, c)"));
}
}

View File

@ -56,30 +56,4 @@ public class Exception1Test extends BaseTest {
config.setRuleSource("error/flow.txt");
flowExecutor.reloadRule();
}
@Test
public void testChainElBuilderOnlyValidate() {
LiteFlowNodeBuilder.createNode().setId("a")
.setName("组件A")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.builder.cmp.ACmp")
.build();
LiteFlowNodeBuilder.createNode().setId("b")
.setName("组件B")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.builder.cmp.BCmp")
.build();
LiteFlowNodeBuilder.createNode().setId("c")
.setName("组件C")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.builder.cmp.CCmp")
.build();
try {
LiteFlowChainELBuilder.createChain().setChainId("chain3").setEL(
"THEN(a, b, d)"
).setOnlyValidate().build();
} catch ( Exception ex) {
Assert.assertTrue(ex instanceof ELParseException);
}
}
}