底层代码优化
This commit is contained in:
parent
558853e530
commit
32f8c33589
|
@ -16,8 +16,7 @@ public abstract class NodeBreakComponent extends NodeComponent {
|
|||
public void process() throws Exception {
|
||||
boolean breakFlag = processBreak();
|
||||
Slot slot = this.getSlot();
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
slot.setBreakResult(originalClass.getName(), breakFlag);
|
||||
slot.setBreakResult(this.getMetaValueKey(), breakFlag);
|
||||
}
|
||||
|
||||
public abstract boolean processBreak() throws Exception;
|
||||
|
@ -25,8 +24,7 @@ public abstract class NodeBreakComponent extends NodeComponent {
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Boolean getItemResultMetaValue(Integer slotIndex) {
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
return DataBus.getSlot(slotIndex).getBreakResult(originalClass.getName());
|
||||
return DataBus.getSlot(slotIndex).getBreakResult(this.getMetaValueKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import cn.hutool.core.date.StopWatch;
|
|||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import com.yomahub.liteflow.core.proxy.LiteFlowProxyUtil;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.flow.element.Node;
|
||||
import com.yomahub.liteflow.flow.executor.NodeExecutor;
|
||||
|
@ -429,4 +430,9 @@ public abstract class NodeComponent{
|
|||
public <T> T getItemResultMetaValue(Integer slotIndex){
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String getMetaValueKey(){
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
return originalClass.getName();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,8 +16,7 @@ public abstract class NodeForComponent extends NodeComponent {
|
|||
public void process() throws Exception {
|
||||
int forCount = processFor();
|
||||
Slot slot = this.getSlot();
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
slot.setForResult(originalClass.getName(), forCount);
|
||||
slot.setForResult(this.getMetaValueKey(), forCount);
|
||||
}
|
||||
|
||||
public abstract int processFor() throws Exception;
|
||||
|
@ -25,8 +24,7 @@ public abstract class NodeForComponent extends NodeComponent {
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Integer getItemResultMetaValue(Integer slotIndex) {
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
return DataBus.getSlot(slotIndex).getForResult(originalClass.getName());
|
||||
return DataBus.getSlot(slotIndex).getForResult(this.getMetaValueKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,8 +14,7 @@ public abstract class NodeIfComponent extends NodeComponent {
|
|||
@Override
|
||||
public void process() throws Exception {
|
||||
boolean result = this.processIf();
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
this.getSlot().setIfResult(originalClass.getName(), result);
|
||||
this.getSlot().setIfResult(this.getMetaValueKey(), result);
|
||||
}
|
||||
|
||||
public abstract boolean processIf() throws Exception;
|
||||
|
@ -23,7 +22,6 @@ public abstract class NodeIfComponent extends NodeComponent {
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Boolean getItemResultMetaValue(Integer slotIndex) {
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
return DataBus.getSlot(slotIndex).getIfResult(originalClass.getName());
|
||||
return DataBus.getSlot(slotIndex).getIfResult(this.getMetaValueKey());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,8 +18,7 @@ public abstract class NodeIteratorComponent extends NodeComponent {
|
|||
public void process() throws Exception {
|
||||
Iterator<?> it = processIterator();
|
||||
Slot slot = this.getSlot();
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
slot.setIteratorResult(originalClass.getName(), it);
|
||||
slot.setIteratorResult(this.getMetaValueKey(), it);
|
||||
}
|
||||
|
||||
public abstract Iterator<?> processIterator() throws Exception;
|
||||
|
@ -27,8 +26,7 @@ public abstract class NodeIteratorComponent extends NodeComponent {
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Iterator<?> getItemResultMetaValue(Integer slotIndex) {
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
return DataBus.getSlot(slotIndex).getIteratorResult(originalClass.getName());
|
||||
return DataBus.getSlot(slotIndex).getIteratorResult(this.getMetaValueKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,8 +20,7 @@ public abstract class NodeSwitchComponent extends NodeComponent {
|
|||
@Override
|
||||
public void process() throws Exception {
|
||||
String nodeId = this.processSwitch();
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
this.getSlot().setSwitchResult(originalClass.getName(), nodeId);
|
||||
this.getSlot().setSwitchResult(this.getMetaValueKey(), nodeId);
|
||||
}
|
||||
|
||||
// 用以返回路由节点的beanId
|
||||
|
@ -30,8 +29,7 @@ public abstract class NodeSwitchComponent extends NodeComponent {
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public String getItemResultMetaValue(Integer slotIndex) {
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
return DataBus.getSlot(slotIndex).getSwitchResult(originalClass.getName());
|
||||
return DataBus.getSlot(slotIndex).getSwitchResult(this.getMetaValueKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,8 +16,7 @@ public abstract class NodeWhileComponent extends NodeComponent {
|
|||
public void process() throws Exception {
|
||||
boolean whileFlag = processWhile();
|
||||
Slot slot = this.getSlot();
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
slot.setWhileResult(originalClass.getName(), whileFlag);
|
||||
slot.setWhileResult(this.getMetaValueKey(), whileFlag);
|
||||
}
|
||||
|
||||
public abstract boolean processWhile() throws Exception;
|
||||
|
@ -25,8 +24,7 @@ public abstract class NodeWhileComponent extends NodeComponent {
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Boolean getItemResultMetaValue(Integer slotIndex) {
|
||||
Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
|
||||
return DataBus.getSlot(slotIndex).getWhileResult(originalClass.getName());
|
||||
return DataBus.getSlot(slotIndex).getWhileResult(this.getMetaValueKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.yomahub.liteflow.enums.NodeTypeEnum;
|
|||
import com.yomahub.liteflow.exception.ComponentCannotRegisterException;
|
||||
import com.yomahub.liteflow.exception.NullNodeTypeException;
|
||||
import com.yomahub.liteflow.flow.element.Chain;
|
||||
import com.yomahub.liteflow.flow.element.Condition;
|
||||
import com.yomahub.liteflow.flow.element.Node;
|
||||
import com.yomahub.liteflow.log.LFLog;
|
||||
import com.yomahub.liteflow.log.LFLoggerManager;
|
||||
|
@ -37,11 +38,12 @@ import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
|||
import com.yomahub.liteflow.spi.holder.DeclComponentParserHolder;
|
||||
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
|
||||
import com.yomahub.liteflow.core.proxy.LiteFlowProxyUtil;
|
||||
import com.yomahub.liteflow.util.NodeScanner;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* 流程元数据类
|
||||
|
@ -245,7 +247,9 @@ public class FlowBus {
|
|||
// 获取某一个 chainId 下的所有 nodeId
|
||||
public static List<Node> getNodesByChainId(String chainId) {
|
||||
Chain chain = getChain(chainId);
|
||||
return NodeScanner.getNodesInChain(chain);
|
||||
return chain.getConditionList().stream().flatMap(
|
||||
(Function<Condition, Stream<Node>>) condition -> condition.getAllNodeInCondition().stream()
|
||||
).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static Map<String, Node> getNodeMap() {
|
||||
|
|
|
@ -22,6 +22,11 @@ import java.util.ArrayList;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Condition的抽象类
|
||||
|
@ -103,6 +108,24 @@ public abstract class Condition implements Executable{
|
|||
}
|
||||
}
|
||||
|
||||
public List<Node> getAllNodeInCondition(){
|
||||
List<Executable> executableList = this.executableGroup.entrySet().stream().flatMap(
|
||||
(Function<Map.Entry<String, List<Executable>>, Stream<Executable>>) entry -> entry.getValue().stream()
|
||||
).collect(Collectors.toList());
|
||||
|
||||
List<Node> resultList = new ArrayList<>();
|
||||
|
||||
executableList.stream().forEach(executable -> {
|
||||
if (executable instanceof Condition){
|
||||
resultList.addAll(((Condition)executable).getAllNodeInCondition());
|
||||
}else if(executable instanceof Node){
|
||||
resultList.add((Node)executable);
|
||||
}
|
||||
});
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public void setExecutableList(List<Executable> executableList) {
|
||||
this.executableGroup.put(ConditionKey.DEFAULT_KEY, executableList);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.yomahub.liteflow.flow.element;
|
|||
|
||||
import cn.hutool.core.text.StrFormatter;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||
import com.yomahub.liteflow.exception.FallbackCmpNotFoundException;
|
||||
|
@ -162,6 +163,14 @@ public class FallbackNode extends Node {
|
|||
return this.fallbackNode.isAccess(slotIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeComponent getInstance() {
|
||||
if (fallbackNode == null){
|
||||
return null;
|
||||
}
|
||||
return fallbackNode.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return this.fallbackNode == null ? null : this.fallbackNode.getId();
|
||||
|
|
|
@ -180,7 +180,7 @@ public class Node implements Executable, Cloneable, Rollbackable{
|
|||
}
|
||||
finally {
|
||||
// 移除threadLocal里的信息
|
||||
instance.removeRefNode();
|
||||
this.getInstance().removeRefNode();
|
||||
removeSlotIndex();
|
||||
removeIsEnd();
|
||||
removeLoopIndex();
|
||||
|
|
|
@ -1,69 +0,0 @@
|
|||
package com.yomahub.liteflow.util;
|
||||
|
||||
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 java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* 节点扫描器
|
||||
*
|
||||
* @author DaleLee
|
||||
* @since 2.12.0
|
||||
*/
|
||||
public class NodeScanner {
|
||||
|
||||
/**
|
||||
* 获取 Chain 中的所有 Node
|
||||
*
|
||||
* @param chain Chain
|
||||
* @return Node 集合
|
||||
*/
|
||||
public static List<Node> getNodesInChain(Chain chain) {
|
||||
List<Node> result = new ArrayList<>();
|
||||
if (chain == null) {
|
||||
return result;
|
||||
}
|
||||
for (Condition condition : chain.getConditionList()) {
|
||||
result.addAll(getNodesInCondition(condition));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Condition 中的所有 Node
|
||||
*
|
||||
* @param condition Condition
|
||||
* @return Node 集合
|
||||
*/
|
||||
public static List<Node> getNodesInCondition(Condition condition) {
|
||||
List<Node> result = new ArrayList<>();
|
||||
if (condition == null) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 层序遍历
|
||||
Queue<Executable> queue = new LinkedList<>();
|
||||
queue.offer(condition);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
Executable cur = queue.poll();
|
||||
if (cur instanceof Condition) {
|
||||
Map<String, List<Executable>> executableGroup = ((Condition) cur).getExecutableGroup();
|
||||
for (List<Executable> executables : executableGroup.values()) {
|
||||
executables.forEach(queue::offer);
|
||||
}
|
||||
} else if (cur instanceof Node) {
|
||||
result.add((Node) cur);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue