|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.singularsys.jep.Jep
public class Jep
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(); 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.getMessage()); }
The class is serializable. Please see the documentation section "Serialization" for more information on the various options for serializing.
Field Summary | |
---|---|
protected java.util.List<JepComponent> |
additionalComponents
Additional components out side the standard set |
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 OperatorTableI |
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. |
Method Summary | |
---|---|
Variable |
addConstant(java.lang.String name,
java.lang.Object value)
Add a constant: a variable who's value cannot be changed. |
protected void |
addDefaultComponents()
Set the default values for components if none are specified. |
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. |
Node |
continueParsing()
Continue parsing without re-initialising the stream. |
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. |
JepComponent |
getAdditionalComponent(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. |
boolean |
getAllowAssignment()
Whether assignment equation y=x+1 equations are allowed. |
boolean |
getAllowUndeclared()
Returns the value of the allowUndeclared option. |
java.lang.Object |
getDefaultValue()
Returns the default value used for new variables. |
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 |
OperatorTableI |
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 . |
void |
initMultiParse(java.io.Reader reader)
Initialises the parser with a Reader for parsing with multiple expressions. |
void |
initMultiParse(java.lang.String str)
Initialises the parser for parsing a string with multiple expressions. |
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. |
protected void |
setComponentInternal(JepComponent comp)
Identifies the type of comp and set the associated member
accordingly. |
void |
setComponents(JepComponent... components)
Sets one or more components for this Jep instance. |
protected void |
setComponentsInternal(ComponentSet compSet)
|
void |
setDefaultValue(java.lang.Object defaultValue)
Sets the default value used for new variables. |
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. |
boolean |
tryAddConstant(java.lang.String name,
java.lang.Object value)
Tries to add a constant. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected NumberFactory numFac
protected VariableFactory varFac
protected NodeFactory nodeFac
protected VariableTable varTab
protected FunctionTable funTab
protected OperatorTableI opTab
protected PrintVisitor pv
protected transient Node lastRootNode
protected Parser parser
protected Evaluator evaluator
protected java.util.List<JepComponent> additionalComponents
protected boolean implicitMul
protected boolean allowUndeclared
protected boolean allowAssignment
Constructor Detail |
---|
public Jep()
StandardComponents
component set is used.
public Jep(ComponentSet compSet)
compSet
- The component set to be used.public Jep(JepComponent... components)
components
- Method Detail |
---|
public void setComponent(JepComponent component)
component
passed in as a parameter overrides the
active component and the active component is no longer used.
reinitializeComponents()
is called after the component is added.
component
- The component to be set.public void setComponents(JepComponent... components)
components
- An array of components to be set.protected void addDefaultComponents()
protected void setComponentsInternal(ComponentSet compSet)
protected void setComponentInternal(JepComponent comp)
comp
and set the associated member
accordingly. This overrides the current component set.
comp
- The component to be set.public void reinitializeComponents()
init
method of all components. This is necessary
in a few cases such as after adding new operators to the configurable
parser.
public NumberFactory getNumberFactory()
public NodeFactory getNodeFactory()
public VariableFactory getVariableFactory()
public FunctionTable getFunctionTable()
public OperatorTableI getOperatorTable()
public VariableTable getVariableTable()
public Parser getParser()
public Evaluator getEvaluator()
public PrintVisitor getPrintVisitor()
public java.util.List<JepComponent> getAdditionalComponents()
reinitializeComponents()
. New components should be added
by calling setComponent(JepComponent)
.
public JepComponent getAdditionalComponent(java.lang.Class<?> cl)
cl
- The class to test for
public Variable addVariable(java.lang.String name)
name
- the name of the variable to be added
Variable
instancepublic Variable addVariable(java.lang.String name, java.lang.Object value) throws JepException
name
- the name of the variable to be addedvalue
- the value of the variable to be added
Variable
instance
JepException
- if an attempt is made to set the value of a constant variablepublic Variable addVariable(java.lang.String name, double value) throws JepException
name
- the name of the variable to be addedvalue
- the value of the variable to be added
Variable
instance
JepException
- if an attempt is made to set the value of a constant variablepublic Variable addVariable(java.lang.String name, double re, double im) throws JepException
name
- the name of the variable to be addedre
- the real component of the variable to be addedim
- the imaginary component of the variable to be added
Variable
instance
JepException
- if an attempt is made to set the value of a constant variablepublic Variable addConstant(java.lang.String name, java.lang.Object value) throws JepException
name
- value
-
JepException
- if a constant variable already existspublic boolean tryAddConstant(java.lang.String name, java.lang.Object value)
name
- value
-
public boolean addStandardConstants()
public Variable getVariable(java.lang.String name)
Variable
instance of the variable
name
. If the variable has not been added, null is returned.
name
- the name of the variable
Variable
instance, or null if the variable
has not been added.public java.lang.Object getVariableValue(java.lang.String name)
name
. If the variable has
not been added, or if it's value is null
, null
is returned.
name
- the name of the variable
public void setAllowAssignment(boolean value)
public boolean getAllowAssignment()
public void setAllowUndeclared(boolean 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.
value
- The boolean option for allowing undeclared variables.public boolean getAllowUndeclared()
public void setImplicitMul(boolean value)
"1 2"is valid and is interpreted as
"1*2".
The default value is true.
value
- The boolean implicit multiplication option.public java.lang.Object getDefaultValue()
public void setDefaultValue(java.lang.Object defaultValue)
defaultValue
- StandardEvaluator.setTrapNullValues(boolean)
public boolean getImplicitMul()
public PostfixMathCommandI addFunction(java.lang.String name, PostfixMathCommandI pfmc)
name
- the name of the functionpfmc
- the function class instance to be used to evaluate the function
public Node getLastRootNode()
public Node parse(java.io.Reader reader) throws ParseException
reader
- the input reader
ParseException
public Node parse(java.lang.String str) throws ParseException
str
- the input string
ParseException
public void initMultiParse(java.lang.String str)
str
- String containing a sequence of expressions separated by semi-colons.continueParsing()
public void initMultiParse(java.io.Reader reader)
reader
- Reader from which equations separated by semi-colons will be read.continueParsing()
public Node continueParsing() throws ParseException
XJep j = new XJep(); 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.
ParseException
initMultiParse(String)
public double evaluateD() throws EvaluationException
EvaluationException
- if an error occurs while evaluating or
if the returned data type can not be converted to a double
number.public java.lang.Object evaluate() throws EvaluationException
EvaluationException
- if an error occurspublic java.lang.Object evaluate(Node node) throws EvaluationException
EvaluationException
- if an error occurspublic void print(Node node, java.io.PrintStream out)
node
- root node of the expression to printout
- stream to print topublic void print(Node node)
node
- root node of the expression to printpublic void print()
public void println(Node node, java.io.PrintStream out)
node
- root node of the expression to printout
- stream to print topublic void println(Node node)
node
- root node of the expression to printpublic void println()
public java.lang.String toString(Node node)
node
- root node of the expression.
public java.lang.String rootNodeToString()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |