diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java b/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java index df3ba483f6787..153c38f2592a3 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/DAGraph.java @@ -167,9 +167,8 @@ public void visitNode(U node) { } @Override - public void visitEdge(String fromKey, String toKey, GraphEdgeType edgeType) { - System.out.println("{" + fromKey + ", " + toKey + "} " + edgeType); - if (edgeType == GraphEdgeType.BACK) { + public void visitEdge(String fromKey, String toKey, EdgeType edgeType) { + if (edgeType == EdgeType.BACK) { throw new IllegalStateException("Detected circular dependency: " + findPath(fromKey, toKey)); } } diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/Graph.java b/azure-client-runtime/src/main/java/com/microsoft/azure/Graph.java index 17440ddbeb193..89087dd6d3580 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/Graph.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/Graph.java @@ -12,16 +12,6 @@ import java.util.Map; import java.util.Set; -/** - * The edge types in a graph. - */ -enum GraphEdgeType { - TREE, - FORWARD, - BACK, - CROSS -} - /** * Type representing a directed graph data structure. *

@@ -61,30 +51,6 @@ public void addNode(U node) { graph.put(node.key(), node); } - /** - * Represents a visitor to be implemented by the consumer who want to visit the - * graph's nodes in DFS order. - * - * @param the type of the node - */ - interface Visitor { - /** - * visit a node. - * - * @param node the node to visited - */ - void visitNode(U node); - - /** - * visit an edge. - * - * @param fromKey key of the from node - * @param toKey key of the to node - * @param graphEdgeType the edge type - */ - void visitEdge(String fromKey, String toKey, GraphEdgeType graphEdgeType); - } - /** * Perform DFS visit in this graph. *

@@ -128,22 +94,22 @@ private void dfs(Visitor visitor, Node node) { processed.add(fromKey); } - private GraphEdgeType edgeType(String fromKey, String toKey) { + private EdgeType edgeType(String fromKey, String toKey) { if (parent.containsKey(toKey) && parent.get(toKey).equals(fromKey)) { - return GraphEdgeType.TREE; + return EdgeType.TREE; } if (visited.contains(toKey) && !processed.contains(toKey)) { - return GraphEdgeType.BACK; + return EdgeType.BACK; } if (processed.contains(toKey) && entryTime.containsKey(toKey) && entryTime.containsKey(fromKey)) { if (entryTime.get(toKey) > entryTime.get(fromKey)) { - return GraphEdgeType.FORWARD; + return EdgeType.FORWARD; } if (entryTime.get(toKey) < entryTime.get(fromKey)) { - return GraphEdgeType.CROSS; + return EdgeType.CROSS; } } @@ -157,4 +123,50 @@ protected String findPath(String start, String end) { return findPath(start, parent.get(end)) + " -> " + end; } } + + /** + * The edge types in a graph. + */ + enum EdgeType { + /** + * An edge (u, v) is a tree edge if v is visited the first time. + */ + TREE, + /** + * An edge (u, v) is a forward edge if v is descendant of u. + */ + FORWARD, + /** + * An edge (u, v) is a back edge if v is ancestor of u. + */ + BACK, + /** + * An edge (u, v) is a cross edge if v is neither ancestor or descendant of u. + */ + CROSS + } + + /** + * Represents a visitor to be implemented by the consumer who want to visit the + * graph's nodes in DFS order by calling visit method. + * + * @param the type of the node + */ + interface Visitor { + /** + * visit a node. + * + * @param node the node to visited + */ + void visitNode(U node); + + /** + * visit an edge. + * + * @param fromKey key of the from node + * @param toKey key of the to node + * @param edgeType the edge type + */ + void visitEdge(String fromKey, String toKey, EdgeType edgeType); + } }