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

java.lang.Object
  extended by java.io.Writer
      extended by lp.struct.util.LpPrinter<W>
          extended by lp.struct.util.LpEncoder<W>
All Implemented Interfaces:
Closeable, Flushable, Appendable, LpStructureUnitVisitor

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

Transforms a logic program (or its components} represented by objects implementing the LpStructureUnit interface into an encoded textual form that only contains the characters [a-zA-Z_] and can be decoded back to the original components. This is achieved by encoding all parenthesis, commas, dots, arrows separating heads of rules from their bodies and the "not" representing default negation in a special way. In particular, a left parenthesis is written as 'L', a right parenthesis as 'R', a comma as 'C', a dot as 'D' and the arrow as 'A'. Default negation is represented by the prefix "_N" instead of "not ". Letters '_', 'L', 'R', 'A', 'C' and 'D' are escaped with an extra '_' and made lowercase (just because I thought it looks better that way :o). The decoder associated to this encoder should replace each occurence of

After that the input should be parseable by LpParser.

Version:
1.0.0
Author:
Martin Slota
See Also:
LpPrinter

Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
LpEncoder(W out)
          Creates a new instance of LpEncoder.
 
Method Summary
protected  void appendEncoded(CharSequence seq)
          Appends seq but escapes the characters '_', 'L', 'R', 'C', 'A', 'D' by writing "__", "_l", "_r", "_c", "_a", "_d" instead of them.
protected  boolean escapeChar(char c)
          Returns true iff c is one of '_', 'L', 'R', 'C', 'A', 'D'.
static LpBuffer getBuffer()
          A convenience method that creates a new LpBuffer that uses a new LpEncoder instance to write LpStructureUnits.
 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 an encoded form of a LpConstant instance to this LpEncoder.
 void visit(LpFunction fun)
          Appends an encoded form of a LpFunction instance to this LpEncoder.
 void visit(LpLiteral lit)
          Appends an encoded form of a LpLiteral instance to the underlying Writer.
 void visit(LpPredicate pred)
          Appends an encoded form of a LpPredicate instance to this LpEncoder.
 void visit(LpRule rule)
          Appends an encoded form of a LpRule instance to the underlying Writer.
 void visit(LpVariable var)
          Appends an encoded form of a LpVariable instance to this LpEncoder.
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
 

Constructor Detail

LpEncoder

public LpEncoder(W out)
Creates a new instance of LpEncoder.

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

getBuffer

public static LpBuffer getBuffer()
A convenience method that creates a new LpBuffer that uses a new LpEncoder instance to write LpStructureUnits.

Returns:
as specified above
See Also:
LpBuffer

appendEncoded

protected void appendEncoded(CharSequence seq)
Appends seq but escapes the characters '_', 'L', 'R', 'C', 'A', 'D' by writing "__", "_l", "_r", "_c", "_a", "_d" instead of them.

Parameters:
seq - the character sequence to be appended
Throws:
IOException - (wrapped in an ExceptionAdapter) in case an I/O exception occurs while writing to the underlying Writer

escapeChar

protected boolean escapeChar(char c)
Returns true iff c is one of '_', 'L', 'R', 'C', 'A', 'D'.

Parameters:
c - examined character (should we escape it or not?)
Returns:
as specified above

visit

public void visit(LpConstant con)
Appends an encoded form of a LpConstant instance to this LpEncoder. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The encoded form of a constant is its name (see LpConstant.getName()) in which the characters '_', 'L', 'R', 'C', 'A' and 'D' are escaped (an extra '_' is prepended and they are converted to lowercase).

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 an encoded form of a LpVariable instance to this LpEncoder. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The encoded form of a variable is its name (see LpVariable.getName()) in which the characters '_', 'L', 'R', 'C', 'A' and 'D' are escaped (an extra '_' is prepended and they are converted to lowercase).

Parameters:
var - the LpVariable 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(LpVariable)

visit

public void visit(LpFunction fun)
Appends an encoded form of a LpFunction instance to this LpEncoder. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The encoded form of a function symbol is its name (see LpFunction.getName()) in which the characters '_', 'L', 'R', 'C', 'A' and 'D' are escaped (an extra '_' is prepended and they are converted to lowercase).

Parameters:
fun - the LpFunction 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(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 an encoded form of a list of terms. It is composed of a 'L' by encoded representations of the terms in the list, separated from each other by a 'C'. It ends with an 'R'.

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 encoded form of a compound term is its function's encoded form followed by an 'L' and the encoded textual representations of its arguments, separated from each other by a 'C'. It ends with an 'R'.

Parameters:
term - the LpCompoundTerm instance whose encoded 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 an encoded form of a LpPredicate instance to this LpEncoder. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The encoded form of a predicate symbol is its name (see LpPredicate.getName()) in which the characters '_', 'L', 'R', 'C', 'A' and 'D' are escaped (an extra '_' is prepended and they are converted to lowercase).

Parameters:
pred - the LpPredicate 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(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 encoded form of an atom is its predicate's encoded form followed by an 'L' and the encoded textual representations of its arguments, separated from each other by a 'C'. It ends with an 'R'.

Parameters:
atom - the LpAtom instance whose encoded 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(LpLiteral lit)
Appends an encoded form of a LpLiteral instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The encoded form of a literal is the encoded form of its underlying atom with "_N" in front of it if LpLiteral.isPositive() is false.

Parameters:
lit - the LpLiteral instance whose encoded 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)

visit

public void visit(LpRule rule)
Appends an encoded form of a LpRule instance to the underlying Writer. Shouldn't be called directly, LpPrinter.append(LpStructureUnit) should be used instead. The encoded form of a rule consists of the encoded form of its head followed by 'A' and natural forms of literals in its body, separated from each other by a 'C'. It ends with a 'D'. If LpRule.getHead() is null, the encoded form begins with 'A' (the head's encoded form is left out).

Parameters:
rule - the LpRule instance whose encoded 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)