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