View Javadoc
1   package eu.simuline.relana.expressions;
2   
3   import eu.simuline.relana.model.Deficiency;
4   //import eu.simuline.relana.model.CClass;
5   import eu.simuline.relana.model.SInstance;
6   
7   import java.util.Set;
8   import java.util.HashSet;
9   
10  /**
11   * Represents a formula which is either a constant, a variable 
12   * or composed via an operator. 
13   * In contrast to a formula declaration represented by {@link FormulaDecl}, 
14   * a formula is within the context of an {@link SInstance}. 
15   *
16   *
17   * Created: Fri Apr 29 10:56:37 2005
18   *
19   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
20   * @version 1.0
21   */
22  public abstract class Formula {
23  
24      /* -------------------------------------------------------------------- *
25       * inner classes.                                                       *
26       * -------------------------------------------------------------------- */
27  
28      // **** This suppression does not work... why not???? 
29      @edu.umd.cs.findbugs.annotations.SuppressWarnings
30      (value = "IC_SUPERCLASS_USES_SUBCLASS_DURING_INITIALIZATION",
31       justification = "check in main method: field correctly initialized ")
32      public static final Formula EMPTY_EXPRESSION = 
33  	Const.create(new HashSet<Deficiency>(), Type.getEmpty());
34  
35      /**
36       * Represents a constant interpreted as atomic formula. 
37       */
38      public static final class Const extends Formula {
39  
40  	/* ---------------------------------------------------------------- *
41  	 * fields.                                                          *
42  	 * ---------------------------------------------------------------- */
43  
44  	private final Set<Deficiency> val;
45  	private final Type type;
46  
47  	/* ---------------------------------------------------------------- *
48  	 * constructors.                                                    *
49  	 * ---------------------------------------------------------------- */
50  
51  	@SuppressWarnings("checkstyle:MethodParamPad")
52  	public Const(Set<Deficiency> val, Type type) {
53  	    this.val  = val;
54  	    this.type = type;
55  	    if (this.type != null && !this.type.isValid(val)) {
56  		throw new IllegalArgumentException
57  		    ("invalid set " + this.val 
58  		     + " for type " + this.type + ". ");
59  	    }
60  
61  	}
62  
63  	/* ---------------------------------------------------------------- *
64  	 * methods.                                                         *
65  	 * ---------------------------------------------------------------- */
66  
67  	private static Const create(Set<Deficiency> val, Type type) {
68  	    return new Const(val, type);
69  	}
70  
71  	public Formula remove(SInstance serv, Deficiency def) {
72  	    return this;
73  	}
74  
75  	public Formula add(SInstance serv, Deficiency def) {
76  	    return this;
77  	}
78  
79  	public Formula substitute(SInstance serv, Formula form) {
80  	    return this;
81  	}
82  	
83  	public Set<SInstance> getVars() {
84  	    return new HashSet<SInstance>();
85  	}
86  	public Set<Deficiency> getConst() {
87  	    return this.val;
88  	}
89  	public Set<Deficiency> getMax() {
90  	    return this.val;
91  	}
92  	public Set<Deficiency> getMin() {
93  	    return this.val;
94  	}
95  	public String toString() {
96  	    return "<>{" + this.val + "}";
97  	}
98      } // class Const 
99  
100     /**
101      * Represents a variable interpreted as atomic formula. 
102      */
103     public static final class Var extends Formula {
104 
105 	/* ---------------------------------------------------------------- *
106 	 * fields.                                                          *
107 	 * ---------------------------------------------------------------- */
108 
109 	private final SInstance varS;
110 	private final String name;
111 
112 	/* ---------------------------------------------------------------- *
113 	 * constructors.                                                    *
114 	 * ---------------------------------------------------------------- */
115 
116 	public Var(SInstance varS, String name) {
117 	    this.varS = varS;
118 	    this.name = name;
119 	}
120 
121 	/* ---------------------------------------------------------------- *
122 	 * methods.                                                         *
123 	 * ---------------------------------------------------------------- */
124 
125 	public Formula remove(SInstance serv, Deficiency def) {
126 	    throw new eu.simuline.util.NotYetImplementedException();
127 	    // return new Var(var);
128 	}
129 
130 	public Formula add(SInstance serv, Deficiency def) {
131 	    throw new eu.simuline.util.NotYetImplementedException();
132 	    // return new Var(var);
133 	}
134 
135 	public Formula substitute(SInstance serv, Formula form) {
136 //System.out.println("-----serv : "+serv);
137 //System.out.println("this.var : "+this.var);
138 //System.out.println(" (serv == this.var): "+(serv == this.var));
139 
140 	    return (serv == this.varS) ? form : this;
141 	}
142 
143 	public Set<SInstance> getVars() {
144 	    Set<SInstance> res = new HashSet<SInstance>();
145 	    res.add(this.varS);
146 	    return res;
147 	}
148 
149 	public Set<Deficiency> getConst() {
150 	    return null;
151 	}
152 
153 	public Set<Deficiency> getMax() {
154 	    return this.varS.getType().asSet();
155 	}
156 
157 	public Set<Deficiency> getMin() {
158 	    return new HashSet<Deficiency>();
159 	}
160 
161 	public String toString() {
162 	    StringBuffer res = new StringBuffer();
163 	    res.append("$" + this.name + "$");
164 	    return res.toString();
165 	}
166     } // class Var 
167 
168     /**
169      * Represents a compound formula, i.e. one defined via an operation. 
170      */
171     public static final class Comp extends Formula {
172 
173 	/* ---------------------------------------------------------------- *
174 	 * fields.                                                          *
175 	 * ---------------------------------------------------------------- */
176 
177 	private Set<Deficiency> min;
178 	private Set<Deficiency> max;
179 
180 	private final Operation.Eval oper;
181 	private final Set<Formula> args;
182 
183 	/* ---------------------------------------------------------------- *
184 	 * constructors.                                                    *
185 	 * ---------------------------------------------------------------- */
186 
187 	private Comp(Operation.Eval oper, Set<Formula> args) {
188 	    this.oper = oper;
189 	    this.args = args;
190 
191 	    Set<Set<Deficiency>> param = new HashSet<Set<Deficiency>>();
192 	    for (Formula arg : args) {
193 		param.add(arg.getMin());
194 	    }
195 	    this.min = oper.eval(param);
196 
197 	    param = new HashSet<Set<Deficiency>>();
198 	    for (Formula arg : args) {
199 		param.add(arg.getMax());
200 	    }
201 	    this.max = this.oper.eval(param);
202 
203 	    if (!this.oper.getOperation().isIsoAntitone()) {
204 		// exchange 
205 		Set<Deficiency> inter;
206 		inter = this.max;
207 		this.max = this.min;
208 		this.min = inter;
209 	    }
210 	}
211 
212 	/* ---------------------------------------------------------------- *
213 	 * methods.                                                         *
214 	 * ---------------------------------------------------------------- */
215 
216 	private static Comp create(Operation.Eval oper, 
217 				   Set<Formula> args) {
218 	    return new Comp(oper, args);
219 	}
220 
221 	public Formula remove(SInstance serv, Deficiency def) {
222 	    Set<Formula> newArgs = new HashSet<Formula>();
223 	    for (Formula form : args) {
224 		newArgs.add(form.remove(serv, def));
225 	    }
226 	    return getFormula(this.oper, newArgs);
227 	}
228 
229 	public Formula add(SInstance serv, Deficiency def) {
230 	    Set<Formula> newArgs = new HashSet<Formula>();
231 	    for (Formula form : args) {
232 		newArgs.add(form.add(serv, def));
233 	    }
234 	    return getFormula(this.oper, newArgs);
235 	}
236 
237 	public Formula substitute(SInstance serv, Formula form) {
238 	    Set<Formula> newArgs = new HashSet<Formula>();
239 	    for (Formula arg : this.args) {
240 		newArgs.add(arg.substitute(serv, form));
241 	    }
242 	    return getFormula(this.oper, newArgs);
243 	}
244 
245 	public Set<SInstance> getVars() {
246 	    Set<SInstance> res = new HashSet<SInstance>();
247 	    for (Formula arg : args) {
248 		res.addAll(arg.getVars());
249 	    }
250 	    return res;
251 	}
252 
253 	public Set<Deficiency> getConst() {
254 	    return null;
255 	}
256 
257 	public Set<Deficiency> getMax() {
258 	    return this.max;
259 	}
260 
261 	public Set<Deficiency> getMin() {
262 	    return this.min;
263 	}
264 
265 	public String toString() {
266 	    StringBuffer res = new StringBuffer();
267 	    res.append(this.oper);
268 	    res.append('(');
269 	    res.append(this.args);
270 	    res.append(')');
271 	    return res.toString();
272 	}
273     } // class Comp 
274 
275 
276     /* -------------------------------------------------------------------- *
277      * fields.                                                              *
278      * -------------------------------------------------------------------- */
279 
280 
281     /* -------------------------------------------------------------------- *
282      * static creator methods.                                              *
283      * -------------------------------------------------------------------- */
284 
285     private Formula() {
286     }
287 
288     public static Formula getFormula(Operation.Eval oper, Set<Formula> args) {
289 	Formula res = Comp.create(oper, args);
290 	return (res.getMin().size() == res.getMax().size())
291 	    ? Const.create(res.getMin(), null)
292 	    : res;
293     }
294 
295     /* -------------------------------------------------------------------- *
296      * further methods.                                                     *
297      * -------------------------------------------------------------------- */
298 
299     public abstract Formula     remove(SInstance serv, Deficiency def);
300     public abstract Formula        add(SInstance serv, Deficiency def);
301     public abstract Formula substitute(SInstance serv, Formula form);
302 
303     public abstract Set<SInstance> getVars();
304 
305     /**
306      * Returns the constant represented by this formula, if any. 
307      * If this is not a constant formula, 
308      * i.e. an instance of {@link Formula.Const}, 
309      * then <code>null</code> is returned. 
310      *
311      * @return
312      *    <ul>
313      *    <li>
314      *    The constant represented by this formula 
315      *    if this is an instance of {@link Formula.Const}. 
316      *    <li>
317      *    <code>null</code> otherwise. 
318      *    </ul>
319      */
320     public abstract Set<Deficiency> getConst();
321     public abstract Set<Deficiency> getMin();
322     public abstract Set<Deficiency> getMax();
323 
324     public final boolean equals(Object obj) {
325 	return super.equals(obj);
326     }
327 
328     public final int hashCode() {
329 	return super.hashCode();
330     }
331 
332     // shows that EMPTY_EXPRESSION is correctly initialized 
333     public static final void main(String[] args) {
334 	System.out.println("EE: " + EMPTY_EXPRESSION);
335     }
336 
337 } // Formula