Browse Source

enhancement I40DWO 流程配置文件中增加业务描述,打印步骤中带入业务描述

bryan31 3 years ago
parent
commit
f03beb494f

+ 27 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java

@@ -0,0 +1,27 @@
+package com.yomahub.liteflow.annotation;
+
+import org.springframework.core.annotation.AliasFor;
+import org.springframework.stereotype.Component;
+
+import java.lang.annotation.*;
+
+/**
+ * LiteFlow的组件标识注解
+ *
+ * @author Bryan.Zhang
+ * @since 2.5.11
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Component
+public @interface LiteflowComponent {
+
+    @AliasFor(annotation = Component.class)
+    String value() default "";
+
+    @AliasFor("value")
+    String id();
+
+    String name() default "";
+}

+ 10 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java

@@ -41,6 +41,8 @@ public abstract class NodeComponent {
 
 	private String nodeId;
 
+	private String name;
+
 	//这是自己的实例,取代this
 	//为何要设置这个,用this不行么,因为如果有aop去切的话,this在spring的aop里是切不到的。self对象有可能是代理过的对象
 	private NodeComponent self;
@@ -163,4 +165,12 @@ public abstract class NodeComponent {
 	public void setSelf(NodeComponent self) {
 		this.self = self;
 	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
 }

+ 8 - 8
liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Node.java

@@ -32,7 +32,7 @@ public class Node implements Executable{
 
 	private String id;
 
-	private String clazz;
+	private String name;
 
 	private NodeComponent instance;
 
@@ -42,9 +42,9 @@ public class Node implements Executable{
 
 	}
 
-	public Node(String id, String clazz, NodeComponent instance) {
-		this.id = id;
-		this.clazz = clazz;
+	public Node(NodeComponent instance) {
+		this.id = instance.getNodeId();
+		this.name = instance.getName();
 		this.instance = instance;
 	}
 
@@ -56,12 +56,12 @@ public class Node implements Executable{
 		this.id = id;
 	}
 
-	public String getClazz() {
-		return clazz;
+	public String getName() {
+		return name;
 	}
 
-	public void setClazz(String clazz) {
-		this.clazz = clazz;
+	public void setName(String name) {
+		this.name = name;
 	}
 
 	public NodeComponent getInstance() {

+ 9 - 4
liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java

@@ -69,13 +69,17 @@ public class FlowBus {
         nodeMap.put(nodeId, node);
     }
 
-    public static void addNode(String nodeId, String cmpClazzStr) throws Exception {
+    public static void addNode(String nodeId, String name, String cmpClazzStr) throws Exception {
         if (containNode(nodeId)) return;
         Class<NodeComponent> cmpClazz = (Class<NodeComponent>) Class.forName(cmpClazzStr);
-        addNode(nodeId, cmpClazz);
+        addNode(nodeId, name ,cmpClazz);
     }
 
-    public static void addNode(String nodeId, Class<? extends NodeComponent> cmpClazz) {
+    public static void addNode(String nodeId, Class<? extends NodeComponent> cmpClazz){
+        addNode(nodeId, null, cmpClazz);
+    }
+
+    public static void addNode(String nodeId, String name, Class<? extends NodeComponent> cmpClazz) {
         if (containNode(nodeId)) return;
         try {
             //以node方式配置,本质上是为了适配无spring的环境,如果有spring环境,其实不用这么配置
@@ -86,8 +90,9 @@ public class FlowBus {
                 cmpInstance = cmpClazz.newInstance();
             }
             cmpInstance.setNodeId(nodeId);
+            cmpInstance.setName(name);
             cmpInstance.setSelf(cmpInstance);
-            nodeMap.put(nodeId, new Node(nodeId, cmpClazz.getName(), cmpInstance));
+            nodeMap.put(nodeId, new Node(cmpInstance));
         } catch (Exception e) {
             String error = StrUtil.format("component[{}] register error", cmpClazz.getName());
             LOG.error(error, e);

+ 20 - 19
liteflow-core/src/main/java/com/yomahub/liteflow/parser/JsonFlowParser.java

@@ -19,14 +19,15 @@ import java.util.*;
 
 /**
  * Json格式解析器
+ *
  * @author guodongqing
  * @since 2.5.0
  */
