mathExpr by
jtem group

mathExpr
Class ExpressionConfiguration

java.lang.Object
  extended bymathExpr.ExpressionConfiguration

public class ExpressionConfiguration
extends java.lang.Object

An ExpressionConfiguration is a fundamental class for parsing and evaluating Expressions. This class provides the easiest way of working with the mathExpr library and it should be your starting point whenever you decide to work with 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 Summary
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)
          Creates a new ExpressionConfiguration instance with the specified Type for evaluating Symbols.
ExpressionConfiguration(Type defaultType, DefinitionModel defModel)
          Creates a new ExpressionConfiguration instance with the specified Type for evaluating Symbols and the specified DefinitionModel.
 
Method Summary
 UserDefinedFunction defineFunction(java.lang.String name, java.lang.String[] param, Expression def)
          Defines or changes a UserDefinedFunction by delegating the method DefinitionModel.defineFunction(String,String[],Expression) of this' DefinitionModel.
 UserDefinedFunction defineFunction(java.lang.String name, java.lang.String[] param, java.lang.String def)
          Defines or changes a UserDefinedFunction by delegating the method DefinitionModel.defineFunction(String,String[],Expression) of this' DefinitionModel.
 Variable defineVariable(java.lang.String name, java.lang.Object value, Type type)
          Defines or changes a Variable by delegating the method DefinitionModel.defineVariable(String,Object,Type) of this' DefinitionModel.
 Expression differentiate(java.lang.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.
 java.lang.Object evaluateExpression()
          Evaluates the current Expression by calling the method Evaluator.evaluate() on the expression's Evaluator.
 DefinitionModel getDefinitionModel()
          Returns the DefinitionModel holding all Variables and Functions.
 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(java.lang.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(java.lang.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionConfiguration

public ExpressionConfiguration()
Creates a new ExpressionConfiguration instance with ComplexType.TYPE as the Type for evaluating Symbols.


ExpressionConfiguration

public ExpressionConfiguration(DefinitionModel defModel)
Creates a new 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.

Parameters:
defModel - the DefinitionModel to be used for this ExpressionConfiguration.

ExpressionConfiguration

public ExpressionConfiguration(Type defaultType)
Creates a new ExpressionConfiguration instance with the specified Type for evaluating Symbols.

Parameters:
defaultType - the Type Symbols will be evaluated to.

ExpressionConfiguration

public ExpressionConfiguration(Type defaultType,
                               DefinitionModel defModel)
Creates a new 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.

Parameters:
defaultType - the Type Symbols will be evaluated to.
defModel - the DefinitionModel to be used for this ExpressionConfiguration.
Method Detail

defineFunction

public UserDefinedFunction defineFunction(java.lang.String name,
                                          java.lang.String[] param,
                                          Expression def)
                                   throws IncompatibleDefinitionException
Defines or changes a UserDefinedFunction by delegating the method DefinitionModel.defineFunction(String,String[],Expression) of this' DefinitionModel. See the documentation of that method for details.

Parameters:
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.
Returns:
the UserDefinedFunction which was defined respectively changed.
Throws:
IncompatibleDefinitionException - if a Variable with the same name is already defined.
See Also:
getDefinitionModel()

defineFunction

public UserDefinedFunction defineFunction(java.lang.String name,
                                          java.lang.String[] param,
                                          java.lang.String def)
Defines or changes a UserDefinedFunction by delegating the method DefinitionModel.defineFunction(String,String[],Expression) of this' DefinitionModel. See the documentation of that method for details.
The specified String def will be parsed before the delegation.

Parameters:
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.
Returns:
the UserDefinedFunction which was defined respectively changed.
Throws:
IncompatibleDefinitionException - if a Variable with the same name is already defined.
See Also:
getDefinitionModel()

defineVariable

public Variable defineVariable(java.lang.String name,
                               java.lang.Object value,
                               Type type)
                        throws IncompatibleDefinitionException
Defines or changes a Variable by delegating the method DefinitionModel.defineVariable(String,Object,Type) of this' DefinitionModel. See the documentation of that method for details.

Parameters:
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.
Returns:
the Variable which was defined respectively changed.
Throws:
IncompatibleDefinitionException - if a Function with the same name is already defined.
See Also:
getDefinitionModel()

differentiate

public Expression differentiate(java.lang.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.

See Also:
Expression.simplify()

differentiate

public Expression differentiate(Symbol s)
Returns the simplified derivative of the current set Expression with respect to the Symbol s.

Returns:
the simplified derivative of the current set Expression with respect to the Symbol s.
Throws:
java.lang.IllegalStateException - if no Expression was set yet.
See Also:
Expression.differentiate(Symbol), Expression.simplify()

dissolveFunctionCalls

public Expression dissolveFunctionCalls()
Calls Expression.dissolveFunctionCalls() on the current set Expression and returns the result.

Returns:
a new Expression which is equal to the current set Expression but all function calls are dissolved.

evaluateExpression

public java.lang.Object evaluateExpression()
                                    throws UnknownDefinitionException
Evaluates the current 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.

Returns:
the Object as the result of the evaluation.
Throws:
UnknownDefinitionException
See Also:
getEvaluator(), getExpression()

getDefinitionModel

public DefinitionModel getDefinitionModel()
Returns the DefinitionModel holding all Variables and Functions.

Returns:
the DefinitionModel of this ExpressionConfiguration.
See Also:
defineVariable(String,Object,Type), defineFunction(String,String[],String), defineFunction(String,String[],Expression)

getEvaluator

public Evaluator getEvaluator()
Returns the Evaluator for the current Expression.

Returns:
the Evaluator for the current Expression.
Throws:
java.lang.NullPointerException - if there is not an Expression setted yet.
See Also:
setExpression(Expression), setExpression(String)

getExpression

public Expression getExpression()
Returns the Expression this ExpressionConfiguration currently represents or null if an Expression wasn't set yet.

Returns:
the Expression this ExpressionConfiguration currently represents.
See Also:
setExpression(Expression), setExpression(String)

getExpressionFactory

public ExpressionFactory getExpressionFactory()
Returns the ExpressionFactory which is neccessary for any instantiated Parser.

Returns:
the ExpressionFactory of this ExpressionConfiguration.

getType

public Type getType()
Returns the current default Type for evaluating Symbols.

Returns:
the Type for evaluating Symbols.
See Also:
setType(Type), evaluateExpression()

getVariable

public Variable getVariable(java.lang.String name)
Returns the Variable with the specified name by delegating the method DefinitionModel.getVariable(String) of this' DefinitionModel.

Parameters:
name - the name of the Variable this method tries to return.
Returns:
the Variable with the specified name or null if the Variable does not exist yet.
See Also:
getDefinitionModel()

replaceSymbol

public Expression replaceSymbol(Symbol s,
                                Expression expr)
Calls Expression.replaceSymbol(Symbol, Expression) on the current set Expression and returns the result.


setDefinitionModel

public void setDefinitionModel(DefinitionModel newDefModel)

Sets a new DefinitionModel for this ExpressionConfiguration.

Attention: All of the created definitions are no longer available. Maybe the current setted 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.

Parameters:
newDefModel - the DefinitionModel to be used for this ExpressionConfiguration.
See Also:
setExpression(String), setExpression(Expression), getDefinitionModel()

setExpression

public void setExpression(Expression exp)
Sets the Expression this ExpressionConfiguration is to be representing.

Parameters:
exp - the new Expression for this ExpressionConfiguration.
See Also:
setExpression(String), getExpression()

setExpression

public void setExpression(java.lang.String exp)
Sets the 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.

You do not need to create a Parser. This method creates a new Parser and sets the Expression of this ExpressionConfiguration object to the parsed one.

Parameters:
exp - a String representing the new Expression for this ExpressionConfiguration.
See Also:
getEvaluator(), setExpression(Expression), getExpression()

setType

public void setType(Type type)
Sets a new default Type for evaluating Symbols.

Parameters:
type - the new Type to be set.
See Also:
getType(), evaluateExpression()

simplify

public Expression simplify()
Returns the current set Expression simplified. Calling this methods delegates to Expression.simplify().

Throws:
java.lang.IllegalStateException - if no Expression was set yet.

mathExpr by
jtem group

© 2002 sfb288 · jtem group