Package com.singularsys.jep
Class Jep
- java.lang.Object
- 
- com.singularsys.jep.Jep
 
- 
- All Implemented Interfaces:
- java.io.Serializable
 - Direct Known Subclasses:
- XJep
 
 public class Jep extends java.lang.Object implements java.io.SerializableThe 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 theparse(String)andevaluatemethods after each other. The following code snippet shows a parsing and evaluating a simple expression with a single variable:Jep jep = new Jep(); try { jep.addVariable("x", 10); jep.parse("x+1"); Object result = jep.evaluate(); System.out.println("x + 1 = " + result); } catch (JepException e) { System.out.println("An error occurred: " + e.toString()); }The class is serializable. Please see the documentation section "Serialization" for more information on the various options for serializing. - See Also:
- Serialized Form
 
- 
- 
Field SummaryFields Modifier and Type Field Description protected java.util.List<JepComponent>additionalComponentsAdditional components out side the standard setprotected booleanallowAssignmentAllow Assignment optionprotected booleanallowUndeclaredAllow Undeclared Variables optionprotected EvaluatorevaluatorEvaluator instance used for evaluating parse treesprotected FunctionTablefunTabFunction Tableprotected booleanimplicitMulImplicit Multiplication optionprotected NodelastRootNodeRoot node of the last parsed expressionprotected NodeFactorynodeFacNode Factoryprotected NumberFactorynumFacNumber Factoryprotected OperatorTableIopTabOperator Tableprotected ParserparserParser instance used for parsing expressionsprotected PrintVisitorpvPrintVisitorprotected VariableFactoryvarFacVariable Factoryprotected VariableTablevarTabVariable Table
 - 
Constructor SummaryConstructors Constructor Description 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.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description VariableaddConstant(java.lang.String name, java.lang.Object value)Add a constant: a variable who's value cannot be changed.protected voidaddDefaultComponents()Set the default values for components if none are specified.PostfixMathCommandIaddFunction(java.lang.String name, PostfixMathCommandI pfmc)Adds a function to the parser.booleanaddStandardConstants()Adds the constants pi and e to the parser.VariableaddVariable(java.lang.String name)Adds a variable with a given name.VariableaddVariable(java.lang.String name, double value)Adds or sets a double variable with a given name and value.VariableaddVariable(java.lang.String name, double re, double im)Adds or sets a complex variable with a given name and value.VariableaddVariable(java.lang.String name, java.lang.Object value)Adds or sets a variable with a given name and value.NodecontinueParsing()Continue parsing without re-initialising the stream.java.lang.Objectevaluate()Evaluates the most recently parsed expression.java.lang.Objectevaluate(Node node)Evaluates the expression tree pointed to by the node parameter.doubleevaluateD()Evaluates the most recently parsed expression and returns the result as a double.JepComponentgetAdditionalComponent(java.lang.Class<?> cl)Return an additional component which is an instance of a particular class.java.util.List<JepComponent>getAdditionalComponents()Returns the additional components.booleangetAllowAssignment()Whether assignment equationy=x+1equations are allowed.booleangetAllowUndeclared()Returns the value of the allowUndeclared option.java.lang.ObjectgetDefaultValue()Returns the default value used for new variables.EvaluatorgetEvaluator()Returns the evaluatorFunctionTablegetFunctionTable()Returns the function tablebooleangetImplicitMul()Returns the value of the implicit multiplication option.NodegetLastRootNode()Returns the root node of the last successfully parsed expressionNodeFactorygetNodeFactory()Returns the node factoryNumberFactorygetNumberFactory()Returns the number factoryOperatorTableIgetOperatorTable()Returns the operator tableParsergetParser()Returns the parserPrintVisitorgetPrintVisitor()Returns the print visitorVariablegetVariable(java.lang.String name)Returns theVariableinstance of the variablename.VariableFactorygetVariableFactory()Returns the variable factoryVariableTablegetVariableTable()Returns the variable tablejava.lang.ObjectgetVariableValue(java.lang.String name)Returns the value of the variablename.voidinitMultiParse(java.io.Reader reader)Initialises the parser with a Reader for parsing with multiple expressions.voidinitMultiParse(java.lang.String str)Initialises the parser for parsing a string with multiple expressions.Nodeparse(java.io.Reader reader)Parses the input from a Reader.Nodeparse(java.lang.String str)Parses a string.voidprint()Print the last parsed expression to System.out.voidprint(Node node)Print an expression to System.out.voidprint(Node node, java.io.PrintStream out)Print an expression to a given stream.voidprintln()Print the last parsed expression to System.out with a new line.voidprintln(Node node)Print an expression to System.out with a new line.voidprintln(Node node, java.io.PrintStream out)Print an expression to a given stream with a newline.voidreinitializeComponents()Calls theinitmethod of all components.java.lang.StringrootNodeToString()Returns a string representation of the last expression parsed.voidsetAllowAssignment(boolean value)Sets whether assignment equations likey=x+1are allowed.voidsetAllowUndeclared(boolean value)Sets the value for the undeclared variables option.voidsetComponent(JepComponent component)Sets a single component.protected voidsetComponentInternal(JepComponent comp)Identifies the type ofcompand set the associated member accordingly.voidsetComponents(JepComponent... components)Sets one or more components for this Jep instance.protected voidsetComponentsInternal(ComponentSet compSet)voidsetDefaultValue(java.lang.Object defaultValue)Sets the default value used for new variables.voidsetImplicitMul(boolean value)Sets the value of the implicit multiplication option.VariablesetVariable(java.lang.String name, java.lang.Object value)Adds or sets a variable with a given name and value.java.lang.StringtoString(Node node)Returns a string representation of an expression.booleantryAddConstant(java.lang.String name, java.lang.Object value)Tries to add a constant.
 
