前端在绘制流程图的时候,某些情况需要对某个节点之后的流程图进行折叠,因此需要得到某个节点的ID后,计算出这个ID下游之后的所有节点(找到的节点,边也就找到了)
某个节点的ID,流程图解析成对应的JSON对象文件(有的是将流程图解析成XML文件)
例如:
- {
- "nodes": [
- {
- "id": "A"
- },
- {
- "id": "B"
- },
- {
- "id": "C"
- }
- ],
- "edges": [
- {
- "sourceNode": "A",
- "targetNode": "B"
- },
- {
- "sourceNode": "B",
- "targetNode": "C"
- }
- ]
- }
- import com.alibaba.fastjson2.JSONArray;
- import com.alibaba.fastjson2.JSONObject;
-
- import java.util.*;
-
- public class FlowChartDFS {
-
- // 定义节点类
- static class Node {
- public String id; // 节点ID
- public Set
nextNodes = new HashSet<>(); // 存放下游节点id的集合 -
- public Node(String id) {
- this.id = id;
- }
- }
-
- // 定义边类
- static class Edge {
- public String sourceNode; // 边的起始节点ID
- public String targetNode; // 边的目标节点ID
-
- public Edge(String sourceNode, String targetNode) {
- this.sourceNode = sourceNode;
- this.targetNode = targetNode;
- }
- }
-
- // 解析JSON数据,构建节点和边的关系图
- public static Map
buildGraphFromJson(String jsonStr) { -
- JSONObject flowChartJson = JSONObject.parseObject(jsonStr);
- JSONArray nodesJson = flowChartJson.getJSONArray("nodes");
- JSONArray edgesJson = flowChartJson.getJSONArray("edges");
-
- Map
graph = new HashMap<>(); - for (int i = 0; i < nodesJson.size(); i++) {
- JSONObject nodeJson = nodesJson.getJSONObject(i);
- String nodeId = nodeJson.getString("id");
- Node node = new Node(nodeId);
- graph.put(nodeId, node);
- }
- for (int i = 0; i < edgesJson.size(); i++) {
- JSONObject edgeJson = edgesJson.getJSONObject(i);
- String sourceNodeId = edgeJson.getString("sourceNode");
- String targetNodeId = edgeJson.getString("targetNode");
- Node sourceNode = graph.get(sourceNodeId);
- sourceNode.nextNodes.add(targetNodeId);
- }
- return graph;
- }
-
- // DFS深度优先递归搜索指定节点的下游所有节点和边
- public static void dfs(String startNodeId, Set
visitedNodeIds, List edges, Map graph) { - Node node = graph.get(startNodeId);
- if (node == null || visitedNodeIds.contains(startNodeId)) {
- return;
- }
- visitedNodeIds.add(startNodeId);
- for (String nextNodeId : node.nextNodes) {
- Edge edge = new Edge(startNodeId, nextNodeId); // 记录边的信息
- edges.add(edge);
- // 递归循环
- dfs(nextNodeId, visitedNodeIds, edges, graph);
- }
- }
-
- public static void main(String[] args) {
- // JSON数据示例
- String jsonStr = "{\"nodes\": [{\"id\": \"A\"},{\"id\": \"B\"},{\"id\": \"C\"}], \"edges\": [{\"sourceNode\": \"A\", \"targetNode\": \"B\"},{\"sourceNode\": \"B\",\"targetNode\": \"C\"}]}";
-
- // 构建关系图
- Map
graph = buildGraphFromJson(jsonStr); -
- // 指定起始节点ID
- String startNodeId = "A";
-
- // 初始化已访问节点集合和边列表
- Set
visitedNodeIds = new HashSet<>(); - List
edges = new ArrayList<>(); -
- // 进行DFS深度优先搜索
- dfs(startNodeId, visitedNodeIds, edges, graph);
-
- // 输出搜索结果
- System.out.println("节点" + startNodeId + "的下游节点和边:");
- for (Edge edge : edges) {
- System.out.println(edge.sourceNode + "->" + edge.targetNode);
- }
- }
- }
- <dependency>
- <groupId>com.google.code.gsongroupId>
- <artifactId>gsonartifactId>
- <version>2.10.1version>
- dependency>
- Gson gson = new Gson();
- String jsonString = gson.toJson();