001: /* Vertex.java */
002:
003: package org.quilt.graph;
004:
005: /**
006: * A vertex in a directed graph.
007: *
008: * @author < a href="jddixon@users.sourceforge.net">Jim Dixon</a>
009: */
010: public class Vertex {
011:
012: /** Unique non-negative assigned to the Vertex; -1 means 'unassigned' */
013: protected int index = -1;
014: /** The graph this vertex belongs to. */
015: protected Directed graph = null;
016: /** Connects this vertex to one or more other vertices. */
017: protected Connector connector = null;
018: /** Optional label. */
019: protected String label_ = null;
020:
021: /** Creates a vertex without an index and belonging to no graph. */
022: protected Vertex() {
023: }
024:
025: /**
026: * Creates a vertex belonging to a graph, assigns an index unique
027: * within this graph.
028: * @param g The graph the vertex belongs to.
029: */
030: public Vertex(Directed g) {
031: checkForNull(g, "graph");
032: graph = g;
033: index = g.anotherVertex(this );
034: }
035:
036: // ACCESSOR METHODS /////////////////////////////////////////////
037: public Connector getConnector() {
038: return connector;
039: }
040:
041: public void setConnector(Connector c) {
042: checkForNull(c, "connector");
043: connector = c;
044: }
045:
046: public Edge getEdge() {
047: if (connector == null) {
048: return null;
049: } else {
050: return connector.getEdge();
051: }
052: }
053:
054: public Vertex getTarget() {
055: if (connector == null) {
056: return null;
057: } else {
058: return connector.getTarget();
059: }
060: }
061:
062: /** Get the graph this vertex is in. */
063: public Directed getGraph() {
064: return graph;
065: }
066:
067: /** @return Vertex index, a non-negative integer. */
068: public int getIndex() {
069: return index;
070: }
071:
072: /** @return String label or null */
073: public String getLabel() {
074: return label_;
075: }
076:
077: /** Assign a label to the Vertex. */
078: public void setLabel(String s) {
079: label_ = s;
080: }
081:
082: // CONNECTOR CONVERTERS //////////////////////////////////////////
083: /**
084: * Convert the existing connector to a BinaryConnector.
085: *
086: * @return The 'other' edge created.
087: */
088: public Edge makeBinary() {
089: Edge otherEdge = new Edge(this , graph.getExit());
090: connector = new BinaryConnector(connector, otherEdge);
091: return otherEdge;
092: }
093:
094: /**
095: * Convert the exiting connector to a ComplexConnector, using the
096: * existing Edge as seed.
097: */
098: public ComplexConnector makeComplex(int n) {
099: // rely on range check in constructor;
100: connector = new ComplexConnector(connector, n);
101: return (ComplexConnector) connector;
102: }
103:
104: /**
105: * Convert the exiting connector to a MultiConnector, using the
106: * existing Edge as seed.
107: */
108: public MultiConnector makeMulti(int n) {
109: // rely on range check in constructor;
110: connector = new MultiConnector(connector, n);
111: return (MultiConnector) connector;
112: }
113:
114: // UTILITY FUNCTIONS ////////////////////////////////////////////
115:
116: /**
117: * Is the graph a parent, grandparent of this vertex?
118: *
119: * @param g Candidate progenitor.
120: * @return True if match is found.
121: */
122: public boolean above(final Directed g) {
123: // DEBUG
124: System.out.println("above: checking whether graph "
125: + g.getIndex() + " is above vertex " + toString()
126: + " whose parent is graph "
127: + getGraph().getParent().getIndex());
128: // END
129: if (g == null || g == graph) {
130: return false;
131: }
132:
133: // search upward through parent graphs
134: for (Directed pop = graph.getParent(); pop != null; pop = pop
135: .getParent()) {
136: // DEBUG
137: System.out.println(" checking whether graph "
138: + g.getIndex() + " is the same as graph "
139: + pop.getIndex());
140: // END
141: if (pop == g) {
142: return true;
143: }
144: }
145: return false;
146: }
147:
148: /**
149: * Throw an exception if the argument is null.
150: *
151: * @param o Argument being checked
152: * @param what What it is - for error message.
153: */
154: public static void checkForNull(Object o, String what) {
155: if (o == null) {
156: throw new IllegalArgumentException("null " + what);
157: }
158: }
159:
160: /**
161: * @return A String in parent-index:my-index form.
162: */
163: public String toString() {
164: StringBuffer sb = new StringBuffer().append(graph.getIndex())
165: .append(":").append(index);
166: return sb.toString();
167: }
168: }
|