feature #I7YYLE 格式化代码

This commit is contained in:
Dale Lee
2023-10-02 19:10:53 +08:00
parent ee598eb773
commit acdafba090
21 changed files with 173 additions and 170 deletions

View File

@@ -9,20 +9,20 @@ import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.flow.element.condition.IteratorCondition;
public class IteratorOperator extends BaseOperator<IteratorCondition> {
@Override
public IteratorCondition build(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEq(objects, 1);
Node node = OperatorHelper.convert(objects[0], Node.class);
if (!ListUtil.toList(NodeTypeEnum.ITERATOR, NodeTypeEnum.FALLBACK).contains(node.getType())) {
throw new QLException("The parameter must be iterator-node item");
}
IteratorCondition iteratorCondition = new IteratorCondition();
iteratorCondition.setIteratorNode(node);
return iteratorCondition;
}
}

View File

@@ -24,26 +24,26 @@ import com.yomahub.liteflow.property.LiteflowConfigGetter;
* @since 2.8.3
*/
public class NodeOperator extends BaseOperator<Node> {
@Override
public Node build(Object[] objects) throws Exception {
// 检查是否开启了组件降级功能
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
Boolean enable = liteflowConfig.getFallbackCmpEnable();
if (!enable) {
throw new ELParseException("The fallback component is disabled");
}
OperatorHelper.checkObjectSizeEqOne(objects);
String nodeId = OperatorHelper.convert(objects[0], String.class);
if (FlowBus.containNode(nodeId)) {
// 找到对应节点
return FlowBus.getNode(nodeId);
} else {
// 生成代理节点
return new FallbackNodeProxy(nodeId);
}
}
@Override
public Node build(Object[] objects) throws Exception {
// 检查是否开启了组件降级功能
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
Boolean enable = liteflowConfig.getFallbackCmpEnable();
if (!enable) {
throw new ELParseException("The fallback component is disabled");
}
OperatorHelper.checkObjectSizeEqOne(objects);
String nodeId = OperatorHelper.convert(objects[0], String.class);
if (FlowBus.containNode(nodeId)) {
// 找到对应节点
return FlowBus.getNode(nodeId);
} else {
// 生成代理节点
return new FallbackNodeProxy(nodeId);
}
}
}

View File

@@ -15,20 +15,20 @@ import com.yomahub.liteflow.flow.element.condition.SwitchCondition;
* @since 2.8.0
*/
public class SwitchOperator extends BaseOperator<SwitchCondition> {
@Override
public SwitchCondition build(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEqOne(objects);
Node switchNode = OperatorHelper.convert(objects[0], Node.class);
if (!ListUtil.toList(NodeTypeEnum.SWITCH, NodeTypeEnum.SWITCH_SCRIPT, NodeTypeEnum.FALLBACK)
.contains(switchNode.getType())) {
throw new QLException("The caller must be Switch item");
}
SwitchCondition switchCondition = new SwitchCondition();
switchCondition.setSwitchNode(switchNode);
return switchCondition;
}
}

View File

