X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FGraphViz.java;h=b6db224cc5c3f86273aa35677a26bb8b5d6c3217;hb=f09d2abb95f106197aea99c345282c3bf0cd3717;hp=13899bb2540b108404b4774eebd4dc9f0a57bd5b;hpb=d95659dd73e9dee2d18417535e4c7d5a010033b6;p=sbp.git diff --git a/src/edu/berkeley/sbp/util/GraphViz.java b/src/edu/berkeley/sbp/util/GraphViz.java index 13899bb..b6db224 100644 --- a/src/edu/berkeley/sbp/util/GraphViz.java +++ b/src/edu/berkeley/sbp/util/GraphViz.java @@ -11,34 +11,67 @@ public class GraphViz { IdentityHashMap ihm = new IdentityHashMap(); HashMap groups = new HashMap(); - public class Group { + public class Group extends Node { + private final int idx = master_idx++; + public boolean cluster = false; + public boolean primary = true; public Group() { } public void add(Node n) { groups.put(n, this); } + public String name() { return cluster?("cluster_"+idx):("subgraph_"+idx); } + public boolean simple() { return false; } + public void dump(PrintWriter pw, IdentityHashMap done) { + Group g = this; + if (done.get(g)!=null) return; + done.put(g,g); + pw.println(" subgraph "+name()+" { rank=same;\n"); + pw.println(" label=\""+StringUtil.escapify(label.toString(), "\\\"\r\n")+"\";\n"); + pw.println(" color="+g.color+";\n"); + pw.println(" shape="+g.shape+";\n"); + for(Node n : groups.keySet()) + if (groups.get(n)==g) + n.dump(pw, done); + pw.println(" }\n"); + } } private static int master_idx=0; + public class Node { private final int idx = master_idx++; public String label; + public String comment; public boolean directed = false; public String color="black"; + public String fill="white"; + public String shape="ellipse"; public ArrayList edges = new ArrayList(); + public ArrayList labels = new ArrayList(); public ArrayList inbound = new ArrayList(); - public void edge(ToGraphViz o) { + public void edge(ToGraphViz o, Object label) { Node n = o.toGraphViz(GraphViz.this); if (n==null) return; edges.add(n); + labels.add(label); n.inbound.add(this); } + public String getParentName() { + if (inbound.size()==1 && inbound.get(0).simple()) + return inbound.get(0).getParentName(); + return name(); + } public String name() { if (inbound.size()==1 && inbound.get(0).simple()) - return inbound.get(0).name()+":node_"+idx; + return inbound.get(0).getParentName()+":node_"+idx; return "node_"+idx; } public void edges(PrintWriter pw) { if (simple()) return; - for(Node n : edges) - pw.println(" "+name()+" -> " + n.name() + " [color="+color+"];\n"); + for(int i=0; i " + n.name() + " [color="+color+" " + +(label==null?"":("label=\""+StringUtil.escapify(label.toString(), "\\\"\r\n")+"\""))+ "];\n"); + } } public int numEdges() { return edges.size(); } public boolean simple() { @@ -50,7 +83,9 @@ public class GraphViz { if (n.inbound.size() > 1) { simple = false; break; } return simple; } - public void dump(PrintWriter pw) { + public void dump(PrintWriter pw, IdentityHashMap done) { + if (done.get(this)!=null) return; + done.put(this, this); if (inbound.size() > 0) { boolean good = false; for(Node n : inbound) @@ -74,16 +109,17 @@ public class GraphViz { if (!first) pw.print("|"); first = false; pw.print(""); - pw.print(StringUtil.escapify(n.label,"\\\"")); + pw.print(StringUtil.escapify(n.label,"\\\"\r\n")); } if (!complex) pw.print("}"); - pw.print("\""); + pw.print("\" "); } else { pw.print(" label=\""); - pw.print(StringUtil.escapify(label,"\\\"")); - pw.print("\""); + pw.print(StringUtil.escapify(label,"\\\"\r\n")); + pw.print("\" "); } pw.print("color="+color); + if (comment!=null) pw.print(" comment=\""+StringUtil.escapify(comment,"\\\"\r\n")+"\" "); pw.print("];\n"); } } @@ -100,30 +136,39 @@ public class GraphViz { return n; } + public Group createGroup(ToGraphViz o) { + Group n = (Group)ihm.get(o); + if (n!=null) return n; + n = new Group(); + ihm.put(o, n); + return n; + } + public static interface ToGraphViz { - public Node toGraphViz(GraphViz gv); - public boolean isTransparent(); - public boolean isHidden(); + Node toGraphViz(GraphViz gv); + boolean isTransparent(); + boolean isHidden(); + } + + public void show() throws IOException { + Runtime.getRuntime().exec(new String[] { "dot", "-Tsvg" }); } + public void dump(OutputStream os) { dump(new PrintWriter(new OutputStreamWriter(os))); } public void dump(PrintWriter pw) { IdentityHashMap done = new IdentityHashMap(); - pw.println("digraph G { rankdir=LR; \n"); - for(Group g : groups.values()) { - pw.println(" { rank=same;\n"); - for(Node n : groups.keySet()) - if (groups.get(n)==g) { - done.put(n,n); - n.dump(pw); - } - pw.println(" }\n"); - } + pw.println("digraph G { rankdir=LR; ordering=out; compound=true; \n"); + for(Group g : groups.values()) + if (g.primary) + g.dump(pw, done); for(Node n : ihm.values()) { if (done.get(n)!=null) continue; - n.dump(pw); + if (n instanceof Group) continue; + n.dump(pw, done); } for(Node n : ihm.values()) n.edges(pw); pw.println("}\n"); + pw.flush(); } }