public class ExpressionConfiguration extends Object
ExpressionConfiguration
is a fundamental class for parsing
and evaluating Expressions
. This class provides the
easiest way of working with the de.jtem.mathExpr
library and it should be
your starting point whenever you decide to work with de.jtem.mathExpr
classes.
A single instance of this class is a representation of a single
Expression
.
It eases parsing and evaluating by corresponding methods like
setExpression(String exp)
or evaluateExpression()
.
You don't need to create a Parser
or Evaluator
- they were
created by this ExpressionConfiguration
if necessary.
All you have to do is to choose a default Type
(e.g.: ComplexType.TYPE
) and construct
an ExpressionConfiguration
.
For example:
Type defaultType=new ComplexType.TYPE; ExpressionConfiguration config=new ExpressionConfiguration(defaultType);
//parse the expression "2+2i" config.setExpression
("2+2i");
//get the expression as an Expression object Expression exp=config.getExpression()
;
//evaluate the expression and get the result as an Object Object o=config.evaluateExpression()
;
...
If you use the constructor ExpressionConfiguration(Type)
, this
class creates a new DefinitionModel
and a new ExpressionFactory
parameterized with that DefinitionModel
.
Use the other constructor, if you want to use a single
DefinitionModel
for several
ExpressionConfigurations
,
e.g.:
ExpressionConfiguration config1=new ExpressionConfiguration(ComplexType.TYPE); DefinitionModel defModel=config1.getDefinitionModel()
; ExpressionConfiguration config2=new ExpressionConfiguration(ComplexType.TYPE, defModel); config1.defineVariable
("a", new Complex(2), ComplexType.TYPE); config1.defineVariable("b", new Complex(4), ComplexType.TYPE); config1.setExpression
("a+b"); //the next command is possible because the DefinitionModel of config1 and config2 is the same instance config2.setExpression("a*b");
Constructor and Description |
---|
ExpressionConfiguration()
Creates a new
ExpressionConfiguration instance with
ComplexType.TYPE as the Type for evaluating
Symbols . |
ExpressionConfiguration(DefinitionModel defModel)
Creates a new
ExpressionConfiguration instance with
ComplexType.TYPE as the Type for evaluating
Symbols and
the specified DefinitionModel . |
ExpressionConfiguration(Type defaultType)
|
ExpressionConfiguration(Type defaultType,
DefinitionModel defModel)
Creates a new
ExpressionConfiguration instance with the
specified Type for evaluating Symbols and
the specified DefinitionModel . |
Modifier and Type | Method and Description |
---|---|
UserDefinedFunction |
defineFunction(String name,
String[] param,
Expression def)
Defines or changes a
UserDefinedFunction by delegating the
method DefinitionModel.defineFunction(String,String[],Expression)
of this' DefinitionModel . |
UserDefinedFunction |
defineFunction(String name,
String[] param,
String def)
Defines or changes a
UserDefinedFunction by delegating the
method DefinitionModel.defineFunction(String,String[],Expression)
of this' DefinitionModel . |
Variable |
defineVariable(String name,
Object value,
Type type)
Defines or changes a
Variable by delegating the method DefinitionModel.defineVariable(String,Object,Type) of this'
DefinitionModel . |
Expression |
differentiate(String str)
Creates a new Symbol with String str and delegates to
differentiate(Symbol) and returns the simplified derivative of
the current set Expression with respect to that Symbol. |
Expression |
differentiate(Symbol s)
Returns the simplified derivative of the current set Expression with
respect to the Symbol s.
|
Expression |
dissolveFunctionCalls()
Calls
Expression.dissolveFunctionCalls() on the current
set Expression and returns the result. |
Object |
evaluateExpression()
Evaluates the current
Expression by calling the method Evaluator.evaluate() on the expression's Evaluator . |
DefinitionModel |
getDefinitionModel()
|
Evaluator |
getEvaluator()
Returns the
Evaluator for the current Expression . |
Expression |
getExpression()
Returns the
Expression this
ExpressionConfiguration currently represents or null
if an Expression wasn't set yet. |
ExpressionFactory |
getExpressionFactory()
Returns the
ExpressionFactory which is neccessary for any
instantiated Parser . |
Type |
getType()
Returns the current default
Type for evaluating Symbols . |
Variable |
getVariable(String name)
Returns the
Variable with the specified name by delegating the
method DefinitionModel.getVariable(String) of this'
DefinitionModel . |
Expression |
replaceSymbol(Symbol s,
Expression expr)
Calls
Expression.replaceSymbol(Symbol, Expression) on the current
set Expression and returns the result. |
void |
setDefinitionModel(DefinitionModel newDefModel)
Sets a new
DefinitionModel for this
ExpressionConfiguration . |
void |
setExpression(Expression exp)
Sets the
Expression this ExpressionConfiguration
is to be representing. |
void |
setExpression(String exp)
Sets the
Expression this ExpressionConfiguration
is to be representing by parsing the specified String . |
void |
setType(Type type)
Sets a new default
Type for evaluating Symbols . |
Expression |
simplify()
Returns the current set Expression simplified.
|
public ExpressionConfiguration()
ExpressionConfiguration
instance with
ComplexType.TYPE
as the Type
for evaluating
Symbols
.public ExpressionConfiguration(DefinitionModel defModel)
ExpressionConfiguration
instance with
ComplexType.TYPE
as the Type
for evaluating
Symbols
and
the specified DefinitionModel
. Use this constructor if you
want to have more than one ExpressionConfiguration
with
the same DefinitionModel
.defModel
- the DefinitionModel
to be used for this
ExpressionConfiguration
.public ExpressionConfiguration(Type defaultType)
defaultType
- the Type
Symbols
will
be evaluated to.public ExpressionConfiguration(Type defaultType, DefinitionModel defModel)
ExpressionConfiguration
instance with the
specified Type
for evaluating Symbols
and
the specified DefinitionModel
. Use this constructor if you
want to have more than one ExpressionConfiguration
with
the same DefinitionModel
.defaultType
- the Type
Symbols
will
be evaluated to.defModel
- the DefinitionModel
to be used for this
ExpressionConfiguration
.public UserDefinedFunction defineFunction(String name, String[] param, Expression def) throws IncompatibleDefinitionException
UserDefinedFunction
by delegating the
method DefinitionModel.defineFunction(String,String[],Expression)
of this' DefinitionModel
. See the documentation of that method
for details.name
- the name for the UserDefinedFunction
to be defined
respectively the name of the UserDefinedFunction
to be
changed.param
- an array of names of the parameters for the
UserDefinedFunction
to be defined respectively the new
parameters for the UserDefinedFunction
to be changed.def
- the definition for the UserDefinedFunction
to be
defined respectively the new definition for the UserDefinedFunction
to be changed.UserDefinedFunction
which was defined respectively
changed.IncompatibleDefinitionException
- if a Variable
with the same
name is already defined.getDefinitionModel()
public UserDefinedFunction defineFunction(String name, String[] param, String def)
UserDefinedFunction
by delegating the
method DefinitionModel.defineFunction(String,String[],Expression)
of this' DefinitionModel
. See the documentation of that method
for details.
String def
will be parsed before the delegation.name
- the name for the UserDefinedFunction
to be defined
respectively the name of the UserDefinedFunction
to be
changed.param
- an array of names of the parameters for the
UserDefinedFunction
to be defined respectively the new
parameters for the UserDefinedFunction
to be changed.def
- a String
representing the definition for the
UserDefinedFunction
to be defined respectively a
String
representing the new definition for the
UserDefinedFunction
to be changed.UserDefinedFunction
which was defined respectively
changed.IncompatibleDefinitionException
- if a Variable
with the same
name is already defined.getDefinitionModel()
public Variable defineVariable(String name, Object value, Type type) throws IncompatibleDefinitionException
Variable
by delegating the method DefinitionModel.defineVariable(String,Object,Type)
of this'
DefinitionModel
. See the documentation of that method for
details.name
- the name for the Variable
to be defined
respectively the name of the Variable
to be changed.value
- the value for the Variable
to be defined
respectively the new value for the Variable
to be changed.type
- the Type
for the Variable
to be
defined respectively the new Type
for the
Variable
to be changed.Variable
which was defined respectively changed.IncompatibleDefinitionException
- if a Function
with the same
name is already defined.getDefinitionModel()
public Expression differentiate(String str)
differentiate(Symbol)
and returns the simplified derivative of
the current set Expression with respect to that Symbol.Expression.simplify()
public Expression differentiate(Symbol s)
IllegalStateException
- if no Expression was set yet.Expression.differentiate(Symbol)
,
Expression.simplify()
public Expression dissolveFunctionCalls()
Expression.dissolveFunctionCalls()
on the current
set Expression and returns the result.public Object evaluateExpression() throws UnknownDefinitionException
Expression
by calling the method Evaluator.evaluate()
on the expression's Evaluator
.
This method creates the evaluator-tree for the current expression
(the setted expression) if it's not already created (because this
method or the method getEvaluator()
was already called).
After that it returns the evaluation result as an Object.
Object
as the result of the evaluation.UnknownDefinitionException
getEvaluator()
,
getExpression()
public DefinitionModel getDefinitionModel()
DefinitionModel
of this
ExpressionConfiguration
.defineVariable(String,Object,Type)
,
defineFunction(String,String[],String)
,
defineFunction(String,String[],Expression)
public Evaluator getEvaluator()
Evaluator
for the current Expression
.Evaluator
for the current Expression
.NullPointerException
- if there is not an Expression
setted yet.setExpression(Expression)
,
setExpression(String)
public Expression getExpression()
Expression
this
ExpressionConfiguration
currently represents or null
if an Expression
wasn't set yet.Expression
this
ExpressionConfiguration
currently represents.setExpression(Expression)
,
setExpression(String)
public ExpressionFactory getExpressionFactory()
ExpressionFactory
which is neccessary for any
instantiated Parser
.ExpressionFactory
of this
ExpressionConfiguration
.public Type getType()
Type
for evaluating Symbols
.Type
for evaluating Symbols
.setType(Type)
,
evaluateExpression()
public Variable getVariable(String name)
Variable
with the specified name by delegating the
method DefinitionModel.getVariable(String)
of this'
DefinitionModel
.name
- the name of the Variable
this method tries to
return.Variable
with the specified name
or
null
if the Variable
does not exist yet.getDefinitionModel()
public Expression replaceSymbol(Symbol s, Expression expr)
Expression.replaceSymbol(Symbol, Expression)
on the current
set Expression and returns the result.public void setDefinitionModel(DefinitionModel newDefModel)
Sets a new DefinitionModel
for this
ExpressionConfiguration
.
Expression
makes use
of some of these definitions. That's why the Expression
of this ExpressionConfiguration
becomes null
if the method setDefinitionModel(DefinitionModel)
is
calling. You have to reset it.newDefModel
- the DefinitionModel
to be used for this
ExpressionConfiguration
.setExpression(String)
,
setExpression(Expression)
,
getDefinitionModel()
public void setExpression(Expression exp)
Expression
this ExpressionConfiguration
is to be representing.exp
- the new Expression
for this
ExpressionConfiguration
.setExpression(String)
,
getExpression()
public void setExpression(String exp)
Expression
this ExpressionConfiguration
is to be representing by parsing the specified String
. See
documentation of class Parser
to get information about the
syntax the specified String
has to have.Parser
. This method creates a
new Parser
and sets the Expression
of this
ExpressionConfiguration
object to the parsed one.exp
- a String
representing the new
Expression
for this ExpressionConfiguration
.getEvaluator()
,
setExpression(Expression)
,
getExpression()
public void setType(Type type)
Type
for evaluating Symbols
.type
- the new Type
to be set.getType()
,
evaluateExpression()
public Expression simplify()
Expression.simplify()
.IllegalStateException
- if no Expression was set yet.