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 String comment;
24 public boolean directed = false;
25 public String color="black";
26 public ArrayList<Node> edges = new ArrayList<Node>();
27 public ArrayList<Object> labels = new ArrayList<Object>();
28 public ArrayList<Node> inbound = new ArrayList<Node>();
29 public void edge(ToGraphViz o, Object label) {
30 Node n = o.toGraphViz(GraphViz.this);
36 public String getParentName() {
37 if (inbound.size()==1 && inbound.get(0).simple())
38 return inbound.get(0).getParentName();
41 public String name() {
42 if (inbound.size()==1 && inbound.get(0).simple())
43 return inbound.get(0).getParentName()+":node_"+idx;
46 public void edges(PrintWriter pw) {
48 for(int i=0; i<edges.size(); i++) {
49 Node n = edges.get(i);
50 Object label = labels.get(i);
51 pw.println(" "+name()+" -> " + n.name() + " [color="+color+" "
52 +(label==null?"":("label=\""+StringUtil.escapify(label.toString(), "\\\"\r\n")+"\""))+ "];\n");
55 public int numEdges() { return edges.size(); }
56 public boolean simple() {
57 boolean simple = true;
58 if (label!=null && !label.equals("")) simple = false;
61 //if (n.numEdges()>0) { simple = false; break; }
62 if (n.inbound.size() > 1) { simple = false; break; }
65 public void dump(PrintWriter pw) {
66 if (inbound.size() > 0) {
70 { good = true; break; }
75 if (directed) pw.print("ordering=out");
77 pw.print(" shape=record ");
78 pw.print(" label=\"");
79 boolean complex = false;
83 if (!complex) pw.print("{");
86 if (!first) pw.print("|");
88 pw.print("<node_"+n.idx+">");
89 pw.print(StringUtil.escapify(n.label,"\\\"\r\n"));
91 if (!complex) pw.print("}");
94 pw.print(" label=\"");
95 pw.print(StringUtil.escapify(label,"\\\"\r\n"));
98 pw.print("color="+color);
99 if (comment!=null) pw.print(" comment=\""+StringUtil.escapify(comment,"\\\"\r\n")+"\" ");
104 public boolean hasNode(ToGraphViz o) {
105 return ihm.get(o)!=null;
108 public Node createNode(ToGraphViz o) {
110 if (n!=null) return n;
116 public static interface ToGraphViz {
117 public Node toGraphViz(GraphViz gv);
118 public boolean isTransparent();
119 public boolean isHidden();
122 public void show() throws IOException {
123 Runtime.getRuntime().exec(new String[] { "dot", "-Tsvg" });
126 public void dump(OutputStream os) { dump(new PrintWriter(new OutputStreamWriter(os))); }
127 public void dump(PrintWriter pw) {
128 IdentityHashMap<Node,Node> done = new IdentityHashMap<Node,Node>();
129 pw.println("digraph G { rankdir=LR; ordering=out; \n");
130 for(Group g : groups.values()) {
131 pw.println(" { rank=same;\n");
132 for(Node n : groups.keySet())
133 if (groups.get(n)==g) {
139 for(Node n : ihm.values()) {
140 if (done.get(n)!=null) continue;
143 for(Node n : ihm.values()) n.edges(pw);