public class Node {
private final int idx = master_idx++;
public String label;
+ public String comment;
public boolean directed = false;
public String color="black";
public ArrayList<Node> edges = new ArrayList<Node>();
+ public ArrayList<Object> labels = new ArrayList<Object>();
public ArrayList<Node> inbound = new ArrayList<Node>();
- 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());
+ for(int i=0; i<edges.size(); i++) {
+ Node n = edges.get(i);
+ Object label = labels.get(i);
+ pw.println(" "+name()+" -> " + n.name() + " [color="+color+" "
+ +(label==null?"":("label=\""+StringUtil.escapify(label.toString(), "\\\"\r\n")+"\""))+ "];\n");
+ }
}
public int numEdges() { return edges.size(); }
public boolean simple() {
if (directed) pw.print("ordering=out");
if (simple()) {
pw.print(" shape=record ");
- pw.print(" label=\"{");
+ pw.print(" label=\"");
+ boolean complex = false;
+ for(Node n : edges)
+ if (n.edges.size()>0)
+ complex = true;
+ if (!complex) pw.print("{");
boolean first = true;
for(Node n : edges) {
if (!first) pw.print("|");
first = false;
- pw.print("<"+n.name()+">");
- pw.print(StringUtil.escapify(n.label,"\\\""));
+ pw.print("<node_"+n.idx+">");
+ pw.print(StringUtil.escapify(n.label,"\\\"\r\n"));
}
- pw.print("}\"");
+ if (!complex) 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");
}
}
public 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<Node,Node> done = new IdentityHashMap<Node,Node>();
- pw.println("digraph G {\n");
+ pw.println("digraph G { rankdir=LR; ordering=out; \n");
for(Group g : groups.values()) {
pw.println(" { rank=same;\n");
for(Node n : groups.keySet())
}
for(Node n : ihm.values()) n.edges(pw);
pw.println("}\n");
+ pw.flush();
}
}