lp.struct.util
Class LpPrettyPrinter<W extends Writer>

java.lang.Object
  extended by java.io.Writer
      extended by lp.struct.util.LpPrinter<W>
          extended by lp.struct.util.LpPrettyPrinter<W>
All Implemented Interfaces:
Closeable, Flushable, Appendable, LpStructureUnitVisitor
Direct Known Subclasses:
DlpGrounder.DlpTagger, EvolpPrettyPrinter

public class LpPrettyPrinter<W extends Writer>
extends LpPrinter<W>

Transforms a logic program (or its components} represented by objects implementing the LpStructureUnit interface into a human-readable (standard) textual representation. For example the following code:

LpPredicate predP = LpPredicate.getInstance("p", 0);
LpLiteral litP = LpAtom.getInstance(predP, null).getNegativeLiteral();
LpRule fact = new LpRule(litP, null);
System.out.println(LpPrettyPrinter.asString(fact));
would produce the following output:
not p.

Version:
1.0.0
Author:
Martin Slota
See Also:
LpPrinter

Field Summary
protected  String ARROW_STRING
          String that separates the head and body of a rule.
protected  String SPACE_STRING
          String containing a space or nothing.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
LpPrettyPrinter(W out)
          Creates a new instance of LpPrettyPrinter.
LpPrettyPrinter(W out, String arrow, boolean withSpaces)
          Creates a new instance of LpPrettyPrinter.
 
Method Summary
protected  void appendRuleNoDot(LpRule rule)
          Appends a natural textual form of rule to this character sequence not taking into account the position of the rule within the structure and without the trailing dot.
protected  void arrow()
          Appends the ARROW_STRING.
protected  void beginParen()
          Appends a left parenthesis.
protected  void comma()
          Appends a comma.
protected  void dot()
          Appends a dot.
protected  void endParen()
          Appends a right parenthesis.
static LpBuffer getBuffer()
          A convenience method that creates a new LpBuffer that uses a new LpPrettyPrinter instance to write LpStructureUnits.
static LpBuffer getBuffer(String arrow, boolean withSpaces)
          A convenience method that creates a new LpBuffer that uses a new LpPrettyPrinter instance to write LpStructureUnits.
protected  void literalPrefix(boolean positive)
          Appends an empty string when positive is true and the string "not " otherwise.
protected  void space()
          Appends the SPACE_STRING.
 void visit(LpAtom atom)
          Appends a natural textual form of a LpAtom instance to the underlying Writer.
 void visit(LpCompoundTerm term)
          Appends a natural textual form of a LpCompoundTerm instance to the underlying Writer.
 void visit(LpConstant con)
          Appends a natural textual form of a LpConstant instance to the underlying Writer.
 void visit(LpFunction fun)
          Appends a natural textual form of a LpFunction instance to the underlying Writer.
 void visit(LpLiteral lit)
          Appends a natural textual form of a LpLiteral instance to the underlying Writer.
 void visit(LpPredicate pred)
          Appends a natural textual form of a LpPredicate instance to the underlying Writer.
 void visit(LpRule rule)
          Appends a natural textual form of a LpRule instance to the underlying Writer.
 void visit(LpVariable var)
          Appends a natural textual form of a LpVariable instance to the underlying Writer.
protected  void visitArgumentList(List<LpTerm> args)
          This method is used in visit(LpCompoundTerm) and visit(LpLiteral).
 
Methods inherited from class lp.struct.util.LpPrinter
append, append, append, append, close, flush, getOut, setOut, write, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ARROW_STRING

protected final String ARROW_STRING
String that separates the head and body of a rule.


SPACE_STRING

protected final String SPACE_STRING
String containing a space or nothing. It is added to places where extra whitespace will improve the readability of the output.

Constructor Detail

LpPrettyPrinter

public LpPrettyPrinter(W out)
Creates a new instance of LpPrettyPrinter. It will use the string "<-" to separate the head and body of a rule. The output will also contain extra spaces to enhance readability.

Parameters:
out - the underlying writer into which everything will be written

LpPrettyPrinter

public LpPrettyPrinter(W out,
                       String arrow,
                       boolean withSpaces)
Creates a new instance of LpPrettyPrinter. It will use the string arrow to separate the head and body of a rule. If withSpaces is true, then the output will also contain extra spaces to enhance readability.

Parameters:
out - the underlying writer into which everything will be written
arrow - the string to separate the head and body of a rule
withSpaces - determines whether extra spaces should be added to places where they can make the output more readable
Throws:
IllegalArgumentException - if arrow is null
Method Detail

getBuffer

public static LpBuffer getBuffer()
A convenience method that creates a new LpBuffer that uses a new LpPrettyPrinter instance to write LpStructureUnits. It will use the string "<-" to separate the head and body of a rule. The output will also contain extra spaces to enhance readability.

Returns:
as specified above
See Also:
LpBuffer

getBuffer

public static LpBuffer getBuffer(String arrow,
                                 boolean withSpaces)
A convenience method that creates a new LpBuffer that uses a new LpPrettyPrinter instance to write LpStructureUnits. It will use the string arrow to separate the head and body of a rule. If withSpaces is true, then the output will also contain extra spaces to enhance readability.

Parameters:
arrow - the string to separate the head and body of a rule
withSpaces - determines whether extra spaces should be added to places where they can make the output more readable
Returns:
as specified above
See Also:
LpBuffer

beginParen

protected void beginParen()
Appends a left parenthesis.

Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

endParen

protected void endParen()
Appends a right parenthesis.

Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

comma

protected void comma()
Appends a comma.

Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

space

protected void space()
Appends the SPACE_STRING.

Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

literalPrefix

protected void literalPrefix(boolean positive)
Appends an empty string when positive is true and the string "not " otherwise.

Parameters:
positive - identifies whether a prefix for a positive or a negative literal should be appended
Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

arrow

protected void arrow()
Appends the ARROW_STRING.

Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

dot

protected void dot()
Appends a dot.

Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

visit

public void visit(LpConstant con)
Appends a natural textual form of a LpConstant instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The natural form of a constant is its name (see LpConstant.getName()).

Parameters:
con - the LpConstant instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if con is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpConstant)

visit

public void visit(LpVariable var)
Appends a natural textual form of a LpVariable instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The natural form of a variable is its name (see LpVariable.getName()).

Parameters:
var - the LpVariable instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if var is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpVariable)

visit

public void visit(LpFunction fun)
Appends a natural textual form of a LpFunction instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The natural form of a function is its name (see LpFunction.getName()).

Parameters:
fun - the LpFunction instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if fun is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpFunction)

visitArgumentList

protected void visitArgumentList(List<LpTerm> args)
This method is used in visit(LpCompoundTerm) and visit(LpLiteral). Can also be useful for overriding classes. It appends the natural textual form of a list of terms. It is composed of a left parenthesis followed by natural textual representations of the terms in the list, separated from each other by commas. It ends with a right parenthesis.

Parameters:
args - the argument list that will be appended to the underlying Writer
Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

visit

public void visit(LpCompoundTerm term)
Appends a natural textual form of a LpCompoundTerm instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The natural form of a compound term is its function's natural form followed by a left parenthesis and the natural textual representations of its arguments, separated from each other by commas. It ends with a right parenthesis.

Parameters:
term - the LpCompoundTerm instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if term is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpCompoundTerm)

visit

public void visit(LpPredicate pred)
Appends a natural textual form of a LpPredicate instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The natural form of a predicate is its name (see LpPredicate.getName()).

Parameters:
pred - the LpPredicate instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if pred is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpPredicate)

visit

public void visit(LpAtom atom)
Appends a natural textual form of a LpAtom instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The natural form of an atom is the natural form of its predicate followed by a left parenthesis and the natural textual representations of its arguments, separated from each other by commas. It ends with a right parenthesis.

Parameters:
atom - the LpAtom instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if atom is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpAtom)

visit

public void visit(LpLiteral lit)
Appends a natural textual form of a LpLiteral instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The natural form of a literal is the natural form of its underlying atom with "not" and the SPACE_STRING string in front of it if LpLiteral.isPositive() is false.

Parameters:
lit - the LpLiteral instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if lit is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpLiteral)

appendRuleNoDot

protected void appendRuleNoDot(LpRule rule)
Appends a natural textual form of rule to this character sequence not taking into account the position of the rule within the structure and without the trailing dot.

Parameters:
rule - the LpRule instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if rule is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
visit(LpRule)

visit

public void visit(LpRule rule)
Appends a natural textual form of a LpRule instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. A natural form of a rule consists of the natural form of its head followed by special separator (set in the constructor) and natural forms of literals in its body, separated from each other by commas. It ends with a dot. If LpRule.getHead() is null, the natural form begins with the separator string (the head's natural form is left out). If LpRule.getBody() is null or has no members, the natural form only consists of the head's natural form and a dot.

Parameters:
rule - the LpRule instance whose natural textual representation should be appended to the underlying Writer
Throws:
NullPointerException - if rule is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpRule)