1 2 3 package org.quilt.graph; 4 5 import java.util.HashSet ; 6 7 14 15 public class Walker { 16 17 private Directed graph = null; 18 private Entry entry = null; 19 private Exit exit = null; 20 private Visitor visitor = null; 21 private HashSet vertices = new HashSet (); 22 private HashSet edges = new HashSet (); 23 24 25 public Walker() { } 26 27 35 public Exit visit (Directed g, Visitor guest) { 36 Directed.checkForNull(g, "graph"); 37 Directed.checkForNull(guest, "Visitor"); 38 graph = g; 39 entry = g.getEntry(); 40 exit = g.getExit(); 41 visitor = guest; 42 visitor.discoverGraph(graph); 43 visitVertex( (Vertex) graph.getEntry() ); 44 visitor.finishGraph(graph); 45 return exit; 46 } 47 48 53 private void visitVertex (Vertex v) { 54 Directed.checkForNull(v, "vertex"); 55 if (vertices.contains(v)) { 56 return; 58 } 59 vertices.add(v); 61 if ( v == exit) { 62 visitor.discoverVertex(v); 64 Edge e = v.getEdge(); 68 if (e.getTarget().getGraph() == graph) { 69 visitEdge (v.getEdge()); 70 } 71 visitor.finishVertex(v); 72 } else { 73 if (v != entry && v instanceof Entry) { 74 Walker subWalker = new Walker(); 76 Exit subExit = subWalker.visit (v.getGraph(), visitor); 77 visitVertex ( 78 ((UnaryConnector)subExit.getConnector()) 79 .getEdge().getTarget() ); 80 } else { 81 visitor.discoverVertex(v); Connector conn = v.getConnector(); 86 visitEdge ( conn.getEdge() ); 88 if (conn instanceof BinaryConnector) { 89 visitEdge ( ((BinaryConnector)conn).getOtherEdge()); 90 } else if (conn instanceof ComplexConnector) { 91 int size = conn.size(); 92 for (int i = 0; i < size; i++) { 93 visitEdge ( ((ComplexConnector)conn).getEdge(i)); 94 } 95 } else if (conn instanceof UnaryConnector) { 96 } else if (conn instanceof MultiConnector) { 98 for (int i = 1; i < conn.size(); i++) { 100 visitEdge ( ((MultiConnector)conn).getEdge(i)); 101 } 102 } else { 103 System.out.println ( 104 "Walker.visitVertex: INTERNAL ERROR\n" + 105 " don't know how to handle this kind of connection"); 106 } 107 visitor.finishVertex(v); 108 } 109 } 110 } 111 114 private void visitEdge(Edge e) { 115 Directed.checkForNull(e, "edge"); 116 if (edges.contains(e)) { 117 return; } 119 edges.add(e); 121 visitor.discoverEdge(e); 122 Vertex target = e.getTarget(); 123 Directed.checkForNull(target, "edge target"); 124 125 if ( target instanceof Entry ) { 127 Vertex source = e.getSource(); 130 Directed sourceGraph = source.getGraph(); 131 Directed targetGraph = e.getTarget().getGraph(); 132 if (sourceGraph != targetGraph 133 && targetGraph != sourceGraph.getParent() ) { 134 visitVertex(target); 135 } 136 } else if ( target instanceof Exit ) { 137 Directed sourceGraph = e.getSource().getGraph(); 140 Directed targetGraph = e.getTarget().getGraph(); 141 if (sourceGraph == targetGraph ) { 142 visitVertex(target); 143 } 144 } else { 145 visitVertex(target); 146 } 147 visitor.finishEdge(e); 148 } 149 } 150 151 | Popular Tags |