added GraphViz support
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index abc5874..b9c4fcc 100644 (file)
@@ -7,7 +7,7 @@ import java.util.*;
 import java.lang.reflect.*;
 
 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
-public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>>, IntegerMappable {
+public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>>, IntegerMappable, GraphViz.ToGraphViz {
 
     private static int master_idx = 0;
     private final int idx = master_idx++;
@@ -24,7 +24,6 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ impl
 
     /** expand this forest into a set of trees */
     public HashSet<Tree<T>> expand(boolean toss) {
-        //if (toss) scan();
         final HashSetTreeConsumer<T> ret = new HashSetTreeConsumer<T>();
         visit(new TreeMaker2<T>(toss, ret), null, null);
         if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
@@ -50,19 +49,37 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ impl
         return body;
     }
     static        <T> Forest<T> leaf(Input.Location loc, T tag, Position p) { return create(loc, tag, null, false, false, p); }
-
     public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position p) {
         return new MyBody<T>(loc, tag, tokens, unwrap, singleton, p);
     }
-
     // Body //////////////////////////////////////////////////////////////////////////////
 
-    protected static interface Body<T> {
+    protected static interface Body<T> extends GraphViz.ToGraphViz {
         void expand(int i, TreeMaker<T> h);
     }
-
+    public abstract void edges(GraphViz.Node n);
     protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
 
+        public boolean isTransparent() { return false; }
+        public boolean isHidden() { return false; }
+        public GraphViz.Node toGraphViz(GraphViz gv) {
+            if (gv.hasNode(this)) return gv.createNode(this);
+            GraphViz.Node n = gv.createNode(this);
+            n.label = headToString()==null?"":headToString();
+            n.directed = true;
+            edges(n);
+            return n;
+        }
+        public void edges(GraphViz.Node n) {
+            for(int i=0; i<tokens.length; i++) {
+                if (i==tokens.length-1 && unwrap) {
+                    tokens[i].edges(n);
+                } else {
+                    n.edge(tokens[i]);
+                }
+            }
+        }
+
         public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
             ivbc.invoke(this, b, c);
         }
@@ -113,7 +130,7 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ impl
             }
         }
 
-        protected String  headToString()         { return null; }
+        protected String  headToString()         { return tag==null?null:tag.toString(); }
         protected String  headToJava()           { return null; }
         protected String  left()                 { return "{"; }
         protected String  right()                { return "}"; }
@@ -136,6 +153,21 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ impl
             return super.toInt();
         }
         public void merge(Forest p) { if (p!=this) hp.add(p, true); }
+
+        public boolean isTransparent() { return hp.size()==1; }
+        public boolean isHidden() { return hp.size()==0; }
+        public void edges(GraphViz.Node n) { for(Forest f : hp) f.edges(n); }
+        public GraphViz.Node toGraphViz(GraphViz gv) {
+            if (hp.size()==0) return null;
+            if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
+            if (gv.hasNode(this)) return gv.createNode(this);
+            GraphViz.Node n = gv.createNode(this);
+            n.label = "?";
+            n.color = "red";
+            for(Forest f : hp) n.edge(f);
+            return n;
+        }
+
         public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
             if (hp==null) return;
             for(Forest<T> f : hp)