use Appendable rather than StringBuffer for toJava()
[sbp.git] / src / edu / berkeley / sbp / Forest.java
1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.util.*;
5 import java.io.*;
6 import java.util.*;
7
8 /**
9  *   <font color=blue>
10  *   An efficient representation of a collection of trees (Tomita's
11  *   shared packed parse forest).
12  *   </font>
13  */
14 public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
15
16     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
17     public abstract Tree<NodeType> expand1() throws Ambiguous;
18
19     /** expand this forest into a set of trees */
20     public Iterable<Tree<NodeType>> expand() {
21         HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
22         expand(ht, new HashSet<Forest<NodeType>>(), null);
23         return ht;
24     }
25
26     /** returns the input Region which this Forest was parsed from */
27     public abstract Input.Region getRegion();
28
29     // Package-Private //////////////////////////////////////////////////////////////////////////////
30
31     public static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children) {
32         return create(region, head, children, new boolean[children==null ? 0 : children.length]); }
33     public static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children, boolean[] lifts) {
34         if (region == null) throw new RuntimeException("invoked Forest.create(region=null) -- this should never happen");
35         return new One<NodeType>(region, head, children, lifts);
36     }
37
38     abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
39     abstract void gather(HashSet<Forest<NodeType>> ignore);
40     abstract void edges(GraphViz.StateNode n);
41     boolean ambiguous() { return false; }
42     
43     // One //////////////////////////////////////////////////////////////////////////////
44
45     /** A "single" forest with a head and child subforests */    
46     private static class One<NodeType> extends Forest<NodeType> {
47
48         private final Input.Region      location;
49         private final NodeType                head;
50         private final Forest<NodeType>[]       children;
51
52         /** if true, the last child's children are considered children of this node */
53         private final boolean[]         lifts;
54
55         public Input.Region getRegion() { return location; }
56
57         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean[] lifts) {
58             this.location = loc;
59             this.head = head;
60             if (head==null) throw new RuntimeException("invoked Forest.create(,null,,,) -- this should never happen");
61             this.children = children==null ? emptyForestArray : new Forest[children.length];
62             if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
63             if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
64             this.lifts = lifts;
65         }
66
67         public Tree<NodeType> expand1() throws Ambiguous {
68             if (children.length == 0)
69                 return new Tree<NodeType>(location, head);
70             else
71                 return expand1ForOneAndMany((Forest<NodeType>)this);
72         }
73
74         void gather(HashSet<Forest<NodeType>> hf) {
75             hf.add(this);
76             for(Forest<NodeType> f : children) f.gather(hf);
77         }
78         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
79             if (ignore.contains(this)) { ht.add(bogus); return; }
80             expand(0, new Tree[children.length], ht, ignore, bogus);
81         }
82         private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore,
83                             Tree<NodeType> bogus) {
84             if (i==children.length) {
85                 ht.add(new Tree<NodeType>(location, head, ta, lifts));
86             } else {
87                 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
88                 children[i].expand(ht2, ignore, bogus);
89                 for(Tree<NodeType> tc : ht2) {
90                     ta[i] = tc;
91                     expand(i+1, ta, ht, ignore, bogus);
92                     ta[i] = null;
93                 }
94             }
95         }
96
97         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
98
99         public boolean isTransparent() { return false; }
100         public boolean isHidden() { return false; }
101         public GraphViz.StateNode toGraphViz(GraphViz gv) {
102             if (gv.hasNode(this)) return gv.createNode(this);
103             GraphViz.StateNode n = gv.createNode(this);
104             n.label = headToString()==null?"":headToString();
105             n.directed = true;
106             edges(n);
107             return n;
108         }
109         boolean edges = false; // FIXME ??
110         public void edges(GraphViz.StateNode n) {
111             if (edges) return;
112             edges = true;
113             for(int i=0; i<children.length; i++) {
114                 if (lifts[i] && !children[i].ambiguous()) {
115                     children[i].edges(n);
116                 } else {
117                     n.edge(children[i], null);
118                 }
119             }
120         }
121
122         protected String  headToString()         { return head==null?null:head.toString(); }
123         protected String  headToJava()           { return "null"; }
124         protected String  left()                 { return "{"; }
125         protected String  right()                { return "}"; }
126         protected boolean ignoreSingleton()      { return false; }
127     }
128
129
130     // Many //////////////////////////////////////////////////////////////////////////////
131
132     /** An "ambiguity node"; this is immutable once it has been "looked at" */
133     static class Many<NodeType> extends Forest<NodeType> {
134
135         private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
136         private boolean touched = false;
137
138         public Many() { }
139
140         public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
141
142         Forest<NodeType> simplify() throws Ambiguous {
143             touched();
144             if (hp.size() > 1) {
145                 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
146                 Iterator<Forest<NodeType>> ih = hp.iterator();
147                 ih.next().gather(hf0);
148                 for(Forest<NodeType> f : hp) {
149                     HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
150                     f.gather(hf1);
151                     hf0.retainAll(hf1);
152                 }
153                 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
154                 expand(ht, hf0, new Tree(null, "*"));
155                 throw new Ambiguous((Forest<?>)this,
156                                     (HashSet<Tree<?>>)(Object)ht);
157             }
158
159             return hp.iterator().next();
160         }
161
162         public Tree<NodeType> expand1() throws Ambiguous {
163             return simplify().expand1();
164         }
165         
166         void gather(HashSet<Forest<NodeType>> ht) {
167             touched();
168
169             // FIXME: do something more sensible here
170             if (ht.contains(this)) {
171                 System.err.println("WARNING: grammar produced a circular forest\n" + this);
172                 //throw new Error("grammar produced a circular forest:\n" + this);
173                 return;
174             }
175
176             ht.add(this);
177             for(Forest<NodeType> f : hp) f.gather(ht);
178         }
179
180         private void touched() {
181             if (touched) return;
182             touched = true;
183             /*
184             FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
185             for(Forest f : hp)
186                 if (f instanceof Forest.One) f2.add(f);
187                 else for(Forest ff : ((Forest.Many<NodeType>)f))
188                     f2.add(ff);
189             hp = f2;
190             */
191         }
192         public boolean contains(Forest f) {
193             touched();
194             return hp.contains(f);
195         }
196         public void merge(Forest p) { 
197             if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
198             if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
199             hp.add(p, true);
200         }
201         boolean ambiguous() {
202             touched();
203             if (hp.size()==0) return false;
204             if (hp.size()==1) return hp.iterator().next().ambiguous();
205             return true;
206         }
207
208         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
209             touched();
210             if (ignore.contains(this)) { ht.add(bogus); return; }
211             for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
212         }
213
214
215         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
216
217         public boolean isTransparent() { return hp.size()==1; }
218         public boolean isHidden() { return hp.size()==0; }
219         public void edges(GraphViz.StateNode n) {
220             if (hp.size()==1) { hp.iterator().next().edges(n); return; }
221             for(Forest f : hp) f.edges(n);
222         }
223         public GraphViz.StateNode toGraphViz(GraphViz gv) {
224             if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
225             if (gv.hasNode(this)) return gv.createNode(this);
226             GraphViz.StateNode n = gv.createNode(this);
227             n.label = "?";
228             n.color = "red";
229             for(Forest f : hp) n.edge(f, null);
230             return n;
231         }
232     }
233
234
235     // Statics //////////////////////////////////////////////////////////////////////////////
236
237     /** Depth-first expansion, handling .One and .Many without recursion. */
238     private static <NodeType> Tree<NodeType> expand1ForOneAndMany(
239             Forest<NodeType> forest) throws Ambiguous {
240         List<Tree<NodeType>> nodes = new ArrayList<Tree<NodeType>>();
241         List<Tree<NodeType>> build = new ArrayList<Tree<NodeType>>();
242
243         // path stack
244         List<Forest.One<NodeType>> path = new ArrayList<Forest.One<NodeType>>();
245         List<Integer> sizes = new ArrayList<Integer>(),
246                       poss  = new ArrayList<Integer>();
247
248         // handle the left-most leaf
249         expand1Descend(path, nodes, sizes, poss, forest);
250
251         for (Forest.One<NodeType> f; path.size() > 0;) {
252             // up one
253             f = pop(path);
254             int size = pop(sizes) + 1;
255             int pos = pop(poss) + 1;
256             Forest<NodeType>[] children = f.children;
257             if (pos < children.length) {
258                 // down the next branch
259                 path.add(f);
260                 sizes.add(new Integer(size));
261                 poss.add(new Integer(pos));
262
263                 // handle the left-most leaf
264                 expand1Descend(path, nodes, sizes, poss, children[pos]);
265             } else {
266                 if (path.size() > 0 && peek(path).lifts[peek(poss)]) {
267                     // skip assembling this node, lift children
268                     sizes.add(pop(sizes) + size - 1);
269                     continue;
270                 }
271                 // assemble this node
272                 for (int i=size; i > 0; i--)
273                     build.add(nodes.remove(nodes.size() - i));
274                 nodes.add(new Tree<NodeType>(f.location, f.head, build));
275                 build.clear();
276             }
277         }
278
279         return pop(nodes);
280     }
281
282     /** Descend to the left-most leaf, building the path. */
283     private static <NodeType> void expand1Descend(
284             List<Forest.One<NodeType>> path, List<Tree<NodeType>> nodes,
285             List<Integer> sizes, List<Integer> poss, Forest<NodeType> f)
286             throws Ambiguous {
287         while (true) {
288             if (f instanceof Forest.Many)
289                 f = ((Forest.Many<NodeType>)f).simplify();
290             else if (f instanceof Forest.One) {
291                 Forest.One<NodeType> one = (Forest.One<NodeType>)f;
292                 if (one.children.length == 0)
293                     break;
294                 path.add(one);
295                 sizes.add(0);
296                 poss.add(0);
297                 f = one.children[0];
298             } else {
299                 nodes.add(f.expand1());
300                 return;
301             }
302         }
303
304         if (path.size() > 0 && peek(path).lifts[peek(poss)])
305             sizes.add(pop(sizes) - 1); // ignore lifted leafs
306         else
307             nodes.add(f.expand1());
308     }
309
310     private static <T> T pop(List<T> list) {
311         return list.remove(list.size() - 1); }
312     private static <T> T peek(List<T> list) {
313         return list.get(list.size() - 1); }
314
315     private static Tree[] tree_hint = new Tree[0];
316     private static String[] string_hint = new String[0];
317     private static final Forest[] emptyForestArray = new Forest[0];
318
319     protected String  headToString()    { return null; }
320     protected String  headToJava()      { return "null"; }
321     protected String  left()            { return "<?"; }
322     protected String  right()           { return "?>"; }
323     protected boolean ignoreSingleton() { return true; }
324 }