Package org.projog.api
Class QueryStatement
- java.lang.Object
-
- org.projog.api.QueryStatement
-
public final class QueryStatement extends java.lang.ObjectRepresents a query.single use, not multi-threaded
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.String,Variable>EMPTY_VARIABLESprivate booleaninvokedprivate TermparsedInputprivate PredicateFactorypredicateFactoryprivate java.util.Map<java.lang.String,Variable>variables
-
Constructor Summary
Constructors Constructor Description QueryStatement(KnowledgeBase kb, java.lang.String prologQuery)Creates a newQueryStatementrepresenting a query specified byprologQuery.QueryStatement(PredicateFactory predicateFactory, Term prologQuery)Creates a newQueryStatementrepresenting a query specified byprologQuery.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexecuteOnce()Evaluate once the query represented by this statement.QueryResultexecuteQuery()Returns a newQueryResultfor 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 thedoublevalues the single query variable was unified with.java.util.List<java.lang.Long>findAllAsLong()Find all solutions generated by the query and return thelongvalues the single query variable was unified with.java.util.List<Term>findAllAsTerm()Find all solutions generated by the query and return theTermvalues the single query variable was unified with.java.lang.StringfindFirstAsAtomName()Execute the query once and return a String representation of the atom the single query variable was unified with.doublefindFirstAsDouble()Execute the query once and return adoublerepresentation of the term the single query variable was unified with.longfindFirstAsLong()Execute the query once and return alongrepresentation 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 aDoublerepresentation 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 aLongrepresentation of the term the single query variable was unified with.java.util.Optional<Term>findFirstAsOptionalTerm()Attempt to execute the query once and return aTermrepresentation of the term the single query variable was unified with.TermfindFirstAsTerm()Execute the query once and return theTermthe single query variable was unified with.private java.lang.StringgetSingleVariableId()Returns the ID of the single variable contained in the query this statement represents.private ProjogExceptionnoSolutionFound()voidsetAtomName(java.lang.String variableId, java.lang.String atomName)Attempts to unify the specifiedStringvalue as anAtomto the variable with the specified id.voidsetDouble(java.lang.String variableId, double value)Attempts to unify the specifieddoubleas aDecimalFractionto the variable with the specified id.voidsetListOfAtomNames(java.lang.String variableId, java.lang.String... atomNames)Attempts to unify the specifiedStringvalues as a Prolog list of atoms to the variable with the specified id.voidsetListOfAtomNames(java.lang.String variableId, java.util.List<java.lang.String> atomNames)Attempts to unify the specifiedStringvalues as a Prolog list of atoms to the variable with the specified id.voidsetListOfDoubles(java.lang.String variableId, double... doubles)Attempts to unify the specifieddoublevalues as a Prolog list to the variable with the specified id.voidsetListOfDoubles(java.lang.String variableId, java.util.List<java.lang.Double> doubles)Attempts to unify the specifiedDoublevalues as a Prolog list to the variable with the specified id.voidsetListOfLongs(java.lang.String variableId, long... longs)Attempts to unify the specifiedlongvalues as a Prolog list to the variable with the specified id.voidsetListOfLongs(java.lang.String variableId, java.util.List<java.lang.Long> longs)Attempts to unify the specifiedLongvalues as a Prolog list to the variable with the specified id.voidsetListOfTerms(java.lang.String variableId, java.util.List<? extends Term> terms)Attempts to unify the specifiedTermvalues as a Prolog list to the variable with the specified id.voidsetListOfTerms(java.lang.String variableId, Term... terms)Attempts to unify the specifiedTermvalues as a Prolog list to the variable with the specified id.voidsetLong(java.lang.String variableId, long value)Attempts to unify the specifiedlongas aIntegerNumberto the variable with the specified id.voidsetTerm(java.lang.String variableId, Term term)Attempts to unify the specified term to the variable with the specified id.
-
-
-
Field Detail
-
EMPTY_VARIABLES
private static final java.util.Map<java.lang.String,Variable> EMPTY_VARIABLES
-
predicateFactory
private final PredicateFactory predicateFactory
-
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 newQueryStatementrepresenting a query specified byprologQuery.- Parameters:
kb- theKnowledgeBaseto query againstprologQuery- prolog syntax representing a query (do not prefix with a?-)- Throws:
ProjogException- if an error occurs parsingprologQuery
-
QueryStatement
QueryStatement(PredicateFactory predicateFactory, Term prologQuery)
Creates a newQueryStatementrepresenting a query specified byprologQuery.- Parameters:
PredicateFactory- thePredicateFactorythat will be used to execute the queryprologQuery- prolog syntax representing a query (do not prefix with a?-)- Throws:
ProjogException- if an error occurs parsingprologQuery
-
-
Method Detail
-
setTerm
public void setTerm(java.lang.String variableId, Term term)Attempts to unify the specified term to the variable with the specified id.- Parameters:
variableId- the id of the variableterm- 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(String, String),setDouble(String, double),setLong(String, long),setListOfAtomNames(String, List),setListOfAtomNames(String, String...),setListOfDoubles(String, List),setListOfDoubles(String, double...),setListOfLongs(String, List),setListOfLongs(String, long...),setListOfTerms(String, List),setListOfTerms(String, Term...)
-
setAtomName
public void setAtomName(java.lang.String variableId, java.lang.String atomName)Attempts to unify the specifiedStringvalue as anAtomto the variable with the specified id.- Parameters:
variableId- the id of the variableatomName- the value to use as the name of theAtomthat 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 specifieddoubleas aDecimalFractionto the variable with the specified id.- Parameters:
variableId- the id of the variablevalue- the value to use as the name of theDecimalFractionthat 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 specifiedlongas aIntegerNumberto the variable with the specified id.- Parameters:
variableId- the id of the variablevalue- the value to use as the name of theIntegerNumberthat 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 specifiedStringvalues as a Prolog list of atoms to the variable with the specified id.- Parameters:
variableId- the id of the variableatomNames- 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 specifiedStringvalues as a Prolog list of atoms to the variable with the specified id.- Parameters:
variableId- the id of the variableatomNames- 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 specifieddoublevalues as a Prolog list to the variable with the specified id.- Parameters:
variableId- the id of the variabledoubles- 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 specifiedDoublevalues as a Prolog list to the variable with the specified id.- Parameters:
variableId- the id of the variabledoubles- 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 specifiedlongvalues as a Prolog list to the variable with the specified id.- Parameters:
variableId- the id of the variablelongs- 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 specifiedLongvalues as a Prolog list to the variable with the specified id.- Parameters:
variableId- the id of the variablelongs- 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 specifiedTermvalues as a Prolog list to the variable with the specified id.- Parameters:
variableId- the id of the variableterms- 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 specifiedTermvalues as a Prolog list to the variable with the specified id.- Parameters:
variableId- the id of the variableterms- 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 newQueryResultfor 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 ofQueryResult.next()that the first attempt to evaluate the query will be made.- Returns:
- a new
QueryResultfor 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 successfullyProjogException- of there is not exactly one named variable in the query this statement represents
-
findFirstAsDouble
public double findFirstAsDouble()
Execute the query once and return adoublerepresentation 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 successfullyProjogException- of there is not exactly one named variable in the query this statement represents
-
findFirstAsLong
public long findFirstAsLong()
Execute the query once and return alongrepresentation 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 successfullyProjogException- of there is not exactly one named variable in the query this statement represents
-
findFirstAsTerm
public Term findFirstAsTerm()
Execute the query once and return theTermthe 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 successfullyProjogException- of there is not exactly one named variable in the query this statement represents
-
noSolutionFound
private ProjogException noSolutionFound()
-
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 successfullyProjogException- 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 aDoublerepresentation 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 successfullyProjogException- 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 aLongrepresentation 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 successfullyProjogException- 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 aTermrepresentation 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 successfullyProjogException- 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 successfullyProjogException- 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 thedoublevalues 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 successfullyProjogException- 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 thelongvalues 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 successfullyProjogException- 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 theTermvalues 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 successfullyProjogException- 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
-
-