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>();
14 public class Group extends Node {
15 private final int idx = master_idx++;
16 public boolean cluster = false;
17 public boolean primary = true;
19 public void add(Node n) { groups.put(n, this); }
20 public String name() { return cluster?("cluster_"+idx):("subgraph_"+idx); }
21 public boolean simple() { return false; }
22 public void dump(PrintWriter pw, IdentityHashMap<Node,Node> done) {
24 if (done.get(g)!=null) return;
26 pw.println(" subgraph "+name()+" { rank=same;\n");
27 pw.println(" label=\""+StringUtil.escapify(label.toString(), "\\\"\r\n")+"\";\n");
28 pw.println(" color="+g.color+";\n");
29 pw.println(" shape="+g.shape+";\n");
30 for(Node n : groups.keySet())
37 private static int master_idx=0;
40 private final int idx = master_idx++;
42 public String comment;
43 public boolean directed = false;
44 public String color="black";
45 public String fill="white";
46 public String shape="ellipse";
47 public ArrayList<Node> edges = new ArrayList<Node>();
48 public ArrayList<Object> labels = new ArrayList<Object>();
49 public ArrayList<Node> inbound = new ArrayList<Node>();
50 public void edge(ToGraphViz o, Object label) {
51 Node n = o.toGraphViz(GraphViz.this);
57 public String getParentName() {
58 if (inbound.size()==1 && inbound.get(0).simple())
59 return inbound.get(0).getParentName();
62 public String name() {
63 if (inbound.size()==1 && inbound.get(0).simple())
64 return inbound.get(0).getParentName()+":node_"+idx;
67 public void edges(PrintWriter pw) {
69 for(int i=0; i<edges.size(); i++) {
70 Node n = edges.get(i);
71 Object label = labels.get(i);
72 pw.println(" "+name()+" -> " + n.name() + " [color="+color+" "
73 +(label==null?"":("label=\""+StringUtil.escapify(label.toString(), "\\\"\r\n")+"\""))+ "];\n");
76 public int numEdges() { return edges.size(); }
77 public boolean simple() {
78 boolean simple = true;
79 if (label!=null && !label.equals("")) simple = false;
82 //if (n.numEdges()>0) { simple = false; break; }
83 if (n.inbound.size() > 1) { simple = false; break; }
86 public void dump(PrintWriter pw, IdentityHashMap<Node,Node> done) {
87 if (done.get(this)!=null) return;
89 if (inbound.size() > 0) {
93 { good = true; break; }
98 if (directed) pw.print("ordering=out");
100 pw.print(" shape=record ");
101 pw.print(" label=\"");
102 boolean complex = false;
104 if (n.edges.size()>0)
106 if (!complex) pw.print("{");
107 boolean first = true;
108 for(Node n : edges) {
109 if (!first) pw.print("|");
111 pw.print("<node_"+n.idx+">");
112 pw.print(StringUtil.escapify(n.label,"\\\"\r\n"));
114 if (!complex) pw.print("}");
117 pw.print(" label=\"");
118 pw.print(StringUtil.escapify(label,"\\\"\r\n"));
121 pw.print("color="+color);
122 if (comment!=null) pw.print(" comment=\""+StringUtil.escapify(comment,"\\\"\r\n")+"\" ");
127 public boolean hasNode(ToGraphViz o) {
128 return ihm.get(o)!=null;
131 public Node createNode(ToGraphViz o) {
133 if (n!=null) return n;
139 public Group createGroup(ToGraphViz o) {
140 Group n = (Group)ihm.get(o);
141 if (n!=null) return n;
147 public static interface ToGraphViz {
148 Node toGraphViz(GraphViz gv);
149 boolean isTransparent();
153 public void show() throws IOException {
154 Runtime.getRuntime().exec(new String[] { "dot", "-Tsvg" });
157 public void dump(OutputStream os) { dump(new PrintWriter(new OutputStreamWriter(os))); }
158 public void dump(PrintWriter pw) {
159 IdentityHashMap<Node,Node> done = new IdentityHashMap<Node,Node>();
160 pw.println("digraph G { rankdir=LR; ordering=out; compound=true; \n");
161 for(Group g : groups.values())
164 for(Node n : ihm.values()) {
165 if (done.get(n)!=null) continue;
166 if (n instanceof Group) continue;
169 for(Node n : ihm.values()) n.edges(pw);