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.CInstance;
6   
7   import java.util.Set;
8   import java.util.HashSet;
9   import java.util.List;
10  
11  /**
12   * Represents a formula declaration which is either a constant, a variable 
13   * or composed via an operator. 
14   * Note the difference to a {@link Formula}. 
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 FormulaDecl {
23  
24      /* -------------------------------------------------------------------- *
25       * inner classes.                                                       *
26       * -------------------------------------------------------------------- */
27  
28      /**
29       * Represents atomic formulae consisting of a constant only. 
30       */
31      static final class Const extends FormulaDecl {
32  	private final Set<Deficiency> val;
33  	private final Type type;
34  	Const(Type type, Set<Deficiency> val) {
35  	    this.type = type;
36  	    this.val  = val;
37  	}
38  	public Type retType() {
39  	    return this.type;
40  	}
41  	public Formula resolve(CInstance cInst) {
42  	    return new Formula.Const(this.val, this.type);
43  	}
44  
45  	public String toString() {
46  	    StringBuffer res = new StringBuffer();
47  	    res.append('<');
48  	    res.append(this.type);
49  	    res.append(">{");
50  	    res.append(this.val);
51  	    res.append('}');
52  	    return res.toString();
53  	}
54      } // class Const 
55  
56      /**
57       * Represents atomic formulae consisting of a variable only. 
58       */
59      static final class Var extends FormulaDecl {
60  	private final CClass.SClassDecl decl;
61  	// from variable where the root formula is defined to this variable 
62  	// which is "more interior". 
63  	private final List<String> path;
64  
65  	Var(CClass.SClassDecl decl, List<String> path) {
66  	    this.decl = decl;
67  	    this.path = path;
68  	}
69  	public Type retType() {
70  	    return this.decl.getSClass().getType();
71  	}
72  	public Formula resolve(CInstance cInst) {
73  	    return new Formula.Var(cInst.getEffect(this.path),
74  				   this.decl.getName());
75  	}
76  	public String toString() {
77  	    StringBuffer res = new StringBuffer();
78  	    res.append("$" + this.decl.getName() + "$");
79  	    return res.toString();
80  	}
81      } // class Var 
82  
83      /**
84       * Represents composite formulae 
85       * consisting of a operation {@link #oper} 
86       * and a set of arguments {@link #args}. 
87       */
88      static final class Comp extends FormulaDecl {
89  
90  	private final Operation oper;
91  	private final Set<FormulaDecl> args;
92  
93  	Comp(Operation oper, Set<FormulaDecl> args) {
94  	    this.oper = oper;
95  	    this.args = args;
96  	    retType(); // for tests only 
97  	}
98  
99  	public Type retType() {
100 	    return this.oper.retType(this.args);
101 	}
102 
103 	public Formula resolve(CInstance cInst) {
104 	    Set<Formula> fArgs = new HashSet<Formula>();
105 	    for (FormulaDecl decl : this.args) {
106 		fArgs.add(decl.resolve(cInst));
107 	    }
108 
109 	    return Formula.getFormula(this.oper.getEval(retType()), fArgs);
110 	}
111 
112 	public String toString() {
113 	    StringBuffer res = new StringBuffer();
114 	    // remove the enclosing brackets from argument list 
115 	    String argsStr = this.args.toString();
116 	    argsStr = argsStr.substring(1, argsStr.length() - 2);
117 
118 	    res.append(this.oper);
119 	    res.append('(');
120 	    res.append(argsStr);
121 	    res.append(')');
122 	    return res.toString();
123 	}
124     } // class Var 
125 
126 
127     /* -------------------------------------------------------------------- *
128      * static creator methods used by CClassParser.                         *
129      * -------------------------------------------------------------------- */
130 
131     private FormulaDecl() {
132     }
133 
134     public static FormulaDecl getConst(Type type, Set<Deficiency> val) {
135 	return new Const(type, val);
136     }
137 
138     public static FormulaDecl getVar(CClass.SClassDecl decl,
139 				     List<String> path) {
140 	return new Var(decl, path);
141     }
142 
143     public static FormulaDecl getComp(Operation oper, Set<FormulaDecl> args) {
144 	return new Comp(oper, args);
145     }
146 
147     /* -------------------------------------------------------------------- *
148      * further methods.                                                     *
149      * -------------------------------------------------------------------- */
150 
151     public abstract Type retType();
152 
153     public abstract Formula resolve(CInstance cInst);
154 
155     public final boolean equals(Object obj) {
156 	return super.equals(obj);
157     }
158 
159     public final int hashCode() {
160 	return super.hashCode();
161     }
162 
163 
164 } // FormulaDecl