throw error when Forest created with head()
[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.*;
5 import edu.berkeley.sbp.Sequence.Position;
6 import edu.berkeley.sbp.util.*;
7 import java.io.*;
8 import java.util.*;
9 import java.lang.reflect.*;
10
11 /**
12  *   <font color=blue>
13  *   An efficient representation of a collection of trees (Tomita's
14  *   shared packed parse forest).
15  *   </font>
16  */
17 public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
18
19     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
20     public abstract Tree<NodeType> expand1() throws Ambiguous;
21
22     /** expand this forest into a set of trees */
23     public void expand(HashSet<Tree<NodeType>> ht) { expand(ht, new HashSet<Forest<NodeType>>(), null); }
24
25     static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
26         if (loc == null) throw new RuntimeException("invoked Forest.create(null,,,,) -- this should never happen");
27         return new One<NodeType>(loc, head, children, lift);
28     }
29
30     /** create a new forest */
31     public static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children) {
32         return Forest.create(loc, head, children, false); }
33
34     // Package-Private //////////////////////////////////////////////////////////////////////////////
35
36     abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
37     abstract void gather(HashSet<Forest<NodeType>> ignore);
38     abstract void edges(GraphViz.Node n);
39     boolean ambiguous() { return false; }
40     
41     abstract Input.Region getRegion();
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           lift;
54
55         Input.Region getRegion() { return location; }
56
57         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
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.lift = lift;
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, lift);
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, Tree<NodeType> bogus) {
82             if (i==children.length) {
83                 ht.add(new Tree<NodeType>(location, head, ta, lift));
84             } else {
85                 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
86                 children[i].expand(ht2, ignore, bogus);
87                 for(Tree<NodeType> tc : ht2) {
88                     ta[i] = tc;
89                     expand(i+1, ta, ht, ignore, bogus);
90                     ta[i] = null;
91                 }
92             }
93         }
94
95         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
96
97         public boolean isTransparent() { return false; }
98         public boolean isHidden() { return false; }
99         public GraphViz.Node toGraphViz(GraphViz gv) {
100             if (gv.hasNode(this)) return gv.createNode(this);
101             GraphViz.Node n = gv.createNode(this);
102             n.label = headToString()==null?"":headToString();
103             n.directed = true;
104             edges(n);
105             return n;
106         }
107         boolean edges = false; // FIXME ??
108         public void edges(GraphViz.Node n) {
109             if (edges) return;
110             edges = true;
111             for(int i=0; i<children.length; i++) {
112                 if (i==children.length-1 && lift && !children[i].ambiguous()) {
113                     children[i].edges(n);
114                 } else {
115                     n.edge(children[i], null);
116                 }
117             }
118         }
119
120         protected String  headToString()         { return head==null?null:head.toString(); }
121         protected String  headToJava()           { return "null"; }
122         protected String  left()                 { return "{"; }
123         protected String  right()                { return "}"; }
124         protected boolean ignoreSingleton()      { return false; }
125     }
126
127
128     // Many //////////////////////////////////////////////////////////////////////////////
129
130     /** An "ambiguity node"; this is immutable once it has been "looked at" */
131     static class Many<NodeType> extends Forest<NodeType> {
132
133         private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
134         private boolean touched = false;
135
136         public Many() { }
137
138         Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
139
140         public Tree<NodeType> expand1() throws Ambiguous {
141             touched();
142             if (hp.size() > 1) {
143                 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
144                 Iterator<Forest<NodeType>> ih = hp.iterator();
145                 ih.next().gather(hf0);
146                 for(Forest<NodeType> f : hp) {
147                     HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
148                     f.gather(hf1);
149                     hf0.retainAll(hf1);
150                 }
151                 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
152                 expand(ht, hf0, new Tree(null, "*"));
153                 throw new Ambiguous((Forest<?>)this,
154                                     (HashSet<Tree<?>>)(Object)ht);
155             }
156             return hp.iterator().next().expand1();
157         }
158         
159         void gather(HashSet<Forest<NodeType>> ht) {
160             touched();
161
162             // FIXME: do something more sensible here
163             if (ht.contains(this)) {
164                 System.err.println("WARNING: grammar produced a circular forest\n" + this);
165                 //throw new Error("grammar produced a circular forest:\n" + this);
166                 return;
167             }
168
169             ht.add(this);
170             for(Forest<NodeType> f : hp) f.gather(ht);
171         }
172
173         private void touched() {
174             if (touched) return;
175             touched = true;
176             /*
177             FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
178             for(Forest f : hp)
179                 if (f instanceof Forest.One) f2.add(f);
180                 else for(Forest ff : ((Forest.Many<NodeType>)f))
181                     f2.add(ff);
182             hp = f2;
183             */
184         }
185         public boolean contains(Forest f) {
186             touched();
187             return hp.contains(f);
188         }
189         public void merge(Forest p) { 
190             if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
191             if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
192             hp.add(p, true);
193         }
194         boolean ambiguous() {
195             touched();
196             if (hp.size()==0) return false;
197             if (hp.size()==1) return hp.iterator().next().ambiguous();
198             return true;
199         }
200
201         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
202             touched();
203             if (ignore.contains(this)) { ht.add(bogus); return; }
204             for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
205         }
206
207
208         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
209
210         public boolean isTransparent() { return hp.size()==1; }
211         public boolean isHidden() { return hp.size()==0; }
212         public void edges(GraphViz.Node n) {
213             if (hp.size()==1) { hp.iterator().next().edges(n); return; }
214             for(Forest f : hp) f.edges(n);
215         }
216         public GraphViz.Node toGraphViz(GraphViz gv) {
217             if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
218             if (gv.hasNode(this)) return gv.createNode(this);
219             GraphViz.Node n = gv.createNode(this);
220             n.label = "?";
221             n.color = "red";
222             for(Forest f : hp) n.edge(f, null);
223             return n;
224         }
225     }
226
227     // Statics //////////////////////////////////////////////////////////////////////////////
228
229     private static Tree[] tree_hint = new Tree[0];
230     private static String[] string_hint = new String[0];
231     private static final Forest[] emptyForestArray = new Forest[0];
232
233     protected String  headToString()    { return null; }
234     protected String  headToJava()      { return "null"; }
235     protected String  left()            { return "<?"; }
236     protected String  right()           { return "?>"; }
237     protected boolean ignoreSingleton() { return true; }
238 }