com.singularsys.jep
Class Jep

java.lang.Object
  extended by com.singularsys.jep.Jep
All Implemented Interfaces:
java.io.Serializable

public class Jep
extends java.lang.Object
implements java.io.Serializable

The Jep class is the main interface with which the user should interact. It contains all necessary methods to parse and evaluate expressions. To evaluate an expression, simply call the parse(String) and evaluate methods after each other. The following code snippet shows a parsing and evaluating a simple expression with a single variable:

Jep jep = new Jep();
jep.addVariable("x", 10);
try {
        jep.parse("x+1");
        Object result = jep.evaluate();
        System.out.println("x + 1 = " + result);
} catch (Exception e) {
        System.out.println("An error occured: " + e.getMessage());
}

The class is serializable. Please see the documentation section "Serialization" for more information on the various options for serializing.

See Also:
Serialized Form

Field Summary
protected  boolean allowAssignment
          Allow Assignment option
protected  boolean allowUndeclared
          Allow Undeclared Variables option
protected  Evaluator evaluator
          Evaluator instance used for evaluating parse trees
protected  FunctionTable funTab
          Function Table
protected  boolean implicitMul
          Implicit Multiplication option
protected  Node lastRootNode
          Root node of the last parsed expression
protected  NodeFactory nodeFac
          Node Factory
protected  NumberFactory numFac
          Number Factory
protected  OperatorTable opTab
          Operator Table
protected  Parser parser
          Parser instance used for parsing expressions
protected  PrintVisitor pv
          PrintVisitor
protected  VariableFactory varFac
          Variable Factory
protected  VariableTable varTab
          Variable Table
 
Constructor Summary
Jep()
          Creates a new Jep instance using the standard components with default settings.
Jep(ComponentSet compSet)
          Creates a new Jep instance with a specific component set.
Jep(JepComponent[] components)
          Creates a new Jep instance with a set of components specified by an array of components.
 
Method Summary
 PostfixMathCommandI addFunction(java.lang.String name, PostfixMathCommandI pfmc)
          Adds a function to the parser.
 boolean addStandardConstants()
          Adds the constants pi and e to the parser.
 Variable addVariable(java.lang.String name)
          Adds a variable with a given name.
 Variable addVariable(java.lang.String name, double value)
          Adds or sets a double variable with a given name and value.
 Variable addVariable(java.lang.String name, double re, double im)
          Adds or sets a complex variable with a given name and value.
 Variable addVariable(java.lang.String name, java.lang.Object value)
          Adds or sets a variable with a given name and value.
 java.lang.Object evaluate()
          Evaluates the most recently parsed expression.
 java.lang.Object evaluate(Node node)
          Evaluates the expression tree pointed to by the node parameter.
 double evaluateD()
          Evaluates the most recently parsed expression and returns the result as a double.
 boolean getAllowAssignment()
          Whether assignment equation y=x+1 equations are allowed.
 boolean getAllowUndeclared()
          Returns the value of the allowUndeclared option.
 Evaluator getEvaluator()
          Returns the evaluator
 FunctionTable getFunctionTable()
          Returns the function table
 boolean getImplicitMul()
          Returns the value of the implicit multiplication option.
 Node getLastRootNode()
          Returns the root node of the last successfully parsed expression
 NodeFactory getNodeFactory()
          Returns the node factory
 NumberFactory getNumberFactory()
          Returns the number factory
 OperatorTable getOperatorTable()
          Returns the operator table
 Parser getParser()
          Returns the parser
 PrintVisitor getPrintVisitor()
          Returns the print visitor
 Variable getVariable(java.lang.String name)
          Returns the Variable instance of the variable name.
 VariableFactory getVariableFactory()
          Returns the variable factory
 VariableTable getVariableTable()
          Returns the variable table
 java.lang.Object getVariableValue(java.lang.String name)
          Returns the value of the variable name.
 Node parse(java.io.Reader reader)
          Parses the input from a Reader.
 Node parse(java.lang.String str)
          Parses a string.
 void print()
          Print the last parsed expression to System.out.
 void print(Node node)
          Print an expression to System.out.
 void print(Node node, java.io.PrintStream out)
          Print an expression to a given stream.
 void println()
          Print the last parsed expression to System.out with a new line.
 void println(Node node)
          Print an expression to System.out with a new line.
 void println(Node node, java.io.PrintStream out)
          Print an expression to a given stream with a newline.
 void reinitializeComponents()
          Calls the init method of all components.
 java.lang.String rootNodeToString()
          Returns a string representation of the last expression parsed.
 void setAllowAssignment(boolean value)
          Sets whether assignment equations like y=x+1 are allowed.
 void setAllowUndeclared(boolean value)
          Sets the value for the undeclared variables option.
 void setComponent(JepComponent component)
          Sets a single component.
 void setComponents(JepComponent[] components)
          Sets one or more components for this Jep instance.
 void setImplicitMul(boolean value)
          Sets the value of the implicit multiplication option.
 java.lang.String toString(Node node)
          Returns a string representation of an expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numFac

