d52c6e96209c53ebffa76640995dc33e126635b0
[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 /**
11  *  <font color=green>an element which can produce one of several alternatives</font>.
12  *  <p>
13  *
14  *  Unlike the other Elements, Union is not immutable once
15  *  constructed.  To simulate this desirable feature, it is immutable
16  *  <i>once examined</i> by taking its iterator or calling contains().
17  */
18 public class Union extends Element implements Iterable<Sequence> {
19
20     private final String name;
21     private final boolean synthetic;
22     private boolean viewed = false;
23
24     private final List<Sequence> alternatives = new ArrayList<Sequence>();
25
26     public Union(String name) { this(name, false); }
27
28     /**
29      *  Since every cycle in a non-degenerate grammar contains at
30      *  least one Union, every instance of this class must be able to
31      *  display itself in both "long form" (list of the long forms of
32      *  its alternatives) and "short form" (some name).
33      *
34      *  @param shortForm the "short form" display; for display purposes only
35      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
36      */
37     public Union(String name, boolean synthetic) {
38         this.name = name;
39         this.synthetic = synthetic;
40     }
41
42     public boolean contains(Sequence s) {
43         viewed = true;
44         return alternatives.contains(s);
45     }
46
47     /** iterator over this Union's Sequences */
48     public Iterator<Sequence> iterator() {
49         viewed = true;
50         return alternatives.iterator();
51     }
52
53     /** adds an alternative */
54     public void add(Sequence s) {
55         /*
56           FIXME
57         if (viewed)
58             throw new RuntimeException("attempt to add a Sequence to a Union that has already been examined:\n  "+this);
59         */
60         if (alternatives.contains(s)) return;
61         alternatives.add(s);
62     }
63
64     /** adds a one-element sequence */
65     public void add(Element e) {
66         add(Sequence.create(e));
67     }
68
69
70     // Epsilon Form //////////////////////////////////////////////////////////////////////////////
71
72     // FIXME
73     private Forest.Many epsilonForm = null;
74     Forest epsilonForm() {
75         if (epsilonForm != null) return epsilonForm;
76         epsilonForm = new Forest.Many();
77         for(Sequence s : this) {
78             // FIXME FIXME FIXME
79             if (new Walk.Cache().possiblyEpsilon(s))
80                 epsilonForm.merge(s.epsilonForm());
81         }
82         return epsilonForm;
83     }
84
85
86     // Display //////////////////////////////////////////////////////////////////////////////
87
88     public String getName() {
89         if (name != null) return name;
90         return "(anon_union)";
91     }
92     public String toString() {
93         viewed = true;
94         if (name != null) return name;
95         StringBuffer sb = new StringBuffer();
96         sb.append("(");
97         bodyToString(sb, "", " | ");
98         sb.append(")");
99         return sb.toString();
100     }
101
102     /** display this union in long/expanded form */
103     public StringBuffer toString(StringBuffer sb) {
104         viewed = true;
105         if (synthetic) return sb;
106         boolean first = true;
107         String before = StringUtil.pad(15, getName()) + " = ";
108         if (alternatives.size()==0) {
109             sb.append(before);
110         } else {
111             bodyToString(sb,
112                          before,
113                          "\n" + StringUtil.pad(15, "")        + " | ");
114             sb.append('\n');
115         }
116         return sb;
117     }
118     
119     private void bodyToString(StringBuffer sb, String before, String between) {
120         viewed = true;
121         boolean first = true;
122         for(Sequence s : this) {
123             // FIXME: what to do here about printing out negated sequences?
124             sb.append(first ? before : between);
125             first = false;
126             sb.append(s.toString());
127         }
128     }
129
130 }