Package com.singularsys.jep
Class PrintVisitor
java.lang.Object
com.singularsys.jep.PrintVisitor
- All Implemented Interfaces:
JepComponent,ParserVisitor,Serializable
- Direct Known Subclasses:
PrintVisitor.NullPrintVisitor
Prints an expression with configuration options.
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.
- Since:
- Dec 04 and NumberFormat object can be supplied to modify printing of numbers., Dec 04 PrintVisitor can now cope with 3 or more arguments to + and *., 3.3 greater customisation now possible, 4.0 Operators can implement PrintRulesI
- Author:
- Rich Morris Created on 20-Jun-2003
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA do-nothing version of a PrintVisitor for reducing memory usage.static interfaceThis interface specifies the method needed to implement a special print rule.static final classPrints a list access in form a[3] or for multidimensional access use the list syntax.static final classPrints a list of elements using '[' and ']' as delimiters and ',' as a separators. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intEnable this mode to print complex numbers as 3+2 iprotected NumberFormatThe NumberFormat object used to print numbers.protected FieldPositionstatic final intEnable this mode to print all brackets.protected Stringprotected Stringprotected Stringprotected Stringprotected intThe current mode for printing.static final PrintVisitor.NullPrintVisitorA do-nothing PrintVisitor, useful for reducing memory usage when printing not required.protected OperatorTableIprotected Stringprotected StringBufferprotected HashMap<String,PrintVisitor.PrintRulesI> protected HashMap<Operator,PrintVisitor.PrintRulesI> -
Constructor Summary
ConstructorsConstructorDescriptionCreates a visitor to create and print string representations of an expression tree. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSpecialRule(Operator op, PrintVisitor.PrintRulesI rule) Adds a special print rule to be added for a given operator.voidaddSpecialRule(String funName, PrintVisitor.PrintRulesI rule) Adds a special print rule to be added for a given function.addSpecialVarName(String oldName, String newName) Allows a replacement for a specific variable namevoidAdd a string to buffer.formatValue(Object val) Returns a formatted version of the value.voidformatValue(Object val, StringBuffer sb1) Appends a formatted versions of val to the string buffer.Get the string separating function arguments.Get the string for left bracket around function arguments.Get the string for right bracket around function arguments.Get the string for left brackets.Gets a light-weight instance suitable for using in multiple threads.intintgetMode()Return the current print mode.booleangetMode(int testmode) Returns true if the specified testmode is active.Get the string for right brackets.voidInitialize the component.voidPrints on System.out.voidprint(Node node, PrintStream out) Prints the tree descending from node with lots of brackets or specified stream.voidprintBrackets(Node node) Print a node surrounded by brackets.voidPrints on System.out.voidprintln(Node node, PrintStream out) Prints the tree descending from node with a newline at end.voidprintNoBrackets(Node node) Print the node with no brackets.voidprintWrap(StringBuffer sb1, PrintStream out) Utility method to print a wrapped version of the output.voidsetFunArgSep(String functionArgSep) Set the string separating function arguments.voidsetFunLBracket(String funLBracket) Set the string for left bracket around function arguments.voidsetFunRBracket(String funRBracket) Set the string for right bracket around function arguments.voidsetLBracket(String bracket) Set the string for left brackets.voidsetMaxLen(int i) Sets the maximum length printed per line.voidsetMode(int mode, boolean flag) Set printing mode.voidsetNumberFormat(NumberFormat format) Sets the number format to use.voidsetRBracket(String bracket) Set the string for right brackets.booleanTest if brackets are necessary around left-hand side.booleanTest if brackets are needed around a middle argumentx + (y) + zbooleanTests if brackets are necessary around the RHS child of an operator.Returns a String representation of the equation.visit(ASTConstant node, Object data) visit(ASTFunNode node, Object data) Prints a standard function: fun(arg, arg)visit(ASTVarNode node, Object data) protected voidvisitBinary(ASTOpNode node) protected voidvisitUnary(ASTOpNode node) Print a unary operator.
-
Field Details
-
LBracket
-
RBracket
-
FunLBracket
-
FunRBracket
-
FunctionArgSep
-
FULL_BRACKET
public static final int FULL_BRACKETEnable this mode to print all brackets. Removes all ambiguity.- See Also:
-
COMPLEX_I
public static final int COMPLEX_IEnable this mode to print complex numbers as 3+2 i- See Also:
-
sb
-
mode
protected int modeThe current mode for printing. Stores multiple flags as a bit field. -
specialRules
-
specialFunRules
-
specialVarName
-
format
The NumberFormat object used to print numbers. -
fp
-
ot
-
NULL_PRINT_VISITOR
A do-nothing PrintVisitor, useful for reducing memory usage when printing not required. This is static Singleton instance which can be safely be reused.- Since:
- 3.5
-
-
Constructor Details
-
PrintVisitor
public PrintVisitor()Creates a visitor to create and print string representations of an expression tree.
-
-
Method Details
-
init
Description copied from interface:JepComponentInitialize the component. This method is called whenever a component is added to Jep. Hence, it allows components to keep track of the other components they may rely on.- Specified by:
initin interfaceJepComponent- Parameters:
jep- the current Jep instance
-
print
Prints the tree descending from node with lots of brackets or specified stream.- See Also:
-
printWrap
Utility method to print a wrapped version of the output. Each line will be a maximum of maxLen characters wide with lines broken on- Parameters:
sb1- the StringBuffer to printout- the output stream
-
print
Prints on System.out. -
println
Prints the tree descending from node with a newline at end. -
println
Prints on System.out. -
toString
Returns a String representation of the equation. -
append
Add a string to buffer. Classes implementingPrintVisitor.PrintRulesIshould call this method.- Parameters:
s- the string to append- Since:
- 3.4.0
-
addSpecialRule
Adds a special print rule to be added for a given operator.- Parameters:
op- the operator the rule applies torule- the print rule to use
-
addSpecialRule
Adds a special print rule to be added for a given function.- Parameters:
funName- name of functionrule- the print rule to use
-
addSpecialVarName
Allows a replacement for a specific variable name- Parameters:
oldName- name of variable to replacenewName- string to print as replacement
-
printNoBrackets
Print the node with no brackets.- Throws:
JepException
-
printBrackets
Print a node surrounded by brackets.- Throws:
JepException
-
visitUnary
Print a unary operator.- Throws:
JepException
-
visitBinary
- Throws:
JepException
-
testLeft
Test if brackets are necessary around left-hand side.- Parameters:
top- The current operator being printed.lhs- The node on the left-hand side.- Returns:
- True if a bracket is necessary, false otherwise.
-
testMid
Test if brackets are needed around a middle argumentx + (y) + z- Parameters:
top- The current operator being printed.mid- The node in the middle of a chain.- Returns:
- True if a bracket is necessary, false otherwise.
-
testRight
Tests if brackets are necessary around the RHS child of an operator.- Parameters:
top- The operator.rhs- The right hand side node.- Returns:
- True if brackets are necessary, false otherwise.
-
visit
Prints a standard function: fun(arg, arg)- Specified by:
visitin interfaceParserVisitor- Throws:
JepException
-
visit
- Specified by:
visitin interfaceParserVisitor- Throws:
JepException
-
visit
- Specified by:
visitin interfaceParserVisitor- Throws:
JepException
-
visit
- Specified by:
visitin interfaceParserVisitor
-
formatValue
Appends a formatted versions of val to the string buffer.- Parameters:
val- The value to formatsb1- The StringBuffer to append to
-
formatValue
Returns a formatted version of the value. -
getMode
public int getMode()Return the current print mode. Note that the mode is a bit field. -
getMode
public boolean getMode(int testmode) Returns true if the specified testmode is active.- Parameters:
testmode- The mode to check.- Returns:
- True if the specified mode is active.
-
setMode
public void setMode(int mode, boolean flag) Set printing mode. In full bracket mode the brackets each element in the tree will be surrounded by brackets to indicate the tree structure. In the default mode, (full bracket off) the number of brackets is minimized so (x+y)+z will be printed as x+y+z.- Parameters:
mode- which flags to change, typically FULL_BRACKETflag- whether to switch this mode on or off
-
setNumberFormat
Sets the number format to use. A useful format is a DecimalFormat with minimum fraction digits set to zero. This makes simple powers, like x^2, print nicely.DecimalFormat fmt = new DecimalFormat(); fmt.setMinimumFractionDigits(0); fmt.setMaximumFractionDigits(15); jep.getPrintVisitor().setNumberFormat(fmt);
- Parameters:
format- format to use, if null just uses toString
-
setMaxLen
public void setMaxLen(int i) Sets the maximum length printed per line. If the value is not -1 then the string will be broken into chunks each of which is less than the max length.- Parameters:
i- the maximum length
-
getMaxLen
public int getMaxLen()- Returns:
- the maximum length printed per line
-
getLightWeightInstance
Description copied from interface:JepComponentGets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstancein interfaceJepComponent- Returns:
- either a new instance, null or 'this'.
-
getLBracket
Get the string for left brackets. -
setLBracket
Set the string for left brackets. -
getRBracket
Get the string for right brackets. -
setRBracket
Set the string for right brackets. -
getFunLBracket
Get the string for left bracket around function arguments. -
setFunLBracket
Set the string for left bracket around function arguments. -
getFunRBracket
Get the string for right bracket around function arguments. -
setFunRBracket
Set the string for right bracket around function arguments. -
getFunArgSep
Get the string separating function arguments. -
setFunArgSep
Set the string separating function arguments.
-