ai.krr.propositions
Class KifSentenceAdaptor

java.lang.Object
  extended by ai.krr.propositions.KifSentenceAdaptor
All Implemented Interfaces:
inf.compilers.SyntaxAdaptor<Sentence>

public class KifSentenceAdaptor
extends java.lang.Object
implements inf.compilers.SyntaxAdaptor<Sentence>

This class is an adaptor between the ai.krr.propositions.Sentence class that can be used as an internal representation for reasoning and its external form as defined by the KIF syntax as described below.

Writing

Atoms in propositional KIF can be either the boolean constants TRUE and FALSE, or they can be a named proposition represented by a NamedSymbol. The boolean constants are written as TRUE and FALSE. Named propositions that are represented by internal Symbols cannot be handled here and an attempt to write them will cause an Exception. NamedSymbols that have an associated NameSpace are written with their NameSpace. The full NameSpace URI will prefix the name of the proposition.

Standard KIF is defined to be case-insensitive. However, NamedSymbols are case-sensitive. This fundamental incompatibility is handled here by the property "case-sensitive" which should have the value "true" or "false". Only if the property is set to "true" are lower case letters in NamedSymbols not explicitly escaped. This may increase readablility and result in shorter String representations. The default behaviour for this SyntaxAdaptor is to be case-insensitive, which is standard for KIF.

The syntax of Atoms can be defined as follows:

 <Atom>        ::= TRUE | FALSE | <NamedSymbol>
 <NamedSymbol> ::= <NameSpace-URI>\#<Name>
 

Literals must contain a named proposition, i.e. they cannot be the boolean constants. If the proposition is an internal Symbol, this will cause an Exception. Symbols with NameSpace are written with the full NameSpace URI. Positive Literals are written like an Atom whereas negative Literals are written like a NegatedSentece. The formal syntax is as follows:

 <Literal> ::= <NamedSymbol> | (not <NamedSymbol>)
 

Atoms and Literals are the basic building blocks for Sentences. The remaining constructs are more complex in the sense that they allow for Sentences as their content, which means definitions become recursive. The simplest of these more complex structures is a NegatedSentence which contains exactly one Sentence, which is what is negated here. The syntax is defined as:

 <NegatedSentence> ::= (not <Sentence>)
 

A ConnectedSentence is a List containing the String representing the Connective followed by the connected Sentences (at least two):

 <ConnectedSentence> ::= (<Connective> <Sentence> <Sentence><sup>+</sup>)
 <Connective> ::= and | or | => | <= | <=>
 

Finally, a Sentence can be any of the basic or complex structures defined above:

 <Sentence> ::= <Atom> | <Literal> | <NegatedSentence> | <ConnectedSentence>
 

Reading

Standard KIF is defined to be case-insensitive. However, NamedSymbols are case-sensitive. This fundamental incompatibility is handled here by the property "case-sensitive" which should have the value "true" or "false". Only if the property is set to "true" are word lexemes not converted to their upper case equivalents as prescribed in KIF. The default behaviour for this SyntaxAdaptor is to be case-insensitive, which is standard for KIF.

By default, it is not allowed to use NamedSymbols that are internal, i.e. that do not have a NameSpace. This can be changed by setting the property ~"namespace-aware" to "false".

property possible values default
case-sensitive true, false true
namespace-aware true, false true
indent-string any string " "
line-width positive int 80

Author:
Gerhard Wickler

Field Summary
protected static inf.compilers.LexicalAnalyzer.TokenType andTT
          the token types used
protected static inf.compilers.LexicalAnalyzer.TokenType closebracketTT
          the token types used
protected static inf.compilers.LexicalAnalyzer.TokenType coimplTT
          the token types used
protected static inf.compilers.LexicalAnalyzer.TokenType ignorableTT
          the token types used
protected static inf.compilers.LexicalAnalyzer.TokenType limplTT
          the token types used