- 
- 
- 
Field Detail- 
numFacprotected NumberFactory numFac Number Factory
 - 
varFacprotected VariableFactory varFac Variable Factory
 - 
nodeFacprotected NodeFactory nodeFac Node Factory
 - 
varTabprotected VariableTable varTab Variable Table
 - 
funTabprotected FunctionTable funTab Function Table
 - 
opTabprotected OperatorTableI opTab Operator Table
 - 
pvprotected PrintVisitor pv PrintVisitor
 - 
lastRootNodeprotected transient Node lastRootNode Root node of the last parsed expression
 - 
parserprotected Parser parser Parser instance used for parsing expressions
 - 
evaluatorprotected Evaluator evaluator Evaluator instance used for evaluating parse trees
 - 
additionalComponentsprotected java.util.List<JepComponent> additionalComponents Additional components out side the standard set
 - 
implicitMulprotected boolean implicitMul Implicit Multiplication option
 - 
allowUndeclaredprotected boolean allowUndeclared Allow Undeclared Variables option
 - 
allowAssignmentprotected boolean allowAssignment Allow Assignment option
 
- 
 - 
Constructor Detail- 
Jeppublic Jep() Creates a new Jep instance using the standard components with default settings. TheStandardComponentscomponent set is used.
 - 
Jeppublic Jep(ComponentSet compSet) Creates a new Jep instance with a specific component set.- Parameters:
- compSet- The component set to be used.
 
 - 
Jeppublic Jep(JepComponent... components) Creates a new Jep instance with a set of components. If the full set of components is not specified then default components will be used.- Parameters:
- components- a set of components which are used to build this Jep instance
- Since:
- 3.3 now varargs, default components used
 
 
- 
 - 
Method Detail- 
setComponentpublic void setComponent(JepComponent component) Sets a single component. Thecomponentpassed in as a parameter overrides the active component and the active component is no longer used.reinitializeComponents()is called after the component is added.- Parameters:
- component- The component to be set.
 
 - 
setComponentspublic void setComponents(JepComponent... components) Sets one or more components for this Jep instance. The current components are overridden by the components passed in. Each component can be one of the following:- NumberFactory
- VariableFactory
- NodeFactory
- VariableTable
- FunctionTable
- OperatorTable
- Parser
- Evaluator
 - Parameters:
- components- An array of components to be set.
 
 - 
addDefaultComponentsprotected void addDefaultComponents() Set the default values for components if none are specified. Called after any components specified in the constructor has been set.
 - 
setComponentsInternalprotected void setComponentsInternal(ComponentSet compSet) 
 - 
setComponentInternalprotected void setComponentInternal(JepComponent comp) Identifies the type ofcompand set the associated member accordingly. This overrides the current component set.- Parameters:
- comp- The component to be set.
 
 - 
