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() + " [color="+color+"];\n");
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=\"");
67 boolean complex = false;
71 if (!complex) pw.print("{");
74 if (!first) pw.print("|");
76 pw.print("<node_"+n.idx+">");
77 pw.print(StringUtil.escapify(n.label,"\\\""));
79 if (!complex) pw.print("}");
82 pw.print(" label=\"");
83 pw.print(StringUtil.escapify(label,"\\\""));
86 pw.print("color="+color);
91 public boolean hasNode(ToGraphViz o) {
92 return ihm.get(o)!=null;
95 public Node createNode(ToGraphViz o) {
97 if (n!=null) return n;
103 public static interface ToGraphViz {
104 public Node toGraphViz(GraphViz gv);
105 public boolean isTransparent();
106 public boolean isHidden();
109 public void dump(PrintWriter pw) {
110 IdentityHashMap<Node,Node> done = new IdentityHashMap<Node,Node>();
111 pw.println("digraph G { rankdir=LR; \n");
112 for(Group g : groups.values()) {
113 pw.println(" { rank=same;\n");
114 for(Node n : groups.keySet())
115 if (groups.get(n)==g) {
121 for(Node n : ihm.values()) {
122 if (done.get(n)!=null) continue;
125 for(Node n : ihm.values()) n.edges(pw);