1 package edu.berkeley.sbp.util;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
6 import java.lang.reflect.*;
7 import java.lang.ref.*;
9 public class GraphViz {
11 IdentityHashMap<ToGraphViz,Node> ihm = new IdentityHashMap<ToGraphViz,Node>();
12 HashMap<Node,Group> groups = new HashMap<Node,Group>();
16 public void add(Node n) { groups.put(n, this); }
19 private static int master_idx=0;
21 private final int idx = master_idx++;
23 public boolean directed = false;
24 public String color="black";
25 public ArrayList<Node> edges = new ArrayList<Node>();
26 public ArrayList<Node> inbound = new ArrayList<Node>();
27 public void edge(ToGraphViz o) {
28 Node n = o.toGraphViz(GraphViz.this);
33 public String name() {
34 if (inbound.size()==1 && inbound.get(0).simple())
35 return inbound.get(0).name()+":node_"+idx;
38 public void edges(PrintWriter pw) {
41 pw.println(" "+name()+" -> " + n.name());
43 public int numEdges() { return edges.size(); }
44 public boolean simple() {
45 boolean simple = true;
46 if (label!=null && !label.equals("")) simple = false;
49 //if (n.numEdges()>0) { simple = false; break; }
50 if (n.inbound.size() > 1) { simple = false; break; }
53 public void dump(PrintWriter pw) {
54 if (inbound.size() > 0) {
58 { good = true; break; }
63 if (directed) pw.print("ordering=out");
65 pw.print(" shape=record ");
66 pw.print(" label=\"{");
69 if (!first) pw.print("|");
71 pw.print("<"+n.name()+">");
72 pw.print(StringUtil.escapify(n.label,"\\\""));
76 pw.print(" label=\"");
77 pw.print(StringUtil.escapify(label,"\\\""));
80 pw.print("color="+color);
85 public boolean hasNode(ToGraphViz o) {
86 return ihm.get(o)!=null;
89 public Node createNode(ToGraphViz o) {
91 if (n!=null) return n;
97 public static interface ToGraphViz {
98 public Node toGraphViz(GraphViz gv);
99 public boolean isTransparent();
100 public boolean isHidden();
103 public void dump(PrintWriter pw) {
104 IdentityHashMap<Node,Node> done = new IdentityHashMap<Node,Node>();
105 pw.println("digraph G {\n");
106 for(Group g : groups.values()) {
107 pw.println(" { rank=same;\n");
108 for(Node n : groups.keySet())
109 if (groups.get(n)==g) {
115 for(Node n : ihm.values()) {
116 if (done.get(n)!=null) continue;
119 for(Node n : ihm.values()) n.edges(pw);