Class QueryStatement

java.lang.Object
org.projog.api.QueryStatement

public final class QueryStatement extends Object
Represents a query.

single use, not multi-threaded

See Also:
  • Field Details

    • EMPTY_VARIABLES

      private static final Map<String,Variable> EMPTY_VARIABLES
    • predicateFactory

      private final PredicateFactory predicateFactory
    • parsedInput

      private final Term parsedInput
    • variables

      private final Map<String,Variable> variables
    • invoked

      private boolean invoked
  • Constructor Details

    • QueryStatement

      QueryStatement(KnowledgeBase kb, 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:
      prologQuery - prolog syntax representing a query (do not prefix with a ?-)
      PredicateFactory - the PredicateFactory that will be used to execute the query
      Throws:
      ProjogException - if an error occurs parsing prologQuery
  • Method Details

    • setTerm

      public void setTerm(String variableId, Term term)
      Attempts to unify the specified term to the variable with the specified id.
      Parameters:
      variableId - the id of the variable
      term - the term to unify to the variable
      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:
    • setAtomName

      public void setAtomName(String variableId, 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:
    • setDouble

      public void setDouble(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:
    • setLong

      public void setLong(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:
    • setListOfAtomNames

      public void setListOfAtomNames(String variableId, 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:
    • setListOfAtomNames

      public void setListOfAtomNames(String variableId, List<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:
    • setListOfDoubles

      public void setListOfDoubles(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:
    • setListOfDoubles

      public void setListOfDoubles(String variableId, List<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:
    • setListOfLongs

      public void setListOfLongs(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:
    • setListOfLongs

      public void setListOfLongs(String variableId, List<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:
    • setListOfTerms

      public void setListOfTerms(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:
    • setListOfTerms

      public void setListOfTerms(String variableId, 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:
    • 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:
    • findFirstAsAtomName

      public 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
    • noSolutionFound

      private ProjogException noSolutionFound()
    • findFirstAsOptionalAtomName

      public Optional<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 Optional<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 Optional<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 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 List<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 List<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 List<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 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 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