Class Sparql

java.lang.Object
ai.chatur.cortex.core.jena.Sparql

public final class Sparql extends Object
Runs a Query against a Dataset inside a read transaction.

Every query runs in its own QueryExecution, opened and closed around a single read transaction on the dataset: forEachSolution(java.util.function.Consumer<org.apache.jena.query.QuerySolution>) for queries whose results are accumulated into a value the caller already holds, execute(java.util.function.Function<org.apache.jena.query.QueryExecution, T>) for anything that needs the QueryExecution itself — to distinguish SELECT/ASK/DESCRIBE, for instance. QuerySolutions are only valid while the execution is open, so neither method lets one escape the transaction.

  • Method Summary

    Modifier and Type
    Method
    Description
    bind(String var, org.apache.jena.rdf.model.RDFNode value)
    Binds a variable of the query to a value.
    <T> T
    execute(Function<org.apache.jena.query.QueryExecution,T> mapper)
    Runs the query, applying the mapper to the execution inside the transaction.
    void
    forEachSolution(Consumer<org.apache.jena.query.QuerySolution> action)
    Runs the query as a SELECT, applying the action to each solution inside the transaction.
    static Sparql
    on(org.apache.jena.query.Dataset dataset, org.apache.jena.query.Query query)
    Prepares the query to run against the dataset.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • on

      public static Sparql on(org.apache.jena.query.Dataset dataset, org.apache.jena.query.Query query)
      Prepares the query to run against the dataset.
      Parameters:
      dataset - the dataset to query
      query - the query to run
      Returns:
      the prepared query, ready to bind variables and run
    • bind

      public Sparql bind(String var, org.apache.jena.rdf.model.RDFNode value)
      Binds a variable of the query to a value.
      Parameters:
      var - the name of the variable, without the leading ?
      value - the value to substitute
      Returns:
      this query, for chaining
    • forEachSolution

      public void forEachSolution(Consumer<org.apache.jena.query.QuerySolution> action)
      Runs the query as a SELECT, applying the action to each solution inside the transaction.
      Parameters:
      action - applied to every solution in order, while the query execution is still open
    • execute

      public <T> T execute(Function<org.apache.jena.query.QueryExecution,T> mapper)
      Runs the query, applying the mapper to the execution inside the transaction.
      Type Parameters:
      T - the type of the result
      Parameters:
      mapper - computes the result from the query execution, while it is still open
      Returns:
      the value the mapper computed