6 public class GraphNode {
8 public static boolean useEdgeLabels;
10 /* NodeStatus enumeration pattern ***********/
12 public static final NodeStatus UNVISITED = new NodeStatus("UNVISITED");
13 public static final NodeStatus PROCESSING = new NodeStatus("PROCESSING");
14 public static final NodeStatus FINISHED = new NodeStatus("FINISHED");
16 public static class NodeStatus {
17 private static String name;
18 private NodeStatus(String name) { this.name = name; }
19 public String toString() { return name; }
22 /* Edge *****************/
24 public static class Edge {
27 private GraphNode target;
28 private String dotnodeparams = new String();
30 public Edge(String label, GraphNode target) {
35 public String getLabel() {
39 public GraphNode getTarget() {
43 public void setDotNodeParameters(String param) {
45 throw new NullPointerException();
47 if (param.length() > 0) {
48 dotnodeparams = "," + param;
50 dotnodeparams = new String();
56 int discoverytime = -1;
57 int finishingtime = -1; /* used for searches */
58 Vector edges = new Vector();
61 NodeStatus status = UNVISITED;
62 String dotnodeparams = new String();
65 public GraphNode(String label) {
66 this.nodelabel = label;
67 this.textlabel = label;
70 public GraphNode(String label, Object owner) {
71 this.nodelabel = label;
72 this.textlabel = label;
76 public GraphNode(String label, String textlabel, Object owner) {
77 this.nodelabel = label;
78 this.textlabel = textlabel;
82 public Object getOwner() {
86 public static void computeclosure(Set nodes) {
87 Stack tovisit=new Stack();
88 tovisit.addAll(nodes);
89 while(!tovisit.isEmpty()) {
90 GraphNode gn=(GraphNode)tovisit.pop();
91 for(Iterator it=gn.edges();it.hasNext();) {
92 Edge edge=(Edge)it.next();
93 GraphNode target=edge.getTarget();
94 if (!nodes.contains(target)) {
102 public void setDotNodeParameters(String param) {
104 throw new NullPointerException();
106 if (param.length() > 0) {
107 dotnodeparams = "," + param;
109 dotnodeparams = new String();
113 public void setStatus(NodeStatus status) {
114 if (status == null) {
115 throw new NullPointerException();
117 this.status = status;
120 public String getLabel() {
124 public String getTextLabel() {
128 public NodeStatus getStatus() {
132 public Iterator edges() {
133 return edges.iterator();
136 public void addEdge(Edge newedge) {
137 edges.addElement(newedge);
140 public void reset() {
146 public void discover(int time) {
147 discoverytime = time++;
151 public void finish(int time) {
152 assert status == PROCESSING;
153 finishingtime = time++;
157 public int getFinishingTime() {
158 return finishingtime;
161 public static class DOTVisitor {
163 java.io.PrintWriter output;
167 private DOTVisitor(java.io.OutputStream output) {
170 this.output = new java.io.PrintWriter(output, true);
173 private String getNewID(String name) {
174 tokennumber = tokennumber + 1;
175 return new String (name+tokennumber);
180 public static void visit(java.io.OutputStream output, Collection nodes) {
181 DOTVisitor visitor = new DOTVisitor(output);
182 visitor.nodes = nodes;
187 private void make() {
188 output.println("digraph dotvisitor {");
189 output.println("\trotate=90;");
190 /* output.println("\tpage=\"8.5,11\";");
191 output.println("\tnslimit=1000.0;");
192 output.println("\tnslimit1=1000.0;");
193 output.println("\tmclimit=1000.0;");
194 output.println("\tremincross=true;");*/
195 output.println("\tnode [fontsize=10,height=\"0.1\", width=\"0.1\"];");
196 output.println("\tedge [fontsize=6];");
200 output.println("}\n");
203 private void traverse() {
204 Iterator i = nodes.iterator();
205 while (i.hasNext()) {
206 GraphNode gn = (GraphNode) i.next();
207 Iterator edges = gn.edges();
208 String label = gn.getTextLabel(); // + " [" + gn.discoverytime + "," + gn.finishingtime + "];";
209 output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + gn.dotnodeparams + "];");
211 while (edges.hasNext()) {
212 Edge edge = (Edge) edges.next();
213 GraphNode node = edge.getTarget();
214 if (nodes.contains(node)) {
215 String edgelabel = useEdgeLabels ? "label=\"" + edge.getLabel() + "\"" : "label=\"\"";
216 output.println("\t" + gn.getLabel() + " -> " + node.getLabel() + " [" + edgelabel + edge.dotnodeparams + "];");
224 * DFS encapsulates the depth first search algorithm
226 public static class DFS {
231 private DFS(Collection nodes) {
235 public static void depthFirstSearch(Collection nodes) {
237 throw new NullPointerException();
240 DFS dfs = new DFS(nodes);
248 i = nodes.iterator();
249 while (i.hasNext()) {
250 GraphNode gn = (GraphNode) i.next();
254 i = nodes.iterator();
255 while (i.hasNext()) {
256 GraphNode gn = (GraphNode) i.next();
257 assert gn.getStatus() != PROCESSING;
258 if (gn.getStatus() == UNVISITED) {
264 private void dfs(GraphNode gn) {
266 Iterator edges = gn.edges();
268 while (edges.hasNext()) {
269 Edge edge = (Edge) edges.next();
270 GraphNode node = edge.getTarget();
271 if (node.getStatus() == UNVISITED) {