reinitializeComponentspublic void reinitializeComponents() Calls theinitmethod of all components. This is necessary in a few cases such as after adding new operators to the configurable parser.
 - 
getNumberFactorypublic NumberFactory getNumberFactory() Returns the number factory- Returns:
- number factor used
 
 - 
getNodeFactorypublic NodeFactory getNodeFactory() Returns the node factory- Returns:
- node factory used
 
 - 
getVariableFactorypublic VariableFactory getVariableFactory() Returns the variable factory- Returns:
- variable factory used
 
 - 
getFunctionTablepublic FunctionTable getFunctionTable() Returns the function table- Returns:
- Function table used
 
 - 
getOperatorTablepublic OperatorTableI getOperatorTable() Returns the operator table- Returns:
- Operator table used
 
 - 
getVariableTablepublic VariableTable getVariableTable() Returns the variable table- Returns:
- variable table used
 
 - 
getParserpublic Parser getParser() Returns the parser- Returns:
- parser used
 
 - 
getEvaluatorpublic Evaluator getEvaluator() Returns the evaluator- Returns:
- evaluator used
 
 - 
getPrintVisitorpublic PrintVisitor getPrintVisitor() Returns the print visitor- Returns:
- print visitor used
 
 - 
getAdditionalComponentspublic java.util.List<JepComponent> getAdditionalComponents() Returns the additional components. Any modification to the list should be followed by a call toreinitializeComponents(). New components should be added by callingsetComponent(JepComponent).- Returns:
- the list
 
 - 
getAdditionalComponentpublic JepComponent getAdditionalComponent(java.lang.Class<?> cl) Return an additional component which is an instance of a particular class.- Parameters:
- cl- The class to test for
- Returns:
- the first component found which is an instance or null if none found.
- Since:
- 3.4.0
 
 - 
addVariablepublic 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 Variableinstance
 
 - 
addVariablepublic Variable addVariable(java.lang.String name, java.lang.Object value) throws JepException 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 Variableinstance
- Throws:
- JepException- if an attempt is made to set the value of a constant variable
 
 - 
