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
13
14
15
16
17
18
19
20
21
22 public abstract class FormulaDecl {
23
24
25
26
27
28
29
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 }
55
56
57
58
59 static final class Var extends FormulaDecl {
60 private final CClass.SClassDecl decl;
61
62
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 }
82
83
84
85
86
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();
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
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 }
125
126
127
128
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
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 }