Class AbstractEvaluator<T>
java.lang.Object
studio.magemonkey.codex.util.eval.javaluator.AbstractEvaluator<T>
- Type Parameters:
T- The type of values handled by the evaluator
- All Implemented Interfaces:
EvaluationContext
- Direct Known Subclasses:
DoubleEvaluator
An abstract evaluator, able to evaluate infix expressions.
Some standard evaluators are included in the library, you can define your own by subclassing this class.
This class is thread safe.
Some standard evaluators are included in the library, you can define your own by subclassing this class.
This class is thread safe.
- Author:
- Jean-Marc Astesana
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Map<String, BracketPair> protected final Stringprotected final Map<String, BracketPair> protected final Tokenizer -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoFunction(Deque<T> values, Token functionTok, int argCount, EvaluationContext evaluationContext) Evaluates an expression.evaluate(String expression, EvaluationContext evaluationContext) Evaluates an expression that contains variables.protected Tevaluate(Constant constant, EvaluationContext evaluationContext) Evaluates a constant.protected Tevaluate(Function function, Iterator<T> arguments, EvaluationContext evaluationContext) Evaluates a function.protected Tevaluate(Operator operator, Iterator<T> operands, EvaluationContext evaluationContext) Evaluates an operation.getArguments(Deque<T> values, int nb) protected BracketPairgetBracketPair(String token) Gets the constants supported by this evaluator.Gets the functions supported by this evaluator.Gets the operators supported by this evaluator.protected voidoutput(Deque<T> values, Token token, EvaluationContext evaluationContext) protected Collection<Token> Converts the evaluated expression into tokens.protected abstract TtoValue(Token literal, EvaluationContext evaluationContext) Evaluates a literal (Converts it to a value).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface studio.magemonkey.codex.util.eval.javaluator.EvaluationContext
getError, getError
-
Field Details
-
tokenizer
-
functions
-
operators
-
constants
-
functionArgumentSeparator
-
functionBrackets
-
expressionBrackets
-
-
Constructor Details
-
AbstractEvaluator
Constructor.- Parameters:
parameters- The evaluator parameters.
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.
-
-
Method Details
-
output
-
evaluate
Evaluates a constant.
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.- Parameters:
constant- The constantevaluationContext- The context of the evaluation- Returns:
- The constant's value
-
evaluate
Evaluates an operation.
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.- Parameters:
operator- The operatoroperands- The operandsevaluationContext- The context of the evaluation- Returns:
- The result of the operation
-
evaluate
Evaluates a function.
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.- Parameters:
function- The functionarguments- The function's argumentsevaluationContext- The context of the evaluation- Returns:
- The result of the function
-
doFunction
protected void doFunction(Deque<T> values, Token functionTok, int argCount, EvaluationContext evaluationContext) -
getArguments
-
toValue
Evaluates a literal (Converts it to a value).- Parameters:
literal- The literal to evaluate.evaluationContext- The context of the evaluation- Returns:
- an instance of T.
- Throws:
IllegalArgumentException- if the literal can't be converted to a value.
-
evaluate
Evaluates an expression.- Parameters:
expression- The expression to evaluate.- Returns:
- the result of the evaluation.
- Throws:
IllegalArgumentException- if the expression is not correct.
-
evaluate
Evaluates an expression that contains variables.- Parameters:
expression- The expression to evaluate.evaluationContext- The context of the evaluation.
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).
The context is not limited to variable values but can be used for any dynamic information. A good example is the BooleanSetEvaluator one.- Returns:
- the result of the evaluation.
- Throws:
IllegalArgumentException- if the expression is not correct.- See Also:
-
getBracketPair
-
getOperators
Gets the operators supported by this evaluator.- Returns:
- a collection of operators.
-
getFunctions
Gets the functions supported by this evaluator.- Returns:
- a collection of functions.
-
getConstants
Gets the constants supported by this evaluator.- Returns:
- a collection of constants.
-
tokenize
Converts the evaluated expression into tokens.
Example: The result for the expression "-1+min(10,3)" is an iterator on "-", "1", "+", "min", "(", "10", ",", "3", ")".
By default, the operators symbols, the brackets and the function argument separator are used as delimiter in the string.- Parameters:
expression- The expression that is evaluated- Returns:
- A string iterator.
-