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

Last change on this file since 311 was 311, checked in by ra33, 16 years ago

Fixed a few bugs that were makin unit tests fail...
Also added Greenstone search of HCI Bib tex

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