mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-06-14 11:31:46 +08:00
@@ -0,0 +1,93 @@
|
||||
package com.yomahub.liteflow.builder.prop;
|
||||
|
||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||
|
||||
/**
|
||||
* 构建 chain 的中间属性
|
||||
*/
|
||||
public class ChainPropBean {
|
||||
|
||||
/**
|
||||
* 执行规则
|
||||
*/
|
||||
String condValueStr;
|
||||
|
||||
/**
|
||||
* 分组
|
||||
*/
|
||||
String group;
|
||||
|
||||
/**
|
||||
* 是否抛出异常
|
||||
*/
|
||||
String errorResume;
|
||||
|
||||
/**
|
||||
* 满足任意条件,执行完成
|
||||
*/
|
||||
String any;
|
||||
|
||||
/**
|
||||
* 指定线程池
|
||||
*/
|
||||
String threadExecutorClass;
|
||||
|
||||
/**
|
||||
* chain 类型
|
||||
*/
|
||||
ConditionTypeEnum conditionType;
|
||||
|
||||
public String getCondValueStr() {
|
||||
return condValueStr;
|
||||
}
|
||||
|
||||
public ChainPropBean setCondValueStr(String condValueStr) {
|
||||
this.condValueStr = condValueStr;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public ChainPropBean setGroup(String group) {
|
||||
this.group = group;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getErrorResume() {
|
||||
return errorResume;
|
||||
}
|
||||
|
||||
public ChainPropBean setErrorResume(String errorResume) {
|
||||
this.errorResume = errorResume;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getAny() {
|
||||
return any;
|
||||
}
|
||||
|
||||
public ChainPropBean setAny(String any) {
|
||||
this.any = any;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getThreadExecutorClass() {
|
||||
return threadExecutorClass;
|
||||
}
|
||||
|
||||
public ChainPropBean setThreadExecutorClass(String threadExecutorClass) {
|
||||
this.threadExecutorClass = threadExecutorClass;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConditionTypeEnum getConditionType() {
|
||||
return conditionType;
|
||||
}
|
||||
|
||||
public ChainPropBean setConditionType(ConditionTypeEnum conditionType) {
|
||||
this.conditionType = conditionType;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.yomahub.liteflow.builder.prop;
|
||||
|
||||
/**
|
||||
* 构建 node 的中间属性
|
||||
*/
|
||||
public class NodePropBean {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
String id;
|
||||
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
String name;
|
||||
|
||||
/**
|
||||
* 类
|
||||
*/
|
||||
String clazz;
|
||||
|
||||
/**
|
||||
* 脚本
|
||||
*/
|
||||
String script;
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
String type;
|
||||
|
||||
/**
|
||||
* 脚本存放位置
|
||||
*/
|
||||
String file;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public NodePropBean setId(String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public NodePropBean setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getClazz() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
public NodePropBean setClazz(String clazz) {
|
||||
this.clazz = clazz;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getScript() {
|
||||
return script;
|
||||
}
|
||||
|
||||
public NodePropBean setScript(String script) {
|
||||
this.script = script;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public NodePropBean setType(String type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public NodePropBean setFile(String file) {
|
||||
this.file = file;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.yomahub.liteflow.common;
|
||||
|
||||
/**
|
||||
* Chain 常量
|
||||
*
|
||||
* @author tangkc
|
||||
*/
|
||||
public interface ChainConstant {
|
||||
|
||||
String CHAIN = "chain";
|
||||
|
||||
String FLOW = "flow";
|
||||
|
||||
String NODES = "nodes";
|
||||
|
||||
String NODE = "node";
|
||||
|
||||
String ID = "id";
|
||||
|
||||
String _CLASS = "class";
|
||||
|
||||
String FILE = "file";
|
||||
|
||||
String NAME = "name";
|
||||
|
||||
String VALUE = "value";
|
||||
|
||||
String ERROR_RESUME = "errorResume";
|
||||
|
||||
String GROUP = "group";
|
||||
|
||||
String ANY = "any";
|
||||
|
||||
String THREAD_EXECUTOR_CLASS = "threadExecutorClass";
|
||||
|
||||
String CONDITION = "condition";
|
||||
|
||||
String TYPE = "type";
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.yomahub.liteflow.parser;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
|
||||
import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
|
||||
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
|
||||
import com.yomahub.liteflow.builder.prop.ChainPropBean;
|
||||
import com.yomahub.liteflow.builder.prop.NodePropBean;
|
||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||
import com.yomahub.liteflow.exception.EmptyConditionValueException;
|
||||
import com.yomahub.liteflow.exception.NodeTypeNotSupportException;
|
||||
import com.yomahub.liteflow.exception.NotSupportConditionException;
|
||||
|
||||
/**
|
||||
* 基类,用于存放通用方法
|
||||
*
|
||||
* @author tangkc
|
||||
*/
|
||||
public abstract class BaseFlowParser implements FlowParser {
|
||||
|
||||
/**
|
||||
* 构建 node
|
||||
*
|
||||
* @param nodePropBean 构建 node 的中间属性
|
||||
*/
|
||||
public void buildNode(NodePropBean nodePropBean) {
|
||||
String id = nodePropBean.getId();
|
||||
String name = nodePropBean.getName();
|
||||
String clazz = nodePropBean.getClazz();
|
||||
String script = nodePropBean.getScript();
|
||||
String type = nodePropBean.getType();
|
||||
String file = nodePropBean.getFile();
|
||||
|
||||
//初始化type
|
||||
if (StrUtil.isBlank(type)) {
|
||||
type = NodeTypeEnum.COMMON.getCode();
|
||||
}
|
||||
|
||||
//检查nodeType是不是规定的类型
|
||||
NodeTypeEnum nodeTypeEnum = NodeTypeEnum.getEnumByCode(type);
|
||||
if (ObjectUtil.isNull(nodeTypeEnum)) {
|
||||
throw new NodeTypeNotSupportException(StrUtil.format("type [{}] is not support", type));
|
||||
}
|
||||
|
||||
//进行node的build过程
|
||||
LiteFlowNodeBuilder.createNode().setId(id).setName(name).setClazz(clazz).setType(nodeTypeEnum).setScript(script).setFile(file).build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建 chain
|
||||
*
|
||||
* @param chainPropBean 构建 chain 的中间属性
|
||||
* @param chainBuilder chainBuilder
|
||||
*/
|
||||
public void buildChain(ChainPropBean chainPropBean
|
||||
, LiteFlowChainBuilder chainBuilder) {
|
||||
String condValueStr = chainPropBean.getCondValueStr();
|
||||
String group = chainPropBean.getGroup();
|
||||
String errorResume = chainPropBean.getErrorResume();
|
||||
String any = chainPropBean.getAny();
|
||||
String threadExecutorClass = chainPropBean.getThreadExecutorClass();
|
||||
ConditionTypeEnum conditionType = chainPropBean.getConditionType();
|
||||
|
||||
if (ObjectUtil.isNull(conditionType)) {
|
||||
throw new NotSupportConditionException("ConditionType is not supported");
|
||||
}
|
||||
|
||||
if (StrUtil.isBlank(condValueStr)) {
|
||||
throw new EmptyConditionValueException("Condition value cannot be empty");
|
||||
}
|
||||
|
||||
//如果是when类型的话,有特殊化参数要设置,只针对于when的
|
||||
if (conditionType.equals(ConditionTypeEnum.TYPE_WHEN)) {
|
||||
chainBuilder.setCondition(
|
||||
LiteFlowConditionBuilder.createWhenCondition()
|
||||
.setErrorResume(errorResume)
|
||||
.setGroup(group)
|
||||
.setAny(any)
|
||||
.setThreadExecutorClass(threadExecutorClass)
|
||||
.setValue(condValueStr)
|
||||
.build()
|
||||
).build();
|
||||
} else {
|
||||
chainBuilder.setCondition(
|
||||
LiteFlowConditionBuilder.createCondition(conditionType)
|
||||
.setValue(condValueStr)
|
||||
.build()
|
||||
).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,24 +2,35 @@ package com.yomahub.liteflow.parser;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.parser.Feature;
|
||||
import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
|
||||
import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
|
||||
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
|
||||
import com.yomahub.liteflow.builder.prop.ChainPropBean;
|
||||
import com.yomahub.liteflow.builder.prop.NodePropBean;
|
||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||
import com.yomahub.liteflow.exception.EmptyConditionValueException;
|
||||
import com.yomahub.liteflow.exception.NodeTypeNotSupportException;
|
||||
import com.yomahub.liteflow.exception.NotSupportConditionException;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.spi.holder.ContextCmpInitHolder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import java.util.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.yomahub.liteflow.common.ChainConstant.ANY;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.CHAIN;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.CONDITION;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.ERROR_RESUME;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.FILE;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.FLOW;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.GROUP;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.ID;
|
||||
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.THREAD_EXECUTOR_CLASS;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.TYPE;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.VALUE;
|
||||
import static com.yomahub.liteflow.common.ChainConstant._CLASS;
|
||||
|
||||
/**
|
||||
* Json格式解析器
|
||||
@@ -27,148 +38,123 @@ import java.util.*;
|
||||
* @author guodongqing
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public abstract class JsonFlowParser implements FlowParser {
|
||||
public abstract class JsonFlowParser extends BaseFlowParser {
|
||||
|
||||
private final Logger LOG = LoggerFactory.getLogger(JsonFlowParser.class);
|
||||
private final Logger LOG = LoggerFactory.getLogger(JsonFlowParser.class);
|
||||
|
||||
public void parse(String content) throws Exception {
|
||||
parse(ListUtil.toList(content));
|
||||
}
|
||||
public void parse(String content) throws Exception {
|
||||
parse(ListUtil.toList(content));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(List<String> contentList) throws Exception {
|
||||
if (CollectionUtil.isEmpty(contentList)) {
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
public void parse(List<String> contentList) throws Exception {
|
||||
if (CollectionUtil.isEmpty(contentList)) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<JSONObject> jsonObjectList = ListUtil.toList();
|
||||
for (String content : contentList) {
|
||||
//把字符串原生转换为json对象,如果不加第二个参数OrderedField,会无序
|
||||
JSONObject flowJsonObject = JSONObject.parseObject(content, Feature.OrderedField);
|
||||
jsonObjectList.add(flowJsonObject);
|
||||
}
|
||||
List<JSONObject> jsonObjectList = ListUtil.toList();
|
||||
for (String content : contentList) {
|
||||
//把字符串原生转换为json对象,如果不加第二个参数OrderedField,会无序
|
||||
JSONObject flowJsonObject = JSONObject.parseObject(content, Feature.OrderedField);
|
||||
jsonObjectList.add(flowJsonObject);
|
||||
}
|
||||
|
||||
parseJsonObject(jsonObjectList);
|
||||
}
|
||||
parseJsonObject(jsonObjectList);
|
||||
}
|
||||
|
||||
//json格式,解析过程
|
||||
public void parseJsonObject(List<JSONObject> flowJsonObjectList) throws Exception {
|
||||
//在相应的环境下进行节点的初始化工作
|
||||
//在spring体系下会获得spring扫描后的节点,接入元数据
|
||||
//在非spring体系下是一个空实现,等于不做此步骤
|
||||
ContextCmpInitHolder.loadContextCmpInit().initCmp();
|
||||
//json格式,解析过程
|
||||
public void parseJsonObject(List<JSONObject> flowJsonObjectList) throws Exception {
|
||||
//在相应的环境下进行节点的初始化工作
|
||||
//在spring体系下会获得spring扫描后的节点,接入元数据
|
||||
//在非spring体系下是一个空实现,等于不做此步骤
|
||||
ContextCmpInitHolder.loadContextCmpInit().initCmp();
|
||||
|
||||
//先在元数据里放上chain
|
||||
//先放有一个好处,可以在parse的时候先映射到FlowBus的chainMap,然后再去解析
|
||||
//这样就不用去像之前的版本那样回归调用
|
||||
//同时也解决了不能循环依赖的问题
|
||||
flowJsonObjectList.forEach(jsonObject -> {
|
||||
// 解析chain节点
|
||||
JSONArray chainArray = jsonObject.getJSONObject("flow").getJSONArray("chain");
|
||||
//先在元数据里放上chain
|
||||
//先放有一个好处,可以在parse的时候先映射到FlowBus的chainMap,然后再去解析
|
||||
//这样就不用去像之前的版本那样回归调用
|
||||
//同时也解决了不能循环依赖的问题
|
||||
flowJsonObjectList.forEach(jsonObject -> {
|
||||
// 解析chain节点
|
||||
JSONArray chainArray = jsonObject.getJSONObject(FLOW).getJSONArray(CHAIN);
|
||||
|
||||
//先在元数据里放上chain
|
||||
chainArray.forEach(o -> {
|
||||
JSONObject innerJsonObject = (JSONObject)o;
|
||||
FlowBus.addChain(innerJsonObject.getString("name"));
|
||||
});
|
||||
});
|
||||
//先在元数据里放上chain
|
||||
chainArray.forEach(o -> {
|
||||
JSONObject innerJsonObject = (JSONObject) o;
|
||||
FlowBus.addChain(innerJsonObject.getString(NAME));
|
||||
});
|
||||
});
|
||||
|
||||
for (JSONObject flowJsonObject : flowJsonObjectList) {
|
||||
// 当存在<nodes>节点定义时,解析node节点
|
||||
if (flowJsonObject.getJSONObject("flow").containsKey("nodes")){
|
||||
JSONArray nodeArrayList = flowJsonObject.getJSONObject("flow").getJSONObject("nodes").getJSONArray("node");
|
||||
String id, name, clazz, script, type, file;
|
||||
for (int i = 0; i < nodeArrayList.size(); i++) {
|
||||
JSONObject nodeObject = nodeArrayList.getJSONObject(i);
|
||||
id = nodeObject.getString("id");
|
||||
name = nodeObject.getString("name");
|
||||
clazz = nodeObject.getString("class");
|
||||
type = nodeObject.getString("type");
|
||||
script = nodeObject.getString("value");
|
||||
file = nodeObject.getString("file");
|
||||
for (JSONObject flowJsonObject : flowJsonObjectList) {
|
||||
// 当存在<nodes>节点定义时,解析node节点
|
||||
if (flowJsonObject.getJSONObject(FLOW).containsKey(NODES)) {
|
||||
JSONArray nodeArrayList = flowJsonObject.getJSONObject(FLOW).getJSONObject(NODES).getJSONArray(NODE);
|
||||
String id, name, clazz, script, type, file;
|
||||
for (int i = 0; i < nodeArrayList.size(); i++) {
|
||||
JSONObject nodeObject = nodeArrayList.getJSONObject(i);
|
||||
id = nodeObject.getString(ID);
|
||||
name = nodeObject.getString(NAME);
|
||||
clazz = nodeObject.getString(_CLASS);
|
||||
type = nodeObject.getString(TYPE);
|
||||
script = nodeObject.getString(VALUE);
|
||||
file = nodeObject.getString(FILE);
|
||||
|
||||
//初始化type
|
||||
if (StrUtil.isBlank(type)){
|
||||
type = NodeTypeEnum.COMMON.getCode();
|
||||
}
|
||||
// 构建 node
|
||||
NodePropBean nodePropBean = new NodePropBean()
|
||||
.setId(id)
|
||||
.setName(name)
|
||||
.setClazz(clazz)
|
||||
.setScript(script)
|
||||
.setType(type)
|
||||
.setFile(file);
|
||||
|
||||
//检查nodeType是不是规定的类型
|
||||
NodeTypeEnum nodeTypeEnum = NodeTypeEnum.getEnumByCode(type);
|
||||
if (ObjectUtil.isNull(nodeTypeEnum)){
|
||||
throw new NodeTypeNotSupportException(StrUtil.format("type [{}] is not support", type));
|
||||
}
|
||||
buildNode(nodePropBean);
|
||||
}
|
||||
}
|
||||
|
||||
//进行node的build过程
|
||||
LiteFlowNodeBuilder.createNode().setId(id)
|
||||
.setName(name)
|
||||
.setClazz(clazz)
|
||||
.setType(nodeTypeEnum)
|
||||
.setScript(script)
|
||||
.setFile(file)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
//解析每一个chain
|
||||
JSONArray chainArray = flowJsonObject.getJSONObject("flow").getJSONArray("chain");
|
||||
chainArray.forEach(o -> {
|
||||
JSONObject jsonObject = (JSONObject)o;
|
||||
parseOneChain(jsonObject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析一个chain的过程
|
||||
*/
|
||||
private void parseOneChain(JSONObject chainObject){
|
||||
String condValueStr;
|
||||
ConditionTypeEnum conditionType;
|
||||
String group;
|
||||
String errorResume;
|
||||
String any;
|
||||
String threadExecutorClass;
|
||||
|
||||
//构建chainBuilder
|
||||
String chainName = chainObject.getString("name");
|
||||
LiteFlowChainBuilder chainBuilder = LiteFlowChainBuilder.createChain().setChainName(chainName);
|
||||
|
||||
for (Object o : chainObject.getJSONArray("condition")) {
|
||||
JSONObject condObject = (JSONObject) o;
|
||||
conditionType = ConditionTypeEnum.getEnumByCode(condObject.getString("type"));
|
||||
condValueStr = condObject.getString("value");
|
||||
errorResume = condObject.getString("errorResume");
|
||||
group = condObject.getString("group");
|
||||
any = condObject.getString("any");
|
||||
threadExecutorClass = condObject.getString("threadExecutorClass");
|
||||
|
||||
if (ObjectUtil.isNull(conditionType)){
|
||||
throw new NotSupportConditionException("ConditionType is not supported");
|
||||
}
|
||||
|
||||
if (StrUtil.isBlank(condValueStr)) {
|
||||
throw new EmptyConditionValueException("Condition value cannot be empty");
|
||||
}
|
||||
//解析每一个chain
|
||||
JSONArray chainArray = flowJsonObject.getJSONObject(FLOW).getJSONArray(CHAIN);
|
||||
chainArray.forEach(o -> {
|
||||
JSONObject jsonObject = (JSONObject) o;
|
||||
parseOneChain(jsonObject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//如果是when类型的话,有特殊化参数要设置,只针对于when的
|
||||
if (conditionType.equals(ConditionTypeEnum.TYPE_WHEN)){
|
||||
chainBuilder.setCondition(
|
||||
LiteFlowConditionBuilder.createWhenCondition()
|
||||
.setErrorResume(errorResume)
|
||||
.setGroup(group)
|
||||
.setAny(any)
|
||||
.setThreadExecutorClass(threadExecutorClass)
|
||||
.setValue(condValueStr)
|
||||
.build()
|
||||
).build();
|
||||
}else{
|
||||
chainBuilder.setCondition(
|
||||
LiteFlowConditionBuilder.createCondition(conditionType)
|
||||
.setValue(condValueStr)
|
||||
.build()
|
||||
).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 解析一个chain的过程
|
||||
*/
|
||||
private void parseOneChain(JSONObject chainObject) {
|
||||
String condValueStr;
|
||||
ConditionTypeEnum conditionType;
|
||||
String group;
|
||||
String errorResume;
|
||||
String any;
|
||||
String threadExecutorClass;
|
||||
|
||||
//构建chainBuilder
|
||||
String chainName = chainObject.getString(NAME);
|
||||
LiteFlowChainBuilder chainBuilder = LiteFlowChainBuilder.createChain().setChainName(chainName);
|
||||
|
||||
for (Object o : chainObject.getJSONArray(CONDITION)) {
|
||||
JSONObject condObject = (JSONObject) o;
|
||||
conditionType = ConditionTypeEnum.getEnumByCode(condObject.getString(TYPE));
|
||||
condValueStr = condObject.getString(VALUE);
|
||||
errorResume = condObject.getString(ERROR_RESUME);
|
||||
group = condObject.getString(GROUP);
|
||||
any = condObject.getString(ANY);
|
||||
threadExecutorClass = condObject.getString(THREAD_EXECUTOR_CLASS);
|
||||
|
||||
ChainPropBean chainPropBean = new ChainPropBean()
|
||||
.setCondValueStr(condValueStr)
|
||||
.setGroup(group)
|
||||
.setErrorResume(errorResume)
|
||||
.setAny(any)
|
||||
.setThreadExecutorClass(threadExecutorClass)
|
||||
.setConditionType(conditionType);
|
||||
|
||||
// 构建 chain
|
||||
buildChain(chainPropBean, chainBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,10 @@ package com.yomahub.liteflow.parser;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
|
||||
import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
|
||||
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
|
||||
import com.yomahub.liteflow.builder.prop.ChainPropBean;
|
||||
import com.yomahub.liteflow.builder.prop.NodePropBean;
|
||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||
import com.yomahub.liteflow.exception.*;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.spi.holder.ContextCmpInitHolder;
|
||||
import org.dom4j.Document;
|
||||
@@ -17,14 +14,32 @@ import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static com.yomahub.liteflow.common.ChainConstant.ANY;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.CHAIN;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.ERROR_RESUME;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.FILE;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.GROUP;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.ID;
|
||||
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.THREAD_EXECUTOR_CLASS;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.TYPE;
|
||||
import static com.yomahub.liteflow.common.ChainConstant.VALUE;
|
||||
import static com.yomahub.liteflow.common.ChainConstant._CLASS;
|
||||
|
||||
;
|
||||
|
||||
/**
|
||||
* xml形式的解析器
|
||||
*
|
||||
* @author Bryan.Zhang
|
||||
*/
|
||||
public abstract class XmlFlowParser implements FlowParser {
|
||||
public abstract class XmlFlowParser extends BaseFlowParser {
|
||||
|
||||
private final Logger LOG = LoggerFactory.getLogger(XmlFlowParser.class);
|
||||
|
||||
@@ -57,51 +72,42 @@ public abstract class XmlFlowParser implements FlowParser {
|
||||
//同时也解决了不能循环依赖的问题
|
||||
documentList.forEach(document -> {
|
||||
// 解析chain节点
|
||||
List<Element> chainList = document.getRootElement().elements("chain");
|
||||
List<Element> chainList = document.getRootElement().elements(CHAIN);
|
||||
|
||||
//先在元数据里放上chain
|
||||
chainList.forEach(e -> FlowBus.addChain(e.attributeValue("name")));
|
||||
chainList.forEach(e -> FlowBus.addChain(e.attributeValue(NAME)));
|
||||
});
|
||||
|
||||
for (Document document : documentList) {
|
||||
Element rootElement = document.getRootElement();
|
||||
Element nodesElement = rootElement.element("nodes");
|
||||
Element nodesElement = rootElement.element(NODES);
|
||||
// 当存在<nodes>节点定义时,解析node节点
|
||||
if (ObjectUtil.isNotNull(nodesElement)){
|
||||
List<Element> nodeList = nodesElement.elements("node");
|
||||
if (ObjectUtil.isNotNull(nodesElement)) {
|
||||
List<Element> nodeList = nodesElement.elements(NODE);
|
||||
String id, name, clazz, type, script, file;
|
||||
for (Element e : nodeList) {
|
||||
id = e.attributeValue("id");
|
||||
name = e.attributeValue("name");
|
||||
clazz = e.attributeValue("class");
|
||||
type = e.attributeValue("type");
|
||||
id = e.attributeValue(ID);
|
||||
name = e.attributeValue(NAME);
|
||||
clazz = e.attributeValue(_CLASS);
|
||||
type = e.attributeValue(TYPE);
|
||||
script = e.getTextTrim();
|
||||
file = e.attributeValue("file");
|
||||
file = e.attributeValue(FILE);
|
||||
|
||||
//初始化type
|
||||
if (StrUtil.isBlank(type)){
|
||||
type = NodeTypeEnum.COMMON.getCode();
|
||||
}
|
||||
|
||||
//检查nodeType是不是规定的类型
|
||||
NodeTypeEnum nodeTypeEnum = NodeTypeEnum.getEnumByCode(type);
|
||||
if (ObjectUtil.isNull(nodeTypeEnum)){
|
||||
throw new NodeTypeNotSupportException(StrUtil.format("type [{}] is not support", type));
|
||||
}
|
||||
|
||||
//进行node的build过程
|
||||
LiteFlowNodeBuilder.createNode().setId(id)
|
||||
// 构建 node
|
||||
NodePropBean nodePropBean = new NodePropBean()
|
||||
.setId(id)
|
||||
.setName(name)
|
||||
.setClazz(clazz)
|
||||
.setType(nodeTypeEnum)
|
||||
.setScript(script)
|
||||
.setFile(file)
|
||||
.build();
|
||||
.setType(type)
|
||||
.setFile(file);
|
||||
|
||||
buildNode(nodePropBean);
|
||||
}
|
||||
}
|
||||
|
||||
//解析每一个chain
|
||||
List<Element> chainList = rootElement.elements("chain");
|
||||
List<Element> chainList = rootElement.elements(CHAIN);
|
||||
chainList.forEach(this::parseOneChain);
|
||||
}
|
||||
}
|
||||
@@ -118,44 +124,29 @@ public abstract class XmlFlowParser implements FlowParser {
|
||||
ConditionTypeEnum conditionType;
|
||||
|
||||
//构建chainBuilder
|
||||
String chainName = e.attributeValue("name");
|
||||
String chainName = e.attributeValue(NAME);
|
||||
LiteFlowChainBuilder chainBuilder = LiteFlowChainBuilder.createChain().setChainName(chainName);
|
||||
|
||||
for (Iterator<Element> it = e.elementIterator(); it.hasNext(); ) {
|
||||
Element condE = it.next();
|
||||
conditionType = ConditionTypeEnum.getEnumByCode(condE.getName());
|
||||
condValueStr = condE.attributeValue("value");
|
||||
errorResume = condE.attributeValue("errorResume");
|
||||
group = condE.attributeValue("group");
|
||||
any = condE.attributeValue("any");
|
||||
threadExecutorClass = condE.attributeValue("threadExecutorClass");
|
||||
condValueStr = condE.attributeValue(VALUE);
|
||||
errorResume = condE.attributeValue(ERROR_RESUME);
|
||||
group = condE.attributeValue(GROUP);
|
||||
any = condE.attributeValue(ANY);
|
||||
threadExecutorClass = condE.attributeValue(THREAD_EXECUTOR_CLASS);
|
||||
|
||||
if (ObjectUtil.isNull(conditionType)){
|
||||
throw new NotSupportConditionException("ConditionType is not supported");
|
||||
}
|
||||
ChainPropBean chainPropBean = new ChainPropBean()
|
||||
.setCondValueStr(condValueStr)
|
||||
.setGroup(group)
|
||||
.setErrorResume(errorResume)
|
||||
.setAny(any)
|
||||
.setThreadExecutorClass(threadExecutorClass)
|
||||
.setConditionType(conditionType);
|
||||
|
||||
if (StrUtil.isBlank(condValueStr)) {
|
||||
throw new EmptyConditionValueException("Condition value cannot be empty");
|
||||
}
|
||||
|
||||
//如果是when类型的话,有特殊化参数要设置,只针对于when的
|
||||
if (conditionType.equals(ConditionTypeEnum.TYPE_WHEN)){
|
||||
chainBuilder.setCondition(
|
||||
LiteFlowConditionBuilder.createWhenCondition()
|
||||
.setErrorResume(errorResume)
|
||||
.setGroup(group)
|
||||
.setAny(any)
|
||||
.setThreadExecutorClass(threadExecutorClass)
|
||||
.setValue(condValueStr)
|
||||
.build()
|
||||
).build();
|
||||
}else{
|
||||
chainBuilder.setCondition(
|
||||
LiteFlowConditionBuilder.createCondition(conditionType)
|
||||
.setValue(condValueStr)
|
||||
.build()
|
||||
).build();
|
||||
}
|
||||
// 构建 chain
|
||||
buildChain(chainPropBean, chainBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user