|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.Writer
lp.struct.util.LpPrinter<W>
lp.struct.util.LpEncoder<W>
public class LpEncoder<W extends Writer>
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
LpParser
.
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 LpStructureUnit s. |
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 |
---|
public LpEncoder(W out)
LpEncoder
.
out
- the underlying writer into which everything will be writtenMethod Detail |
---|
public static LpBuffer getBuffer()
LpBuffer
that uses a new
LpEncoder
instance to write LpStructureUnit
s.
LpBuffer
protected void appendEncoded(CharSequence seq)
seq
but escapes the characters '_', 'L', 'R', 'C', 'A',
'D' by writing "__", "_l", "_r", "_c", "_a", "_d" instead of them.
seq
- the character sequence to be appended
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
protected boolean escapeChar(char c)
true
iff c
is one of '_', 'L', 'R', 'C', 'A',
'D'.
c
- examined character (should we escape it or not?)
public void visit(LpConstant con)
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).
con
- the LpConstant
instance whose natural textual
representation should be appended to the underlying Writer
NullPointerException
- if con
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpConstant)
public void visit(LpVariable var)
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).
var
- the LpVariable
instance whose natural textual
representation should be appended to the underlying Writer
NullPointerException
- if con
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpVariable)
public void visit(LpFunction fun)
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).
fun
- the LpFunction
instance whose natural textual
representation should be appended to the underlying Writer
NullPointerException
- if con
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpFunction)
protected void visitArgumentList(List<LpTerm> args)
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'.
args
- the argument list that will be appended to the
underlying Writer
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
public void visit(LpCompoundTerm term)
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'.
term
- the LpCompoundTerm
instance whose encoded
representation should be appended to the underlying Writer
NullPointerException
- if term
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpCompoundTerm)
public void visit(LpPredicate pred)
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).
pred
- the LpPredicate
instance whose natural textual
representation should be appended to the underlying Writer
NullPointerException
- if con
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpPredicate)
public void visit(LpAtom atom)
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'.
atom
- the LpAtom
instance whose encoded
representation should be appended to the underlying Writer
NullPointerException
- if term
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpCompoundTerm)
public void visit(LpLiteral lit)
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
.
lit
- the LpLiteral
instance whose encoded representation
should be appended to the underlying Writer
NullPointerException
- if lit
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpLiteral)
public void visit(LpRule rule)
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).
rule
- the LpRule
instance whose encoded representation
should be appended to the underlying Writer
NullPointerException
- if rule
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpRule)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |