public class PrintVisitor extends java.lang.Object implements ParserVisitor, JepComponent
By default, expressions are printed with only necessary
bracketing. For example, if an expression "a+(b+c)" is parsed and printed,
it will be printed as a+b+c
since the bracketing does not affect the
evaluation.
Enabling the FULL_BRACKET mode prints the expression with many brackets as
((-1.0)/sqrt((1.0-(x^2.0))))
.
To use the PrintVisitor, you can invoke it through the Jep class as follows:
Jep j = ...; Node in = ...; j.print(in, "x");
You can also print expressions independently from the Jep class by using this class and calling the various print methods.
Jep.print(Node)
,
Jep.print(Node, PrintStream)
,
Jep.println(Node)
,
Jep.println(Node, PrintStream)
,
Jep.toString(Node)
,
Serialized FormModifier and Type | Class and Description |
---|---|
static interface |
PrintVisitor.PrintRulesI
This interface specifies the method needed to implement a special print rule.
|
static class |
PrintVisitor.StdElePrintRule
Prints an list access in form a[3] or for multidimensional access use the list syntax.
|
static class |
PrintVisitor.StdListPrintRule
Prints a list of elements using '[' and ']' as delimiters and ',' as a separators.
|
Modifier and Type | Field and Description |
---|---|
static int |
COMPLEX_I
Enable this mode to print complex numbers as 3+2 i
|
protected java.text.NumberFormat |
format
The NumberFormat object used to print numbers.
|
protected java.text.FieldPosition |
fp |
static int |
FULL_BRACKET
Enable this mode to print all brackets.
|
protected java.lang.String |
FunctionArgSep |
protected java.lang.String |
FunLBracket |
protected java.lang.String |
FunRBracket |
protected java.lang.String |
LBracket |
protected int |
mode
The current mode for printing.
|
static com.singularsys.jep.PrintVisitor.NullPrintVisitor |
NULL_PRINT_VISITOR
A do-nothing PrintVisitor, useful for reducing memory usage when printing not required.
|
protected OperatorTableI |
ot |
protected java.lang.String |
RBracket |
protected java.lang.StringBuffer |
sb |
protected java.util.HashMap<java.lang.String,PrintVisitor.PrintRulesI> |
specialFunRules |
protected java.util.HashMap<Operator,PrintVisitor.PrintRulesI> |
specialRules |
protected java.util.HashMap<java.lang.String,java.lang.String> |
specialVarName |
Constructor and Description |
---|
PrintVisitor()
Creates a visitor to create and print string representations of an expression tree.
|
Modifier and Type | Method and Description |
---|---|
void |
addSpecialRule(Operator op,
PrintVisitor.PrintRulesI rule)
Adds a special print rule to be added for a given operator.
|
void |
addSpecialRule(java.lang.String funName,
PrintVisitor.PrintRulesI rule)
Adds a special print rule to be added for a given function.
|
java.lang.String |
addSpecialVarName(java.lang.String oldName,
java.lang.String newName)
Allows a replacement for a specific variable name
|
void |
append(java.lang.String s)
Add a string to buffer.
|
java.lang.String |
formatValue(java.lang.Object val)
Returns a formatted version of the value.
|
void |
formatValue(java.lang.Object val,
java.lang.StringBuffer sb1)
Appends a formatted versions of val to the string buffer.
|
java.lang.String |
getFunArgSep()
Get the string separating function arguments.
|
java.lang.String |
getFunLBracket()
Get the string for left bracket around function arguments.
|
java.lang.String |
getFunRBracket()
Get the string for right bracket around function arguments.
|
java.lang.String |
getLBracket()
Get the string for left brackets.
|
JepComponent |
getLightWeightInstance()
Gets a light-weight instance suitable for using in multiple threads.
|
int |
getMaxLen() |
int |
getMode()
Return the current print mode.
|
boolean |
getMode(int testmode)
Returns true if the specified testmode is active.
|
java.lang.String |
getRBracket()
Get the string for right brackets.
|
void |
init(Jep jep)
Initialize the component.
|
void |
print(Node node)
Prints on System.out.
|
void |
print(Node node,
java.io.PrintStream out)
Prints the tree descending from node with lots of brackets
or specified stream.
|
void |
printBrackets(Node node)
Print a node surrounded by brackets.
|
void |
println(Node node)
Prints on System.out.
|
void |
println(Node node,
java.io.PrintStream out)
Prints the tree descending from node with a newline at end.
|
void |
printNoBrackets(Node node)
Print the node with no brackets.
|
void |
printWrap(java.lang.StringBuffer sb1,
java.io.PrintStream out)
Utility method to print a wrapped version of the output.
|
void |
setFunArgSep(java.lang.String functionArgSep)
Set the string separating function arguments.
|
void |
setFunLBracket(java.lang.String funLBracket)
Set the string for left bracket around function arguments.
|
void |
setFunRBracket(java.lang.String funRBracket)
Set the string for right bracket around function arguments.
|
void |
setLBracket(java.lang.String bracket)
Set the string for left brackets.
|
void |
setMaxLen(int i)
Sets the maximum length printed per line.
|
void |
setMode(int mode,
boolean flag)
Set printing mode.
|
void |
setNumberFormat(java.text.NumberFormat format)
Sets the number format to use.
|
void |
setRBracket(java.lang.String bracket)
Set the string for right brackets.
|
protected boolean |
testLeft(Operator top,
Node lhs)
Test if brackets are necessary around left hand side.
|
protected boolean |
testMid(Operator top,
Node rhs) |
protected boolean |
testRight(Operator top,
Node rhs)
Tests if closing brackets are necessary.
|
java.lang.String |
toString(Node node)
Returns a String representation of the equation.
|
java.lang.Object |
visit(ASTConstant node,
java.lang.Object data) |
java.lang.Object |
visit(ASTFunNode node,
java.lang.Object data)
Prints a standard function: fun(arg, arg)
|
java.lang.Object |
visit(ASTOpNode node,
java.lang.Object data) |
java.lang.Object |
visit(ASTVarNode node,
java.lang.Object data) |
protected java.lang.String LBracket
protected java.lang.String RBracket
protected java.lang.String FunLBracket
protected java.lang.String FunRBracket
protected java.lang.String FunctionArgSep
public static final int FULL_BRACKET
public static final int COMPLEX_I
protected transient java.lang.StringBuffer sb
protected int mode
protected java.util.HashMap<Operator,PrintVisitor.PrintRulesI> specialRules
protected java.util.HashMap<java.lang.String,PrintVisitor.PrintRulesI> specialFunRules
protected java.util.HashMap<java.lang.String,java.lang.String> specialVarName
protected java.text.NumberFormat format
protected transient java.text.FieldPosition fp
protected OperatorTableI ot
public static final com.singularsys.jep.PrintVisitor.NullPrintVisitor NULL_PRINT_VISITOR
public PrintVisitor()
public void init(Jep jep)
JepComponent
init
in interface JepComponent
jep
- the current Jep instancepublic void print(Node node, java.io.PrintStream out)
Jep.println(Node, PrintStream)
public void printWrap(java.lang.StringBuffer sb1, java.io.PrintStream out)
sb1
- the StringBuffer to printout
- the output streampublic void print(Node node)
public void println(Node node, java.io.PrintStream out)
public void println(Node node)
public java.lang.String toString(Node node)
public void append(java.lang.String s)
s
- the string to appendpublic void addSpecialRule(Operator op, PrintVisitor.PrintRulesI rule)
op
- the operator the rule applies torule
- the print rule to usepublic void addSpecialRule(java.lang.String funName, PrintVisitor.PrintRulesI rule)
funName
- name of functionrule
- the print rule to usepublic java.lang.String addSpecialVarName(java.lang.String oldName, java.lang.String newName)
oldName
- name of variable to replacenewName
- string to print as replacementpublic void printNoBrackets(Node node) throws JepException
JepException
public void printBrackets(Node node) throws JepException
JepException
protected boolean testLeft(Operator top, Node lhs)
top
- The current operator being printed.lhs
- The node on the left hand side.protected boolean testRight(Operator top, Node rhs)
top
- The operator.rhs
- The right hand side node.public java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws JepException
visit
in interface ParserVisitor
JepException
public java.lang.Object visit(ASTOpNode node, java.lang.Object data) throws JepException
visit
in interface ParserVisitor
JepException
public java.lang.Object visit(ASTVarNode node, java.lang.Object data) throws JepException
visit
in interface ParserVisitor
JepException
public java.lang.Object visit(ASTConstant node, java.lang.Object data) throws JepException
visit
in interface ParserVisitor
JepException
public void formatValue(java.lang.Object val, java.lang.StringBuffer sb1)
val
- The value to formatsb1
- The StringBuffer to append topublic java.lang.String formatValue(java.lang.Object val)
public int getMode()
public boolean getMode(int testmode)
testmode
- The mode to check.public void setMode(int mode, boolean flag)
mode
- which flags to change, typically FULL_BRACKETflag
- whether to switch this mode on or offpublic void setNumberFormat(java.text.NumberFormat format)
DecimalFormat fmt = new DecimalFormat(); fmt.setMinimumFractionDigits(0); fmt.setMaximumFractionDigits(15); jep.getPrintVisitor().setNumberFormat(fmt);
format
- format to use, if null just uses toStringpublic void setMaxLen(int i)
i
- the maximum lengthpublic int getMaxLen()
public JepComponent getLightWeightInstance()
JepComponent
getLightWeightInstance
in interface JepComponent
public java.lang.String getLBracket()
public void setLBracket(java.lang.String bracket)
public java.lang.String getRBracket()
public void setRBracket(java.lang.String bracket)
public java.lang.String getFunLBracket()
public void setFunLBracket(java.lang.String funLBracket)
public java.lang.String getFunRBracket()
public void setFunRBracket(java.lang.String funRBracket)
public java.lang.String getFunArgSep()
public void setFunArgSep(java.lang.String functionArgSep)
Copyright © 2018 Singular Systems http://www.singularsys.com/jep