@Test public void testFindNodeDepDag() { FlowDag flowDag = new FlowDag(); List<FlowNodeRelation> relas = new ArrayList<>(); relas.add(new FlowNodeRelation("shell1", "shell2")); relas.add(new FlowNodeRelation("shell1", "shell3")); relas.add(new FlowNodeRelation("shell2", "shell4")); relas.add(new FlowNodeRelation("shell4", "shell5")); flowDag.setEdges(relas); List<FlowNode> nodes = new ArrayList<>(); nodes.add(JsonUtil.parseObject("{\"name\":\"shell1\"}", FlowNode.class)); nodes.add(JsonUtil.parseObject("{\"name\":\"shell2\"}", FlowNode.class)); nodes.add(JsonUtil.parseObject("{\"name\":\"shell3\"}", FlowNode.class)); nodes.add(JsonUtil.parseObject("{\"name\":\"shell4\"}", FlowNode.class)); nodes.add(JsonUtil.parseObject("{\"name\":\"shell5\"}", FlowNode.class)); flowDag.setNodes(nodes); FlowNode node = JsonUtil.parseObject("{\"name\":\"shell2\"}", FlowNode.class); FlowDag flowDag1 = DagHelper.findNodeDepDag(flowDag, node, true); assertEquals(3, flowDag1.getNodes().size()); assertEquals("shell2:shell4,shell4:shell5", flowDag1.getEdges().stream().map(rela -> rela.getStartNode() + ":" + rela.getEndNode()).collect(Collectors.joining(","))); FlowDag flowDagPre = DagHelper.findNodeDepDag(flowDag, node, false); assertEquals("shell1,shell2", flowDagPre.getNodes().stream().map(n -> n.getName()).sorted().collect(Collectors.joining(","))); assertEquals("shell1:shell2", flowDagPre.getEdges().stream().map(rela -> rela.getStartNode() + ":" + rela.getEndNode()).collect(Collectors.joining(","))); }
public static FlowDag findNodeDepDag(FlowDag flowDag, FlowNode node, boolean isDependentBy) { FlowDag dagRequestDep = new FlowDag(); List<FlowNode> nodesDep = new ArrayList<>(); dagRequestDep.setNodes(nodesDep); List<FlowNodeRelation> edgesDep = new ArrayList<>(); dagRequestDep.setEdges(edgesDep); List<FlowNode> nodesTemp = new ArrayList<>(); List<FlowNode> nodesTempSwap = new ArrayList<>(); List<FlowNodeRelation> edges = flowDag.getEdges(); nodesTemp.add(node); while (!nodesTemp.isEmpty()) { Iterator<FlowNode> iterator = nodesTemp.iterator(); while (iterator.hasNext()) { FlowNode flowNode = iterator.next(); for (FlowNodeRelation edge : edges) { if (isDependentBy) { if (edge.getStartNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode endFlowNode = findNodeByName(flowDag.getNodes(), edge.getEndNode()); if (endFlowNode != null) { if (!nodesDep.contains(endFlowNode)) { nodesTempSwap.add(endFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getEndNode())); } } } else { if (edge.getEndNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode startFlowNode = findNodeByName(flowDag.getNodes(), edge.getStartNode()); if (startFlowNode != null) { if (!nodesDep.contains(startFlowNode)) { nodesTempSwap.add(startFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getStartNode())); } } } } nodesDep.add(flowNode); } nodesTemp.clear(); nodesTemp.addAll(nodesTempSwap); nodesTempSwap.clear(); } return dagRequestDep; }
DagHelper { public static FlowDag findNodeDepDag(FlowDag flowDag, FlowNode node, boolean isDependentBy) { FlowDag dagRequestDep = new FlowDag(); List<FlowNode> nodesDep = new ArrayList<>(); dagRequestDep.setNodes(nodesDep); List<FlowNodeRelation> edgesDep = new ArrayList<>(); dagRequestDep.setEdges(edgesDep); List<FlowNode> nodesTemp = new ArrayList<>(); List<FlowNode> nodesTempSwap = new ArrayList<>(); List<FlowNodeRelation> edges = flowDag.getEdges(); nodesTemp.add(node); while (!nodesTemp.isEmpty()) { Iterator<FlowNode> iterator = nodesTemp.iterator(); while (iterator.hasNext()) { FlowNode flowNode = iterator.next(); for (FlowNodeRelation edge : edges) { if (isDependentBy) { if (edge.getStartNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode endFlowNode = findNodeByName(flowDag.getNodes(), edge.getEndNode()); if (endFlowNode != null) { if (!nodesDep.contains(endFlowNode)) { nodesTempSwap.add(endFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getEndNode())); } } } else { if (edge.getEndNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode startFlowNode = findNodeByName(flowDag.getNodes(), edge.getStartNode()); if (startFlowNode != null) { if (!nodesDep.contains(startFlowNode)) { nodesTempSwap.add(startFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getStartNode())); } } } } nodesDep.add(flowNode); } nodesTemp.clear(); nodesTemp.addAll(nodesTempSwap); nodesTempSwap.clear(); } return dagRequestDep; } }
DagHelper { public static FlowDag findNodeDepDag(FlowDag flowDag, FlowNode node, boolean isDependentBy) { FlowDag dagRequestDep = new FlowDag(); List<FlowNode> nodesDep = new ArrayList<>(); dagRequestDep.setNodes(nodesDep); List<FlowNodeRelation> edgesDep = new ArrayList<>(); dagRequestDep.setEdges(edgesDep); List<FlowNode> nodesTemp = new ArrayList<>(); List<FlowNode> nodesTempSwap = new ArrayList<>(); List<FlowNodeRelation> edges = flowDag.getEdges(); nodesTemp.add(node); while (!nodesTemp.isEmpty()) { Iterator<FlowNode> iterator = nodesTemp.iterator(); while (iterator.hasNext()) { FlowNode flowNode = iterator.next(); for (FlowNodeRelation edge : edges) { if (isDependentBy) { if (edge.getStartNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode endFlowNode = findNodeByName(flowDag.getNodes(), edge.getEndNode()); if (endFlowNode != null) { if (!nodesDep.contains(endFlowNode)) { nodesTempSwap.add(endFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getEndNode())); } } } else { if (edge.getEndNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode startFlowNode = findNodeByName(flowDag.getNodes(), edge.getStartNode()); if (startFlowNode != null) { if (!nodesDep.contains(startFlowNode)) { nodesTempSwap.add(startFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getStartNode())); } } } } nodesDep.add(flowNode); } nodesTemp.clear(); nodesTemp.addAll(nodesTempSwap); nodesTempSwap.clear(); } return dagRequestDep; }  }
DagHelper { public static FlowDag findNodeDepDag(FlowDag flowDag, FlowNode node, boolean isDependentBy) { FlowDag dagRequestDep = new FlowDag(); List<FlowNode> nodesDep = new ArrayList<>(); dagRequestDep.setNodes(nodesDep); List<FlowNodeRelation> edgesDep = new ArrayList<>(); dagRequestDep.setEdges(edgesDep); List<FlowNode> nodesTemp = new ArrayList<>(); List<FlowNode> nodesTempSwap = new ArrayList<>(); List<FlowNodeRelation> edges = flowDag.getEdges(); nodesTemp.add(node); while (!nodesTemp.isEmpty()) { Iterator<FlowNode> iterator = nodesTemp.iterator(); while (iterator.hasNext()) { FlowNode flowNode = iterator.next(); for (FlowNodeRelation edge : edges) { if (isDependentBy) { if (edge.getStartNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode endFlowNode = findNodeByName(flowDag.getNodes(), edge.getEndNode()); if (endFlowNode != null) { if (!nodesDep.contains(endFlowNode)) { nodesTempSwap.add(endFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getEndNode())); } } } else { if (edge.getEndNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode startFlowNode = findNodeByName(flowDag.getNodes(), edge.getStartNode()); if (startFlowNode != null) { if (!nodesDep.contains(startFlowNode)) { nodesTempSwap.add(startFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getStartNode())); } } } } nodesDep.add(flowNode); } nodesTemp.clear(); nodesTemp.addAll(nodesTempSwap); nodesTempSwap.clear(); } return dagRequestDep; }  static FlowDag findNodeDepDag(FlowDag flowDag, FlowNode node, boolean isDependentBy); static FlowNode findNodeByName(List<FlowNode> nodeDetails, String nodeName); }
DagHelper { public static FlowDag findNodeDepDag(FlowDag flowDag, FlowNode node, boolean isDependentBy) { FlowDag dagRequestDep = new FlowDag(); List<FlowNode> nodesDep = new ArrayList<>(); dagRequestDep.setNodes(nodesDep); List<FlowNodeRelation> edgesDep = new ArrayList<>(); dagRequestDep.setEdges(edgesDep); List<FlowNode> nodesTemp = new ArrayList<>(); List<FlowNode> nodesTempSwap = new ArrayList<>(); List<FlowNodeRelation> edges = flowDag.getEdges(); nodesTemp.add(node); while (!nodesTemp.isEmpty()) { Iterator<FlowNode> iterator = nodesTemp.iterator(); while (iterator.hasNext()) { FlowNode flowNode = iterator.next(); for (FlowNodeRelation edge : edges) { if (isDependentBy) { if (edge.getStartNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode endFlowNode = findNodeByName(flowDag.getNodes(), edge.getEndNode()); if (endFlowNode != null) { if (!nodesDep.contains(endFlowNode)) { nodesTempSwap.add(endFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getEndNode())); } } } else { if (edge.getEndNode().equals(flowNode.getName())) { edgesDep.add(edge); FlowNode startFlowNode = findNodeByName(flowDag.getNodes(), edge.getStartNode()); if (startFlowNode != null) { if (!nodesDep.contains(startFlowNode)) { nodesTempSwap.add(startFlowNode); } } else { throw new DagException(String.format("DAG error ，end node %s not in flow dag", edge.getStartNode())); } } } } nodesDep.add(flowNode); } nodesTemp.clear(); nodesTemp.addAll(nodesTempSwap); nodesTempSwap.clear(); } return dagRequestDep; }  static FlowDag findNodeDepDag(FlowDag flowDag, FlowNode node, boolean isDependentBy); static FlowNode findNodeByName(List<FlowNode> nodeDetails, String nodeName);  }