Package org.eclipse.rdf4j.query
Class QueryResults
- java.lang.Object
-
- org.eclipse.rdf4j.common.iteration.Iterations
-
- org.eclipse.rdf4j.query.QueryResults
-
- Direct Known Subclasses:
QueryResultUtil
public class QueryResults extends Iterations
Utility methods related to query results.- Author:
- Jeen Broekstra
-
-
Constructor Summary
Constructors Constructor Description QueryResults()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> List<T>asList(QueryResult<T> queryResult)Get a List containing all elements obtained from the specifiedQueryResult.static ModelasModel(CloseableIteration<? extends Statement,? extends RDF4JException> iteration)Get aModelcontaining all elements obtained from the specified query result.static ModelasModel(CloseableIteration<? extends Statement,? extends RDF4JException> iteration, ModelFactory modelFactory)Get aModelcontaining all elements obtained from the specified query result.static <T> Set<T>asSet(QueryResult<T> queryResult)Get a Set containing all elements obtained from the specifiedQueryResult.static booleanbindingSetsCompatible(BindingSet bs1, BindingSet bs2)Check whether twoBindingSets are compatible.static GraphQueryResultdistinctResults(GraphQueryResult queryResult)Returns aGraphQueryResultthat filters out any duplicate solutions from the supplied queryResult.static TupleQueryResultdistinctResults(TupleQueryResult queryResult)Returns aTupleQueryResultthat filters out any duplicate solutions from the supplied queryResult.static booleanequals(GraphQueryResult result1, GraphQueryResult result2)Compares two graph query results and returnstrueif they are equal.static booleanequals(TupleQueryResult tqr1, TupleQueryResult tqr2)Compares two tuple query results and returnstrueif they are equal.Tuple query results are equal if they contain the same set ofBindingSets and have the same headers.static List<Value>getAllValues(TupleQueryResult result, String var)Returns a list of values of a particular variable out of the QueryResult.static booleanisSubset(TupleQueryResult tqr1, TupleQueryResult tqr2)static GraphQueryResultlimitResults(GraphQueryResult queryResult, long limit, long offset)Returns aGraphQueryResultthat returns at most the specified maximum number of solutions, starting at the supplied offset.static TupleQueryResultlimitResults(TupleQueryResult queryResult, long limit, long offset)Returns aTupleQueryResultthat returns at most the specified maximum number of solutions, starting at the supplied offset.static GraphQueryResultparseGraphBackground(InputStream in, String baseURI, RDFFormat format, WeakReference<?> callerReference)Deprecated.WeakReference> callerReference argument will be removedstatic GraphQueryResultparseGraphBackground(InputStream in, String baseURI, RDFParser parser, WeakReference<?> callerReference)Deprecated.WeakReference> callerReference argument will be removedstatic voidreport(GraphQueryResult graphQueryResult, RDFHandler rdfHandler)Reports a graph query result to anRDFHandler.static voidreport(TupleQueryResult tqr, QueryResultHandler handler)Reports a tuple query result to aTupleQueryResultHandler.static StatementsingleResult(GraphQueryResult result)Returns a single element from the query result.The QueryResult is automatically closed by this method.static BindingSetsingleResult(TupleQueryResult result)Returns a single element from the query result.The QueryResult is automatically closed by this method.
-
-
-
Method Detail
-
asModel
public static Model asModel(CloseableIteration<? extends Statement,? extends RDF4JException> iteration) throws QueryEvaluationException
Get aModelcontaining all elements obtained from the specified query result.- Parameters:
iteration- the source iteration to get the statements from.- Returns:
- a
Modelcontaining all statements obtained from the specified source iteration. - Throws:
QueryEvaluationException
-
asModel
public static Model asModel(CloseableIteration<? extends Statement,? extends RDF4JException> iteration, ModelFactory modelFactory) throws QueryEvaluationException
Get aModelcontaining all elements obtained from the specified query result.- Parameters:
iteration- the source iteration to get the statements from.modelFactory- the ModelFactory used to instantiate the model that gets returned.- Returns:
- a
Modelcontaining all statements obtained from the specified source iteration. - Throws:
QueryEvaluationException
-
asList
public static <T> List<T> asList(QueryResult<T> queryResult) throws QueryEvaluationException
Get a List containing all elements obtained from the specifiedQueryResult.- Parameters:
queryResult- theQueryResultto get the elements from- Returns:
- a List containing all elements obtained from the specified query result.
- Throws:
QueryEvaluationException
-
asSet
public static <T> Set<T> asSet(QueryResult<T> queryResult) throws QueryEvaluationException
Get a Set containing all elements obtained from the specifiedQueryResult.- Parameters:
queryResult- theQueryResultto get the elements from- Returns:
- a Set containing all elements obtained from the specified query result.
- Throws:
QueryEvaluationException
-
getAllValues
public static List<Value> getAllValues(TupleQueryResult result, String var) throws QueryEvaluationException
Returns a list of values of a particular variable out of the QueryResult.- Parameters:
result-var- variable for which list of values needs to be returned- Returns:
- a list of Values of var
- Throws:
QueryEvaluationException
-
singleResult
public static Statement singleResult(GraphQueryResult result) throws QueryEvaluationException
Returns a single element from the query result.The QueryResult is automatically closed by this method.- Parameters:
result-- Returns:
- a single query result element or null
- Throws:
QueryEvaluationException
-
singleResult
public static BindingSet singleResult(TupleQueryResult result) throws QueryEvaluationException
Returns a single element from the query result.The QueryResult is automatically closed by this method.- Parameters:
result-- Returns:
- a single query result element or null
- Throws:
QueryEvaluationException
-
distinctResults
public static GraphQueryResult distinctResults(GraphQueryResult queryResult)
Returns aGraphQueryResultthat filters out any duplicate solutions from the supplied queryResult.- Parameters:
queryResult- a queryResult containing possible duplicate statements.- Returns:
- a
GraphQueryResultwith any duplicates filtered out.
-
distinctResults
public static TupleQueryResult distinctResults(TupleQueryResult queryResult)
Returns aTupleQueryResultthat filters out any duplicate solutions from the supplied queryResult.- Parameters:
queryResult- a queryResult containing possible duplicate solutions.- Returns:
- a
TupleQueryResultwith any duplicates filtered out.
-
limitResults
public static TupleQueryResult limitResults(TupleQueryResult queryResult, long limit, long offset)
Returns aTupleQueryResultthat returns at most the specified maximum number of solutions, starting at the supplied offset.- Parameters:
queryResult- a query result possibly containing more solutions than the specified maximum.limit- the maximum number of solutions to return. If set to 0 or lower, no limit will be applied.offset- the number of solutions to skip at the beginning. If set to 0 or lower, no offset will be applied.- Returns:
- A
TupleQueryResultthat will at return at most the specified maximum number of solutions. If neitherlimitnoroffsetare applied, this returns the originalqueryResult.
-
limitResults
public static GraphQueryResult limitResults(GraphQueryResult queryResult, long limit, long offset)
Returns aGraphQueryResultthat returns at most the specified maximum number of solutions, starting at the supplied offset.- Parameters:
queryResult- a query result possibly containing more solutions than the specified maximum.limit- the maximum number of solutions to return. If set to 0 or lower, no limit will be applied.offset- the number of solutions to skip at the beginning. If set to 0 or lower, no offset will be applied.- Returns:
- A
GraphQueryResultthat will at return at most the specified maximum number of solutions. If neitherlimitnoroffsetare applied, this returns the originalqueryResult.
-
parseGraphBackground
@Deprecated(since="4.1.2") public static GraphQueryResult parseGraphBackground(InputStream in, String baseURI, RDFFormat format, WeakReference<?> callerReference) throws UnsupportedRDFormatException
Deprecated.WeakReference> callerReference argument will be removedParses an RDF document and returns it as a GraphQueryResult object, with parsing done on a separate thread in the background.
IMPORTANT: As this method will spawn a new thread in the background, it is vitally important that the resulting GraphQueryResult be closed consistently when it is no longer required, to prevent resource leaks.- Parameters:
in- TheInputStreamcontaining the RDF document.baseURI- The base URI for the RDF document.format- TheRDFFormatof the RDF document.- Returns:
- A
GraphQueryResultthat parses in the background, and must be closed to prevent resource leaks. - Throws:
UnsupportedRDFormatException
-
parseGraphBackground
@Deprecated(since="4.1.2") public static GraphQueryResult parseGraphBackground(InputStream in, String baseURI, RDFParser parser, WeakReference<?> callerReference)
Deprecated.WeakReference> callerReference argument will be removedParses an RDF document and returns it as a GraphQueryResult object, with parsing done on a separate thread in the background.
IMPORTANT: As this method will spawn a new thread in the background, it is vitally important that the resulting GraphQueryResult be closed consistently when it is no longer required, to prevent resource leaks.- Parameters:
in- TheInputStreamcontaining the RDF document.baseURI- The base URI for the RDF document.parser- TheRDFParser.- Returns:
- A
GraphQueryResultthat parses in the background, and must be closed to prevent resource leaks.
-
report
public static void report(TupleQueryResult tqr, QueryResultHandler handler) throws TupleQueryResultHandlerException, QueryEvaluationException
Reports a tuple query result to aTupleQueryResultHandler.
TheAutoCloseable.close()method will always be called before this method returns.
If there is an exception generated by the TupleQueryResult,QueryResultHandler.endQueryResult()will not be called.- Parameters:
tqr- The query result to report.handler- The handler to report the query result to.- Throws:
TupleQueryResultHandlerException- If such an exception is thrown by the used query result writer.QueryEvaluationException
-
report
public static void report(GraphQueryResult graphQueryResult, RDFHandler rdfHandler) throws RDFHandlerException, QueryEvaluationException
Reports a graph query result to anRDFHandler.
TheAutoCloseable.close()method will always be called before this method returns.
If there is an exception generated by the GraphQueryResult,RDFHandler.endRDF()will not be called.- Parameters:
graphQueryResult- The query result to report.rdfHandler- The handler to report the query result to.- Throws:
RDFHandlerException- If such an exception is thrown by the used RDF writer.QueryEvaluationException
-
equals
public static boolean equals(TupleQueryResult tqr1, TupleQueryResult tqr2) throws QueryEvaluationException
Compares two tuple query results and returnstrueif they are equal.Tuple query results are equal if they contain the same set ofBindingSets and have the same headers. Blank nodes identifiers are not relevant for equality, they are matched by trying to find compatible mappings between BindingSets. Note that the method consumes both query results fully.- Parameters:
tqr1- the firstTupleQueryResultto compare.tqr2- the secondTupleQueryResultto compare.- Returns:
- true if equal
- Throws:
QueryEvaluationException
-
isSubset
public static boolean isSubset(TupleQueryResult tqr1, TupleQueryResult tqr2) throws QueryEvaluationException
- Throws:
QueryEvaluationException
-
equals
public static boolean equals(GraphQueryResult result1, GraphQueryResult result2) throws QueryEvaluationException
Compares two graph query results and returnstrueif they are equal. Two graph query results are considered equal if they are isomorphic graphs. Note that the method consumes both query results fully.- Parameters:
result1- the first query result to compareresult2- the second query result to compare.- Returns:
trueif the supplied graph query results are isomorphic graphs,falseotherwise.- Throws:
QueryEvaluationException- See Also:
Models.isomorphic(Iterable, Iterable)
-
bindingSetsCompatible
public static boolean bindingSetsCompatible(BindingSet bs1, BindingSet bs2)
Check whether twoBindingSets are compatible. Two binding sets are compatible if they have equal values for each variable that is bound in both binding sets.- Parameters:
bs1-bs2-- Returns:
- true if compatible
-
-