feature #I96A33 为LF增加决策表特性

This commit is contained in:
everywhere.z
2024-03-06 14:03:11 +08:00
parent 3d65d77730
commit 9b8902c01c
12 changed files with 236 additions and 30 deletions

View File

@@ -21,6 +21,7 @@ import com.yomahub.liteflow.exception.ParseException;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.element.Chain;
import com.yomahub.liteflow.flow.element.Condition;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
@@ -44,6 +45,11 @@ public class LiteFlowChainELBuilder {
private Chain chain;
/**
* 这是route EL的文本
*/
private Executable route;
/**
* 这是主体的Condition //声明这个变量而不是用chain.getConditionList的目的是为了辅助平滑加载
* 虽然FlowBus里面的map都是CopyOnWrite类型的但是在buildCondition的时候为了平滑加载所以不能事先把chain.getConditionList给设为空List
@@ -130,6 +136,45 @@ public class LiteFlowChainELBuilder {
return this;
}
public LiteFlowChainELBuilder setRoute(String routeEl){
if (StrUtil.isBlank(routeEl)) {
String errMsg = StrUtil.format("You have defined the label <route> but there is no content in the chain[{}].", chain.getChainId());
throw new FlowSystemException(errMsg);
}
List<String> errorList = new ArrayList<>();
try {
DefaultContext<String, Object> context = new DefaultContext<>();
// 往上下文里放入所有的node使得el表达式可以直接引用到nodeId
FlowBus.getNodeMap().keySet().forEach(nodeId -> context.put(nodeId, FlowBus.getNode(nodeId)));
// 解析route el成为一个executable
Executable routeExecutable = (Executable) EXPRESS_RUNNER.execute(routeEl, context, errorList, true, true);
if (Objects.isNull(routeExecutable)){
throw new QLException(StrUtil.format("parse route el fail,el:[{}]", routeEl));
}
// 把主要的condition加入
this.route = routeExecutable;
return this;
} catch (QLException e) {
// EL 底层会包装异常,这里是曲线处理
if (ObjectUtil.isNotNull(e.getCause()) && Objects.equals(e.getCause().getMessage(), DataNotFoundException.MSG)) {
// 构建错误信息
String msg = buildDataNotFoundExceptionMsg(routeEl);
throw new ELParseException(msg);
}else if (ObjectUtil.isNotNull(e.getCause())){
throw new ELParseException(e.getCause().getMessage());
}else{
throw new ELParseException(e.getMessage());
}
} catch (Exception e) {
String errMsg = StrUtil.format("parse el fail in this chain[{}];\r\n", chain.getChainId());
throw new ELParseException(errMsg + e.getMessage());
}
}
public LiteFlowChainELBuilder setEL(String elStr) {
if (StrUtil.isBlank(elStr)) {
String errMsg = StrUtil.format("no content in this chain[{}]", chain.getChainId());
@@ -197,6 +242,7 @@ public class LiteFlowChainELBuilder {
}
public void build() {
this.chain.setRouteItem(this.route);
this.chain.setConditionList(this.conditionList);
//暂且去掉循环依赖检测因为有发现循环依赖检测在对大的EL进行检测的时候会导致CPU飙升也或许是jackson低版本的问题

View File

@@ -10,6 +10,10 @@ public interface ChainConstant {
String CHAIN = "chain";
String ROUTE = "route";
String BODY = "body";
String FLOW = "flow";
String NODES = "nodes";

View File

@@ -30,6 +30,8 @@ public class Chain implements Executable{
private String chainId;
private Executable routeItem;
private List<Condition> conditionList = new ArrayList<>();
public Chain(String chainName) {
@@ -133,4 +135,12 @@ public class Chain implements Executable{
public String getTag() {
return null;
}
public Executable getRouteItem() {
return routeItem;
}
public void setRouteItem(Executable routeItem) {
this.routeItem = routeItem;
}
}

View File

@@ -8,12 +8,7 @@ import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.builder.prop.NodePropBean;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.exception.ChainDuplicateException;
import com.yomahub.liteflow.exception.ChainNotFoundException;
import com.yomahub.liteflow.exception.NodeClassNotFoundException;
import com.yomahub.liteflow.exception.NodeTypeCanNotGuessException;
import com.yomahub.liteflow.exception.NodeTypeNotSupportException;
import com.yomahub.liteflow.exception.ParseException;
import com.yomahub.liteflow.exception.*;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.element.Chain;
import com.yomahub.liteflow.flow.element.condition.AbstractCondition;
@@ -30,18 +25,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import static com.yomahub.liteflow.common.ChainConstant.CHAIN;
import static com.yomahub.liteflow.common.ChainConstant.EXTENDS;
import static com.yomahub.liteflow.common.ChainConstant.FILE;
import static com.yomahub.liteflow.common.ChainConstant.FLOW;
import static com.yomahub.liteflow.common.ChainConstant.ID;
import static com.yomahub.liteflow.common.ChainConstant.LANGUAGE;
import static com.yomahub.liteflow.common.ChainConstant.NAME;
import static com.yomahub.liteflow.common.ChainConstant.NODE;
import static com.yomahub.liteflow.common.ChainConstant.NODES;
import static com.yomahub.liteflow.common.ChainConstant.TYPE;
import static com.yomahub.liteflow.common.ChainConstant.VALUE;
import static com.yomahub.liteflow.common.ChainConstant._CLASS;
import static com.yomahub.liteflow.common.ChainConstant.*;
/**
* Parser 通用 Helper
@@ -296,11 +280,27 @@ public class ParserHelper {
public static void parseOneChainEl(JsonNode chainNode) {
// 构建chainBuilder
String chainId = Optional.ofNullable(chainNode.get(ID)).orElse(chainNode.get(NAME)).textValue();
String el = chainNode.get(VALUE).textValue();
LiteFlowChainELBuilder.createChain()
.setChainId(chainId)
.setEL(el)
.build();
JsonNode routeJsonNode = chainNode.get(ROUTE);
LiteFlowChainELBuilder builder = LiteFlowChainELBuilder.createChain().setChainId(chainId);
// 如果有route这个标签说明是决策表chain
// 决策表链路必须有route和body这两个标签
if (routeJsonNode != null){
builder.setRoute(routeJsonNode.textValue());
JsonNode bodyJsonNode = chainNode.get(BODY);
if (bodyJsonNode == null){
String errMsg = StrUtil.format("If you have defined the field route, then you must define the field body in chain[{}]", chainId);
throw new FlowSystemException(errMsg);
}
builder.setEL(bodyJsonNode.textValue());
}else{
builder.setEL(chainNode.get(VALUE).textValue());
}
builder.build();
}
/**
@@ -310,12 +310,27 @@ public class ParserHelper {
public static void parseOneChainEl(Element e) {
// 构建chainBuilder
String chainId = Optional.ofNullable(e.attributeValue(ID)).orElse(e.attributeValue(NAME));
String text = e.getText();
String el = ElRegexUtil.removeComments(text);
LiteFlowChainELBuilder.createChain()
.setChainId(chainId)
.setEL(el)
.build();
Element routeElement = e.element(ROUTE);
LiteFlowChainELBuilder builder = LiteFlowChainELBuilder.createChain().setChainId(chainId);
// 如果有route这个标签说明是决策表chain
// 决策表链路必须有route和body这两个标签
if (routeElement != null){
builder.setRoute(ElRegexUtil.removeComments(routeElement.getText()));
Element bodyElement = e.element(BODY);
if (bodyElement == null){
String errMsg = StrUtil.format("If you have defined the tag <route>, then you must define the tag <body> in chain[{}]", chainId);
throw new FlowSystemException(errMsg);
}
builder.setEL(ElRegexUtil.removeComments(bodyElement.getText()));
}else{
builder.setEL(ElRegexUtil.removeComments(e.getText()));
}
builder.build();
}
/**

View File

@@ -3,7 +3,9 @@
<!ELEMENT flow ((nodes)? , (chain)+)>
<!ELEMENT nodes (node)+>
<!ELEMENT node (#PCDATA | EMPTY)*>
<!ELEMENT chain (#PCDATA)>
<!ELEMENT chain ((route)? | (body)? | #PCDATA)>
<!ELEMENT route (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ATTLIST node
id CDATA #REQUIRED