setVariablepublic Variable setVariable(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 Variableinstance, return null if error setting variable
- Since:
- Jep 3.5
 
 - 
addVariablepublic Variable addVariable(java.lang.String name, double value) throws JepException 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 Variableinstance
- Throws:
- JepException- if an attempt is made to set the value of a constant variable
 
 - 
addVariablepublic Variable addVariable(java.lang.String name, double re, double im) throws JepException 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 Variableinstance
- Throws:
- JepException- if an attempt is made to set the value of a constant variable
 
 - 
addConstantpublic Variable addConstant(java.lang.String name, java.lang.Object value) throws JepException Add a constant: a variable who's value cannot be changed.- Parameters:
- name- constants name
- value- its value
- Returns:
- the corresponding Variable
- Throws:
- JepException- if a constant variable already exists
 
 - 
tryAddConstantpublic boolean tryAddConstant(java.lang.String name, java.lang.Object value)Tries to add a constant.- Parameters:
- name- name of constant
- value- value of constant
- Returns:
- true if successfully added or a constant with same name and value already exists. false if the constant has a different value
 
 - 
addStandardConstantspublic 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
 
 - 
getVariablepublic Variable getVariable(java.lang.String name) Returns theVariableinstance of the variablename. If the variable has not been added, null is returned.- Parameters:
- name- the name of the variable
- Returns:
- the Variableinstance, or null if the variable has not been added.
 
 - 
getVariableValuepublic java.lang.Object getVariableValue(java.lang.String name) Returns the value of the variablename. If the variable has not been added, or if it's value isnull,nullis 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.
 
 - 
setAllowAssignmentpublic void setAllowAssignment(boolean value) Sets whether assignment equations likey=x+1are allowed. The default is true (assignment allowed).- Parameters:
- value- true is assignment equations are allowed, false otherwise
 
 - 
getAllowAssignmentpublic boolean getAllowAssignment() Whether assignment equationy=x+1equations are allowed.- Returns:
- true is assignment equations are allowed, false otherwise
 
 - 
setAllowUndeclaredpublic 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 be automatically be added while parsing, and initialized to 0. Note this only affects the parsing stage, EvaluationException will still be thrown when trying to evaluate a variable with an undefined value.If this option is set to false, variables that were not previously added to Jep will produce an ParseException while parsing. The default value is true. - Parameters:
- value- The boolean option for allowing undeclared variables.
 
 - 
getAllowUndeclaredpublic boolean getAllowUndeclared() Returns the value of the allowUndeclared option.- Returns:
- True if the allowUndeclared option is enabled. False otherwise.
 
 - 
setImplicitMulpublic 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 true. - Parameters:
- value- The boolean implicit multiplication option.
 
 - 
getDefaultValuepublic java.lang.Object getDefaultValue() Returns the default value used for new variables.- Returns:
- the value
 
 - 
setDefaultValuepublic void setDefaultValue(java.lang.Object defaultValue) Sets the default value used for new variables. If the value is null (the default) then exceptions will be thrown when trying to evaluate the variables which do not have values set.- Parameters:
- defaultValue- value new variables have if not explicitly given
- See Also:
- StandardEvaluator.setTrapNullValues(boolean)
 
 - 
getImplicitMulpublic boolean getImplicitMul() Returns the value of the implicit multiplication option.- Returns:
- True if the implicit multiplication option is enabled. False otherwise.
 
 - 
addFunctionpublic 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:
- null unless there was a previous function with the same name in which case return the previous pfmc
 
 - 
getLastRootNodepublic Node getLastRootNode() Returns the root node of the last successfully parsed expression- Returns:
- the root node of the last successfully parsed expression
 
 - 
parsepublic 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- if there is a syntax error in expression
 
 - 
parsepublic 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- if there is a syntax error in expression
 
 - 
initMultiParsepublic void initMultiParse(java.lang.String str) Initialises the parser for parsing a string with multiple expressions. This method does not perform any parsing. Use continueParsing to parse the first expression and subsequent expressions from the input.- Parameters:
- str- String containing a sequence of expressions separated by semi-colons.
- See Also:
- continueParsing()
 
 - 
initMultiParsepublic void initMultiParse(java.io.Reader reader) Initialises the parser with a Reader for parsing with multiple expressions. This method does not perform any parsing. Use continueParsing to parse the first expression and subsequent expressions from the input.- Parameters:
- reader- Reader from which equations separated by semi-colons will be read.
- See Also:
- continueParsing()
 
 - 
continueParsingpublic Node continueParsing() throws ParseException Continue parsing without re-initialising the stream. This requires initMultiParse to be called first. Allows re-entrance of parser so that strings like "x=1; y=2; z=3;" can be parsed. When a semicolon is encountered parsing finishes leaving the rest of the string unparsed. Parsing can be resumed from the current position by using this method. For exampleJep j = new Jep(); j.initMultiParse("x=1;y=2; z=3;"); Node node; try { while((node = j.continueParsing())!=null) { j.println(node); } }catch(ParseException e) {}Empty equations such as "x=1;;y=2" are silently ignored, in this case the second call will return the node representing y=2.- Returns:
- top node of equation parsed to date or null at EOF.
- Throws:
- ParseException- if there is a syntax error in expression
- See Also:
- initMultiParse(String)
 
 - 
evaluateDpublic double evaluateD() throws EvaluationExceptionEvaluates 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- doublenumber.
 
 - 
evaluatepublic java.lang.Object evaluate() throws EvaluationExceptionEvaluates 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
 
 - 
evaluatepublic java.lang.Object evaluate(Node node) throws EvaluationException Evaluates the expression tree pointed to by the node parameter.- Parameters:
- node- root of expression tree to be evaluated
- Returns:
- Returns the value of the expression tree. If node is null, null is returned.
- Throws:
- EvaluationException- if an error occurs
 
 - 
printpublic 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
 
 - 
printpublic void print(Node node) Print an expression to System.out.- Parameters:
- node- root node of the expression to print
 
 - 
printpublic void print() Print the last parsed expression to System.out.
 - 
printlnpublic 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
 
 - 
printlnpublic void println(Node node) Print an expression to System.out with a new line.- Parameters:
- node- root node of the expression to print
 
 - 
printlnpublic void println() Print the last parsed expression to System.out with a new line.
 - 
toStringpublic java.lang.String toString(Node node) Returns a string representation of an expression.- Parameters:
- node- root node of the expression.
- Returns:
- string representation
 
 - 
rootNodeToStringpublic java.lang.String rootNodeToString() Returns a string representation of the last expression parsed.- Returns:
- string representation
 
 
- 
 
-