protected static inf.compilers.LexicalAnalyzer.TokenType notTT
          the token types used
protected static inf.compilers.LexicalAnalyzer.TokenType openbracketTT
          the token types used
protected static inf.compilers.LexicalAnalyzer.TokenType orTT
          the token types used
protected  java.util.Properties props
          the properties used for reading and writing
protected static inf.compilers.LexicalAnalyzer.TokenType rimplTT
          the token types used
protected  inf.compilers.LexicalAnalyzer scanner
          the lexical analyzer used for tokenizing the input
 
Constructor Summary
KifSentenceAdaptor()
           This constructor creates a SyntaxAdaptor that can be used to read statements in a (propositional) KIF syntax and create a propositional Sentence for it, or it can be used to write out a propositional Sentence in KIF syntax.
 
Method Summary
protected  KifSymbolAdaptor clone()
           
 java.lang.Class<Sentence> getInternalClass()
           This function returns the Class that holds the internal representation this adaptor translates to and from.
 java.lang.String getProperty(java.lang.String key)
           This function gets the property that is associated with the given key.
 java.lang.String getSyntaxName()
           This function returns the name that should identify the syntactical language that this adaptor translates to and from.
static Sentence parseSentence(inf.compilers.LexicalAnalyzer scanner, java.util.Properties props)
           This function attempts to parse a propositional Sentence in KIF syntax from the underlying LexicalAnalyzer.
static java.util.List<Sentence> parseSentences(inf.compilers.LexicalAnalyzer scanner, java.util.Properties props)
           This function attempts to parse a sequence of at least two Sentences from the given LexicalAnalyzer.
 void prettyPrint(int indent, Sentence stmt, java.io.Writer w)
           This function takes a logical Sentence which is a Java Object in the internal representation, and writes it to the given Writer as a string conforming to the KIF syntax.
static void prettyPrintConnectedSentence(int indent, ConnectedSentence stmt, java.io.Writer w, java.util.Properties props)
           This function pretty-prints the given ConnectedSentence to the given Writer.
static void prettyPrintNegatedSentence(int indent, NegatedSentence stmt, java.io.Writer w, java.util.Properties props)
           This function pretty-prints the given NegatedSentence to the given Writer.
static void prettyPrintSentence(int indent, Sentence stmt, java.io.Writer w, java.util.Properties props)
           This function takes a logical Sentence which is a Java Object in the internal representation, and wrties it to the given Writer as a string conforming to the KIF syntax.
 Sentence read(java.io.Reader r)
           This function attempts to parse characters from the given Reader until a Sentence that represents an object in the internal representation has been parsed.
 void setProperty(java.lang.String key, java.lang.String value)
           This function sets the property associated with the given key to the given value.
 void write(Sentence stmt, java.io.Writer w)
           This function takes a logical Sentence which is a Java Object in the internal representation, and writes it to the given Writer as a string conforming to the KIF syntax.
static void writeAtom(Atom atom, java.io.Writer w, java.util.Properties props)
           This function writes the given Atom to the given Writer.
static void writeConnectedSentence(ConnectedSentence stmt, java.io.Writer w, java.util.Properties props)
           This function writes the given ConnectedSentence to the given Writer.
static void writeLiteral(Literal lit, java.io.Writer w, java.util.Properties props)
           This function writes the given Literal to the given Writer.
static void writeNegatedSentence(NegatedSentence stmt, java.io.Writer w, java.util.Properties props)
           This function writes the given NegatedSentence to the given Writer.
static void writeSentence(Sentence stmt, java.io.Writer w, java.util.Properties props)
           This function takes a logical Sentence which is a Java Object in the internal representation, and writes it to the given Writer as a string conforming to the KIF syntax.
static void writeTruthValue(TruthValue tValue, java.io.Writer w, java.util.Properties props)
           This function writes the given TruthValue to the given Writer.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ignorableTT

protected static inf.compilers.LexicalAnalyzer.TokenType ignorableTT
the token types used


openbracketTT

protected static inf.compilers.LexicalAnalyzer.TokenType openbracketTT
the token types used


closebracketTT

protected static inf.compilers.LexicalAnalyzer.TokenType closebracketTT
the token types used


notTT

protected static inf.compilers.LexicalAnalyzer.TokenType notTT
the token types used


andTT

protected static inf.compilers.LexicalAnalyzer.TokenType andTT
the token types used


orTT

protected static inf.compilers.LexicalAnalyzer.TokenType orTT
the token types used


rimplTT

protected static inf.compilers.LexicalAnalyzer.TokenType rimplTT
the token types used


limplTT

protected static inf.compilers.LexicalAnalyzer.TokenType limplTT
the token types used


coimplTT

protected static inf.compilers.LexicalAnalyzer.TokenType coimplTT
the token types used


scanner

protected inf.compilers.LexicalAnalyzer scanner
the lexical analyzer used for tokenizing the input


props

protected java.util.Properties props
the properties used for reading and writing

Constructor Detail

KifSentenceAdaptor

public KifSentenceAdaptor()

This constructor creates a SyntaxAdaptor that can be used to read statements in a (propositional) KIF syntax and create a propositional Sentence for it, or it can be used to write out a propositional Sentence in KIF syntax.

Method Detail

clone

protected KifSymbolAdaptor clone()
                          throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

getInternalClass

public java.lang.Class<Sentence> getInternalClass()

This function returns the Class that holds the internal representation this adaptor translates to and from.

Specified by:
getInternalClass in interface inf.compilers.SyntaxAdaptor<Sentence>
Returns:
the internal representation Class adapted to

getSyntaxName

public java.lang.String getSyntaxName()

This function returns the name that should identify the syntactical language that this adaptor translates to and from.

Specified by:
getSyntaxName in interface inf.compilers.SyntaxAdaptor<Sentence>
Returns:
the name of the language adapted to

write

public void write(Sentence stmt,
                  java.io.Writer w)
           throws inf.compilers.ExpressivenessException,
                  java.io.IOException

This function takes a logical Sentence which is a Java Object in the internal representation, and writes it to the given Writer as a string conforming to the KIF syntax.

Specified by:
write in interface inf.compilers.SyntaxAdaptor<Sentence>
Parameters:
stmt - a Sentence in the internal representation
w - the Writer to which the syntactical translation is written
Throws:
inf.compilers.ExpressivenessException - if the syntactical language cannot represent the given statement
java.io.IOException - if writing to the given Writer fails

prettyPrint

public void prettyPrint(int indent,
                        Sentence stmt,
                        java.io.Writer w)
                 throws inf.compilers.ExpressivenessException,
                        java.io.IOException

This function takes a logical Sentence which is a Java Object in the internal representation, and writes it to the given Writer as a string conforming to the KIF syntax.

Specified by:
prettyPrint in interface inf.compilers.SyntaxAdaptor<Sentence>
Parameters:
indent - the amount of indentation for the first line
stmt - a Sentence in the internal representation
w - the Writer to which the syntactical translation is written
Throws:
inf.compilers.ExpressivenessException - if the syntactical language cannot represent the given statement
java.io.IOException - if writing to the given Writer fails

read

public Sentence read(java.io.Reader r)
              throws inf.compilers.ExpressivenessException,
                     java.text.ParseException,
                     java.io.IOException

This function attempts to parse characters from the given Reader until a Sentence that represents an object in the internal representation has been parsed.

Specified by:
read in interface inf.compilers.SyntaxAdaptor<Sentence>
Parameters:
r - the Reader from which the representation is to be parsed
Returns:
the target internal representation
Throws:
inf.compilers.ExpressivenessException - if the internal representation class is not expressive enough for the given statement
java.text.ParseException - if there is a syntax error in the given string
java.io.IOException - if reading from the Reader fails

