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 name() {
37 if (inbound.size()==1 && inbound.get(0).simple())
38 return inbound.get(0).name()+":node_"+idx;
41 public void edges(PrintWriter pw) {
43 for(int i=0; i<edges.size(); i++) {
44 Node n = edges.get(i);
45 Object label = labels.get(i);
46 pw.println(" "+name()+" -> " + n.name() + " [color="+color+" " +(label==null?"":("label=\""+label+"\""))+ "];\n");
49 public int numEdges() { return edges.size(); }
50 public boolean simple() {
51 boolean simple = true;
52 if (label!=null && !label.equals("")) simple = false;
55 //if (n.numEdges()>0) { simple = false; break; }
56 if (n.inbound.size() > 1) { simple = false; break; }
59 public void dump(PrintWriter pw) {
60 if (inbound.size() > 0) {
64 { good = true; break; }
69 if (directed) pw.print("ordering=out");
71 pw.print(" shape=record ");
72 pw.print(" label=\"");
73 boolean complex = false;
77 if (!complex) pw.print("{");
80 if (!first) pw.print("|");
82 pw.print("<node_"+n.idx+">");
83 pw.print(StringUtil.escapify(n.label,"\\\""));
85 if (!complex) pw.print("}");
88 pw.print(" label=\"");
89 pw.print(StringUtil.escapify(label,"\\\""));
92 pw.print("color="+color);
93 if (comment!=null) pw.print(" comment=\""+StringUtil.escapify(comment,"\\\"")+"\" ");
98 public boolean hasNode(ToGraphViz o) {
99 return ihm.get(o)!=null;
102 public Node createNode(ToGraphViz o) {
104 if (n!=null) return n;
110 public static interface ToGraphViz {
111 public Node toGraphViz(GraphViz gv);
112 public boolean isTransparent();
113 public boolean isHidden();
116 public void dump(PrintWriter pw) {
117 IdentityHashMap<Node,Node> done = new IdentityHashMap<Node,Node>();
118 pw.println("digraph G { rankdir=LR; \n");
119 for(Group g : groups.values()) {
120 pw.println(" { rank=same;\n");
121 for(Node n : groups.keySet())
122 if (groups.get(n)==g) {
128 for(Node n : ihm.values()) {
129 if (done.get(n)!=null) continue;
132 for(Node n : ihm.values()) n.edges(pw);