checkpoint
[sbp.git] / src / edu / berkeley / sbp / Union.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
9
10 /** an element which can produce one of several alternatives */
11 public class Union extends Element implements Iterable<Sequence> {
12
13     private final String name;
14     private final boolean synthetic;
15     private final List<Sequence> alternatives = new ArrayList<Sequence>();
16
17     public Union() { this(null, false); }
18     public Union(String name) { this(name, false); }
19
20     /**
21      *  Since every cycle in a non-degenerate grammar contains at
22      *  least one Union, every instance of this class must be able to
23      *  display itself in both "long form" (list of the long forms of
24      *  its alternatives) and "short form" (some abbreviation).
25      *
26      *  @param shortForm the "short form" display; usually 
27      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
28      */
29     public Union(String name, boolean synthetic) {
30         this.name = name;
31         this.synthetic = synthetic;
32     }
33
34     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
35     public boolean contains(Sequence s) { return alternatives.contains(s); }
36
37     /** adds an alternative */
38     public void add(Sequence s) {
39         alternatives.add(s);
40
41         // FIXME: does this make sense?
42         for(Sequence n : s.needs) add(n);
43         for(Sequence n : s.hates) add(n);
44     }
45
46
47     // Epsilon Form //////////////////////////////////////////////////////////////////////////////
48
49     // FIXME
50     private Forest.Many epsilonForm = null;
51     Forest epsilonForm() {
52         if (epsilonForm != null) return epsilonForm;
53         epsilonForm = new Forest.Many();
54         for(Sequence s : this) {
55             // FIXME FIXME FIXME
56             if (new Walk.Cache().possiblyEpsilon(s))
57                 epsilonForm.merge(s.epsilonForm());
58         }
59         return epsilonForm;
60     }
61
62
63     // Display //////////////////////////////////////////////////////////////////////////////
64
65     public String getName() {
66         if (name != null) return name;
67         return "(anon_union)";
68     }
69     public String toString() {
70         if (name != null) return name;
71         StringBuffer sb = new StringBuffer();
72         sb.append("(");
73         bodyToString(sb, "", " | ");
74         sb.append(")");
75         return sb.toString();
76     }
77     public StringBuffer toString(StringBuffer sb) {
78         if (synthetic) return sb;
79         boolean first = true;
80         String before = StringUtil.pad(15, getName()) + " = ";
81         if (alternatives.size()==0) {
82             sb.append(before);
83         } else {
84             bodyToString(sb,
85                          before,
86                          "\n" + StringUtil.pad(15, "")        + " | ");
87             sb.append('\n');
88         }
89         return sb;
90     }
91     
92     private void bodyToString(StringBuffer sb, String before, String between) {
93         boolean first = true;
94         for(Sequence s : this) {
95             if (s.lame) continue;
96             // FIXME: what to do here about printing out negated sequences?
97             sb.append(first ? before : between);
98             first = false;
99             sb.append(s.toString());
100         }
101     }
102
103 }