1
2
3
4 package net.sf.bddbddb.order;
5
6 import java.util.Comparator;
7
8 import net.sf.bddbddb.Attribute;
9 import net.sf.bddbddb.InferenceRule;
10 import net.sf.bddbddb.Variable;
11 import net.sf.bddbddb.XMLFactory;
12
13 import org.jdom.Element;
14
15 /***
16 * OrderConstraint
17 *
18 * @author jwhaley
19 * @version $Id: OrderConstraint.java 435 2005-02-13 03:24:59Z cs343 $
20 */
21 public abstract class OrderConstraint {
22
23 public static final Comparator elementComparator = new Comparator() {
24
25 public int compare(Object o1, Object o2) {
26 if (o1.equals(o2)) return 0;
27 return OrderConstraint.compare(o1, o2)?-1:1;
28 }
29
30 };
31
32 static final boolean compare(Object a, Object b) {
33 if (a.equals(b)) return true;
34 String s1 = a.toString();
35 String s2 = b.toString();
36 int c = s1.compareTo(s2);
37 if (c == 0) {
38 if (a instanceof Attribute)
39 c = ((Attribute) a).getRelation().toString().compareTo(((Attribute) b).getRelation().toString());
40 }
41
42 return c <= 0;
43 }
44
45 public static OrderConstraint makeConstraint(int type, Object a, Object b) {
46 if (compare(a, b)) {
47 switch (type) {
48 case 0: return new BeforeConstraint(a, b);
49 case 1: return new InterleaveConstraint(a, b);
50 case 2: return new AfterConstraint(a, b);
51 }
52 } else {
53 switch (type) {
54 case 0: return new BeforeConstraint(b, a);
55 case 1: return new InterleaveConstraint(b, a);
56 case 2: return new AfterConstraint(b, a);
57 }
58 }
59 return null;
60 }
61
62 public static OrderConstraint makePrecedenceConstraint(Object a, Object b) {
63 if (compare(a, b)) return new BeforeConstraint(a, b);
64 else return new AfterConstraint(b, a);
65 }
66
67 public static OrderConstraint makeInterleaveConstraint(Object a, Object b) {
68 if (compare(a, b)) return new InterleaveConstraint(a, b);
69 else return new InterleaveConstraint(b, a);
70 }
71
72 Object a, b;
73
74 protected OrderConstraint(Object a, Object b) {
75 this.a = a;
76 this.b = b;
77 }
78
79 public Object getFirst() {
80 return a;
81 }
82
83 public Object getSecond() {
84 return b;
85 }
86
87 public int hashCode() {
88 return a.hashCode() ^ (b.hashCode() << 1);
89 }
90
91 public boolean equals(OrderConstraint that) {
92 return this.getClass().equals(that.getClass()) &&
93 this.a.equals(that.a) && this.b.equals(that.b);
94 }
95
96 public boolean equals(Object o) {
97 if (o instanceof OrderConstraint)
98 return equals((OrderConstraint) o);
99 else
100 return false;
101 }
102
103 public int compareTo(Object o) {
104 return compareTo((OrderConstraint) o);
105 }
106
107 public int compareTo(OrderConstraint o) {
108 return this.toString().compareTo(o.toString());
109 }
110
111 public boolean isAttributeConstraint() {
112 return a instanceof Attribute;
113 }
114 public boolean isVariableConstraint() {
115 return a instanceof Variable;
116 }
117
118 public boolean obeyedBy(Order o) {
119 return o.getConstraints().contains(this);
120 }
121
122 public abstract int getType();
123 public abstract boolean isOpposite(OrderConstraint that);
124 public abstract OrderConstraint getOpposite1();
125 public abstract OrderConstraint getOpposite2();
126 public abstract Element toXMLElement(InferenceRule ir);
127
128 private static Element toXMLElement(Object o, InferenceRule ir) {
129 if (o instanceof Variable) {
130 return ((Variable) o).toXMLElement(ir);
131 } else if (o instanceof Attribute) {
132 return ((Attribute) o).toXMLElement();
133 } else {
134 return null;
135 }
136 }
137
138 public static OrderConstraint fromXMLElement(Element e, XMLFactory f) {
139 Object a = getElement((Element) e.getContent(0), f);
140 Object b = getElement((Element) e.getContent(1), f);
141 if (e.getName().equals("beforeConstraint")) {
142 return new BeforeConstraint(a, b);
143 } else if (e.getName().equals("afterConstraint")) {
144 return new AfterConstraint(a, b);
145 } else if (e.getName().equals("interleaveConstraint")) {
146 return new InterleaveConstraint(a, b);
147 } else {
148 return null;
149 }
150 }
151
152 protected void addXMLContent(Element e, InferenceRule ir) {
153 e.addContent(toXMLElement(a, ir));
154 e.addContent(toXMLElement(b, ir));
155 }
156
157 protected static Object getElement(Element e, XMLFactory f) {
158 if (e.getName().equals("variable"))
159 return Variable.fromXMLElement(e, f);
160 else if (e.getName().equals("attribute"))
161 return Attribute.fromXMLElement(e, f);
162 else
163 return null;
164 }
165
166 public static class BeforeConstraint extends OrderConstraint {
167
168 private BeforeConstraint(Object a, Object b) {
169 super(a, b);
170 }
171
172 public String toString() {
173 return a+"<"+b;
174 }
175
176 public int getType() { return 0; }
177
178 public boolean isOpposite(OrderConstraint that) {
179 if (that instanceof AfterConstraint ||
180 that instanceof InterleaveConstraint) {
181 return this.a.equals(that.a) && this.b.equals(that.b);
182 } else {
183 return false;
184 }
185 }
186
187 public OrderConstraint getOpposite1() {
188 return new AfterConstraint(a, b);
189 }
190
191 public OrderConstraint getOpposite2() {
192 return new InterleaveConstraint(a, b);
193 }
194
195 public Element toXMLElement(InferenceRule ir) {
196 Element e = new Element("beforeConstraint");
197 addXMLContent(e, ir);
198 return e;
199 }
200 }
201
202 public static class AfterConstraint extends OrderConstraint {
203
204 private AfterConstraint(Object a, Object b) {
205 super(a, b);
206 }
207
208 public String toString() {
209 return a+">"+b;
210 }
211
212 public int getType() { return 2; }
213
214 public boolean isOpposite(OrderConstraint that) {
215 if (that instanceof BeforeConstraint ||
216 that instanceof InterleaveConstraint) {
217 return this.a.equals(that.a) && this.b.equals(that.b);
218 } else {
219 return false;
220 }
221 }
222
223 public OrderConstraint getOpposite1() {
224 return new BeforeConstraint(a, b);
225 }
226
227 public OrderConstraint getOpposite2() {
228 return new InterleaveConstraint(a, b);
229 }
230
231 public Element toXMLElement(InferenceRule ir) {
232 Element e = new Element("afterConstraint");
233 addXMLContent(e, ir);
234 return e;
235 }
236 }
237
238 public static class InterleaveConstraint extends OrderConstraint {
239
240 private InterleaveConstraint(Object a, Object b) {
241 super(a, b);
242 }
243
244 public String toString() {
245 return a+"~"+b;
246 }
247
248 public int getType() { return 1; }
249
250 public boolean isOpposite(OrderConstraint that) {
251 if (that instanceof BeforeConstraint ||
252 that instanceof AfterConstraint) {
253 return this.a.equals(that.a) && this.b.equals(that.b);
254 } else {
255 return false;
256 }
257 }
258
259 public OrderConstraint getOpposite1() {
260 return new BeforeConstraint(a, b);
261 }
262
263 public OrderConstraint getOpposite2() {
264 return new AfterConstraint(a, b);
265 }
266
267 public Element toXMLElement(InferenceRule ir) {
268 Element e = new Element("afterConstraint");
269 addXMLContent(e, ir);
270 return e;
271 }
272 }
273 }