-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpression.java
More file actions
303 lines (281 loc) · 8.75 KB
/
Expression.java
File metadata and controls
303 lines (281 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Original Licensing Copyright
*
* Copyright 2014 Frank Asseg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Modifications Licensing Copyright
*
* Lightweight BigDecimal array-based stack.
* Copyright (C) 2021 DZ-FSDev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.dz_fs_dev.objecthunter.expr4j;
import com.dz_fs_dev.objecthunter.exp4j.function.Function;
import com.dz_fs_dev.objecthunter.exp4j.function.Functions;
import com.dz_fs_dev.objecthunter.exp4j.operator.Operator;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import com.dz_fs_dev.objecthunter.exp4j.tokenizer.FunctionToken;
import com.dz_fs_dev.objecthunter.exp4j.tokenizer.NumberToken;
import com.dz_fs_dev.objecthunter.exp4j.tokenizer.OperatorToken;
import com.dz_fs_dev.objecthunter.exp4j.tokenizer.Token;
import com.dz_fs_dev.objecthunter.exp4j.tokenizer.VariableToken;
/**
* A container which holds a tokenized expression. Supports asynchronous
* evaluation of the expression.
*
* @author DZ-FSDev, Frank Asseg
* @since 17.0.1
* @version 0.0.2
*/
public class Expression {
private final Token[] tokens;
private final Map<String, BigDecimal> variables;
private final Set<String> userFunctionNames;
/**
* Returns a map of default variables containing values for the following:
* <ul>
* <li>pi</li>
* <li>π</li>
* <li>φ</li>
* <li>e</li>
* </ul>
*
* @return A map of default variables containing values.
* @since 0.0.2
*/
private static Map<String, BigDecimal> createDefaultVariables() {
final Map<String, BigDecimal> vars = new HashMap<String, BigDecimal>(4);
vars.put("pi", BigDecimal.valueOf(Math.PI));
vars.put("π", BigDecimal.valueOf(Math.PI));
vars.put("φ", BigDecimal.valueOf(1.61803398874d));
vars.put("e", BigDecimal.valueOf(Math.E));
return vars;
}
/**
* Creates a new expression that is a copy of the existing one.
*
* @param existing the expression to copy
*/
public Expression(final Expression existing) {
this.tokens = Arrays.copyOf(existing.tokens, existing.tokens.length);
this.variables = new HashMap<>();
this.variables.putAll(existing.variables);
this.userFunctionNames = new HashSet<>(existing.userFunctionNames);
}
/**
*
* @param tokens
*/
Expression(final Token[] tokens) {
this.tokens = tokens;
this.variables = createDefaultVariables();
this.userFunctionNames = Collections.emptySet();
}
/**
*
* @param tokens
* @param userFunctionNames
*/
Expression(final Token[] tokens, Set<String> userFunctionNames) {
this.tokens = tokens;
this.variables = createDefaultVariables();
this.userFunctionNames = userFunctionNames;
}
/**
*
* @param name
* @param value
* @return
* @since 0.0.2
*/
public Expression setVariable(final String name, final BigDecimal value) {
this.checkVariableName(name);
this.variables.put(name, value);
return this;
}
/**
*
* @param name
*/
private void checkVariableName(String name) {
if (this.userFunctionNames.contains(name) || Functions.getBuiltinFunction(name) != null) {
throw new IllegalArgumentException("The variable name '" + name + "' is invalid. Since there exists a function with the same name");
}
}
/**
*
* @param variables
* @return
* @since 0.0.2
*/
public Expression setVariables(Map<String, BigDecimal> variables) {
for (Map.Entry<String, BigDecimal> v : variables.entrySet()) {
this.setVariable(v.getKey(), v.getValue());
}
return this;
}
/**
*
* @return
*/
public Expression clearVariables() {
this.variables.clear();
return this;
}
/**
*
* @return
*/
public Set<String> getVariableNames() {
final Set<String> variables = new HashSet<>();
for (final Token t : tokens) {
if (t.getType() == Token.TOKEN_VARIABLE)
variables.add(((VariableToken) t).getName());
}
return variables;
}
/**
*
* @param checkVariablesSet
* @return
*/
public ValidationResult validate(boolean checkVariablesSet) {
final List<String> errors = new ArrayList<>(0);
if (checkVariablesSet) {
/* check that all vars have a value set */
for (final Token t : this.tokens) {
if (t.getType() == Token.TOKEN_VARIABLE) {
final String var = ((VariableToken) t).getName();
if (!variables.containsKey(var)) {
errors.add("The setVariable '" + var + "' has not been set");
}
}
}
}
int count = 0;
for (Token tok : this.tokens) {
switch (tok.getType()) {
case Token.TOKEN_NUMBER:
case Token.TOKEN_VARIABLE:
count++;
break;
case Token.TOKEN_FUNCTION:
final Function func = ((FunctionToken) tok).getFunction();
final int argsNum = func.getNumArguments();
if (argsNum > count) {
errors.add("Not enough arguments for '" + func.getName() + "'");
}
if (argsNum > 1) {
count -= argsNum - 1;
} else if (argsNum == 0) {
// see https://github.com/fasseg/exp4j/issues/59
count++;
}
break;
case Token.TOKEN_OPERATOR:
Operator op = ((OperatorToken) tok).getOperator();
if (op.getNumOperands() == 2) {
count--;
}
break;
}
if (count < 1) {
errors.add("Too many operators");
return new ValidationResult(false, errors);
}
}
if (count > 1) {
errors.add("Too many operands");
}
return errors.size() == 0 ? ValidationResult.SUCCESS : new ValidationResult(false, errors);
}
public ValidationResult validate() {
return validate(true);
}
/**
*
* @param executor
* @return
* @since 0.0.2
*/
public Future<BigDecimal> evaluateAsync(ExecutorService executor) {
return executor.submit(this::evaluate);
}
/**
*
* @return
* @since 0.0.2
*/
public BigDecimal evaluate() {
final ArrayStack output = new ArrayStack();
for (Token t : tokens) {
if (t.getType() == Token.TOKEN_NUMBER) {
output.push(((NumberToken) t).getValue());
} else if (t.getType() == Token.TOKEN_VARIABLE) {
final String name = ((VariableToken) t).getName();
final BigDecimal value = this.variables.get(name);
if (value == null) {
throw new IllegalArgumentException("No value has been set for the setVariable '" + name + "'.");
}
output.push(value);
} else if (t.getType() == Token.TOKEN_OPERATOR) {
OperatorToken op = (OperatorToken) t;
if (output.size() < op.getOperator().getNumOperands()) {
throw new IllegalArgumentException("Invalid number of operands available for '" + op.getOperator().getSymbol() + "' operator");
}
if (op.getOperator().getNumOperands() == 2) {
/* pop the operands and push the result of the operation */
BigDecimal rightArg = output.pop();
BigDecimal leftArg = output.pop();
output.push(op.getOperator().apply(leftArg, rightArg));
} else if (op.getOperator().getNumOperands() == 1) {
/* pop the operand and push the result of the operation */
BigDecimal arg = output.pop();
output.push(op.getOperator().apply(arg));
}
} else if (t.getType() == Token.TOKEN_FUNCTION) {
FunctionToken func = (FunctionToken) t;
final int numArguments = func.getFunction().getNumArguments();
if (output.size() < numArguments) {
throw new IllegalArgumentException("Invalid number of arguments available for '" + func.getFunction().getName() + "' function");
}
/* collect the arguments from the stack */
BigDecimal[] args = new BigDecimal[numArguments];
for (int j = numArguments - 1; j >= 0; j--) {
args[j] = output.pop();
}
output.push(func.getFunction().apply(args));
}
}
if (output.size() > 1) {
throw new IllegalArgumentException("Invalid number of items on the output queue. Might be caused by an invalid number of arguments for a function.");
}
return output.pop();
}
}