lp.struct.util
Class EvolpPrettyPrinter<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>
              extended by lp.struct.util.EvolpPrettyPrinter<W>
All Implemented Interfaces:
Closeable, Flushable, Appendable, LpStructureUnitVisitor
Direct Known Subclasses:
LpHtmlPrinter

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

Transforms a logic program (or its components} represented by objects implementing the LpStructureUnit interface into a human-readable (standard) textual representation. In addition to the functionality of LpPrettyPrinter, it also handles rules that appear as arguments of predicate or function symbols correctly. Such rules do not end with a dot their the arrow separating their head from their body is never ommited and they can be enclosed in parenthesis to prevent ambiguity. For example, an atom with the predicate p/2, the rule a <- b. as the first argument and the term f(X) as the second argument will be written as p((a <- b), f(X)).

Version:
1.0.0
Author:
Martin Slota

Nested Class Summary
private static class EvolpPrettyPrinter.MutableInteger
          A wrapper class for a mutable integer.
 
Field Summary
protected  Stack<EvolpPrettyPrinter.MutableInteger> innerTermsLeft
          If trackPosition is true, this stack holds the numbers of remaining inner terms of function and predicate symbols.
protected  boolean trackPosition
          Is set to true when a rule or a compound term is being appended.
 
Fields inherited from class lp.struct.util.LpPrettyPrinter
ARROW_STRING, SPACE_STRING
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
EvolpPrettyPrinter(W out)
          Creates a new instance of EvolpPrettyPrinter.
EvolpPrettyPrinter(W out, String arrow, boolean withSpaces)
          Creates a new instance of EvolpPrettyPrinter.
 
Method Summary
protected  void appendInnerRule(LpRule rule)
          Appends an inner rule, i.e. it is appended without the trailing dot, the separating arrow is never ommited and it is enclosed in parenthesis if more terms follow after the rule and the body of the rule is non-empty.
protected  void appendOuterRule(LpRule rule)
          Takes care of writing an ordinary rule, just like LpPrettyPrinter does.
static LpBuffer getBuffer()
          A convenience method that creates a new LpBuffer that uses a new EvolpPrettyPrinter instance to write LpStructureUnits.It will use the string "<-" to separate the head and body of a rule.
static LpBuffer getBuffer(String arrow, boolean withSpaces)
          A convenience method that creates a new LpBuffer that uses a new EvolpPrettyPrinter instance to write LpStructureUnits.
protected  void registerTerm()
          If trackPosition is true, 1 one is subtracted from the topmost integer in innerTermsLeft.
 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(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 this LpPrinter.
 void visit(LpVariable var)
          Appends a natural textual form of a LpVariable instance to the underlying Writer.
 void write(LpStructureUnit unit)
          Writes a textual representation of unit to this LpPrinter.
 
Methods inherited from class lp.struct.util.LpPrettyPrinter
appendRuleNoDot, arrow, beginParen, comma, dot, endParen, literalPrefix, space, visit, visit, visitArgumentList
 
Methods inherited from class lp.struct.util.LpPrinter
append, append, append, append, close, flush, getOut, setOut, 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

trackPosition

protected boolean trackPosition
Is set to true when a rule or a compound term is being appended. In such a case the class remembers a "position" inside the structure so that it can treat inner and outer rules differently and put the inner rules in parenthesis where appropriate.


innerTermsLeft

protected final Stack<EvolpPrettyPrinter.MutableInteger> innerTermsLeft
If trackPosition is true, this stack holds the numbers of remaining inner terms of function and predicate symbols. The first number relates to the inner-most symbol, the last number relates to the outer-most symbol.

Constructor Detail

EvolpPrettyPrinter

public EvolpPrettyPrinter(W out)
Creates a new instance of EvolpPrettyPrinter. 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

EvolpPrettyPrinter

public EvolpPrettyPrinter(W out,
                          String arrow,
                          boolean withSpaces)
Creates a new instance of EvolpPrettyPrinter. 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 EvolpPrettyPrinter 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 EvolpPrettyPrinter 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

write

public void write(LpStructureUnit unit)
Writes a textual representation of unit to this LpPrinter. The string is constructed in the #visit() methods.

Overrides:
write in class LpPrinter<W extends Writer>
Parameters:
unit - the LpStructureUnit that should be represented as a string and appended to the underlying Writer
Throws:
IllegalArgumentException - if unit is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

registerTerm

protected void registerTerm()
If trackPosition is true, 1 one is subtracted from the topmost integer in innerTermsLeft. Is called when a term is appended to keep track of how many inner terms are left.


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()).

Specified by:
visit in interface LpStructureUnitVisitor
Overrides:
visit in class LpPrettyPrinter<W extends Writer>
Parameters:
con - the LpConstant instance whose natural textual representation should be appended 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()).

Specified by:
visit in interface LpStructureUnitVisitor
Overrides:
visit in class LpPrettyPrinter<W extends Writer>
Parameters:
var - the LpVariable instance whose natural textual representation should be appended 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()).

Specified by:
visit in interface LpStructureUnitVisitor
Overrides:
visit in class LpPrettyPrinter<W extends Writer>
Parameters:
fun - the LpFunction instance whose natural textual representation should be appended to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpFunction)

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.

Specified by:
visit in interface LpStructureUnitVisitor
Overrides:
visit in class LpPrettyPrinter<W extends Writer>
Parameters:
term - the LpCompoundTerm instance whose natural textual representation should be appended 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()).

Specified by:
visit in interface LpStructureUnitVisitor
Overrides:
visit in class LpPrettyPrinter<W extends Writer>
Parameters:
pred - the LpPredicate instance whose natural textual representation should be appended to the underlying Writer
See Also:
LpPrinter, LpStructureUnitVisitor.visit(LpPredicate)

appendOuterRule

protected void appendOuterRule(LpRule rule)
Takes care of writing an ordinary rule, just like LpPrettyPrinter does.

Parameters:
rule - the rule to write
Throws:
NullPointerException - if rule is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

appendInnerRule

protected void appendInnerRule(LpRule rule)
Appends an inner rule, i.e. it is appended without the trailing dot, the separating arrow is never ommited and it is enclosed in parenthesis if more terms follow after the rule and the body of the rule is non-empty.

Parameters:
rule - the rule to write
Throws:
NullPointerException - if rule is null
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

visit

public void visit(LpRule rule)
Appends a natural textual form of a LpRule instance to this LpPrinter. 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. 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. In case the rule is an outer rule (i.e. not inside a function or predicate symbol), it is ended with a dot. If it is an inner rule, the separator string is never omitted and the rule will be put into parenthesis if more terms follow in the argument list of the closest function or predicate symbol (without the parenthesis an ambiguity would arise).

Specified by:
visit in interface LpStructureUnitVisitor
Overrides:
visit in class LpPrettyPrinter<W extends Writer>
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)