source: trunk/src/org/expeditee/simple/Primitives.java@ 309

Last change on this file since 309 was 270, checked in by ra33, 16 years ago
File size: 8.2 KB
Line 
1package org.expeditee.simple;
2
3import org.expeditee.math.ExpediteeJEP;
4
5public class Primitives extends Variables<SPrimitive<?>> {
6 private static final String[] prefixes = new String[] { SInteger.prefix,
7 SBoolean.prefix, SString.prefix, SReal.prefix, SCharacter.prefix };
8
9 public static boolean isPrimitive(String varName) {
10 for (String s : prefixes) {
11 if (varName.startsWith(s))
12 return true;
13 }
14 return false;
15 }
16
17 public static final int PREFIX_LENGTH = SInteger.prefix.length();
18
19 public Primitives() {
20 super();
21 }
22
23 protected String getType() {
24 return "primitive";
25 }
26
27 public void setValue(String variableName, SPrimitive newValue)
28 throws IncorrectTypeException {
29 try {
30 // if it is an existing variable change the value
31 SPrimitive v = getVariable(variableName);
32 v.setValue(newValue);
33 } catch (Exception e) {
34 try{
35 add(variableName, newValue);
36 }catch(IncorrectTypeException ite){
37 throw ite;
38 }catch(Exception ex){
39 //DO NOTHING... THIS SHOULD NOT HAPPEN
40 //But the ANT builder is complaining
41 ex.printStackTrace();
42 }
43 }
44 }
45
46 public void setValue(String variableName, String newValue) throws Exception {
47 setValue(variableName, new SString(newValue));
48 }
49
50 /**
51 * Adds a variable to the list of primitives.
52 *
53 * @param name
54 * the variable name
55 * @param value
56 * a primitive to take the new variables value from
57 * @throws Exception
58 * if the variable name is invalid
59 */
60 public void add(String name, SPrimitive value)
61 throws IncorrectTypeException {
62 // figure out the type and add it...
63 SPrimitive newVar;
64 if (name.startsWith(SInteger.prefix))
65 newVar = new SInteger();
66 else if (name.startsWith(SBoolean.prefix))
67 newVar = new SBoolean();
68 else if (name.startsWith(SReal.prefix))
69 newVar = new SReal();
70 else if (name.startsWith(SString.prefix))
71 newVar = new SString();
72 else if (name.startsWith(SCharacter.prefix))
73 newVar = new SCharacter();
74 else
75 throw new IncorrectTypeException(name, "primitive");
76 newVar.setName(name);
77 newVar.setValue(value);
78 list_.add(newVar);
79 }
80
81 public String getStringValue(String name) throws Exception {
82 return getVariable(name).stringValue();
83 }
84
85 public boolean getBooleanValue(String name) throws Exception {
86 return getVariable(name).booleanValue();
87 }
88
89 public long getIntegerValue(String name) throws Exception {
90 return getVariable(name).integerValue();
91 }
92
93 public double getDoubleValue(String name) throws Exception {
94 return getVariable(name).doubleValue();
95 }
96
97 public char getCharacterValue(String name) throws Exception {
98 return getVariable(name).characterValue();
99 }
100
101 /*
102 * public String setStringValue(String name, String value) throws Exception {
103 * return getVariable(name).stringValue(); }
104 *
105 * public boolean setBooleanValue(String name, boolean value) throws
106 * Exception { return getVariable(name).booleanValue(); }
107 *
108 * public long setIntegerValue(String name, long value) throws Exception {
109 * return getVariable(name).integerValue(); }
110 *
111 * public double setDoubleValue(String name, double value) throws Exception {
112 * return getVariable(name).doubleValue(); }
113 *
114 * public double setCharacterValue(String name, char value) throws Exception {
115 * return getVariable(name).characterValue(); }
116 */
117
118 public static boolean isNumeric(String varName) {
119 return varName.startsWith(SInteger.prefix)
120 || varName.startsWith(SReal.prefix);
121 }
122
123 public boolean equalValues(String var1, String var2) throws Exception {
124 return compareValues(var1, var2) == 0;
125 }
126
127 /**
128 * Checks if the string representation of two variables is identical
129 * ignoring case.
130 *
131 * @param var1
132 * @param var2
133 * @return
134 * @throws Exception
135 */
136 public boolean equalValuesNoCase(String var1, String var2) throws Exception {
137 return getStringValue(var1).equalsIgnoreCase(getStringValue(var2));
138 }
139
140 /**
141 *
142 * @param var1
143 * @param var2
144 * @return
145 * @throws Exception
146 */
147 public int compareValues(String var1, String var2) throws Exception {
148 // If one of them is a real and one is an integer then do a numeric
149 // comparison
150 if ((var1.startsWith(SReal.prefix) || var2.startsWith(SReal.prefix))) {
151 double v1 = getDoubleValue(var1);
152 double v2 = getDoubleValue(var2);
153 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
154 } else if ((var1.startsWith(SInteger.prefix) || var2
155 .startsWith(SInteger.prefix))) {
156 long v1 = getIntegerValue(var1);
157 long v2 = getIntegerValue(var2);
158 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
159 } else if ((var1.startsWith(SBoolean.prefix) || var2
160 .startsWith(SBoolean.prefix))) {
161 boolean v1 = getBooleanValue(var1);
162 boolean v2 = getBooleanValue(var2);
163 return v1 == v2 ? 0 : (v1 == true ? 1 : -1);
164 }
165 return getStringValue(var1).compareTo(getStringValue(var2));
166 }
167
168 /**
169 * Increments a variable.
170 *
171 * @param var
172 * the name of the variable to increment
173 * @throws Exception
174 */
175 public void add(String var) throws Exception {
176 setValue(var, new SReal(getVariable(var).doubleValue() + 1));
177 }
178
179 /**
180 * Decrements a variable.
181 *
182 * @param var
183 * the name of the variable to decrement
184 * @throws Exception
185 */
186 public void subtract(String var) throws Exception {
187 setValue(var, new SReal(getVariable(var).doubleValue() - 1));
188 }
189
190 /**
191 * Adds two variables to gether returning the value in the first.
192 *
193 * @param toSet
194 * @param amount
195 * @throws Exception
196 */
197 public void add(String toSet, String amount) throws Exception {
198 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
199 + getVariable(amount).doubleValue()));
200 }
201
202 public void subtract(String toSet, String amount) throws Exception {
203 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
204 - getVariable(amount).doubleValue()));
205 }
206
207 public void multiply(String toSet, String amount) throws Exception {
208 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
209 - getVariable(amount).doubleValue()));
210 }
211
212 public void divide(String toSet, String amount) throws Exception {
213 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
214 - getVariable(amount).doubleValue()));
215 }
216
217 public void add(String var1, String var2, String varAnswer)
218 throws Exception {
219 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
220 + getVariable(var2).doubleValue()));
221 }
222
223 public void subtract(String var1, String var2, String varAnswer)
224 throws Exception {
225 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
226 - getVariable(var2).doubleValue()));
227 }
228
229 public void divide(String var1, String var2, String varAnswer)
230 throws Exception {
231 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
232 / getVariable(var2).doubleValue()));
233 }
234
235 public void multiply(String var1, String var2, String varAnswer)
236 throws Exception {
237 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
238 * getVariable(var2).doubleValue()));
239 }
240
241 public void modulo(String var1, String var2, String varAnswer)
242 throws Exception {
243 setValue(varAnswer, new SReal(getVariable(var1).integerValue()
244 % getVariable(var2).integerValue()));
245 }
246
247 public void power(String var1, String var2, String varAnswer)
248 throws Exception {
249 setValue(varAnswer, new SReal(Math.pow(getVariable(var1).doubleValue(),
250 getVariable(var2).doubleValue())));
251 }
252
253 public void log(String variable, String answer) throws Exception {
254 setValue(answer, new SReal(Math
255 .log(getVariable(variable).doubleValue())));
256 }
257
258 public void log10(String variable, String answer) throws Exception {
259 setValue(answer, new SReal(Math.log10(getVariable(variable)
260 .doubleValue())));
261 }
262
263 public void sqrt(String variable, String answer) throws Exception {
264 setValue(answer, new SReal(Math.sqrt(getVariable(variable)
265 .doubleValue())));
266 }
267
268 public void exp(String variable, String answer) throws Exception {
269 setValue(answer, new SReal(Math
270 .exp(getVariable(variable).doubleValue())));
271 }
272
273 public void not(String variable, String answer) throws Exception {
274 setValue(answer, new SBoolean(!getVariable(variable).booleanValue()));
275 }
276
277 public void addToParser(ExpediteeJEP myParser) {
278 for (SPrimitive var : list_) {
279 try {
280 Double value = var.doubleValue();
281 myParser.addVariable(var.getName().substring(SReal.prefix.length()), value);
282 } catch (Exception e) {
283 }
284 }
285 }
286}
Note: See TracBrowser for help on using the repository browser.