Class SentenceParser


  • public class SentenceParser
    extends java.lang.Object
    Parses Prolog syntax representing rules including operators.

    Note: not thread safe.

    See Also:
    Operands
    • Field Detail

      • operands

        private final Operands operands
      • variables

        private final java.util.HashMap<java.lang.String,​Variable> variables
        A collection of Variables 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 Variable instance.

        e.g. During the parsing of the sentence Y is 2, X is Y * 2. two Variable instances need to be created - one for the variable name X and one that is shared between both references to the variable name Y.

      • 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 = 1 where the predicate name = is positioned between its two arguments X and 1.

        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/3 will get ordered like: +(1, /(2, 3)) while 1/2+3 will 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+3 will be ordered like: +(/(1, 2), 3), 1/(2+3) (i.e. where 2+3 is 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 new SentenceParser will parse the specified String using the specified Operands.
        Parameters:
        prologSyntax - the prolog syntax to be parsed
        operands - 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 new SentenceParser that will parse Prolog syntax read from the specified Reader using the specified Operands.
        Parameters:
        reader - the source of the prolog syntax to be parsed
        operands - details of the operands to use during parsing
        Returns:
        a new SentenceParser
      • parseSentence

        public Term parseSentence()
        Creates a Term from Prolog syntax, terminated by a ., read from this object's CharacterParser.
        Returns:
        a Term created from Prolog syntax read from this object's CharacterParser or null if 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 a Term from Prolog syntax read from this object's CharacterParser.
        Returns:
        a Term created from Prolog syntax read from this object's CharacterParser or null if 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 of Variable instances created by this SentenceParser.

        Returns all Variables created by this SentenceParser either since it was created or since the last execution of parseTerm().

        Returns:
        collection of Variable instances created by this SentenceParser
      • getTerm

        private Term getTerm​(int maxLevel)
        Creates a Term from Prolog syntax read from this object's CharacterParser.
      • 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 term
        currentLevel - 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 the currentTerm structure (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 (currentTerm is returned "as-is"}.
      • getPossiblePrefixArgument

        private Term getPossiblePrefixArgument​(int currentLevel)
        Parses and returns a Term.

        If the parsed Term represents 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 than currentLevel (a ParserException will be thrown if does).
      • getNegativeNumber

        private Term getNegativeNumber()
      • createPrefixTerm

        private Term createPrefixTerm​(java.lang.String prefixOperandName,
                                      Term argument)
        Returns a new Term representing 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 sentence
        postfixOperand - 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 an Atom or Structure with the specified name.

        If the next character read from the parser is a ( then a newly created Structure is returned else a newly created Atom is returned.

      • getVariable

        private Variable getVariable​(java.lang.String id)
        Returns a variable with the specified id.

        If this object already has an instance of Variable with the specified id then it will be returned else a new Variable will be created. The only exception to this behaviour is when the id equals Variable.ANONYMOUS_VARIABLE_ID - in which case a new Variable will be always be returned.

      • getNamedVariable

        private Variable getNamedVariable​(java.lang.String id)
      • parseList

        private Term parseList()
        Returns a newly created List with 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)
      • toArray

        private Term[] toArray​(java.util.ArrayList<Term> al)
      • newParserException

        private ParserException newParserException​(java.lang.String message)
        Returns a new ParserException with the specified message.