protected NumberFactory numFac
Number Factory


varFac

protected VariableFactory varFac
Variable Factory


nodeFac

protected NodeFactory nodeFac
Node Factory


varTab

protected VariableTable varTab
Variable Table


funTab

protected FunctionTable funTab
Function Table


opTab

protected OperatorTable opTab
Operator Table


pv

protected PrintVisitor pv
PrintVisitor


lastRootNode

protected transient Node lastRootNode
Root node of the last parsed expression


parser

protected Parser parser
Parser instance used for parsing expressions


evaluator

protected Evaluator evaluator
Evaluator instance used for evaluating parse trees


implicitMul

protected boolean implicitMul
Implicit Multiplication option


allowUndeclared

protected boolean allowUndeclared
Allow Undeclared Variables option


allowAssignment

protected boolean allowAssignment
Allow Assignment option

Constructor Detail

Jep

public Jep()
Creates a new Jep instance using the standard components with default settings. The StandardComponents component set is used.


Jep

public Jep(ComponentSet compSet)
Creates a new Jep instance with a specific component set.

Parameters:
compSet - The component set to be used.

Jep

public Jep(JepComponent[] components)
Creates a new Jep instance with a set of components specified by an array of components.

Parameters:
components - An array of components to initialize Jep with.
Method Detail

setComponents

public void setComponents(JepComponent[] components)
Sets one or more components for this Jep instance. Each component can be one of the following:

Parameters:
components - An array of components to be set.

reinitializeComponents

public void reinitializeComponents()
Calls the init method of all components. This is necessary in a few cases such as after adding new operators to the configurable parser.


setComponent

public void setComponent(JepComponent component)
Sets a single component. This is done by calling setComponents with a single element array parameter.

Parameters:
component - The component to be set.

getNumberFactory

public NumberFactory getNumberFactory()
Returns the number factory


getNodeFactory

public NodeFactory getNodeFactory()
Returns the node factory


getVariableFactory

public VariableFactory getVariableFactory()
Returns the variable factory


getFunctionTable

public FunctionTable getFunctionTable()
Returns the function table


getOperatorTable

public OperatorTable getOperatorTable()
Returns the operator table


getVariableTable

public VariableTable getVariableTable()
Returns the variable table


getParser

public Parser getParser()
Returns the parser


getEvaluator

public Evaluator getEvaluator()
Returns the evaluator


getPrintVisitor

public PrintVisitor getPrintVisitor()
Returns the print visitor


addVariable

public Variable addVariable(java.lang.String name)
Adds a variable with a given name. The value is undefined. If the variable already exists, no changes are made.

Parameters:
name - the name of the variable to be added
Returns:
the Variable instance

addVariable

public Variable addVariable(java.lang.String name,
                            java.lang.Object value)
Adds or sets a variable with a given name and value. If a variable with the same name already exists, the value of that variable is updated.

Parameters:
name - the name of the variable to be added
value - the value of the variable to be added
Returns:
the Variable instance

addVariable

public Variable addVariable(java.lang.String name,
                            double value)
Adds or sets a double variable with a given name and value. If a variable with the same name already exists, the value of that variable is updated.

Parameters:
name - the name of the variable to be added
value - the value of the variable to be added
Returns:
the Variable instance

addVariable

public Variable addVariable(java.lang.String name,
                            double re,
                            double im)
Adds or sets a complex variable with a given name and value. If a variable with the same name already exists, the value of that variable is updated.

Parameters:
name - the name of the variable to be added
re - the real component of the variable to be added
im - the imaginary component of the variable to be added
Returns:
the Variable instance

addStandardConstants

public boolean addStandardConstants()
Adds the constants pi and e to the parser. The values are added as the Math.PI and Math.E values.

Returns:
true if successful, false otherwise

getVariable