getProperty

public java.lang.String getProperty(java.lang.String key)

This function gets the property that is associated with the given key. Not e that the key should not be null.

Specified by:
getProperty in interface inf.compilers.SyntaxAdaptor<Sentence>
Returns:
the property for the given key (or null if undefined)

setProperty

public void setProperty(java.lang.String key,
                        java.lang.String value)

This function sets the property associated with the given key to the given value. The given key must not be null, but the value may be.

Specified by:
setProperty in interface inf.compilers.SyntaxAdaptor<Sentence>
Parameters:
key - the key with which the value is associated
value - the associated value

writeSentence

public static void writeSentence(Sentence stmt,
                                 java.io.Writer w,
                                 java.util.Properties props)
                          throws inf.compilers.ExpressivenessException,
                                 java.io.IOException

This function takes a logical Sentence which is a Java Object in the internal representation, and writes it to the given Writer as a string conforming to the KIF syntax.

Parameters:
stmt - a Sentence in the internal representation
w - the Writer to which the syntactical translation is written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if the syntactical language cannot represent the given statement
java.io.IOException - if writing to the given Writer fails

prettyPrintSentence

public static void prettyPrintSentence(int indent,
                                       Sentence stmt,
                                       java.io.Writer w,
                                       java.util.Properties props)
                                throws inf.compilers.ExpressivenessException,
                                       java.io.IOException

This function takes a logical Sentence which is a Java Object in the internal representation, and wrties it to the given Writer as a string conforming to the KIF syntax.

Parameters:
indent - the amount of indentation for the first line
stmt - a Sentence in the internal representation
w - the Writer to which the syntactical translation is written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if the syntactical language cannot represent the given statement
java.io.IOException - if writng to the given Writer fails

writeConnectedSentence

public static void writeConnectedSentence(ConnectedSentence stmt,
                                          java.io.Writer w,
                                          java.util.Properties props)
                                   throws inf.compilers.ExpressivenessException,
                                          java.io.IOException

This function writes the given ConnectedSentence to the given Writer. A ConnectedSentence is a List containing the String representing the Connective followed by the connected Sentences (at least two).

 <ConnectedSentence> ::= (<Connective> <Sentence> <Sentence>+)
 <Connective> ::= and | or | => | <= | <=>
 

Parameters:
stmt - the ConnectedSentence that is to be written
w - the Writer to which the ConnectedSentence is to be written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if any contained NamedSymbol is internal or the Connective is XOR (which KIF does not have)
java.io.IOException - if writing to the Writer fails

prettyPrintConnectedSentence

public static void prettyPrintConnectedSentence(int indent,
                                                ConnectedSentence stmt,
                                                java.io.Writer w,
                                                java.util.Properties props)
                                         throws inf.compilers.ExpressivenessException,
                                                java.io.IOException

This function pretty-prints the given ConnectedSentence to the given Writer.

Parameters:
indent - the amount of indentation for the first line
stmt - the ConnectedSentence that is to be written
w - the Writer to which the ConnectedSentence is to be written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if any contained NamedSymbol is internal or the Connective is XOR (which KIF does not have)
java.io.IOException - if writing to the Writer fails

writeNegatedSentence

public static void writeNegatedSentence(NegatedSentence stmt,
                                        java.io.Writer w,
                                        java.util.Properties props)
                                 throws inf.compilers.ExpressivenessException,
                                        java.io.IOException

This function writes the given NegatedSentence to the given Writer. A NegatedSentence is a List containing the String "not" followed by the contained Sentence.

 <NegatedSentence> ::= (not <Sentence>)
 

Parameters:
stmt - the NegatedSentence that is to be written
w - the Writer to which the NegatedSentence is to be written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if any contained NamedSymbol is internal
java.io.IOException - if writing to the Writer fails

prettyPrintNegatedSentence

