public class DefinitionModel extends Object implements Context
Variables
and Functions
.
With the methods defineVariable(String,Object,Type)
and
defineFunction(String,String[],Expression)
you can define new
Variables
and Functions
.
Usually you need a single instance of the class
ExpressionConfiguration
which either automatically creates a new
instance of this class or gets a DefinitionModel
by the
Constructor ExpressionConfiguration.ExpressionConfiguration(
Type,DefinitionModel)
. Then you can use certain methods of the
ExpressionConfiguration
class delegating appropriate methods
in this class.
This class implements Context
by resolving name symbols to
global Variables
and function calls to Functions
.
Constructor and Description |
---|
DefinitionModel()
Creates a new
DefinitionModel which already holds all possible
PredefinedFunction Objects. |
Modifier and Type | Method and Description |
---|---|
void |
addFunction(Function newFunction)
Adds the specified
Function to this
DefinitionModel . |
void |
addVariable(Variable newVariable)
Adds the specified
Variable to this
DefinitionModel . |
boolean |
containsFunction(String name)
|
boolean |
containsVariable(String name)
|
UserDefinedFunction |
defineFunction(String name,
String[] param,
Expression def)
Defines and returns a new
UserDefinedFunction with the
specified name , parameters and
definition . |
Variable |
defineVariable(String name,
Object value,
Type type)
Defines and returns a new
Variable with the specified
name , value and type . |
Evaluator |
getEvaluator(Symbol s)
Returns the
Variable the specified Symbol is
representing. |
Function |
getFunction(FunctionCall fc)
Returns the
Function according to the specified
FunctionCall . |
Function |
getFunction(String name)
Returns the
Function with the specified name or
null if the Function does not exist yet. |
Variable |
getVariable(String name)
Returns the
Variable with the specified name or
null if the Variable does not exist yet. |
public DefinitionModel()
DefinitionModel
which already holds all possible
PredefinedFunction
Objects.PredefinedFunction.FUNCTIONS
public void addFunction(Function newFunction)
Function
to this
DefinitionModel
. If this DefinitionModel
already contains a UserDefinedFunction
with the same name as
newFunction
, value and type of that
UserDefinedFunction
will be changed to value and type of
newFunction
.newFunction
- the Function
to be added to this
DefinitionModel
IllegalArgumentException
- if a PredefinedFunction
with the same name already exists.IncompatibleDefinitionException
- if a Variable
with the
same name is already defined.UserDefinedFunction.getDefinition()
,
Function.getParameters()
,
UserDefinedFunction.setDefinition(Expression)
,
Function.setParameters(String[])
public void addVariable(Variable newVariable)
Variable
to this
DefinitionModel
. If this DefinitionModel
already
contains a Variable
with the same name as
newVariable
, value and type of that Variable
will be changed to value and type of newVariable
.newVariable
- the Variable
to be added to this
DefinitionModel
IncompatibleDefinitionException
- if a Function
with the
same name is already defined.Variable.getValue()
,
Variable.getReturnType()
,
Variable.setValue(Object,Type)
public boolean containsFunction(String name)
name
- the name of the Function
to be checked if
existing.true
if this DefinitionModel
contains a
Function
with the specified name
;
false
otherwise.defineFunction(String,String[],Expression)
,
addFunction(Function)
public boolean containsVariable(String name)
name
- the name of the Variable
to be checked if
existing.true
if this DefinitionModel
contains a
Variable
with the specified name
;
false
otherwise.defineVariable(String,Object,Type)
,
addVariable(Variable)
public UserDefinedFunction defineFunction(String name, String[] param, Expression def)
UserDefinedFunction
with the
specified name
, parameters
and
definition
. If this DefinitionModel
already
contains a UserDefinedFunction
with the specified
name
, the parameters
and definition
of that UserDefinedFunction
will be changed.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.IllegalArgumentException
- if a PredefinedFunction
with the same name already exists.IncompatibleDefinitionException
- if a Variable
with the same
name is already defined.UserDefinedFunction.UserDefinedFunction(String,String[],Expression)
,
UserDefinedFunction.setDefinition(Expression)
,
Function.setParameters(String[])
public Variable defineVariable(String name, Object value, Type type)
Variable
with the specified
name
, value
and type
. If this
DefinitionModel
already contains a Variable
with the specified name, the value
and type
of that Variable
will be changed.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.Variable.Variable(String,Object,Type)
,
Variable.setValue(Object,Type)
public Evaluator getEvaluator(Symbol s)
Variable
the specified Symbol
is
representing.
This method is necessary for evaluating an Expression
respectively
the Symbols
in the expression-tree-hirarchy. This method
is called whenever a Symbol
was found which represents a
Variable
in a global Context
. This
DefinitionModel
provides that Context
.
Examples:
1. The Expression
"2x+2y" contains the Symbols
"x" and "y" among other things. These Symbols
represent
Variables
in a global Context
so that this method
will be called to get them.
2. A new Function
"f(x,y)=2x+2y" was defined. Now the
Expression
"f(1,1)" would implicate a FunctionCallEvaluator
which provides the Context
for the
function's parameter "x" and "y". They do not represent global
Variables
. If now "2x+2y" will be evaluated, the
Evaluators
for "x" and "y" will be got by calling the method
FunctionCallEvaluator.getEvaluator(Symbol)
.
getEvaluator
in interface Context
s
- the Symbol
representing the Variable
to be returned.Variable
the specified Symbol
is
representing.UnknownDefinitionException
- if the Variable
does not
exist in this DefinitionModel
.public Function getFunction(FunctionCall fc)
Function
according to the specified
FunctionCall
. This method calls the method
getFunction(String)
parameterized with the name of the
FunctionCall
.getFunction
in interface Context
fc
- the FunctionCall
representing the
Function
to return.Function
the specified
FunctionCall
is representing.FunctionCallEvaluator.getFunction(FunctionCall)
public Function getFunction(String name)
Function
with the specified name
or
null
if the Function
does not exist yet.name
- the name of the Function
this method tries to
return.Function
with the specified name
or
null
if the Function
does not exist yet.public Variable getVariable(String name)
Variable
with the specified name
or
null
if the Variable
does not exist yet.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.