feature #I4GS07 代码动态组件装配的特性

This commit is contained in:
bryan31
2022-01-18 23:42:56 +08:00
parent 415e0b19bc
commit 987e5b5eb4
4 changed files with 31 additions and 20 deletions

View File

@@ -17,12 +17,18 @@ public class LiteFlowChainBuilder {
private Chain chain;
//声明这个变量而不是用chain.getConditionList的目的是为了辅助平滑加载
//虽然FlowBus里面的map都是CopyOnWrite类型的但是在buildCondition的时候为了平滑加载所以不能事先把chain.getConditionList给设为空List
//所以在这里做一个缓存等conditionList全部build完毕后再去一次性替换chain里面的conditionList
private final List<Condition> conditionList;
public static LiteFlowChainBuilder createChain(){
return new LiteFlowChainBuilder();
}
public LiteFlowChainBuilder(){
chain = new Chain();
conditionList = new ArrayList<>();
}
//在parser中chain的build是2段式的因为涉及到依赖问题以前是递归parser
@@ -44,6 +50,7 @@ public class LiteFlowChainBuilder {
}
public void build(){
this.chain.setConditionList(this.conditionList);
FlowBus.addChain(this.chain);
}
@@ -52,29 +59,29 @@ public class LiteFlowChainBuilder {
//对于then来说相邻的2个then会合并成一个condition
//对于when来说相同组的when会合并成一个condition不同组的when还是会拆开
if (condition.getConditionType().equals(ConditionTypeEnum.TYPE_PRE)) {
this.chain.getConditionList().add(new PreCondition(condition));
this.conditionList.add(new PreCondition(condition));
} else if (condition.getConditionType().equals(ConditionTypeEnum.TYPE_THEN)) {
if (this.chain.getConditionList().size() >= 1 &&
CollectionUtil.getLast(this.chain.getConditionList()) instanceof ThenCondition) {
CollectionUtil.getLast(this.chain.getConditionList()).getNodeList().addAll(condition.getNodeList());
if (this.conditionList.size() >= 1 &&
CollectionUtil.getLast(this.conditionList) instanceof ThenCondition) {
CollectionUtil.getLast(this.conditionList).getNodeList().addAll(condition.getNodeList());
} else {
this.chain.getConditionList().add(new ThenCondition(condition));
this.conditionList.add(new ThenCondition(condition));
}
} else if (condition.getConditionType().equals(ConditionTypeEnum.TYPE_WHEN)) {
if (this.chain.getConditionList().size() > 1 &&
CollectionUtil.getLast(this.chain.getConditionList()) instanceof WhenCondition &&
CollectionUtil.getLast(this.chain.getConditionList()).getGroup().equals(condition.getGroup())) {
CollectionUtil.getLast(this.chain.getConditionList()).getNodeList().addAll(condition.getNodeList());
if (this.conditionList.size() > 1 &&
CollectionUtil.getLast(this.conditionList) instanceof WhenCondition &&
CollectionUtil.getLast(this.conditionList).getGroup().equals(condition.getGroup())) {
CollectionUtil.getLast(this.conditionList).getNodeList().addAll(condition.getNodeList());
} else {
this.chain.getConditionList().add(new WhenCondition(condition));
this.conditionList.add(new WhenCondition(condition));
}
} else if (condition.getConditionType().equals(ConditionTypeEnum.TYPE_FINALLY)) {
this.chain.getConditionList().add(new FinallyCondition(condition));
this.conditionList.add(new FinallyCondition(condition));
}
//每一次build之后对conditionList进行排序pre最前面finally最后
//这里为什么要排序因为在声明的时候哪怕有人不把pre放最前finally放最后但最终也要确保是正确的顺序
CollectionUtil.sort(this.chain.getConditionList(), (o1, o2) -> {
CollectionUtil.sort(this.conditionList, (o1, o2) -> {
if (o1.getConditionType().equals(ConditionTypeEnum.TYPE_PRE) || o2.getConditionType().equals(ConditionTypeEnum.TYPE_FINALLY)){
return -1;
} else if (o2.getConditionType().equals(ConditionTypeEnum.TYPE_PRE) || o1.getConditionType().equals(ConditionTypeEnum.TYPE_FINALLY)){

View File

@@ -34,6 +34,7 @@ import com.yomahub.liteflow.script.ScriptExecutorFactory;
import com.yomahub.liteflow.script.exception.ScriptSpiException;
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
import com.yomahub.liteflow.util.SpringAware;
import org.checkerframework.checker.units.qual.C;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.SerializationUtils;
@@ -61,14 +62,18 @@ public class FlowBus {
return chainMap.get(id);
}
public static void addChain(Chain chain) {
if (chainMap.containsKey(chain.getChainName()) && CollectionUtil.isEmpty(chain.getConditionList())){
chainMap.get(chain.getChainName()).setConditionList(chain.getConditionList());
}else{
chainMap.put(chain.getChainName(), chain);
//这一方法主要用于第一阶段chain的预装载
public static void addChain(String chainName){
if (!chainMap.containsKey(chainName)){
chainMap.put(chainName, new Chain(chainName));
}
}
//这个方法主要用于第二阶段的替换chain
public static void addChain(Chain chain) {
chainMap.put(chain.getChainName(), chain);
}
public static boolean containChain(String chainId) {
return chainMap.containsKey(chainId);
}
@@ -172,7 +177,6 @@ public class FlowBus {
}catch (ScriptSpiException ignored){}
}
//目前这种方式刷新不完全平滑
public static void refreshFlowMetaData(FlowParserTypeEnum type, String content) throws Exception {
if (type.equals(FlowParserTypeEnum.TYPE_XML)) {
new LocalXmlFlowParser().parse(content);

View File

@@ -78,7 +78,7 @@ public abstract class JsonFlowParser extends FlowParser {
//先在元数据里放上chain
chainArray.forEach(o -> {
JSONObject innerJsonObject = (JSONObject)o;
FlowBus.addChain(new Chain(innerJsonObject.getString("name")));
FlowBus.addChain(innerJsonObject.getString("name"));
});
});

View File

@@ -73,7 +73,7 @@ public abstract class XmlFlowParser extends FlowParser {
List<Element> chainList = document.getRootElement().elements("chain");
//先在元数据里放上chain
chainList.forEach(e -> FlowBus.addChain(new Chain(e.attributeValue("name"))));
chainList.forEach(e -> FlowBus.addChain(e.attributeValue("name")));
});
for (Document document : documentList) {