public static void prettyPrintNegatedSentence(int indent,
                                              NegatedSentence stmt,
                                              java.io.Writer w,
                                              java.util.Properties props)
                                       throws inf.compilers.ExpressivenessException,
                                              java.io.IOException

This function pretty-prints the given NegatedSentence to the given Writer.

Parameters:
indent - the amount of indentation for the first line
stmt - the NegatedSentence that is to be written
w - the Writer to which the NegatedSentence is to be written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if any contained NamedSymbol is internal
java.io.IOException - if writing to the Writer fails

writeLiteral

public static void writeLiteral(Literal lit,
                                java.io.Writer w,
                                java.util.Properties props)
                         throws inf.compilers.ExpressivenessException,
                                java.io.IOException

This function writes the given Literal to the given Writer. If the Literal is positive it is written like an Atom, simply as the contained NamedSymbol. Otherwiese it is written like a negated Sentence.

 <Literal> ::= <NamedSymbol> | (not <NamedSymbol>)
 

Parameters:
lit - the Literal that is to be written
w - the Writer to which the Literal is to be written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if the contained NamedSymbol is internal
java.io.IOException - if writing to the Writer fails

writeAtom

public static void writeAtom(Atom atom,
                             java.io.Writer w,
                             java.util.Properties props)
                      throws inf.compilers.ExpressivenessException,
                             java.io.IOException

This function writes the given Atom to the given Writer. It is written as the NamedSymbol including its NameSpace as a full URI.

 <Atom> ::= <NamedSymbol>
 

Parameters:
atom - the Atom that is to be written
w - the Writer to which the Atom is to be written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if the contained NamedSymbol is internal
java.io.IOException - if writing to the Writer fails

writeTruthValue

public static void writeTruthValue(TruthValue tValue,
                                   java.io.Writer w,
                                   java.util.Properties props)
                            throws inf.compilers.ExpressivenessException,
                                   java.io.IOException

This function writes the given TruthValue to the given Writer. It is written as "TRUE" or "FALSE" (without any NameSpace).

 <TruthValue> ::= TRUE | FALSE
 

Parameters:
tValue - the TruthValue that is to be written
w - the Writer to which the TruthValue is to be written
props - the Properties used for writing
Throws:
java.io.IOException - if writing to the Writer fails
inf.compilers.ExpressivenessException

parseSentence

public static Sentence parseSentence(inf.compilers.LexicalAnalyzer scanner,
                                     java.util.Properties props)
                              throws inf.compilers.ExpressivenessException,
                                     java.text.ParseException,
                                     java.io.IOException

This function attempts to parse a propositional Sentence in KIF syntax from the underlying LexicalAnalyzer. Since the target representation is propositional and KIF is a much more expressive syntax, there are many reasons why an ExpressivenessException may be thrown. However, some of these may look like syntax problem to this parser and thus will be thrown as ParseExceptions.

Parameters:
scanner - the LexicalAnalyzer from which character input is read
props - the properties used for reading
Returns:
the Sentence corresponding to the input from the underlying LexicalAnalyzer
Throws:
inf.compilers.ExpressivenessException - if the input is not propositional
java.text.ParseException - if the KIF syntax is violated
java.io.IOException - if reading from the LexicalAnalyzer fails

parseSentences

public static java.util.List<Sentence> parseSentences(inf.compilers.LexicalAnalyzer scanner,
                                                      java.util.Properties props)
                                               throws inf.compilers.ExpressivenessException,
                                                      java.text.ParseException,
                                                      java.io.IOException

This function attempts to parse a sequence of at least two Sentences from the given LexicalAnalyzer.

Parameters:
scanner - the LexicalAnalyzer from which character input is read
props - the properties used for reading
Returns:
the List of parsed Sentences
Throws:
inf.compilers.ExpressivenessException - form sub-Sentences
java.text.ParseException - if there are fewer than two Sentences
java.io.IOException - if reading fails