1 2 package org.quilt.graph; 3 4 27 public class Directed { 28 29 30 private Entry entry = null; 31 32 private Exit exit = null; 33 34 35 protected static int graphIndex = 0; 36 37 private int index = 0; 38 39 40 private Directed parent_ = null; 41 private int depth = 0; 42 private int vCount = 0; private int eCount = 0; 44 45 56 public Directed ( ) { 57 index = graphIndex = 0; 58 entry = new Entry(this); exit = (Exit) entry.getTarget(); 60 } 61 62 public Directed getParent() { 63 return parent_; 64 } 65 66 public int getIndex() { 67 return index; 68 } 69 76 protected Directed (Directed parent) { 77 index = ++ graphIndex; 78 entry = new Entry(this); exit = (Exit) entry.getTarget(); 80 checkForNull (parent, "parent"); 81 depth = parent.getDepth() + 1; 82 parent_ = parent; 83 } 84 92 final static protected Directed connectSubgraph (final Directed subgraph, 93 final Edge e, final int n) { 94 checkForNull(e, "edge"); 95 if ( n < 1 ) { 96 throw new IllegalArgumentException ( 97 "out of range argument"); 98 } 99 Entry subEntry = subgraph.getEntry(); 101 subEntry.setConnector( 102 new ComplexConnector ( subEntry.getConnector(), n) ); 103 104 Vertex target = e.getTarget(); e.setTarget(subEntry); 108 subgraph.getExit().getEdge().setTarget(target); 110 return subgraph; 111 } 112 121 public Directed subgraph (final Edge e, final int n) { 122 return connectSubgraph (new Directed(this), e, n); 123 } 124 126 public int getDepth() { 127 return depth; 128 } 129 130 public Entry getEntry () { 131 return entry; 132 } 133 134 public Exit getExit() { 135 return exit; 136 } 137 public static void checkForNull (Object o, String what) { 139 if (o == null) { 140 throw new IllegalArgumentException ("null " + what); 141 } 142 } 143 147 public int anotherEdge ( Edge e ) { 148 return (eCount++); 149 } 150 154 public int anotherVertex( Vertex v ) { 155 return (vCount++); 156 } 157 165 final protected Vertex insertVertex (Vertex v, Edge e) { 166 checkForNull (e, "edge"); 167 Vertex source = e.getSource(); 168 if ( ! (source instanceof Exit) && source.getGraph() != this) { 169 System.out.println("Directed.insertVertex:" 171 + "\n vertex: " + v 172 + "\n edge: " + e); 173 throw new IllegalArgumentException ("edge not in this graph"); 175 } 176 Vertex target = e.getTarget(); 177 e.setTarget(v); 178 v.setConnector ( new UnaryConnector (new Edge(v, target))); 179 return v; 180 } 181 185 public Vertex insertVertex (Edge e) { 186 return insertVertex (new Vertex(this), e); 187 } 188 189 private class Sizer implements Visitor { 190 private int graphCount = 0; 191 private int maxDepth = -1; 192 private int vertexCount = 0; 193 private int edgeCount = 0; 194 195 public Sizer() { } 196 197 public void discoverGraph (Directed g) { 198 checkForNull (g, "graph"); 199 int depth = g.getDepth() + 1; 200 graphCount++; 201 if (depth > maxDepth) { 202 maxDepth = depth; 203 } 204 } 205 public void finishGraph (Directed g) { } 206 public void discoverVertex(Vertex v) { 207 checkForNull(v, "vertex"); 208 vertexCount++; 209 } 210 public void finishVertex (Vertex v) { } 211 public void discoverEdge (Edge e) { 212 checkForNull (e, "edge"); 213 edgeCount++; 214 } 215 public void finishEdge (Edge e) { } 216 public int getGraphCount () { return graphCount; } 218 public int getMaxDepth () { return maxDepth; } 219 public int getVertexCount () { return vertexCount; } 220 public int getEdgeCount () { return edgeCount; } 221 } 222 public int size() { 223 Walker johnny = new Walker(); 224 Sizer counter = new Sizer(); 225 johnny.visit (this, counter); 227 return counter.getVertexCount(); 228 } 229 230 240 public Entry closestEntry (final Directed g) { 241 if (g == null) { 246 throw new IllegalArgumentException ("null argument"); 247 } 248 if ( g == this ) { 249 return null; 250 } 251 Directed hisGraph; 252 for (hisGraph = g; hisGraph != null; 253 hisGraph = hisGraph.getParent() ) { 254 if (hisGraph.getParent() == this) { 263 return hisGraph.getEntry(); 269 } 270 } 271 return null; 272 } 273 } 274 | Popular Tags |