Class SentenceParser
- java.lang.Object
-
- org.projog.core.parser.SentenceParser
-
public class SentenceParser extends java.lang.ObjectParses Prolog syntax representing rules including operators.Note: not thread safe.
- See Also:
Operands
-
-
Field Summary
Fields Modifier and Type Field Description private TermIdentitySetbracketedTermsTerms created, during the parsing of the current sentence, that were enclosed in brackets.private static java.lang.StringMINUS_SIGNprivate Operandsoperandsprivate TermIdentitySetparsedInfixTermsTerms created, during the parsing of the current sentence, that were represented using infix notation.private TokenParserparserprivate java.util.HashMap<java.lang.String,Variable>variablesA collection ofVariables this parser currently knows about (key = the variable name).
-
Constructor Summary
Constructors Modifier Constructor Description privateSentenceParser(java.io.Reader reader, Operands operands)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private TermaddPostfixOperand(java.lang.String postfixOperand, Term original)Add a term, representing a post-fix operand, in the appropriate point of a composite term.private TermcreatePrefixTerm(java.lang.String prefixOperandName, Term argument)Returns a newTermrepresenting the specified prefix operand and argument.private TermgetAtomOrStructure(java.lang.String name)Returns either anAtomorStructurewith the specified name.private TermgetCommaSeparatedArgument()Parses and returns the next argument of a list or structure.private TermgetDiscreteTerm()private intgetInfixLevel(Term t)static SentenceParsergetInstance(java.io.Reader reader, Operands operands)Returns a newSentenceParserthat will parse Prolog syntax read from the specifiedReaderusing the specifiedOperands.static SentenceParsergetInstance(java.lang.String prologSyntax, Operands operands)Returns a newSentenceParserwill parse the specifiedStringusing the specifiedOperands.private VariablegetNamedVariable(java.lang.String id)private TermgetNegativeNumber()java.util.Map<java.lang.String,Variable>getParsedTermVariables()Returns collection ofVariableinstances created by thisSentenceParser.private TermgetPossiblePrefixArgument(int currentLevel)Parses and returns aTerm.private intgetPostfixLevel(Term t)private intgetPrefixLevel(Term t)private TermgetTerm(int maxLevel)Creates aTermfrom Prolog syntax read from this object'sCharacterParser.private TermgetTerm(Term currentTerm, int currentLevel, int maxLevel, boolean isFirst)Recursively called to combine individual terms into a composite term.private TermgetTermInBrackets()private VariablegetVariable(java.lang.String id)Returns a variable with the specified id.private booleanisFollowedByNumber()private booleanisParsedInfixTerms(Term t)Has the specified term already been parsed, and included as an argument in an infix operand, as part of parsing the current sentence?private ParserExceptionnewParserException(java.lang.String message)Returns a newParserExceptionwith the specified message.private TermparseList()Returns a newly createdListwith elements read from the parser.TermparseSentence()TermparseTerm()Creates aTermfrom Prolog syntax read from this object'sCharacterParser.private TokenpeekValue()private TokenpopValue()private voidresetState()private Term[]toArray(java.util.ArrayList<Term> al)private DecimalFractiontoDecimalFraction(java.lang.String value)private IntegerNumbertoIntegerNumber(java.lang.String value)
-
-
-
Field Detail
-
MINUS_SIGN
private static final java.lang.String MINUS_SIGN
- See Also:
- Constant Field Values
-
parser
private final TokenParser parser
-
operands
private final Operands operands
-
variables
private final java.util.HashMap<java.lang.String,Variable> variables
A collection ofVariables this parser currently knows about (key = the variable name).The reason this information needs to be stored is so that each instance of the same variable name, in a single sentence, refers to the same
Variableinstance.e.g. During the parsing of the sentence
Y is 2, X is Y * 2.twoVariableinstances need to be created - one for the variable nameXand one that is shared between both references to the variable nameY.
-
parsedInfixTerms
private final TermIdentitySet parsedInfixTerms
Terms created, during the parsing of the current sentence, that were represented using infix notation.Example of infix notation:
X = 1where the predicate name=is positioned between its two argumentsXand1.The reason these need to be kept a record of is, when the sentence has been fully read, the individual terms can be reordered to conform to operator precedence.
e.g.
1+2/3will get ordered like:+(1, /(2, 3))while1/2+3will be ordered like:+(/(1, 2), 3).
-
bracketedTerms
private final TermIdentitySet bracketedTerms
Terms created, during the parsing of the current sentence, that were enclosed in brackets.The reason this information needs to be stored is so that the parser knows to not reorder these terms as part of the reordering of infix terms that occurs once the sentence is fully read. i.e. Using brackets to explicitly define the ordering of terms overrules the default operator precedence of infix terms.
e.g. Although
1/2+3will be ordered like:+(/(1, 2), 3),1/(2+3)(i.e. where2+3is enclosed in brackets) will be ordered like:/(1, +(2, 3)).
-
-
Constructor Detail
-
SentenceParser
private SentenceParser(java.io.Reader reader, Operands operands)
-
-
Method Detail
-
getInstance
public static SentenceParser getInstance(java.lang.String prologSyntax, Operands operands)
Returns a newSentenceParserwill parse the specifiedStringusing the specifiedOperands.- Parameters:
prologSyntax- the prolog syntax to be parsedoperands- details of the operands to use during parsing- Returns:
- a new
SentenceParser
-
getInstance
public static SentenceParser getInstance(java.io.Reader reader, Operands operands)
Returns a newSentenceParserthat will parse Prolog syntax read from the specifiedReaderusing the specifiedOperands.- Parameters:
reader- the source of the prolog syntax to be parsedoperands- details of the operands to use during parsing- Returns:
- a new
SentenceParser
-
parseSentence
public Term parseSentence()
- Returns:
- a
Termcreated from Prolog syntax read from this object'sCharacterParserornullif the end of the underlying stream being parsed has been reached - Throws:
ParserException- if an error parsing the Prolog syntax occurs
-
parseTerm
public Term parseTerm()
Creates aTermfrom Prolog syntax read from this object'sCharacterParser.- Returns:
- a
Termcreated from Prolog syntax read from this object'sCharacterParserornullif the end of the underlying stream being parsed has been reached - Throws:
ParserException- if an error parsing the Prolog syntax occurs- See Also:
parseSentence()
-
resetState
private void resetState()
-
getParsedTermVariables
public java.util.Map<java.lang.String,Variable> getParsedTermVariables()
Returns collection ofVariableinstances created by thisSentenceParser.Returns all
Variables created by thisSentenceParsereither since it was created or since the last execution ofparseTerm().- Returns:
- collection of
Variableinstances created by thisSentenceParser
-
getTerm
private Term getTerm(int maxLevel)
Creates aTermfrom Prolog syntax read from this object'sCharacterParser.
-
getTerm
private Term getTerm(Term currentTerm, int currentLevel, int maxLevel, boolean isFirst)
Recursively called to combine individual terms into a composite term.While the parsing of the individual terms is performed, priority of the operands they represent needs to be considered to make sure the terms are ordered correctly (due to different operand precedence it is not always the case that the terms will be ordered in the resulting composite term in the same order they were parsed from the input stream).
- Parameters:
currentTerm- represents the current state of the process to parse a complete termcurrentLevel- the current priority/precedence/level of terms being parsed - if an operand represented by a term retrieved by this method has a higher priority then reordering needs to take place to position the term in the right position in relation to the other terms that exist within thecurrentTermstructure (in order to maintain the correct priority)maxLevel- the maximum priority/precedence/level of operands to parse - if an operand represented by the next term retrieved by this method has a higher priority then it is ignored for now (currentTermis returned "as-is"}.
-
getPossiblePrefixArgument
private Term getPossiblePrefixArgument(int currentLevel)
Parses and returns aTerm.If the parsed
Termrepresents a prefix operand, then the subsequent term is also parsed so it can be used as an argument in the returned structure.- Parameters:
currentLevel- the current priority level of terms being parsed (if the parsed term represents a prefix operand, then the operand cannot have a higher priority thancurrentLevel(aParserExceptionwill be thrown if does).
-
getNegativeNumber
private Term getNegativeNumber()
-
createPrefixTerm
private Term createPrefixTerm(java.lang.String prefixOperandName, Term argument)
Returns a newTermrepresenting the specified prefix operand and argument.
-
addPostfixOperand
private Term addPostfixOperand(java.lang.String postfixOperand, Term original)
Add a term, representing a post-fix operand, in the appropriate point of a composite term.The correct position of the post-fix operand within the composite term (and so what the post-fix operands actual argument will be) is determined by operand priority.
- Parameters:
original- a composite term representing the current state of parsing the current sentencepostfixOperand- a term which represents a post-fix operand
-
getDiscreteTerm
private Term getDiscreteTerm()
-
toIntegerNumber
private IntegerNumber toIntegerNumber(java.lang.String value)
-
toDecimalFraction
private DecimalFraction toDecimalFraction(java.lang.String value)
-
getAtomOrStructure
private Term getAtomOrStructure(java.lang.String name)
Returns either anAtomorStructurewith the specified name.If the next character read from the parser is a
(then a newly createdStructureis returned else a newly createdAtomis returned.
-
getVariable
private Variable getVariable(java.lang.String id)
Returns a variable with the specified id.If this object already has an instance of
Variablewith the specified id then it will be returned else a newVariablewill be created. The only exception to this behaviour is when the id equalsVariable.ANONYMOUS_VARIABLE_ID- in which case a newVariablewill be always be returned.
-
getNamedVariable
private Variable getNamedVariable(java.lang.String id)
-
parseList
private Term parseList()
Returns a newly createdListwith elements read from the parser.
-
getCommaSeparatedArgument
private Term getCommaSeparatedArgument()
Parses and returns the next argument of a list or structure.As a comma would indicate a delimiter in a sequence of arguments, we only want to continue parsing up to the point of any comma. i.e. Any parsed comma should not be considered as part of the argument currently being parsed.
-
getTermInBrackets
private Term getTermInBrackets()
-
popValue
private Token popValue()
-
peekValue
private Token peekValue()
-
isFollowedByNumber
private boolean isFollowedByNumber()
-
isParsedInfixTerms
private boolean isParsedInfixTerms(Term t)
Has the specified term already been parsed, and included as an argument in an infix operand, as part of parsing the current sentence?
-
getPrefixLevel
private int getPrefixLevel(Term t)
-
getInfixLevel
private int getInfixLevel(Term t)
-
getPostfixLevel
private int getPostfixLevel(Term t)
-
newParserException
private ParserException newParserException(java.lang.String message)
Returns a newParserExceptionwith the specified message.
-
-