cleanups, reorg, and commenting
[sbp.git] / src / edu / berkeley / sbp / Forest.java
1 // Copyright 2006 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     static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children, boolean lift) {
32         if (region == null) throw new RuntimeException("invoked Forest.create(region=null) -- this should never happen");
33         return new One<NodeType>(region, head, children, lift);
34     }
35
36     /** create a new forest */
37     public static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children) {
38         return Forest.create(region, head, children, false); }
39
40     abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
41     abstract void gather(HashSet<Forest<NodeType>> ignore);
42     abstract void edges(GraphViz.Node n);
43     boolean ambiguous() { return false; }
44     
45     // One //////////////////////////////////////////////////////////////////////////////
46
47     /** A "single" forest with a head and child subforests */    
48     private static class One<NodeType> extends Forest<NodeType> {
49
50         private final Input.Region      location;
51         private final NodeType                head;
52         private final Forest<NodeType>[]       children;
53
54         /** if true, the last child's children are considered children of this node */
55         private final boolean           lift;
56
57         public Input.Region getRegion() { return location; }
58
59         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
60             this.location = loc;
61             this.head = head;
62             if (head==null) throw new RuntimeException("invoked Forest.create(,null,,,) -- this should never happen");
63             this.children = children==null ? emptyForestArray : new Forest[children.length];
64             if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
65             if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
66             this.lift = lift;
67         }
68
69         public Tree<NodeType> expand1() throws Ambiguous {
70             Tree<NodeType>[] ret = new Tree[children.length];
71             for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
72             return new Tree<NodeType>(location, head, ret, lift);
73         }
74
75         void gather(HashSet<Forest<NodeType>> hf) {
76             hf.add(this);
77             for(Forest<NodeType> f : children) f.gather(hf);
78         }
79         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
80             if (ignore.contains(this)) { ht.add(bogus); return; }
81             expand(0, new Tree[children.length], ht, ignore, bogus);
82         }
83         private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore,
84                             Tree<NodeType> bogus) {
85             if (i==children.length) {
86                 ht.add(new Tree<NodeType>(location, head, ta, lift));
87             } else {
88                 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
89                 children[i].expand(ht2, ignore, bogus);
90                 for(Tree<NodeType> tc : ht2) {
91                     ta[i] = tc;
92                     expand(i+1, ta, ht, ignore, bogus);
93                     ta[i] = null;
94                 }
95             }
96         }
97
98         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
99
100         public boolean isTransparent() { return false; }
101         public boolean isHidden() { return false; }
102         public GraphViz.Node toGraphViz(GraphViz gv) {
103             if (gv.hasNode(this)) return gv.createNode(this);
104             GraphViz.Node n = gv.createNode(this);
105             n.label = headToString()==null?"":headToString();
106             n.directed = true;
107             edges(n);
108             return n;
109         }
110         boolean edges = false; // FIXME ??
111         public void edges(GraphViz.Node n) {
112             if (edges) return;
113             edges = true;
114             for(int i=0; i<children.length; i++) {
115                 if (i==children.length-1 && lift && !children[i].ambiguous()) {
116                     children[i].edges(n);
117                 } else {
118                     n.edge(children[i], null);
119                 }
120             }
121         }
122
123         protected String  headToString()         { return head==null?null:head.toString(); }
124         protected String  headToJava()           { return "null"; }
125         protected String  left()                 { return "{"; }
126         protected String  right()                { return "}"; }
127         protected boolean ignoreSingleton()      { return false; }
128     }
129
130
131     // Many //////////////////////////////////////////////////////////////////////////////
132
133     /** An "ambiguity node"; this is immutable once it has been "looked at" */
134     static class Many<NodeType> extends Forest<NodeType> {
135
136         private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
137         private boolean touched = false;
138
139         public Many() { }
140
141         public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
142
143         public Tree<NodeType> expand1() throws Ambiguous {
144             touched();
145             if (hp.size() > 1) {
146                 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
147                 Iterator<Forest<NodeType>> ih = hp.iterator();
148                 ih.next().gather(hf0);
149                 for(Forest<NodeType> f : hp) {
150                     HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
151                     f.gather(hf1);
152                     hf0.retainAll(hf1);
153                 }
154                 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
155                 expand(ht, hf0, new Tree(null, "*"));
156                 throw new Ambiguous((Forest<?>)this,
157                                     (HashSet<Tree<?>>)(Object)ht);
158             }
159             return hp.iterator().next().expand1();
160         }
161         
162         void gather(HashSet<Forest<NodeType>> ht) {
163             touched();
164
165             // FIXME: do something more sensible here
166             if (ht.contains(this)) {
167                 System.err.println("WARNING: grammar produced a circular forest\n" + this);
168                 //throw new Error("grammar produced a circular forest:\n" + this);
169                 return;
170             }
171
172             ht.add(this);
173             for(Forest<NodeType> f : hp) f.gather(ht);
174         }
175
176         private void touched() {
177             if (touched) return;
178             touched = true;
179             /*
180             FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
181             for(Forest f : hp)
182                 if (f instanceof Forest.One) f2.add(f);
183                 else for(Forest ff : ((Forest.Many<NodeType>)f))
184                     f2.add(ff);
185             hp = f2;
186             */
187         }
188         public boolean contains(Forest f) {
189             touched();
190             return hp.contains(f);
191         }
192         public void merge(Forest p) { 
193             if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
194             if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
195             hp.add(p, true);
196         }
197         boolean ambiguous() {
198             touched();
199             if (hp.size()==0) return false;
200             if (hp.size()==1) return hp.iterator().next().ambiguous();
201             return true;
202         }
203
204         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
205             touched();
206             if (ignore.contains(this)) { ht.add(bogus); return; }
207             for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
208         }
209
210
211         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
212
213         public boolean isTransparent() { return hp.size()==1; }
214         public boolean isHidden() { return hp.size()==0; }
215         public void edges(GraphViz.Node n) {
216             if (hp.size()==1) { hp.iterator().next().edges(n); return; }
217             for(Forest f : hp) f.edges(n);
218         }
219         public GraphViz.Node toGraphViz(GraphViz gv) {
220             if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
221             if (gv.hasNode(this)) return gv.createNode(this);
222             GraphViz.Node n = gv.createNode(this);
223             n.label = "?";
224             n.color = "red";
225             for(Forest f : hp) n.edge(f, null);
226             return n;
227         }
228     }
229
230     // Statics //////////////////////////////////////////////////////////////////////////////
231
232     private static Tree[] tree_hint = new Tree[0];
233     private static String[] string_hint = new String[0];
234     private static final Forest[] emptyForestArray = new Forest[0];
235
236     protected String  headToString()    { return null; }
237     protected String  headToJava()      { return "null"; }
238     protected String  left()            { return "<?"; }
239     protected String  right()           { return "?>"; }
240     protected boolean ignoreSingleton() { return true; }
241 }