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