Package com.epimorphics.rdfutil
Class QueryUtil
- java.lang.Object
-
- com.epimorphics.rdfutil.QueryUtil
-
public class QueryUtil extends Object
Random small utilities to help with SPARQl queries.
-
-
Constructor Summary
Constructors Constructor Description QueryUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringaddPageLimits(String query, PageInfo pageInfo)Add limit/offset to query based on paging specification.static StringasSPARQLValue(Object val)Format a value in a way that we can include it in a SPARQL query stringstatic List<org.apache.jena.rdf.model.Literal>connectedLiterals(org.apache.jena.rdf.model.Resource root, String path)Return all Literals connected by a property path to the given resource.static List<org.apache.jena.rdf.model.Resource>connectedResources(org.apache.jena.rdf.model.Resource root, String path)Return all resources connected by a property path to the given resource.static org.apache.jena.query.QuerySolutionMapcreateBindings(Object... bindings)Utility to declare bindings in code.static org.apache.jena.query.QueryExecutioncreateQueryExecution(org.apache.jena.rdf.model.Model m, String query)Create aQueryExecutionfor executing the given query against the given model.static org.apache.jena.query.QueryExecutioncreateQueryExecution(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Create aQueryExecutionfor executing the given query against the given model.static org.apache.jena.query.QueryExecutioncreateQueryExecution(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, org.apache.jena.query.QuerySolutionMap bindings)Create aQueryExecutionfor executing the given query against the given model.static org.apache.jena.rdf.model.Modeldescribe(org.apache.jena.rdf.model.Model m, String query)Return the model that results from executing the given describe query.static org.apache.jena.rdf.model.Modeldescribe(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Return the model that results from executing the given describe query.static org.apache.jena.rdf.model.Modeldescribe(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, org.apache.jena.query.QuerySolutionMap bindings)Return the model that results from executing the given describe query.static org.apache.jena.rdf.model.ModeldescribeResource(org.apache.jena.rdf.model.Model m, String uri)Return the model that is the SPARQL description of resource with the given URIstatic org.apache.jena.rdf.model.ModeldescribeResource(org.apache.jena.rdf.model.Model m, org.apache.jena.rdf.model.Resource r)Return the model that is the SPARQL description of resourcerprotected static List<org.apache.jena.rdf.model.RDFNode>getResultSetAll(String var, org.apache.jena.query.ResultSet rs)protected static org.apache.jena.rdf.model.RDFNodegetResultSetFirst(String var, org.apache.jena.query.QueryExecution qe, org.apache.jena.query.ResultSet rs)static List<org.apache.jena.rdf.model.Resource>resultsFor(org.apache.jena.query.ResultSet results, String varname)Take a column from result set and extract it as a list of resources.static <T> List<T>resultsFor(org.apache.jena.query.ResultSet results, String varname, Class<T> cls)Take a column from result set and extract it as a list of values of the given type (e.g.static org.apache.jena.query.ResultSetselectAll(org.apache.jena.rdf.model.Model m, String query)Return all results from executing the given select query.static org.apache.jena.query.ResultSetselectAll(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Return all results from executing the given select query.static org.apache.jena.query.ResultSetselectAll(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, org.apache.jena.query.QuerySolutionMap bindings)Return all results from executing the given select query.static List<org.apache.jena.rdf.model.RDFNode>selectAllVar(String var, org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Return all results from executing the given select query for a given variable.static org.apache.jena.rdf.model.RDFNodeselectFirstVar(String var, org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Return the first result from executing the given select query, for a given variable.static org.apache.jena.rdf.model.ModelserviceDescribe(String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Execute a describe query against a remote SPARQL endpoint.static org.apache.jena.query.ResultSetserviceSelectAll(String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Execute a select query against a remote SPARQL endpoint.static org.apache.jena.query.ResultSetserviceSelectAll(String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, org.apache.jena.query.QuerySolutionMap bindings)Execute a select query against a remote SPARQL endpoint.static List<org.apache.jena.rdf.model.RDFNode>serviceSelectAllVar(String var, String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Return all values for the given var from executing the given query against the given remote sparql endpoint.static org.apache.jena.rdf.model.RDFNodeserviceSelectFirstVar(String var, String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Return the first value for the given var from executing the given query against the given remote sparql endpoint.static StringsubstituteInQuery(String query, Object... strings)Inject strings into a SPARQL query replacing each ${i} with the corresponding element from the arg list.static StringsubstituteVars(String query, org.apache.jena.query.QuerySolutionMap bindings)Substitute the variables in a query string for the values from the given binding.
-
-
-
Method Detail
-
substituteInQuery
public static String substituteInQuery(String query, Object... strings)
Inject strings into a SPARQL query replacing each ${i} with the corresponding element from the arg list. Purely syntactic. Up to the caller to protected any sensitive characters.
-
addPageLimits
public static String addPageLimits(String query, PageInfo pageInfo)
Add limit/offset to query based on paging specification.
-
resultsFor
public static List<org.apache.jena.rdf.model.Resource> resultsFor(org.apache.jena.query.ResultSet results, String varname)
Take a column from result set and extract it as a list of resources. Skips any non-resource results.
-
createBindings
public static org.apache.jena.query.QuerySolutionMap createBindings(Object... bindings)
Utility to declare bindings in code. Called with an array of objects, each pairs of sequential objecst will be interpreted as string denoting a variable name, and a value denoting an RDFNode viaRDFUtil.asRDFNode(Object).- Parameters:
bindings- An array of Objects, which will be taken in pairs to be a string var name and an object to encode as an RDF node.- Returns:
- A
QuerySolutionMapin which the keys are bound to their given values
-
createQueryExecution
public static org.apache.jena.query.QueryExecution createQueryExecution(org.apache.jena.rdf.model.Model m, String query)Create aQueryExecutionfor executing the given query against the given model. Other variants allow prefix mappings and initial variable bindings to be specified. Uses the default common prefixes.- Parameters:
m- The model to run queries againstquery- The query to run- Returns:
- A query execution object
-
createQueryExecution
public static org.apache.jena.query.QueryExecution createQueryExecution(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Create aQueryExecutionfor executing the given query against the given model. This variant allows additional prefixes for the query to be passed in, together with bindings for variables.- Parameters:
m- The model to run queries againstquery- The query to runpm- OptionalPrefixMappingto use when parsing the query, or null. If null is given, the default prefixes fromPrefixUtils.commonPrefixes()will be usedbindings- Optional array of name/value pairs to use as initial variable bindings- Returns:
- A query execution object
-
createQueryExecution
public static org.apache.jena.query.QueryExecution createQueryExecution(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, org.apache.jena.query.QuerySolutionMap bindings)Create aQueryExecutionfor executing the given query against the given model. This variant allows additional prefixes for the query to be passed in, together with bindings for variables.- Parameters:
m- The model to run queries againstquery- The query to runpm- OptionalPrefixMappingto use when parsing the query, or null. If null is given, the default prefixes fromPrefixUtils.commonPrefixes()will be usedbindings- OptionalQuerySolutionMapcontaining initial bindings for query variables- Returns:
- A query execution object
-
selectAll
public static org.apache.jena.query.ResultSet selectAll(org.apache.jena.rdf.model.Model m, String query)Return all results from executing the given select query. Uses the default commont prefixes.- Parameters:
m- The model to run the query againstquery- The sparql query- Returns:
- ResultSet of all values
-
selectAll
public static org.apache.jena.query.ResultSet selectAll(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Return all results from executing the given select query.- Parameters:
m- The model to run the query againstquery- The sparql querypm- OptionalPrefixMappingto use when parsing the query, or null. If null is given, the default prefixes fromPrefixUtils.commonPrefixes()will be usedbindings- Optional array of name/value pairs to use as initial variable bindings- Returns:
- ResultSet of all values
-
selectAll
public static org.apache.jena.query.ResultSet selectAll(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, org.apache.jena.query.QuerySolutionMap bindings)Return all results from executing the given select query.- Parameters:
m- The model to run the query againstquery- The sparql querypm- OptionalPrefixMappingto use when parsing the query, or null. If null is given, the default prefixes fromPrefixUtils.commonPrefixes()will be usedbindings- OptionalQuerySolutionMapcontaining initial bindings for query variables- Returns:
- ResultSet of all values
-
selectAllVar
public static List<org.apache.jena.rdf.model.RDFNode> selectAllVar(String var, org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)
Return all results from executing the given select query for a given variable.- Parameters:
var- The variable name to project from the query resultsm- The model to run the query againstquery- The sparql querypm- OptionalPrefixMappingto use when parsing the query, or null. If null is given, the default prefixes fromPrefixUtils.commonPrefixes()will be usedbindings- OptionalQuerySolutionMapcontaining initial bindings for query variables- Returns:
- Non-null list of the values for
var
-
selectFirstVar
public static org.apache.jena.rdf.model.RDFNode selectFirstVar(String var, org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)
Return the first result from executing the given select query, for a given variable.- Parameters:
var- The variable name to project from the query resultsm- The model to run the query againstquery- The sparql querypm- OptionalPrefixMappingto use when parsing the query, or null. If null is given, the default prefixes fromPrefixUtils.commonPrefixes()will be usedbindings- OptionalQuerySolutionMapcontaining initial bindings for query variables- Returns:
- The first value for
var, or null
-
describeResource
public static org.apache.jena.rdf.model.Model describeResource(org.apache.jena.rdf.model.Model m, org.apache.jena.rdf.model.Resource r)Return the model that is the SPARQL description of resourcer- Parameters:
m-r-- Returns:
-
describeResource
public static org.apache.jena.rdf.model.Model describeResource(org.apache.jena.rdf.model.Model m, String uri)Return the model that is the SPARQL description of resource with the given URI- Parameters:
m-uri-- Returns:
-
describe
public static org.apache.jena.rdf.model.Model describe(org.apache.jena.rdf.model.Model m, String query)Return the model that results from executing the given describe query. Uses the default common prefixes.- Parameters:
m- The model to run the query againstquery- The sparql query- Returns:
- ResultSet of all values
-
describe
public static org.apache.jena.rdf.model.Model describe(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)Return the model that results from executing the given describe query.- Parameters:
m- The model to run the query againstquery- The sparql querypm- OptionalPrefixMappingto use when parsing the query, or null. If null is given, the default prefixes fromPrefixUtils.commonPrefixes()will be usedbindings- Optional array of name/value pairs to use as initial variable bindings- Returns:
- ResultSet of all values
-
describe
public static org.apache.jena.rdf.model.Model describe(org.apache.jena.rdf.model.Model m, String query, org.apache.jena.shared.PrefixMapping pm, org.apache.jena.query.QuerySolutionMap bindings)Return the model that results from executing the given describe query.- Parameters:
m- The model to run the query againstquery- The sparql querypm- OptionalPrefixMappingto use when parsing the query, or null.bindings- OptionalQuerySolutionMapcontaining initial bindings for query variables- Returns:
- ResultSet of all values
-
asSPARQLValue
public static String asSPARQLValue(Object val)
Format a value in a way that we can include it in a SPARQL query string- Parameters:
val-- Returns:
- The value formatted for SPARQL
-
substituteVars
public static String substituteVars(String query, org.apache.jena.query.QuerySolutionMap bindings)
Substitute the variables in a query string for the values from the given binding. This produces a new query string, suitable, for example, for sending to a remote service endpoint.- Parameters:
query- The query string to act onbindings- A set of value bindings for variables that may occur inquery
-
serviceSelectAll
public static org.apache.jena.query.ResultSet serviceSelectAll(String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)
Execute a select query against a remote SPARQL endpoint.- Parameters:
serviceURL- The address of the SPARQL endpoint, as a stringquery- The query string to sendpm- Optional prefix map. If null, the default common prefixes will be usedbindings- Optional bindings for variables in the query string, in pairs of variable name and value- Returns:
- The resultset of all results
-
serviceSelectAll
public static org.apache.jena.query.ResultSet serviceSelectAll(String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, org.apache.jena.query.QuerySolutionMap bindings)
Execute a select query against a remote SPARQL endpoint.- Parameters:
serviceURL- The address of the SPARQL endpoint, as a stringquery- The query string to sendpm- Optional prefix map. If null, the default common prefixes will be usedbindings- Optional bindings for variables in the query string, in pairs of variable name and value- Returns:
- The resultset of all results
-
serviceSelectAllVar
public static List<org.apache.jena.rdf.model.RDFNode> serviceSelectAllVar(String var, String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)
Return all values for the given var from executing the given query against the given remote sparql endpoint.- Parameters:
var-serviceURL-query-pm-bindings-- Returns:
-
serviceSelectFirstVar
public static org.apache.jena.rdf.model.RDFNode serviceSelectFirstVar(String var, String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)
Return the first value for the given var from executing the given query against the given remote sparql endpoint.- Parameters:
var-serviceURL-query-pm-bindings-- Returns:
-
serviceDescribe
public static org.apache.jena.rdf.model.Model serviceDescribe(String serviceURL, String query, org.apache.jena.shared.PrefixMapping pm, Object... bindings)
Execute a describe query against a remote SPARQL endpoint.- Parameters:
serviceURL- The address of the SPARQL endpoint, as a stringquery- The query string to sendpm- Optional prefix map. If null, the default common prefixes will be usedbindings- Optional bindings for variables in the query string, in pairs of variable name and value- Returns:
- The resultset of all results
-
connectedResources
public static List<org.apache.jena.rdf.model.Resource> connectedResources(org.apache.jena.rdf.model.Resource root, String path)
Return all resources connected by a property path to the given resource. Path can use prefixes defined in the resource's Model.
-
connectedLiterals
public static List<org.apache.jena.rdf.model.Literal> connectedLiterals(org.apache.jena.rdf.model.Resource root, String path)
Return all Literals connected by a property path to the given resource. Path can use prefixes defined in the resource's Model.
-
getResultSetFirst
protected static org.apache.jena.rdf.model.RDFNode getResultSetFirst(String var, org.apache.jena.query.QueryExecution qe, org.apache.jena.query.ResultSet rs)
- Parameters:
var-qe-rs-- Returns:
-
getResultSetAll
protected static List<org.apache.jena.rdf.model.RDFNode> getResultSetAll(String var, org.apache.jena.query.ResultSet rs)
- Parameters:
var-rs-- Returns:
-
-