/
AbstractEvaluator.java
401 lines (381 loc) · 17.7 KB
/
AbstractEvaluator.java
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package net.aufdemrand.denizen.utilities.javaluator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
/** An abstract evaluator, able to evaluate infix expressions.
* <br>Some standard evaluators are included in the library, you can define your own by subclassing this class.
* @param <T> The type of values handled by the evaluator
* @author Jean-Marc Astesana
* @see <a href="../../../license.html">License information</a>
*/
public abstract class AbstractEvaluator<T> {
private final Tokenizer tokenizer;
private final Map<String, Function> functions;
private final Map<String, List<Operator>> operators;
private final Map<String, Constant> constants;
private final String functionArgumentSeparator;
private final Map<String, BracketPair> functionBrackets;
private final Map<String, BracketPair> expressionBrackets;
/** Constructor.
* @param parameters The evaluator parameters.
* <br>Please note that there's no side effect between the evaluator and the parameters.
* So, changes made to the parameters after the call to this constructor are ignored by the instance.
*/
protected AbstractEvaluator(Parameters parameters) {
//TODO if constants, operators, functions are duplicated => error
final ArrayList<String> tokenDelimitersBuilder = new ArrayList<String>();
this.functions = new HashMap<String, Function>();
this.operators = new HashMap<String, List<Operator>>();
this.constants = new HashMap<String, Constant>();
this.functionBrackets = new HashMap<String, BracketPair>();
for (final BracketPair pair : parameters.getFunctionBrackets()) {
functionBrackets.put(pair.getOpen(), pair);
functionBrackets.put(pair.getClose(), pair);
tokenDelimitersBuilder.add(pair.getOpen());
tokenDelimitersBuilder.add(pair.getClose());
}
this.expressionBrackets = new HashMap<String, BracketPair>();
for (final BracketPair pair : parameters.getExpressionBrackets()) {
expressionBrackets.put(pair.getOpen(), pair);
expressionBrackets.put(pair.getClose(), pair);
tokenDelimitersBuilder.add(pair.getOpen());
tokenDelimitersBuilder.add(pair.getClose());
}
if (operators!=null) {
for (Operator ope : parameters.getOperators()) {
tokenDelimitersBuilder.add(ope.getSymbol());
List<Operator> known = this.operators.get(ope.getSymbol());
if (known==null) {
known = new ArrayList<Operator>();
this.operators.put(ope.getSymbol(), known);
}
known.add(ope);
if (known.size()>1) validateHomonyms(known);
}
}
boolean needFunctionSeparator = false;
if (parameters.getFunctions()!=null) {
for (Function function : parameters.getFunctions()) {
//TODO if function name contains operators or reserved chars => error
this.functions.put(parameters.getTranslation(function.getName()), function);
if (function.getMaximumArgumentCount()>1) needFunctionSeparator = true;
}
}
if (parameters.getConstants()!=null) {
for (Constant constant : parameters.getConstants()) {
this.constants.put(parameters.getTranslation(constant.getName()), constant);
}
}
functionArgumentSeparator = parameters.getFunctionArgumentSeparator();
if (needFunctionSeparator) {
tokenDelimitersBuilder.add(functionArgumentSeparator);
}
tokenizer = new Tokenizer(tokenDelimitersBuilder);
}
/** Validates that homonym operators are valid.
* <br>Homonym operators are operators with the same name (like the unary - and the binary - operators)
* <br>This method is called when homonyms are passed to the constructor.
* <br>This default implementation only allows the case where there's two operators, one binary and one unary.
* Subclasses can override this method in order to accept others configurations.
* @param operators The operators to validate.
* @throws IllegalArgumentException if the homonyms are not compatibles.
* @see #guessOperator(Token, List)
*/
protected void validateHomonyms(List<Operator> operators) {
if (operators.size()>2) throw new IllegalArgumentException();
}
/** When a token can be more than one operator (homonym operators), this method guesses the right operator.
* <br>A very common case is the - sign in arithmetic computation which can be an unary or a binary operator, depending
* on what was the previous token.
* <br><b>Warning:</b> maybe the arguments of this function are not enough to deal with all the cases.
* So, this part of the evaluation is in alpha state (method may change in the future).
* @param previous The last parsed tokens (the previous token in the infix expression we are evaluating).
* @param candidates The candidate tokens.
* @return A token
* @see #validateHomonyms(List)
*/
protected Operator guessOperator(Token previous, List<Operator> candidates) {
final int argCount = ((previous!=null) && (previous.isCloseBracket() || previous.isLiteral())) ? 2 : 1;
for (Operator operator : candidates) {
if (operator.getOperandCount()==argCount) return operator;
}
return null;
}
@SuppressWarnings("unchecked")
private void output(Stack<T> values, Token token, Object evaluationContext) {
if (token.isLiteral()) { // If the token is a literal, a constant, or a variable name
String literal = token.getLiteral();
Constant ct = this.constants.get(literal);
T value = ct==null?null:evaluate(ct, evaluationContext);
if (value==null && evaluationContext!=null && (evaluationContext instanceof AbstractVariableSet)) value = ((AbstractVariableSet<T>)evaluationContext).get(literal);
values.push(value!=null ? value : toValue(literal, evaluationContext));
} else if (token.isOperator()) {
Operator operator = token.getOperator();
values.push(evaluate(operator, getArguments(values, operator.getOperandCount()), evaluationContext));
} else {
throw new IllegalArgumentException();
}
}
/** Evaluates a constant.
* <br>Subclasses that support constants must override this method.
* The default implementation throws a RuntimeException meaning that implementor forget to implement this method
* while creating a subclass that accepts constants.
* @param constant The constant
* @param evaluationContext The context of the evaluation
* @return The constant's value
*/
protected T evaluate(Constant constant, Object evaluationContext) {
throw new RuntimeException("evaluate(Constant) is not implemented for "+constant.getName());
}
/** Evaluates an operation.
* <br>Subclasses that support operators must override this method.
* The default implementation throws a RuntimeException meaning that implementor forget to implement this method
* while creating a subclass that accepts operators.
* @param operator The operator
* @param operands The operands
* @param evaluationContext The context of the evaluation
* @return The result of the operation
*/
protected T evaluate(Operator operator, Iterator<T> operands, Object evaluationContext) {
throw new RuntimeException("evaluate(Operator, Iterator) is not implemented for "+operator.getSymbol());
}
/** Evaluates a function.
* <br>Subclasses that support functions must override this method.
* The default implementation throws a RuntimeException meaning that implementor forget to implement this method
* while creating a subclass that accepts functions.
* @param function The function
* @param arguments The function's arguments
* @param evaluationContext The context of the evaluation
* @return The result of the function
*/
protected T evaluate(Function function, Iterator<T> arguments, Object evaluationContext) {
throw new RuntimeException("evaluate(Function, Iterator) is not implemented for "+function.getName());
}
private void doFunction(Stack<T> values, Function function, int argCount, Object evaluationContext) {
if (function.getMinimumArgumentCount()>argCount || function.getMaximumArgumentCount()<argCount) {
throw new IllegalArgumentException("Invalid argument count for "+function.getName());
}
values.push(evaluate(function, getArguments(values, argCount), evaluationContext));
}
private Iterator<T> getArguments(Stack<T> values, int nb) {
// Be aware that arguments are in reverse order on the values stack.
// Don't forget to reorder them in the original order (the one they appear in the evaluated formula)
if (values.size()<nb) throw new IllegalArgumentException();
LinkedList<T> result = new LinkedList<T>();
for (int i = 0; i <nb ; i++) {
result.addFirst(values.pop());
}
return result.iterator();
}
/** Evaluates a literal (Converts it to a value).
* @param literal The literal to evaluate.
* @return an instance of T.
* @param evaluationContext The context of the evaluation
* @throws IllegalArgumentException if the literal can't be converted to a value.
*/
protected abstract T toValue(String literal, Object evaluationContext);
/** Evaluates an expression.
* @param expression The expression to evaluate.
* @return the result of the evaluation.
* @throws IllegalArgumentException if the expression is not correct.
*/
public T evaluate(String expression) {
return evaluate(expression, null);
}
/** Evaluates an expression that contains variables.
* @param expression The expression to evaluate.
* @param evaluationContext The context of the evaluation.
* <br>This context is an object that can contain useful dynamic data, for example the values of the variables
* used in the expression (Use an AbstractVariableSet to do that).<br>The context is not limited to variable values but
* can be used for any dynamic information. A good example is the <a href="javaluator.sourceforge.net">BooleanSetEvaluator</a> one.
* @return the result of the evaluation.
* @throws IllegalArgumentException if the expression is not correct.
* @see AbstractVariableSet
*/
public T evaluate(String expression, Object evaluationContext) {
final Stack<T> values = new Stack<T>(); // values stack
final Stack<Token> stack = new Stack<Token>(); // operator stack
final Stack<Integer> previousValuesSize = functions.size()==0?null:new Stack<Integer>();
final Iterator<String> tokens = tokenize(expression);
Token previous = null;
while (tokens.hasNext()) {
// read one token from the input stream
String trimmed = tokens.next().trim();
if (trimmed.length()==0) continue; // Ignored blank tokens
final Token token = toToken(previous, trimmed);
if (token.isOpenBracket()) {
// If the token is a left parenthesis, then push it onto the stack.
stack.push(token);
if (previous!=null && previous.isFunction()) {
if (!functionBrackets.containsKey(token.getBrackets().getOpen())) throw new IllegalArgumentException("Invalid bracket after function: "+trimmed);
} else {
if (!expressionBrackets.containsKey(token.getBrackets().getOpen())) throw new IllegalArgumentException("Invalid bracket in expression: "+trimmed);
}
} else if (token.isCloseBracket()) {
if (previous==null) throw new IllegalArgumentException("expression can't start with a close bracket");
if (previous.isFunctionArgumentSeparator()) throw new IllegalArgumentException("argument is missing");
BracketPair brackets = token.getBrackets();
// If the token is a right parenthesis:
boolean openBracketFound = false;
// Until the token at the top of the stack is a left parenthesis,
// pop operators off the stack onto the output queue
while (!stack.isEmpty()) {
Token sc = stack.pop();
if (sc.isOpenBracket()) {
if (sc.getBrackets().equals(brackets)) {
openBracketFound = true;
break;
} else {
throw new IllegalArgumentException("Invalid parenthesis match "+sc.getBrackets().getOpen()+brackets.getClose());
}
} else {
output(values, sc, evaluationContext);
}
}
if (!openBracketFound) {
// If the stack runs out without finding a left parenthesis, then
// there are mismatched parentheses.
throw new IllegalArgumentException("Parentheses mismatched");
}
if (!stack.isEmpty()) {
if (stack.peek().isFunction()) {
// If the token at the top of the stack is a function token, pop it
// onto the output queue.
int argCount = values.size()-previousValuesSize.pop();
doFunction(values, (Function)stack.pop().getFunction(), argCount, evaluationContext);
}
}
} else if (token.isFunctionArgumentSeparator()) {
if (previous==null) throw new IllegalArgumentException("expression can't start with a function argument separator");
// Verify that there was an argument before this separator
if (previous.isOpenBracket() || previous.isFunctionArgumentSeparator()) {
// The cases were operator miss an operand are detected elsewhere.
throw new IllegalArgumentException("argument is missing");
}
// If the token is a function argument separator
boolean pe = false;
while (!stack.isEmpty()) {
if (stack.peek().isOpenBracket()) {
pe = true;
break;
} else {
// Until the token at the top of the stack is a left parenthesis,
// pop operators off the stack onto the output queue.
output(values, stack.pop(), evaluationContext);
}
}
if (!pe) {
// If no left parentheses are encountered, either the separator was misplaced
// or parentheses were mismatched.
throw new IllegalArgumentException("Separator or parentheses mismatched");
}
} else if (token.isFunction()) {
// If the token is a function token, then push it onto the stack.
stack.push(token);
previousValuesSize.push(values.size());
} else if (token.isOperator()) {
// If the token is an operator, op1, then:
while (!stack.isEmpty()) {
Token sc = stack.peek();
// While there is an operator token, o2, at the top of the stack
// op1 is left-associative and its precedence is less than or equal
// to that of op2,
// or op1 has precedence less than that of op2,
// Let + and ^ be right associative.
// Correct transformation from 1^2+3 is 12^3+
// The differing operator priority decides pop / push
// If 2 operators have equal priority then associativity decides.
if (sc.isOperator()
&& ((token.getAssociativity().equals(Operator.Associativity.LEFT) && (token.getPrecedence() <= sc.getPrecedence())) ||
(token.getPrecedence() < sc.getPrecedence()))) {
// Pop o2 off the stack, onto the output queue;
output(values, stack.pop(), evaluationContext);
} else {
break;
}
}
// push op1 onto the stack.
stack.push(token);
} else {
// If the token is a number (identifier), a constant or a variable, then add its value to the output queue.
if ((previous!=null) && previous.isLiteral()) throw new IllegalArgumentException("A literal can't follow another literal");
output(values, token, evaluationContext);
}
previous = token;
}
// When there are no more tokens to read:
// While there are still operator tokens in the stack:
while (!stack.isEmpty()) {
Token sc = stack.pop();
if (sc.isOpenBracket() || sc.isCloseBracket()) {
throw new IllegalArgumentException("Parentheses mismatched");
}
output(values, sc, evaluationContext);
}
if (values.size()!=1) throw new IllegalArgumentException();
return values.pop();
}
private Token toToken(Token previous, String token) {
if (token.equals(functionArgumentSeparator)) {
return Token.FUNCTION_ARG_SEPARATOR;
} else if (functions.containsKey(token)) {
return Token.buildFunction(functions.get(token));
} else if (operators.containsKey(token)) {
List<Operator> list = operators.get(token);
if (list.size()==1) return Token.buildOperator(list.get(0));
return Token.buildOperator(guessOperator(previous, list));
} else {
final BracketPair brackets = getBracketPair(token);
if (brackets!=null) {
if (brackets.getOpen().equals(token)) {
return Token.buildOpenToken(brackets);
} else {
return Token.buildCloseToken(brackets);
}
} else {
return Token.buildLiteral(token);
}
}
}
private BracketPair getBracketPair(String token) {
BracketPair result = expressionBrackets.get(token);
return result==null ? functionBrackets.get(token) : result;
}
/** Gets the operators supported by this evaluator.
* @return a collection of operators.
*/
public Collection<Operator> getOperators() {
ArrayList<Operator> result = new ArrayList<Operator>();
Collection<List<Operator>> values = this.operators.values();
for (List<Operator> list : values) {
result.addAll(list);
}
return result;
}
/** Gets the functions supported by this evaluator.
* @return a collection of functions.
*/
public Collection<Function> getFunctions() {
return this.functions.values();
}
/** Gets the constants supported by this evaluator.
* @return a collection of constants.
*/
public Collection<Constant> getConstants() {
return this.constants.values();
}
/** Converts the evaluated expression into tokens.
* <br>Example: The result for the expression "<i>-1+min(10,3)</i>" is an iterator on "-", "1", "+", "min", "(", "10", ",", "3", ")".
* <br>By default, the operators symbols, the brackets and the function argument separator are used as delimiter in the string.
* @param expression The expression that is evaluated
* @return A string iterator.
*/
protected Iterator<String> tokenize(String expression) {
return tokenizer.tokenize(expression);
}
}