[back] [home] [content] [continue]
1. Choose a default evaluation type. This type defines how to handle symbols
(like: "a", "b", "hello", "x", ...) in the expression-tree-hierarchy (see:
"What's the default type?").
2. Create an ExpressionConfiguration
instance. It holds exactly one Expression
and provides certain methods to change or evaluate the expression.
3. Parse a new expression by calling the method setExpression(String)
on the ExpressionConfiguration
instance.
4. Evaluate the expression and get the result by calling the method
evaluateExpression()
on the ExpressionConfiguration
instance.
Note: the result comes as an Object
. To know the right
cast, you have to know what type of result your expression will be evaluated
to before evaluation.
Example:
//1.) default type will be complex Type defaultType=ComplexType.TYPE; //2.) create ExpressionConfiguration ExpressionConfiguration config=new ExpressionConfiguration(defaultType); //3.) parse an expression config.setExpression("2+2"); //4.) evaluate the parsed expression Complex c=(Complex)config.evaluateExpression();
Complex
,
Real
, ...) to the evaluated value
If you already have an Object
like the evaluation result will be,
you can set the value of this Object
to the evaluated value
by using the appropriate method:
AbstractRealEvaluator.getRealValue(Real)
AbstractComplexEvaluator.getComplexValue(Complex)
AbstractRealVectorEvaluator.getRealVectorValue(RealVector)
AbstractComplexVectorEvaluator.getComplexVectorValue(ComplexVector)
All you have to do is to cast your Evaluator
to the correct
abstract class and call the method. Example:
//default constructor initialises with ComplexType.TYPE
ExpressionConfiguration config=new ExpressionConfiguration();
config.setExpression("2+2");
config.evaluateExpression();
Complex result=new Complex();
AbstractComplexEvaluator ace=(AbstractComplexEvaluator)config.getEvaluator();
ace.getComplexValue(result);