Class QueryStatement

    • Constructor Summary

      Constructors 
      Constructor Description
      QueryStatement​(KnowledgeBase kb, java.lang.String prologQuery)
      Creates a new QueryStatement representing a query specified by prologQuery.
      QueryStatement​(PredicateFactory predicateFactory, Term prologQuery)
      Creates a new QueryStatement representing a query specified by prologQuery.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void executeOnce()
      Evaluate once the query represented by this statement.
      QueryResult executeQuery()
      Returns a new QueryResult for the query represented by this object.
      java.util.List<java.lang.String> findAllAsAtomName()
      Find all solutions generated by the query and return String representations of the atoms the single query variable was unified with.
      java.util.List<java.lang.Double> findAllAsDouble()
      Find all solutions generated by the query and return the double values the single query variable was unified with.
      java.util.List<java.lang.Long> findAllAsLong()
      Find all solutions generated by the query and return the long values the single query variable was unified with.
      java.util.List<Term> findAllAsTerm()
      Find all solutions generated by the query and return the Term values the single query variable was unified with.
      java.lang.String findFirstAsAtomName()
      Execute the query once and return a String representation of the atom the single query variable was unified with.
      double findFirstAsDouble()
      Execute the query once and return a double representation of the term the single query variable was unified with.
      long findFirstAsLong()
      Execute the query once and return a long representation of the term the single query variable was unified with.
      java.util.Optional<java.lang.String> findFirstAsOptionalAtomName()
      Attempt to execute the query once and return a String representation of the atom the single query variable was unified with.
      java.util.Optional<java.lang.Double> findFirstAsOptionalDouble()
      Attempt to execute the query once and return a Double representation of the term the single query variable was unified with.
      java.util.Optional<java.lang.Long> findFirstAsOptionalLong()
      Attempt to execute the query once and return a Long representation of the term the single query variable was unified with.
      java.util.Optional<Term> findFirstAsOptionalTerm()
      Attempt to execute the query once and return a Term representation of the term the single query variable was unified with.
      Term findFirstAsTerm()
      Execute the query once and return the Term the single query variable was unified with.
      private java.lang.String getSingleVariableId()
      Returns the ID of the single variable contained in the query this statement represents.
      private ProjogException noSolutionFound()  
      void setAtomName​(java.lang.String variableId, java.lang.String atomName)
      Attempts to unify the specified String value as an Atom to the variable with the specified id.
      void setDouble​(java.lang.String variableId, double value)
      Attempts to unify the specified double as a DecimalFraction to the variable with the specified id.
      void setListOfAtomNames​(java.lang.String variableId, java.lang.String... atomNames)
      Attempts to unify the specified String values as a Prolog list of atoms to the variable with the specified id.
      void setListOfAtomNames​(java.lang.String variableId, java.util.List<java.lang.String> atomNames)
      Attempts to unify the specified String values as a Prolog list of atoms to the variable with the specified id.
      void setListOfDoubles​(java.lang.String variableId, double... doubles)
      Attempts to unify the specified double values as a Prolog list to the variable with the specified id.
      void setListOfDoubles​(java.lang.String variableId, java.util.List<java.lang.Double> doubles)
      Attempts to unify the specified Double values as a Prolog list to the variable with the specified id.
      void setListOfLongs​(java.lang.String variableId, long... longs)
      Attempts to unify the specified long values as a Prolog list to the variable with the specified id.
      void setListOfLongs​(java.lang.String variableId, java.util.List<java.lang.Long> longs)
      Attempts to unify the specified Long values as a Prolog list to the variable with the specified id.
      void setListOfTerms​(java.lang.String variableId, java.util.List<? extends Term> terms)
      Attempts to unify the specified Term values as a Prolog list to the variable with the specified id.
      void setListOfTerms​(java.lang.String variableId, Term... terms)
      Attempts to unify the specified Term values as a Prolog list to the variable with the specified id.
      void setLong​(java.lang.String variableId, long value)
      Attempts to unify the specified long as a IntegerNumber to the variable with the specified id.
      void setTerm​(java.lang.String variableId, Term term)
      Attempts to unify the specified term to the variable with the specified id.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_VARIABLES

        private static final java.util.Map<java.lang.String,​Variable> EMPTY_VARIABLES
      • parsedInput

        private final Term parsedInput
      • variables

        private final java.util.Map<java.lang.String,​Variable> variables
      • invoked

        private boolean invoked
    • Constructor Detail

      • QueryStatement

        QueryStatement​(KnowledgeBase kb,
                       java.lang.String prologQuery)
        Creates a new QueryStatement representing a query specified by prologQuery.
        Parameters:
        kb - the KnowledgeBase to query against
        prologQuery - prolog syntax representing a query (do not prefix with a ?-)
        Throws:
        ProjogException - if an error occurs parsing prologQuery
      • QueryStatement

        QueryStatement​(PredicateFactory predicateFactory,
                       Term prologQuery)
        Creates a new QueryStatement representing a query specified by prologQuery.
        Parameters:
        PredicateFactory - the PredicateFactory that will be used to execute the query
        prologQuery - prolog syntax representing a query (do not prefix with a ?-)
        Throws:
        ProjogException - if an error occurs parsing prologQuery
    • Method Detail

      • setAtomName

        public void setAtomName​(java.lang.String variableId,
                                java.lang.String atomName)
        Attempts to unify the specified String value as an Atom to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        atomName - the value to use as the name of the Atom that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setDouble

        public void setDouble​(java.lang.String variableId,
                              double value)
        Attempts to unify the specified double as a DecimalFraction to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        value - the value to use as the name of the DecimalFraction that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setLong

        public void setLong​(java.lang.String variableId,
                            long value)
        Attempts to unify the specified long as a IntegerNumber to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        value - the value to use as the name of the IntegerNumber that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setListOfAtomNames

        public void setListOfAtomNames​(java.lang.String variableId,
                                       java.lang.String... atomNames)
        Attempts to unify the specified String values as a Prolog list of atoms to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        atomNames - the values to use as atom elements in the list that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setListOfAtomNames

        public void setListOfAtomNames​(java.lang.String variableId,
                                       java.util.List<java.lang.String> atomNames)
        Attempts to unify the specified String values as a Prolog list of atoms to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        atomNames - the values to use as atom elements in the list that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setListOfDoubles

        public void setListOfDoubles​(java.lang.String variableId,
                                     double... doubles)
        Attempts to unify the specified double values as a Prolog list to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        doubles - the values to use as elements in the list that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setListOfDoubles

        public void setListOfDoubles​(java.lang.String variableId,
                                     java.util.List<java.lang.Double> doubles)
        Attempts to unify the specified Double values as a Prolog list to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        doubles - the values to use as elements in the list that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setListOfLongs

        public void setListOfLongs​(java.lang.String variableId,
                                   long... longs)
        Attempts to unify the specified long values as a Prolog list to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        longs - the values to use as elements in the list that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setListOfLongs

        public void setListOfLongs​(java.lang.String variableId,
                                   java.util.List<java.lang.Long> longs)
        Attempts to unify the specified Long values as a Prolog list to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        longs - the values to use as elements in the list that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setListOfTerms

        public void setListOfTerms​(java.lang.String variableId,
                                   Term... terms)
        Attempts to unify the specified Term values as a Prolog list to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        terms - the values to use as elements in the list that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • setListOfTerms

        public void setListOfTerms​(java.lang.String variableId,
                                   java.util.List<? extends Term> terms)
        Attempts to unify the specified Term values as a Prolog list to the variable with the specified id.
        Parameters:
        variableId - the id of the variable
        terms - the values to use as elements in the list that the variable will be unified with
        Throws:
        ProjogException - if no variable with the specified id exists in the query this object represents, or the given term cannot be unified with the variable
        See Also:
        setTerm(String, Term)
      • executeQuery

        public QueryResult executeQuery()
        Returns a new QueryResult for the query represented by this object.

        Note that the query is not evaluated as part of a call to executeQuery(). It is on the first call of QueryResult.next() that the first attempt to evaluate the query will be made.

        Returns:
        a new QueryResult for the query represented by this object.
      • executeOnce

        public void executeOnce()
        Evaluate once the query represented by this statement.

        The query will only be evaluated once, even if further solutions could of been found on backtracking.

        Throws:
        ProjogException - if no solution can be found
        See Also:
        executeQuery()
      • findFirstAsAtomName

        public java.lang.String findFirstAsAtomName()
        Execute the query once and return a String representation of the atom the single query variable was unified with.
        Returns:
        the name of the atom the query variable has been unified with as a result of executing the query
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findFirstAsDouble

        public double findFirstAsDouble()
        Execute the query once and return a double representation of the term the single query variable was unified with.
        Returns:
        the value the query variable has been unified with as a result of executing the query
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findFirstAsLong

        public long findFirstAsLong()
        Execute the query once and return a long representation of the term the single query variable was unified with.
        Returns:
        the value query variable has been unified with as a result of executing the query
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findFirstAsTerm

        public Term findFirstAsTerm()
        Execute the query once and return the Term the single query variable was unified with.
        Returns:
        the value query variable has been unified with as a result of executing the query
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findFirstAsOptionalAtomName

        public java.util.Optional<java.lang.String> findFirstAsOptionalAtomName()
        Attempt to execute the query once and return a String representation of the atom the single query variable was unified with.
        Returns:
        the name of the atom the query variable has been unified with, or an empty optional if the query was not successfully evaluated
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findFirstAsOptionalDouble

        public java.util.Optional<java.lang.Double> findFirstAsOptionalDouble()
        Attempt to execute the query once and return a Double representation of the term the single query variable was unified with.
        Returns:
        the value the query variable has been unified with, or an empty optional if the query was not successfully evaluated
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findFirstAsOptionalLong

        public java.util.Optional<java.lang.Long> findFirstAsOptionalLong()
        Attempt to execute the query once and return a Long representation of the term the single query variable was unified with.
        Returns:
        the value the query variable has been unified with, or an empty optional if the query was not successfully evaluated
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findFirstAsOptionalTerm

        public java.util.Optional<Term> findFirstAsOptionalTerm()
        Attempt to execute the query once and return a Term representation of the term the single query variable was unified with.
        Returns:
        the value the query variable has been unified with, or an empty optional if the query was not successfully evaluated
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findAllAsAtomName

        public java.util.List<java.lang.String> findAllAsAtomName()
        Find all solutions generated by the query and return String representations of the atoms the single query variable was unified with.
        Returns:
        list of atom names the query variable was been unified with as a result of executing the query until no more solutions were found
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findAllAsDouble

        public java.util.List<java.lang.Double> findAllAsDouble()
        Find all solutions generated by the query and return the double values the single query variable was unified with.
        Returns:
        list of values the query variable was been unified with as a result of executing the query until no more solutions were found
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findAllAsLong

        public java.util.List<java.lang.Long> findAllAsLong()
        Find all solutions generated by the query and return the long values the single query variable was unified with.
        Returns:
        list of values the query variable was been unified with as a result of executing the query until no more solutions were found
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • findAllAsTerm

        public java.util.List<Term> findAllAsTerm()
        Find all solutions generated by the query and return the Term values the single query variable was unified with.
        Returns:
        list of values the query variable was been unified with as a result of executing the query until no more solutions were found
        Throws:
        ProjogException - if the query could not be evaluated successfully
        ProjogException - of there is not exactly one named variable in the query this statement represents
      • getSingleVariableId

        private java.lang.String getSingleVariableId()
        Returns the ID of the single variable contained in the query this statement represents.
        Returns:
        variable ID
        Throws:
        ProjogException - of there is not exactly one named variable in the query this statement represents