Class 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 String addPageLimits​(String query, PageInfo pageInfo)
      Add limit/offset to query based on paging specification.
      static String asSPARQLValue​(Object val)
      Format a value in a way that we can include it in a SPARQL query string
      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.
      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.QuerySolutionMap createBindings​(Object... bindings)
      Utility to declare bindings in code.
      static org.apache.jena.query.QueryExecution createQueryExecution​(org.apache.jena.rdf.model.Model m, String query)
      Create a QueryExecution for executing the given query against the given model.
      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 a QueryExecution for executing the given query against the given model.
      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 a QueryExecution for executing the given query against the given model.
      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.
      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.
      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.
      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
      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 resource r
      protected static List<org.apache.jena.rdf.model.RDFNode> getResultSetAll​(String var, org.apache.jena.query.ResultSet rs)  
      protected static org.apache.jena.rdf.model.RDFNode getResultSetFirst​(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.ResultSet selectAll​(org.apache.jena.rdf.model.Model m, String query)
      Return all results from executing the given select query.
      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.
      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.
      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.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.
      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.
      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.
      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.
      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.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.
      static String substituteInQuery​(String query, Object... strings)
      Inject strings into a SPARQL query replacing each ${i} with the corresponding element from the arg list.
      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.
    • Constructor Detail

      • QueryUtil

        public QueryUtil()
    • 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 via RDFUtil.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 QuerySolutionMap in 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 a QueryExecution for 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 against
        query - 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 a QueryExecution for 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 against
        query - The query to run
        pm - Optional PrefixMapping to use when parsing the query, or null. If null is given, the default prefixes from PrefixUtils.commonPrefixes() will be used
        bindings - 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 a QueryExecution for 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 against
        query - The query to run
        pm - Optional PrefixMapping to use when parsing the query, or null. If null is given, the default prefixes from PrefixUtils.commonPrefixes() will be used
        bindings - Optional QuerySolutionMap containing 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 against
        query - 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 against
        query - The sparql query
        pm - Optional PrefixMapping to use when parsing the query, or null. If null is given, the default prefixes from PrefixUtils.commonPrefixes() will be used
        bindings - 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 against
        query - The sparql query
        pm - Optional PrefixMapping to use when parsing the query, or null. If null is given, the default prefixes from PrefixUtils.commonPrefixes() will be used
        bindings - Optional QuerySolutionMap containing 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 results
        m - The model to run the query against
        query - The sparql query
        pm - Optional PrefixMapping to use when parsing the query, or null. If null is given, the default prefixes from PrefixUtils.commonPrefixes() will be used
        bindings - Optional QuerySolutionMap containing 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 results
        m - The model to run the query against
        query - The sparql query
        pm - Optional PrefixMapping to use when parsing the query, or null. If null is given, the default prefixes from PrefixUtils.commonPrefixes() will be used
        bindings - Optional QuerySolutionMap containing 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 resource r
        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 against
        query - 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 against
        query - The sparql query
        pm - Optional PrefixMapping to use when parsing the query, or null. If null is given, the default prefixes from PrefixUtils.commonPrefixes() will be used
        bindings - 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 against
        query - The sparql query
        pm - Optional PrefixMapping to use when parsing the query, or null.
        bindings - Optional QuerySolutionMap containing 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 on
        bindings - A set of value bindings for variables that may occur in query
      • 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 string
        query - The query string to send
        pm - Optional prefix map. If null, the default common prefixes will be used
        bindings - 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 string
        query - The query string to send
        pm - Optional prefix map. If null, the default common prefixes will be used
        bindings - 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 string
        query - The query string to send
        pm - Optional prefix map. If null, the default common prefixes will be used
        bindings - 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:
      • resultsFor

        public 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. Resource, Literal or generic RDFNode. Skips any non-matching results
        Type Parameters:
        T -