public Variable getVariable(java.lang.String name)
Returns the Variable instance of the variable name. If the variable has not been added, null is returned.

Parameters:
name - the name of the variable
Returns:
the Variable instance, or null if the variable has not been added.

getVariableValue

public java.lang.Object getVariableValue(java.lang.String name)
Returns the value of the variable name. If the variable has not been added, or if it's value is null, null is returned.

Parameters:
name - the name of the variable
Returns:
the value of the variable, or null if the variable has not been added or has the value null.

setAllowAssignment

public void setAllowAssignment(boolean value)
Sets whether assignment equations like y=x+1 are allowed.

Since:
2.3.0 alpha

getAllowAssignment

public boolean getAllowAssignment()
Whether assignment equation y=x+1 equations are allowed.

Since:
2.3.0 alpha

setAllowUndeclared

public void setAllowUndeclared(boolean value)
Sets the value for the undeclared variables option. If this option is set to true, expressions containing variables that were not previously added to JEP will not produce an "Unrecognized Symbol" error. The new variables will automatically be added while parsing, and initialized to 0.

If this option is set to false, variables that were not previously added to JEP will produce an error while parsing.

The default value is false.

Parameters:
value - The boolean option for allowing undeclared variables.

getAllowUndeclared

public boolean getAllowUndeclared()
Returns the value of the allowUndeclared option.

Returns:
True if the allowUndeclared option is enabled. False otherwise.

setImplicitMul

public void setImplicitMul(boolean value)
Sets the value of the implicit multiplication option. If this option is set to true before parsing, implicit multiplication will be allowed. That means that an expression such as
"1 2"
is valid and is interpreted as
"1*2"
.

The default value is false.

Parameters:
value - The boolean implicit multiplication option.

getImplicitMul

public boolean getImplicitMul()
Returns the value of the implicit multiplication option.

Returns:
True if the implicit multiplication option is enabled. False otherwise.

addFunction

public PostfixMathCommandI addFunction(java.lang.String name,
                                       PostfixMathCommandI pfmc)
Adds a function to the parser.

Parameters:
name - the name of the function
pfmc - the function class instance to be used to evaluate the function
Returns:
the function class added

getLastRootNode

public Node getLastRootNode()
Returns the root node of the last successfully parsed expression

Returns:
the root node of the last successfully parsed expression

parse

public Node parse(java.io.Reader reader)
           throws ParseException
Parses the input from a Reader.

Parameters:
reader - the input reader
Returns:
the root node of the parse tree
Throws:
ParseException

parse

public Node parse(java.lang.String str)
           throws ParseException
Parses a string.

Parameters:
str - the input string
Returns:
the root node of the parse tree
Throws:
ParseException

evaluateD

public double evaluateD()
                 throws EvaluationException
Evaluates the most recently parsed expression and returns the result as a double.

Returns:
Returns the value of the expression tree. If node is null, null is returned.
Throws:
EvaluationException - if an error occurs while evaluating or if the returned data type can not be converted to a double number.

evaluate

public java.lang.Object evaluate()
                          throws EvaluationException
Evaluates the most recently parsed expression.

Returns:
Returns the value of the expression tree. If node is null, null is returned.
Throws:
EvaluationException - if an error occurs

evaluate

public java.lang.Object evaluate(Node node)
                          throws EvaluationException
Evaluates the expression tree pointed to by the node parameter.

Returns:
Returns the value of the expression tree. If node is null, null is returned.
Throws:
EvaluationException - if an error occurs

print

public void print(Node node,
                  java.io.PrintStream out)
Print an expression to a given stream.

Parameters:
node - root node of the expression to print
out - stream to print to

print

public void print(Node node)
Print an expression to System.out.

Parameters:
node - root node of the expression to print

print

public void print()
Print the last parsed expression to System.out.


println

public void println(Node node,
                    java.io.PrintStream out)
Print an expression to a given stream with a newline.

Parameters:
node - root node of the expression to print
out - stream to print to

println

public void println(Node node)
Print an expression to System.out with a new line.

Parameters:
node - root node of the expression to print

println

public void println()
Print the last parsed expression to System.out with a new line.


toString

public java.lang.String toString(Node node)
Returns a string representation of an expression.

Parameters:
node - root node of the expression.
Returns:
string representation

rootNodeToString

public java.lang.String rootNodeToString()
Returns a string representation of the last expression parsed.

Returns:
string representation


Copyright © 2007 Singular Systems http://www.singularsys.com/jep