-public abstract class JsonFlowParser extends FlowParser{
+public abstract class JsonFlowParser extends FlowParser {
 
     private final Logger LOG = LoggerFactory.getLogger(JsonFlowParser.class);
 
-    public void parse(String content) throws Exception{
+    public void parse(String content) throws Exception {
         parse(ListUtil.toList(content));
     }
 
@@ -37,7 +38,7 @@ public abstract class JsonFlowParser extends FlowParser{
         }
 
         List<JSONObject> jsonObjectList = ListUtil.toList();
-        for (String content : contentList){
+        for (String content : contentList) {
             //把字符串原生转换为json对象,如果不加第二个参数OrderedField,会无序
             JSONObject flowJsonObject = JSONObject.parseObject(content, Feature.OrderedField);
             jsonObjectList.add(flowJsonObject);
@@ -49,22 +50,22 @@ public abstract class JsonFlowParser extends FlowParser{
     //json格式,解析过程
     public void parseJsonObject(List<JSONObject> flowJsonObjectList) throws Exception {
         try {
-            for (JSONObject flowJsonObject : flowJsonObjectList){
+            for (JSONObject flowJsonObject : flowJsonObjectList) {
                 //判断是以spring方式注册节点,还是以json方式注册
-                if(ComponentScanner.nodeComponentMap.isEmpty()){
+                if (ComponentScanner.nodeComponentMap.isEmpty()) {
                     JSONArray nodeArrayList = flowJsonObject.getJSONObject("flow").getJSONObject("nodes").getJSONArray("node");
-                    String id;
-                    String clazz;
-                    for(int i = 0; i< nodeArrayList.size(); i++) {
+                    String id, name, clazz;
+                    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");
-                        FlowBus.addNode(id, clazz);
+                        FlowBus.addNode(id, name, clazz);
                     }
                 } else {
-                    for(Map.Entry<String, NodeComponent> componentEntry : ComponentScanner.nodeComponentMap.entrySet()){
-                        if(!FlowBus.containNode(componentEntry.getKey())){
-                            FlowBus.addNode(componentEntry.getKey(), new Node(componentEntry.getKey(), componentEntry.getValue().getClass().getName(), componentEntry.getValue()));
+                    for (Map.Entry<String, NodeComponent> componentEntry : ComponentScanner.nodeComponentMap.entrySet()) {
+                        if (!FlowBus.containNode(componentEntry.getKey())) {
+                            FlowBus.addNode(componentEntry.getKey(), new Node(componentEntry.getValue()));
                         }
                     }
                 }
@@ -74,7 +75,7 @@ public abstract class JsonFlowParser extends FlowParser{
                 for (int i = 0; i < chainArray.size(); i++) {
                     JSONObject jsonObject = chainArray.getJSONObject(i);
                     String chainName = jsonObject.getString("name");
-                    if (!FlowBus.containChain(chainName)){
+                    if (!FlowBus.containChain(chainName)) {
                         parseOneChain(jsonObject, flowJsonObjectList);
                     }
                 }
@@ -88,7 +89,7 @@ public abstract class JsonFlowParser extends FlowParser{
     /**
      * 解析一个chain的过程
      */
-    private void parseOneChain(JSONObject chainObject, List<JSONObject> flowJsonObjectList) throws Exception{
+    private void parseOneChain(JSONObject chainObject, List<JSONObject> flowJsonObjectList) throws Exception {
         String condArrayStr;
         String[] condArray;
         List<Executable> chainNodeList;
@@ -154,7 +155,7 @@ public abstract class JsonFlowParser extends FlowParser{
             condition.setNodeList(chainNodeList);
             super.buildBaseFlowConditions(conditionList, condition);
         }
-        FlowBus.addChain(chainName, new Chain(chainName,conditionList));
+        FlowBus.addChain(chainName, new Chain(chainName, conditionList));
     }
 
     /**
@@ -162,14 +163,14 @@ public abstract class JsonFlowParser extends FlowParser{
      * 因为chain和node都是可执行器,在一个规则文件上,有可能是node,有可能是chain
      */
     private boolean hasChain(List<JSONObject> flowJsonObjectList, String chainName) throws Exception {
-        for (JSONObject jsonObject : flowJsonObjectList){
+        for (JSONObject jsonObject : flowJsonObjectList) {
             JSONArray chainArray = jsonObject.getJSONObject("flow").getJSONArray("chain");
-            for(int i=0; i<chainArray.size(); i++) {
+            for (int i = 0; i < chainArray.size(); i++) {
                 JSONObject chainObject = chainArray.getJSONObject(i);
-                if (chainObject.getString("name").equals(chainName) && !FlowBus.containChain(chainName)){
+                if (chainObject.getString("name").equals(chainName) && !FlowBus.containChain(chainName)) {
                     parseOneChain(chainObject, flowJsonObjectList);
                     return true;
-                }else if(FlowBus.containChain(chainName)){
+                } else if (FlowBus.containChain(chainName)) {
                     return true;
                 }
             }

+ 7 - 7
liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java

@@ -33,7 +33,7 @@ public abstract class XmlFlowParser extends FlowParser {
 
     private final Logger LOG = LoggerFactory.getLogger(XmlFlowParser.class);
 
-    public void parse(String content) throws Exception{
+    public void parse(String content) throws Exception {
         parse(ListUtil.toList(content));
     }
 
@@ -58,17 +58,17 @@ public abstract class XmlFlowParser extends FlowParser {
                 if (ComponentScanner.nodeComponentMap.isEmpty()) {
                     // 解析node节点
                     List<Element> nodeList = rootElement.element("nodes").elements("node");
-                    String id;
-                    String clazz;
+                    String id, name, clazz;
                     for (Element e : nodeList) {
                         id = e.attributeValue("id");
+                        name = e.attributeValue("name");
                         clazz = e.attributeValue("class");
-                        FlowBus.addNode(id, clazz);
+                        FlowBus.addNode(id, name, clazz);
                     }
                 } else {
                     for (Entry<String, NodeComponent> componentEntry : ComponentScanner.nodeComponentMap.entrySet()) {
                         if (!FlowBus.containNode(componentEntry.getKey())) {
-                            FlowBus.addNode(componentEntry.getKey(), new Node(componentEntry.getKey(), componentEntry.getValue().getClass().getName(), componentEntry.getValue()));
+                            FlowBus.addNode(componentEntry.getKey(), new Node(componentEntry.getValue()));
                         }
                     }
                 }
@@ -77,7 +77,7 @@ public abstract class XmlFlowParser extends FlowParser {
                 List<Element> chainList = rootElement.elements("chain");
                 for (Element e : chainList) {
                     String chainName = e.attributeValue("name");
-                    if (!FlowBus.containChain(chainName)){
+                    if (!FlowBus.containChain(chainName)) {
                         parseOneChain(e, documentList);
                     }
                 }
@@ -165,7 +165,7 @@ public abstract class XmlFlowParser extends FlowParser {
     //因为chain和node都是可执行器,在一个规则文件上,有可能是node,有可能是chain
     @SuppressWarnings("unchecked")
     private boolean hasChain(List<Document> documentList, String chainName) throws Exception {
-        for(Document document : documentList){
+        for (Document document : documentList) {
             List<Element> chainList = document.getRootElement().elements("chain");
             for (Element ce : chainList) {
                 String ceName = ce.attributeValue("name");

+ 14 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java

@@ -7,6 +7,9 @@
  */
 package com.yomahub.liteflow.spring;
 
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import com.yomahub.liteflow.annotation.LiteflowComponent;
 import com.yomahub.liteflow.aop.ICmpAroundAspect;
 import com.yomahub.liteflow.core.NodeComponent;
 import com.yomahub.liteflow.util.LOGOPrinter;
@@ -49,6 +52,17 @@ public class ComponentScanner implements BeanPostProcessor {
 			LOG.info("component[{}] has been found", beanName);
 			NodeComponent nodeComponent = (NodeComponent) bean;
 			nodeComponent.setNodeId(beanName);
+
+			//判断NodeComponent是否是标识了@LiteflowComponent的标注
+			//如果标注了,那么要从中取到name字段
+			LiteflowComponent liteflowComponent = bean.getClass().getAnnotation(LiteflowComponent.class);
+			if (ObjectUtil.isNotNull(liteflowComponent)){
+				String name = liteflowComponent.name();
+				if (StrUtil.isNotBlank(name)){
+					nodeComponent.setName(name);
+				}
+			}
+
 			nodeComponent.setSelf(nodeComponent);
 			nodeComponentMap.put(beanName, nodeComponent);
 		}