mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 12:12:08 +08:00
feature #I4GS07 w动态组件装配的特性
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.yomahub.liteflow.builder;
|
||||
|
||||
import com.yomahub.liteflow.entity.flow.Chain;
|
||||
import com.yomahub.liteflow.entity.flow.Condition;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Chain基于代码形式的组装器
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.8
|
||||
*/
|
||||
public class LiteFlowChainBuilder {
|
||||
|
||||
private final Chain chain;
|
||||
|
||||
public static LiteFlowChainBuilder createChain(){
|
||||
return new LiteFlowChainBuilder();
|
||||
}
|
||||
|
||||
public LiteFlowChainBuilder(){
|
||||
chain = new Chain();
|
||||
chain.setConditionList(new ArrayList<>());
|
||||
}
|
||||
|
||||
public LiteFlowChainBuilder setChainName(String chainName){
|
||||
this.chain.setChainName(chainName);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LiteFlowChainBuilder setCondition(Condition condition){
|
||||
this.chain.getConditionList().add(condition);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void build(){
|
||||
FlowBus.addChain(this.chain);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.yomahub.liteflow.builder;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.entity.flow.Chain;
|
||||
import com.yomahub.liteflow.entity.flow.Condition;
|
||||
import com.yomahub.liteflow.entity.flow.Node;
|
||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||
import com.yomahub.liteflow.exception.ExecutableItemNotFoundException;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.parser.RegexEntity;
|
||||
import com.yomahub.liteflow.parser.RegexNodeEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Condition基于代码形式的组装器
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.8
|
||||
*/
|
||||
public class LiteFlowConditionBuilder {
|
||||
|
||||
protected Condition condition;
|
||||
|
||||
public static LiteFlowConditionBuilder createThenCondition(){
|
||||
return new LiteFlowConditionBuilder(ConditionTypeEnum.TYPE_THEN);
|
||||
}
|
||||
|
||||
public static LiteFlowWhenConditionBuilder createWhenCondition(){
|
||||
return new LiteFlowWhenConditionBuilder(ConditionTypeEnum.TYPE_WHEN);
|
||||
}
|
||||
|
||||
public static LiteFlowConditionBuilder createPreCondition(){
|
||||
return new LiteFlowConditionBuilder(ConditionTypeEnum.TYPE_PRE);
|
||||
}
|
||||
|
||||
public static LiteFlowConditionBuilder createFinallyCondition(){
|
||||
return new LiteFlowConditionBuilder(ConditionTypeEnum.TYPE_FINALLY);
|
||||
}
|
||||
|
||||
public LiteFlowConditionBuilder(ConditionTypeEnum conditionType){
|
||||
this.condition = new Condition();
|
||||
this.condition.setConditionType(conditionType.getType());
|
||||
this.condition.setNodeList(new ArrayList<>());
|
||||
}
|
||||
|
||||
public LiteFlowConditionBuilder setValue(String value){
|
||||
if (StrUtil.isBlank(value)){
|
||||
return this;
|
||||
}
|
||||
String[] condArray = value.split(",");
|
||||
|
||||
RegexEntity regexEntity;
|
||||
String itemExpression;
|
||||
RegexNodeEntity item;
|
||||
for (String s : condArray) {
|
||||
itemExpression = s.trim();
|
||||
regexEntity = RegexEntity.parse(itemExpression);
|
||||
item = regexEntity.getItem();
|
||||
if (FlowBus.containNode(item.getId())) {
|
||||
Node node = FlowBus.copyNode(item.getId());
|
||||
node.setTag(regexEntity.getItem().getTag());
|
||||
this.condition.getNodeList().add(node);
|
||||
//这里判断是不是条件节点,条件节点会含有realItem,也就是括号里的node
|
||||
if (ObjectUtil.isNotNull(regexEntity.getRealItemArray())) {
|
||||
for (RegexNodeEntity realItem : regexEntity.getRealItemArray()) {
|
||||
if (FlowBus.containNode(realItem.getId())) {
|
||||
Node condNode = FlowBus.copyNode(realItem.getId());
|
||||
condNode.setTag(realItem.getTag());
|
||||
node.setCondNode(condNode.getId(), condNode);
|
||||
} else if (hasChain(realItem.getId())) {
|
||||
Chain chain = FlowBus.getChain(realItem.getId());
|
||||
node.setCondNode(chain.getChainName(), chain);
|
||||
} else{
|
||||
String errorMsg = StrUtil.format("executable node[{}] is not found!", realItem.getId());
|
||||
throw new ExecutableItemNotFoundException(errorMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (hasChain(item.getId())) {
|
||||
Chain chain = FlowBus.getChain(item.getId());
|
||||
this.condition.getNodeList().add(chain);
|
||||
} else {
|
||||
String errorMsg = StrUtil.format("executable node[{}] is not found!", regexEntity.getItem().getId());
|
||||
throw new ExecutableItemNotFoundException(errorMsg);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Condition build(){
|
||||
return this.condition;
|
||||
}
|
||||
|
||||
private boolean hasChain(String chainId){
|
||||
return FlowBus.containChain(chainId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.yomahub.liteflow.builder;
|
||||
|
||||
import com.yomahub.liteflow.entity.flow.Chain;
|
||||
import com.yomahub.liteflow.entity.flow.Node;
|
||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||
|
||||
public class LiteFlowNodeBuilder {
|
||||
|
||||
private final Node node;
|
||||
|
||||
public static LiteFlowNodeBuilder createNode(){
|
||||
return new LiteFlowNodeBuilder();
|
||||
}
|
||||
|
||||
public LiteFlowNodeBuilder() {
|
||||
this.node = new Node();
|
||||
}
|
||||
|
||||
public LiteFlowNodeBuilder setId(String nodeId){
|
||||
this.node.setId(nodeId);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LiteFlowNodeBuilder setName(String name){
|
||||
this.node.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LiteFlowNodeBuilder setType(NodeTypeEnum type){
|
||||
this.node.setType(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.yomahub.liteflow.builder;
|
||||
|
||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||
|
||||
/**
|
||||
* WhenCondition基于代码形式的组装器
|
||||
* 这个为LiteFlowConditionBuilder的子类,因为when有单独的设置项,所以区分开
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.8
|
||||
*/
|
||||
public class LiteFlowWhenConditionBuilder extends LiteFlowConditionBuilder{
|
||||
|
||||
public LiteFlowWhenConditionBuilder(ConditionTypeEnum conditionType) {
|
||||
super(conditionType);
|
||||
}
|
||||
|
||||
public LiteFlowConditionBuilder setErrorResume(boolean errorResume){
|
||||
this.condition.setErrorResume(errorResume);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LiteFlowConditionBuilder setGroup(String group){
|
||||
this.condition.setGroup(group);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LiteFlowConditionBuilder setAny(boolean any){
|
||||
this.condition.setAny(any);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -368,7 +368,7 @@ public class FlowExecutor {
|
||||
chain.execute(slotIndex);
|
||||
} catch (Exception e) {
|
||||
if (ObjectUtil.isNotNull(chain)){
|
||||
LOG.error("[{}]:chain[{}] execute error on slot[{}]", slot.getRequestId(), chain.getChainName(), slotIndex);
|
||||
LOG.error("[{}]:chain[{}] execute error on slot[{}]", slot.getRequestId(), chain.getChainName(), slotIndex, e);
|
||||
}
|
||||
slot.setException(e);
|
||||
} finally {
|
||||
|
||||
@@ -42,6 +42,10 @@ public class Chain implements Executable {
|
||||
|
||||
private List<Condition> conditionList;
|
||||
|
||||
public Chain(){
|
||||
|
||||
}
|
||||
|
||||
public Chain(String chainName, List<Condition> conditionList) {
|
||||
this.chainName = chainName;
|
||||
this.conditionList = conditionList;
|
||||
|
||||
@@ -41,6 +41,8 @@ public class Node implements Executable,Cloneable{
|
||||
|
||||
private NodeTypeEnum type;
|
||||
|
||||
private String script;
|
||||
|
||||
private NodeComponent instance;
|
||||
|
||||
private final Map<String, Executable> condNodeMap = new HashMap<>();
|
||||
@@ -194,4 +196,12 @@ public class Node implements Executable,Cloneable{
|
||||
public void setTag(String tag) {
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public String getScript() {
|
||||
return script;
|
||||
}
|
||||
|
||||
public void setScript(String script) {
|
||||
this.script = script;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,15 +55,12 @@ public class FlowBus {
|
||||
private FlowBus() {
|
||||
}
|
||||
|
||||
public static Chain getChain(String id) throws Exception {
|
||||
if (MapUtil.isEmpty(chainMap)) {
|
||||
throw new Exception("please config the rule first");
|
||||
}
|
||||
public static Chain getChain(String id){
|
||||
return chainMap.get(id);
|
||||
}
|
||||
|
||||
public static void addChain(String name, Chain chain) {
|
||||
chainMap.put(name, chain);
|
||||
public static void addChain(Chain chain) {
|
||||
chainMap.put(chain.getChainName(), chain);
|
||||
}
|
||||
|
||||
public static boolean containChain(String chainId) {
|
||||
@@ -116,8 +113,12 @@ public class FlowBus {
|
||||
//进行初始化
|
||||
cmpInstance = ComponentInitializer.loadInstance().initComponent(cmpInstance, type, name, nodeId);
|
||||
|
||||
//初始化Node
|
||||
Node node = new Node(cmpInstance);
|
||||
|
||||
//如果是脚本节点(普通脚本节点/条件脚本节点),则还要加载script脚本
|
||||
if (StrUtil.isNotBlank(script)){
|
||||
node.setScript(script);
|
||||
if (type.equals(NodeTypeEnum.SCRIPT)){
|
||||
((ScriptComponent)cmpInstance).loadScript(script);
|
||||
}else if(type.equals(NodeTypeEnum.COND_SCRIPT)){
|
||||
@@ -125,7 +126,7 @@ public class FlowBus {
|
||||
}
|
||||
}
|
||||
|
||||
nodeMap.put(nodeId, new Node(cmpInstance));
|
||||
nodeMap.put(nodeId, node);
|
||||
} catch (Exception e) {
|
||||
String error = StrUtil.format("component[{}] register error", cmpClazz.getName());
|
||||
LOG.error(error, e);
|
||||
|
||||
@@ -198,7 +198,7 @@ public abstract class JsonFlowParser extends FlowParser {
|
||||
//这里把condition组装进conditionList,根据参数有些condition要和conditionList里面的某些进行合并操作
|
||||
super.buildConditions(conditionList, condition);
|
||||
}
|
||||
FlowBus.addChain(chainName, new Chain(chainName, conditionList));
|
||||
FlowBus.addChain(new Chain(chainName, conditionList));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -197,7 +197,7 @@ public abstract class XmlFlowParser extends FlowParser {
|
||||
//这里把condition组装进conditionList,根据参数有些condition要和conditionList里面的某些进行合并操作
|
||||
super.buildConditions(conditionList, condition);
|
||||
}
|
||||
FlowBus.addChain(chainName, new Chain(chainName, conditionList));
|
||||
FlowBus.addChain(new Chain(chainName, conditionList));
|
||||
}
|
||||
|
||||
//判断在这个FlowBus元数据里是否含有这个chain
|
||||
|
||||
Reference in New Issue
Block a user