@@ -6,25 +6,25 @@ package com.yomahub.liteflow.exception;
* @author DaleLee
*/
public class FallbackCmpNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 异常信息
*/
private String message;
public FallbackCmpNotFoundException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@@ -47,63 +47,63 @@ import java.util.stream.Collectors;
* @author Bryan.Zhang
*/
public class FlowBus {
private static final LFLog LOG = LFLoggerManager.getLogger(FlowBus.class);
private static final Map<String, Chain> chainMap = new CopyOnWriteHashMap<>();
private static final Map<String, Node> nodeMap = new CopyOnWriteHashMap<>();
private static final Map<NodeTypeEnum, Node> fallbackNodeMap = new CopyOnWriteHashMap<>();
private FlowBus() {
}
public static Chain getChain(String id) {
return chainMap.get(id);
}
// 这一方法主要用于第一阶段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.getChainId(), chain);
}
public static boolean containChain(String chainId) {
return chainMap.containsKey(chainId);
}
public static boolean needInit() {
return MapUtil.isEmpty(chainMap);
}
public static boolean containNode(String nodeId) {
return nodeMap.containsKey(nodeId);
}
/**
* 添加已托管的节点Spring、Solon 管理的节点)
*/
public static void addManagedNode(String nodeId, NodeComponent nodeComponent) {
// 根据class来猜测类型
NodeTypeEnum type = NodeTypeEnum.guessType(nodeComponent.getClass());
if (type == null) {
throw new NullNodeTypeException(StrUtil.format("node type is null for node[{}]", nodeId));
}
Node node = new Node(ComponentInitializer.loadInstance()
.initComponent(nodeComponent, type, nodeComponent.getName(), nodeId));
nodeMap.put(nodeId, node);
addFallbackNode(node);
}
/**
* 添加 node
*
@@ -115,7 +115,7 @@ public class FlowBus {
public static void addNode(String nodeId, String name, NodeTypeEnum type, Class<?> cmpClazz) {
addNode(nodeId, name, type, cmpClazz, null, null);
}
/**
* 添加 node
*
@@ -133,7 +133,7 @@ public class FlowBus {
}
addNode(nodeId, name, nodeType, cmpClazz, null, null);
}
/**
* 添加脚本 node
*
@@ -143,12 +143,12 @@ public class FlowBus {
* @param script 脚本
*/
public static void addScriptNode(String nodeId, String name, NodeTypeEnum nodeType, String script,
String language) {
String language) {
addNode(nodeId, name, nodeType, ScriptComponent.ScriptComponentClassMap.get(nodeType), script, language);
}
private static void addNode(String nodeId, String name, NodeTypeEnum type, Class<?> cmpClazz, String script,
String language) {
String language) {
try {
// 判断此类是否是声明式的组件,如果是声明式的组件,就用动态代理生成实例
// 如果不是声明式的,就用传统的方式进行判断
@@ -188,10 +188,10 @@ public class FlowBus {
.initComponent(cmpInstance, type, name,
cmpInstance.getNodeId() == null ? nodeId : cmpInstance.getNodeId()))
.collect(Collectors.toList());
// 初始化Node把component放到Node里去
List<Node> nodes = cmpInstances.stream().map(Node::new).collect(Collectors.toList());
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
NodeComponent cmpInstance = cmpInstances.get(i);
@@ -206,12 +206,12 @@ public class FlowBus {
throw new ScriptLoadException(errorMsg);
}
}
String activeNodeId = StrUtil.isEmpty(cmpInstance.getNodeId()) ? nodeId : cmpInstance.getNodeId();
nodeMap.put(activeNodeId, node);
addFallbackNode(node);
}
} catch (Exception e) {
String error = StrUtil.format("component[{}] register error",
StrUtil.isEmpty(name) ? nodeId : StrUtil.format("{}({})", nodeId, name));
@@ -219,30 +219,30 @@ public class FlowBus {
throw new ComponentCannotRegisterException(StrUtil.format("{} {}", error, e.getMessage()));
}
}
public static Node getNode(String nodeId) {
return nodeMap.get(nodeId);
}
public static Map<String, Node> getNodeMap() {
return nodeMap;
}
public static Map<String, Chain> getChainMap() {
return chainMap;
}
public static Node getFallBackNode(NodeTypeEnum nodeType) {
return fallbackNodeMap.get(nodeType);
}
public static void cleanCache() {
chainMap.clear();
nodeMap.clear();
fallbackNodeMap.clear();
cleanScriptCache();
}
public static void cleanScriptCache() {
// 如果引入了脚本组件SPI则还需要清理脚本的缓存
try {
@@ -250,7 +250,7 @@ public class FlowBus {
} catch (ScriptSpiException ignored) {
}
}
public static void refreshFlowMetaData(FlowParserTypeEnum type, String content) throws Exception {
if (type.equals(FlowParserTypeEnum.TYPE_EL_XML)) {
new LocalXmlFlowELParser().parse(content);
@@ -260,7 +260,7 @@ public class FlowBus {
new LocalYmlFlowELParser().parse(content);
}
}
public static boolean removeChain(String chainId) {
if (containChain(chainId)) {
chainMap.remove(chainId);
@@ -271,23 +271,23 @@ public class FlowBus {
return false;
}
}
public static void removeChain(String... chainIds) {
Arrays.stream(chainIds).forEach(FlowBus::removeChain);
}
private static void addFallbackNode(Node node) {
NodeComponent nodeComponent = node.getInstance();
FallbackCmp fallbackCmp = AnnoUtil.getAnnotation(nodeComponent.getClass(), FallbackCmp.class);
if (fallbackCmp == null) {
return;
}
NodeTypeEnum nodeType = node.getType();
if (nodeType == null) {
nodeType = fallbackCmp.type();
}
fallbackNodeMap.put(nodeType, node);
}
}

View File

@@ -30,21 +30,21 @@ import java.util.Map;
* @author Bryan.Zhang
*/
public abstract class Condition implements Executable {
private String id;
private String tag;
/**
* 可执行元素的集合
*/
private final Map<String, List<Executable>> executableGroup = new HashMap<>();
/**
* 当前所在的ChainName 如果对于子流程来说那这个就是子流程所在的Chain
*/
private String currChainId;
@Override
public void execute(Integer slotIndex) throws Exception {
// 当前 Condition 入栈
@@ -70,18 +70,18 @@ public abstract class Condition implements Executable {
slot.popCondition();
}
}
public abstract void executeCondition(Integer slotIndex) throws Exception;
@Override
public ExecuteTypeEnum getExecuteType() {
return ExecuteTypeEnum.CONDITION;
}
public List<Executable> getExecutableList() {
return getExecutableList(ConditionKey.DEFAULT_KEY);
}
public List<Executable> getExecutableList(String groupKey) {
List<Executable> executableList = this.executableGroup.get(groupKey);
if (CollUtil.isEmpty(executableList)) {
@@ -89,7 +89,7 @@ public abstract class Condition implements Executable {
}
return executableList;
}
public Executable getExecutableOne(String groupKey) {
List<Executable> list = getExecutableList(groupKey);
if (CollUtil.isEmpty(list)) {
@@ -98,15 +98,15 @@ public abstract class Condition implements Executable {
return list.get(0);
}
}
public void setExecutableList(List<Executable> executableList) {
this.executableGroup.put(ConditionKey.DEFAULT_KEY, executableList);
}
public void addExecutable(Executable executable) {
addExecutable(ConditionKey.DEFAULT_KEY, executable);
}
public void addExecutable(String groupKey, Executable executable) {
if (ObjectUtil.isNull(executable)) {
return;
@@ -118,29 +118,29 @@ public abstract class Condition implements Executable {
this.executableGroup.get(groupKey).add(executable);
}
}
public abstract ConditionTypeEnum getConditionType();
@Override
@Override
public String getId() {
return id;
}
@Override
@Override
public void setId(String id) {
this.id = id;
}
@Override
public String getTag() {
return tag;
}
@Override
@Override
public void setTag(String tag) {
this.tag = tag;
}
/**
* 请使用 {@link #setCurrChainId(String)}
*/
@@ -148,16 +148,16 @@ public abstract class Condition implements Executable {
public String getCurrChainName() {
return currChainId;
}
public String getCurrChainId() {
return currChainId;
}
@Override
public void setCurrChainId(String currChainId) {
this.currChainId = currChainId;
}
public Map<String, List<Executable>> getExecutableGroup() {
return executableGroup;
}

View File

@@ -18,29 +18,29 @@ import com.yomahub.liteflow.slot.DataBus;
import com.yomahub.liteflow.slot.Slot;
public class FallbackNodeProxy extends Node {
// 原节点 id
private String expectedNodeId;
// 降级节点
private Node fallbackNode;
public FallbackNodeProxy() {
this.setType(NodeTypeEnum.FALLBACK);
}
public FallbackNodeProxy(String expectedNodeId) {
this();
this.expectedNodeId = expectedNodeId;
}
@Override
public void execute(Integer slotIndex) throws Exception {
loadFallBackNode(slotIndex);
this.fallbackNode.setCurrChainId(this.getCurrChainId());
this.fallbackNode.execute(slotIndex);
}
private void loadFallBackNode(Integer slotIndex) throws Exception {
if (ObjectUtil.isNotNull(this.fallbackNode)) {
// 已经加载过了
@@ -60,7 +60,7 @@ public class FallbackNodeProxy extends Node {
// 使用 node 的副本
this.fallbackNode = node.copy();
}
private Node findFallbackNode(Condition condition) {
ConditionTypeEnum conditionType = condition.getConditionType();
switch (conditionType) {
@@ -87,95 +87,95 @@ public class FallbackNodeProxy extends Node {
return null;
}
}
private Node findNodeInIf(IfCondition ifCondition) {
Executable ifItem = ifCondition.getIfItem();
if (ifItem == this) {
// 需要条件组件
return FlowBus.getFallBackNode(NodeTypeEnum.IF);
}
// 需要普通组件
return FlowBus.getFallBackNode(NodeTypeEnum.COMMON);
}
private Node findNodeInSwitch(SwitchCondition switchCondition) {
Node switchNode = switchCondition.getSwitchNode();
if (switchNode == this) {
return FlowBus.getFallBackNode(NodeTypeEnum.SWITCH);
}
return FlowBus.getFallBackNode(NodeTypeEnum.COMMON);
}
private Node findNodeInFor(ForCondition forCondition) {
Node forNode = forCondition.getForNode();
if (forNode == this) {
return FlowBus.getFallBackNode(NodeTypeEnum.FOR);
}
return findNodeInLoop(forCondition);
}
private Node findNodeInWhile(WhileCondition whileCondition) {
Executable whileItem = whileCondition.getWhileItem();
if (whileItem == this) {
return FlowBus.getFallBackNode(NodeTypeEnum.WHILE);
}
return findNodeInLoop(whileCondition);
}
private Node findNodeInIterator(IteratorCondition iteratorCondition) {
Node iteratorNode = iteratorCondition.getIteratorNode();
if (iteratorNode == this) {
return FlowBus.getFallBackNode(NodeTypeEnum.ITERATOR);
}
return findNodeInLoop(iteratorCondition);
}
private Node findNodeInLoop(LoopCondition loopCondition) {
Executable breakItem = loopCondition.getExecutableOne(ConditionKey.BREAK_KEY);
if (breakItem == this) {
return FlowBus.getFallBackNode(NodeTypeEnum.BREAK);
}
return FlowBus.getFallBackNode(NodeTypeEnum.COMMON);
}
@Override
public <T> T getItemResultMetaValue(Integer slotIndex) {
return this.fallbackNode.getItemResultMetaValue(slotIndex);
}
@Override
public boolean isAccess(Integer slotIndex) throws Exception {
// 可能会先访问这个方法,所以在这里就要加载降级节点
loadFallBackNode(slotIndex);
return this.fallbackNode.isAccess(slotIndex);
}
@Override
public String getId() {
return this.fallbackNode == null ? null : this.fallbackNode.getId();
}
@Override
public Node copy() {
// 代理节点不复制
return this;
}
@Override
public NodeTypeEnum getType() {
return NodeTypeEnum.FALLBACK;
}
public String getExpectedNodeId() {
return expectedNodeId;
}
public void setExpectedNodeId(String expectedNodeId) {
this.expectedNodeId = expectedNodeId;
}