make sure we track Input.Region for epsilon reductions (important for ambiguity-hunting)
[sbp.git] / src / edu / berkeley / sbp / Forest.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.Sequence.Position;
4 import edu.berkeley.sbp.util.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /**
10  *   <font color=blue>
11  *   An efficient representation of a collection of trees (Tomita's
12  *   shared packed parse forest).
13  *   </font>
14  */
15 public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
16
17     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
18     public abstract Tree<NodeType> expand1() throws Ambiguous;
19
20     /** expand this forest into a set of trees */
21     public void expand(HashSet<Tree<NodeType>> ht) { expand(ht, new HashSet<Forest<NodeType>>(), null); }
22
23     static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
24         if (loc == null) throw new Error();
25         return new One<NodeType>(loc, head, children, lift);
26     }
27
28     /** create a new forest */
29     public static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children) {
30         return Forest.create(loc, head, children, false); }
31
32     // Package-Private //////////////////////////////////////////////////////////////////////////////
33
34     abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
35     abstract void gather(HashSet<Forest<NodeType>> ignore);
36     abstract void edges(GraphViz.Node n);
37     boolean ambiguous() { return false; }
38     
39     abstract Input.Region getRegion();
40
41     // One //////////////////////////////////////////////////////////////////////////////
42
43     /** A "single" forest with a head and child subforests */    
44     private static class One<NodeType> extends Forest<NodeType> {
45
46         private final Input.Region      location;
47         private final NodeType                head;
48         private final Forest<NodeType>[]       children;
49
50         /** if true, the last child's children are considered children of this node */
51         private final boolean           lift;
52
53         Input.Region getRegion() { return location; }
54
55         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
56             this.location = loc;
57             this.head = head;
58             this.children = children==null ? emptyForestArray : new Forest[children.length];
59             if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
60             if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
61             this.lift = lift;
62         }
63
64         public Tree<NodeType> expand1() throws Ambiguous {
65             Tree<NodeType>[] ret = new Tree[children.length];
66             for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
67             return new Tree<NodeType>(location, head, ret, lift);
68         }
69
70         void gather(HashSet<Forest<NodeType>> hf) {
71             hf.add(this);
72             for(Forest<NodeType> f : children) f.gather(hf);
73         }
74         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
75             if (ignore.contains(this)) { ht.add(bogus); return; }
76             expand(0, new Tree[children.length], ht, ignore, bogus);
77         }
78         private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
79             if (i==children.length) {
80                 ht.add(new Tree<NodeType>(location, head, ta, lift));
81             } else {
82                 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
83                 children[i].expand(ht2, ignore, bogus);
84                 for(Tree<NodeType> tc : ht2) {
85                     ta[i] = tc;
86                     expand(i+1, ta, ht, ignore, bogus);
87                     ta[i] = null;
88                 }
89             }
90         }
91
92         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
93
94         public boolean isTransparent() { return false; }
95         public boolean isHidden() { return false; }
96         public GraphViz.Node toGraphViz(GraphViz gv) {
97             if (gv.hasNode(this)) return gv.createNode(this);
98             GraphViz.Node n = gv.createNode(this);
99             n.label = headToString()==null?"":headToString();
100             n.directed = true;
101             edges(n);
102             return n;
103         }
104         boolean edges = false; // FIXME ??
105         public void edges(GraphViz.Node n) {
106             if (edges) return;
107             edges = true;
108             for(int i=0; i<children.length; i++) {
109                 if (i==children.length-1 && lift && !children[i].ambiguous()) {
110                     children[i].edges(n);
111                 } else {
112                     n.edge(children[i], null);
113                 }
114             }
115         }
116
117         protected String  headToString()         { return head==null?null:head.toString(); }
118         protected String  headToJava()           { return "null"; }
119         protected String  left()                 { return "{"; }
120         protected String  right()                { return "}"; }
121         protected boolean ignoreSingleton()      { return false; }
122     }
123
124
125     // Many //////////////////////////////////////////////////////////////////////////////
126
127     /** An "ambiguity node"; this is immutable once it has been "looked at" */
128     static class Many<NodeType> extends Forest<NodeType> {
129
130         HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
131         private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
132         private boolean touched = false;
133
134         public Many() { }
135
136         Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
137
138         public Tree<NodeType> expand1() throws Ambiguous {
139             touched();
140             if (hp.size() > 1) {
141                 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
142                 Iterator<Forest<NodeType>> ih = hp.iterator();
143                 ih.next().gather(hf0);
144                 for(Forest<NodeType> f : hp) {
145                     HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
146                     f.gather(hf1);
147                     hf0.retainAll(hf1);
148                 }
149                 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
150                 expand(ht, hf0, new Tree(null, "*"));
151                 throw new Ambiguous((Forest<?>)this,
152                                     (HashSet<Tree<?>>)(Object)ht);
153             }
154             return hp.iterator().next().expand1();
155         }
156         
157         void gather(HashSet<Forest<NodeType>> ht) {
158             touched();
159             ht.add(this);
160             for(Forest<NodeType> f : hp) f.gather(ht);
161         }
162
163         private void touched() {
164             if (touched) return;
165             touched = true;
166             /*
167             FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
168             for(Forest f : hp)
169                 if (f instanceof Forest.One) f2.add(f);
170                 else for(Forest ff : ((Forest.Many<NodeType>)f))
171                     f2.add(ff);
172             hp = f2;
173             */
174         }
175         public boolean contains(Forest f) {
176             touched();
177             return hp.contains(f);
178         }
179         public void merge(Forest p) { 
180             if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
181             if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
182             hp.add(p, true);
183         }
184         boolean ambiguous() {
185             touched();
186             if (hp.size()==0) return false;
187             if (hp.size()==1) return hp.iterator().next().ambiguous();
188             return true;
189         }
190
191         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
192             touched();
193             if (ignore.contains(this)) { ht.add(bogus); return; }
194             for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
195         }
196
197
198         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
199
200         public boolean isTransparent() { return hp.size()==1; }
201         public boolean isHidden() { return hp.size()==0; }
202         public void edges(GraphViz.Node n) {
203             if (hp.size()==1) { hp.iterator().next().edges(n); return; }
204             for(Forest f : hp) f.edges(n);
205         }
206         public GraphViz.Node toGraphViz(GraphViz gv) {
207             if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
208             if (gv.hasNode(this)) return gv.createNode(this);
209             GraphViz.Node n = gv.createNode(this);
210             n.label = "?";
211             n.color = "red";
212             for(Forest f : hp) n.edge(f, null);
213             return n;
214         }
215     }
216
217     // Statics //////////////////////////////////////////////////////////////////////////////
218
219     private static Tree[] tree_hint = new Tree[0];
220     private static String[] string_hint = new String[0];
221     private static final Forest[] emptyForestArray = new Forest[0];
222
223     protected String  headToString()    { return null; }
224     protected String  headToJava()      { return "null"; }
225     protected String  left()            { return "<?"; }
226     protected String  right()           { return "?>"; }
227     protected boolean ignoreSingleton() { return true; }
228 }