|
|||||||||
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.LpPrettyPrinter<W>
lp.struct.util.EvolpPrettyPrinter<W>
public class EvolpPrettyPrinter<W extends Writer>
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))
.
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 LpStructureUnit s.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 LpStructureUnit s. |
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 |
---|
protected boolean trackPosition
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.
protected final Stack<EvolpPrettyPrinter.MutableInteger> innerTermsLeft
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 |
---|
public EvolpPrettyPrinter(W out)
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.
out
- the underlying writer into which everything will be writtenpublic EvolpPrettyPrinter(W out, String arrow, boolean withSpaces)
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.
out
- the underlying writer into which everything will be writtenarrow
- the string to separate the head and body of a rulewithSpaces
- determines whether extra spaces should be added to
places where they can make the output more readable
IllegalArgumentException
- if arrow
is null
Method Detail |
---|
public static LpBuffer getBuffer()
LpBuffer
that uses a new
EvolpPrettyPrinter
instance to write LpStructureUnit
s.It
will use the string "<-" to separate the head and body of a rule. The
output will also contain extra spaces to enhance readability.
LpBuffer
public static LpBuffer getBuffer(String arrow, boolean withSpaces)
LpBuffer
that uses a new
EvolpPrettyPrinter
instance to write LpStructureUnit
s. 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.
arrow
- the string to separate the head and body of a rulewithSpaces
- determines whether extra spaces should be added to
places where they can make the output more readable
LpBuffer
public void write(LpStructureUnit unit)
unit
to this
LpPrinter
. The string is constructed in the #visit()
methods.
write
in class LpPrinter<W extends Writer>
unit
- the LpStructureUnit
that should be represented
as a string and appended to the underlying Writer
IllegalArgumentException
- if unit
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
protected void registerTerm()
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.
public void visit(LpConstant con)
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()
).
visit
in interface LpStructureUnitVisitor
visit
in class LpPrettyPrinter<W extends Writer>
con
- the LpConstant
instance whose natural textual
representation should be appended to the underlying Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpConstant)
public void visit(LpVariable var)
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()
).
visit
in interface LpStructureUnitVisitor
visit
in class LpPrettyPrinter<W extends Writer>
var
- the LpVariable
instance whose natural textual
representation should be appended to the underlying Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpVariable)
public void visit(LpFunction fun)
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()
).
visit
in interface LpStructureUnitVisitor
visit
in class LpPrettyPrinter<W extends Writer>
fun
- the LpFunction
instance whose natural textual
representation should be appended to the underlying Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpFunction)
public void visit(LpCompoundTerm term)
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.
visit
in interface LpStructureUnitVisitor
visit
in class LpPrettyPrinter<W extends Writer>
term
- the LpCompoundTerm
instance whose natural textual
representation should be appended to the underlying Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpCompoundTerm)
public void visit(LpPredicate pred)
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()
).
visit
in interface LpStructureUnitVisitor
visit
in class LpPrettyPrinter<W extends Writer>
pred
- the LpPredicate
instance whose natural textual
representation should be appended to the underlying Writer
LpPrinter
,
LpStructureUnitVisitor.visit(LpPredicate)
protected void appendOuterRule(LpRule rule)
LpPrettyPrinter
does.
rule
- the rule to write
NullPointerException
- if rule
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
protected void appendInnerRule(LpRule rule)
rule
- the rule to write
NullPointerException
- if rule
is null
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while writing to the underlying
Writer
public void visit(LpRule rule)
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).
visit
in interface LpStructureUnitVisitor
visit
in class LpPrettyPrinter<W extends Writer>
rule
- the LpRule
instance whose natural textual
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 |