Repository: ontodev/robot
Pull request #328 (2018-08-01 13:45:08)

Query imports as named graphs
See #158


Patch:
diff --git a/docs/examples/named_graph.csv b/docs/examples/named_graph.csv
new file mode 100644
index 00000000..6f13d9be
--- /dev/null
+++ b/docs/examples/named_graph.csv
@@ -0,0 +1,25 @@
+s
+http://purl.obolibrary.org/obo/UBERON_0000061
+http://purl.obolibrary.org/obo/UBERON_0000062
+http://purl.obolibrary.org/obo/UBERON_0000064
+http://purl.obolibrary.org/obo/UBERON_0000465
+http://purl.obolibrary.org/obo/UBERON_0000467
+http://purl.obolibrary.org/obo/UBERON_0000468
+http://purl.obolibrary.org/obo/UBERON_0000475
+http://purl.obolibrary.org/obo/UBERON_0000480
+http://purl.obolibrary.org/obo/UBERON_0000916
+http://purl.obolibrary.org/obo/UBERON_0000949
+http://purl.obolibrary.org/obo/UBERON_0001062
+http://purl.obolibrary.org/obo/UBERON_0001235
+http://purl.obolibrary.org/obo/UBERON_0001851
+http://purl.obolibrary.org/obo/UBERON_0002100
+http://purl.obolibrary.org/obo/UBERON_0002368
+http://purl.obolibrary.org/obo/UBERON_0002369
+http://purl.obolibrary.org/obo/UBERON_0002417
+http://purl.obolibrary.org/obo/UBERON_0002530
+http://purl.obolibrary.org/obo/UBERON_0006858
+http://purl.obolibrary.org/obo/UBERON_0009569
+http://purl.obolibrary.org/obo/UBERON_0010000
+http://purl.obolibrary.org/obo/UBERON_0011676
+http://purl.obolibrary.org/obo/UBERON_0013701
+http://purl.obolibrary.org/obo/UBERON_0013702
diff --git a/docs/examples/named_graph.sparql b/docs/examples/named_graph.sparql
new file mode 100644
index 00000000..5aa2b0ca
--- /dev/null
+++ b/docs/examples/named_graph.sparql
@@ -0,0 +1,11 @@
+PREFIX owl: <http://www.w3.org/2002/07/owl#>
+
+SELECT ?s 
+FROM NAMED <https://github.com/ontodev/robot/examples/edit.owl> 
+WHERE { 
+	GRAPH ?g { 
+		?s a owl:Class .
+		FILTER (!isBlank(?s))
+	}
+}
+ORDER BY ?s
\ No newline at end of file
diff --git a/docs/examples/part_of.ttl b/docs/examples/part_of.ttl
index 775e452e..a98073c0 100644
--- a/docs/examples/part_of.ttl
+++ b/docs/examples/part_of.ttl
@@ -1,10 +1,7 @@
-@prefix :      <http://purl.obolibrary.org/obo/go.owl#> .
 @prefix owl:   <http://www.w3.org/2002/07/owl#> .
 @prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
-@prefix xml:   <http://www.w3.org/XML/1998/namespace> .
-@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
 @prefix GO:    <http://purl.obolibrary.org/obo/GO_> .
-@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
 @prefix BFO:   <http://purl.obolibrary.org/obo/BFO_> .
+@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
 
 GO:0031981  BFO:0000050  GO:0005634 .
diff --git a/docs/query.md b/docs/query.md
index 9f1eeaa9..d5022e80 100644
--- a/docs/query.md
+++ b/docs/query.md
@@ -28,6 +28,18 @@ Instead of specifying one or more pairs (query file, output file), you can speci
       --queries cell_part_ask.sparql \
       --output-dir results/
 
+## Imports
+
+By default, `query` ignores import statements. To include all imports as named graphs, add `--use-graphs true`. 
+
+    robot query --input imports.owl \
+      --use-graphs true --catalog catalog.xml \
+      --query named_graph.sparql results/named_graph.csv
+      
+The example above also uses the [global](/global)  `--catalog` option to specify the catalog file for the import mapping. The default graph is the union of all graphs, which allows querying over an ontology and all its imports.
+
+The names of the graphs correspond to the ontology IRIs of the imports. If the import does not have an ontology IRI, one will be automatically generated. Running `query` with the `-vv` flag will print the names of all graphs as they are added.
+
 ---
 
 ## Error Messages
diff --git a/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java b/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java
index 6abd0716..cfe334d0 100644
--- a/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java
+++ b/robot-command/src/main/java/org/obolibrary/robot/QueryCommand.java
@@ -1,6 +1,5 @@
 package org.obolibrary.robot;
 
-import com.hp.hpl.jena.sparql.core.DatasetGraph;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.OutputStream;
@@ -11,6 +10,7 @@ import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
+import org.apache.jena.query.Dataset;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -26,9 +26,14 @@ public class QueryCommand implements Command {
   /** Namespace for error messages. */
   private static final String NS = "query#";
 
+  /** Error message when a query is not provided */
   private static final String missingQueryError =
       NS + "MISSING QUERY ERROR at least one query must be provided";
 
+  /** Error message when an invalid --imports option is provided */
+  private static final String importsOptionError =
+      NS + "IMPORTS OPTION ERROR --imports must be union, graphs, or ignore.";
+
   /** Store the command-line options for the command. */
   private Options options;
 
@@ -38,25 +43,26 @@ public class QueryCommand implements Command {
     o.addOption("i", "input", true, "load ontology from a file");
     o.addOption("I", "input-iri", true, "load ontology from an IRI");
     o.addOption("f", "format", true, "the query result format: CSV, TSV," + " TTL, JSONLD, etc.");
-
     o.addOption("O", "output-dir", true, "Directory for output");
+    o.addOption("g", "use-graphs", true, "if true, load imports as named graphs");
 
-    Option select = new Option("s", "select", true, "run a SPARQL SELECT query (deprecated)");
-    select.setArgs(2);
-    o.addOption(select);
+    Option opt;
 
-    Option construct =
-        new Option("c", "construct", true, "run a SPARQL CONSTRUCT query (deprecated)");
-    construct.setArgs(2);
-    o.addOption(construct);
+    opt = new Option("s", "select", true, "run a SPARQL SELECT query (deprecated)");
+    opt.setArgs(2);
+    o.addOption(opt);
 
-    Option query = new Option("q", "query", true, "run a SPARQL query");
-    query.setArgs(2);
-    o.addOption(query);
+    opt = new Option("c", "construct", true, "run a SPARQL CONSTRUCT query (deprecated)");
+    opt.setArgs(2);
+    o.addOption(opt);
 
-    Option queries = new Option("Q", "queries", true, "verify one or more SPARQL queries");
-    queries.setArgs(Option.UNLIMITED_VALUES);
-    o.addOption(queries);
+    opt = new Option("q", "query", true, "run a SPARQL query");
+    opt.setArgs(2);
+    o.addOption(opt);
+
+    opt = new Option("Q", "queries", true, "verify one or more SPARQL queries");
+    opt.setArgs(Option.UNLIMITED_VALUES);
+    o.addOption(opt);
 
     options = o;
   }
@@ -120,7 +126,6 @@ public class QueryCommand implements Command {
    * @throws Exception on any problem
    */
   public CommandState execute(CommandState state, String[] args) throws Exception {
-
     CommandLine line = CommandLineHelper.getCommandLine(getUsage(), getOptions(), args);
     if (line == null) {
       return null;
@@ -130,10 +135,18 @@ public class QueryCommand implements Command {
     String outputDir = CommandLineHelper.getDefaultValue(line, "output-dir", "");
     IOHelper ioHelper = CommandLineHelper.getIOHelper(line);
     state = CommandLineHelper.updateInputOntology(ioHelper, state, line);
-    DatasetGraph dsg = QueryOperation.loadOntology(state.getOntology());
+
+    // Determine what to do with the imports and create a new dataset
+    boolean useGraphs = CommandLineHelper.getBooleanValue(line, "use-graphs", false);
+    Dataset dataset;
+    if (useGraphs) {
+      dataset = QueryOperation.loadOntologyAsDataset(state.getOntology(), true);
+    } else {
+      dataset = QueryOperation.loadOntologyAsDataset(state.getOntology(), false);
+    }
 
     // Collect all queries as (queryPath, outputPath) pairs.
-    List<List<String>> queries = new ArrayList<List<String>>();
+    List<List<String>> queries = new ArrayList<>();
     List<String> qs = CommandLineHelper.getOptionalValues(line, "query");
     for (int i = 0; i < qs.size(); i += 2) {
       queries.add(qs.subList(i, i + 2));
@@ -148,7 +161,7 @@ public class QueryCommand implements Command {
     }
     qs = CommandLineHelper.getOptionalValues(line, "queries");
     for (String q : qs) {
-      List<String> xs = new ArrayList<String>();
+      List<String> xs = new ArrayList<>();
       xs.add(q);
       xs.add(null);
       queries.add(xs);
@@ -180,9 +193,8 @@ public class QueryCommand implements Command {
       }
 
       OutputStream output = new FileOutputStream(outputPath);
-      QueryOperation.runSparqlQuery(dsg, query, formatName, output);
+      QueryOperation.runSparqlQuery(dataset, query, formatName, output);
     }
-
     return state;
   }
 }
diff --git a/robot-command/src/main/java/org/obolibrary/robot/VerifyCommand.java b/robot-command/src/main/java/org/obolibrary/robot/VerifyCommand.java
index 561d2fa9..3cb001e4 100644
--- a/robot-command/src/main/java/org/obolibrary/robot/VerifyCommand.java
+++ b/robot-command/src/main/java/org/obolibrary/robot/VerifyCommand.java
@@ -1,12 +1,12 @@
 package org.obolibrary.robot;
 
-import com.hp.hpl.jena.sparql.core.DatasetGraph;
 import java.io.File;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
+import org.apache.jena.query.Dataset;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -111,11 +111,15 @@ public class VerifyCommand implements Command {
 
     IOHelper ioHelper = CommandLineHelper.getIOHelper(line);
     state = CommandLineHelper.updateInputOntology(ioHelper, state, line);
-    DatasetGraph graph = QueryOperation.loadOntology(state.getOntology());
+    // Load into dataset without imports
+    Dataset dataset = QueryOperation.loadOntologyAsDataset(state.getOntology(), false);
 
     File outputDir = new File(CommandLineHelper.getDefaultValue(line, "output-dir", "."));
 
     String[] queryFilePaths = line.getOptionValues("queries");
+    if (queryFilePaths.length == 0) {
+      throw new Exception(missingQueryError);
+    }
     boolean passing = true;
     for (String filePath : queryFilePaths) {
       File queryFile = new File(filePath);
@@ -123,7 +127,7 @@ public class VerifyCommand implements Command {
       String csvPath = FilenameUtils.getBaseName(filePath).concat(".csv");
       boolean result =
           QueryOperation.runVerify(
-              graph, filePath, queryString, outputDir.toPath().resolve(csvPath), null);
+              dataset, filePath, queryString, outputDir.toPath().resolve(csvPath), null);
       if (result) {
         passing = false;
       }
diff --git a/robot-core/pom.xml b/robot-core/pom.xml
index cdf3a5c2..8dd81420 100644
--- a/robot-core/pom.xml
+++ b/robot-core/pom.xml
@@ -51,6 +51,10 @@
             <!-- see documentation: http://siom79.github.io/japicmp/MavenPlugin.html -->
             <breakBuildOnBinaryIncompatibleModifications>true</breakBuildOnBinaryIncompatibleModifications>
             <breakBuildOnSourceIncompatibleModifications>true</breakBuildOnSourceIncompatibleModifications>
+            <!-- Temporarily exclude QueryOperation for Jena upgrade #314. -->
+            <excludes>
+              <exclude>org.obolibrary.robot.QueryOperation</exclude>
+            </excludes>
           </parameter>
         </configuration>
         <executions>
@@ -69,7 +73,12 @@
     <dependency>
       <groupId>org.apache.jena</groupId>
       <artifactId>jena-arq</artifactId>
-      <version>2.13.0</version>
+      <version>3.8.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.jena</groupId>
+      <artifactId>jena-tdb</artifactId>
+      <version>3.8.0</version>
     </dependency>
     <dependency>
       <groupId>com.github.jsonld-java</groupId>
diff --git a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
index 3d7f6462..50a7b833 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
@@ -7,8 +7,6 @@ import com.github.jsonldjava.core.JsonLdOptions;
 import com.github.jsonldjava.core.JsonLdProcessor;
 import com.github.jsonldjava.utils.JsonUtils;
 import com.google.common.collect.Sets;
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
 import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -28,6 +26,8 @@ import java.util.Set;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
+import org.apache.jena.rdf.model.Model;
+import org.apache.jena.rdf.model.ModelFactory;
 import org.geneontology.obographs.io.OboGraphJsonDocumentFormat;
 import org.geneontology.obographs.io.OgJsonGenerator;
 import org.geneontology.obographs.model.GraphDocument;
diff --git a/robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java b/robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java
index 32f172d3..b5c20083 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java
@@ -1,18 +1,24 @@
 package org.obolibrary.robot;
 
-import com.hp.hpl.jena.query.*;
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.sparql.core.DatasetGraph;
-import com.hp.hpl.jena.sparql.core.DatasetGraphFactory;
 import java.io.*;
 import java.nio.file.Path;
+import java.util.HashSet;
 import java.util.Map;
-import org.apache.jena.riot.Lang;
-import org.apache.jena.riot.RDFDataMgr;
-import org.apache.jena.riot.ResultSetMgr;
+import java.util.Set;
+import java.util.UUID;
+import org.apache.jena.query.*;
+import org.apache.jena.rdf.model.Model;
+import org.apache.jena.rdf.model.ModelFactory;
+import org.apache.jena.riot.*;
 import org.apache.jena.riot.resultset.ResultSetLang;
+import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.core.DatasetGraphFactory;
+import org.apache.jena.tdb.TDB;
+import org.apache.jena.tdb.TDBFactory;
 import org.semanticweb.owlapi.formats.TurtleDocumentFormat;
+import org.semanticweb.owlapi.model.IRI;
 import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
 import org.semanticweb.owlapi.model.OWLOntologyStorageException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -33,60 +39,185 @@ public class QueryOperation {
   private static final String queryTypeError = NS + "QUERY TYPE ERROR unknown query type: %s";
 
   /**
-   * Load an ontology into a DatasetGraph. The ontology is not changed. NOTE: This is not elegant!
-   * It basically pipes Turtle output from OWLAPI to Jena Arq.
+   * Load an ontology into a DatasetGraph. The ontology is not changed.
    *
-   * @param ontology The ontology to load into the graph.
-   * @return A new DatasetGraph with the ontology loaded into the default graph.
+   * @param ontology The ontology to load into the graph
+   * @return A new DatasetGraph with the ontology loaded into the default graph
    * @throws OWLOntologyStorageException rarely
    */
   public static DatasetGraph loadOntology(OWLOntology ontology) throws OWLOntologyStorageException {
-    ByteArrayOutputStream out = new ByteArrayOutputStream();
-    ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), out);
-    DatasetGraph dsg = DatasetGraphFactory.createMem();
-    RDFDataMgr.read(
-        dsg.getDefaultGraph(), new ByteArrayInputStream(out.toByteArray()), Lang.TURTLE);
+    ByteArrayOutputStream os = new ByteArrayOutputStream();
+    ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), os);
+    DatasetGraph dsg = DatasetGraphFactory.create();
+    RDFDataMgr.read(dsg.getDefaultGraph(), new ByteArrayInputStream(os.toByteArray()), Lang.TURTLE);
     return dsg;
   }
 
   /**
-   * Create an empty Dataset.
+   * Given an ontology, return a dataset containing that ontology as the only graph.
    *
-   * @return an empty dataset
+   * @param ontology ontology to query
+   * @return dataset to query
+   * @throws IOException on issue creating temp files
+   * @throws OWLOntologyStorageException on issue writing ontology to TTL format
    */
+  public static Dataset loadOntologyAsDataset(OWLOntology ontology)
+      throws IOException, OWLOntologyStorageException {
+    return loadOntologyAsDataset(ontology, false);
+  }
+
+  /**
+   * Given an ontology and a boolean indicating if imports should be included as graphs, return a
+   * dataset either with just the ontology, or the ontology and its imports as separate graphs.
+   *
+   * @param ontology ontology to query
+   * @param useGraphs if true, load imports as separate graphs
+   * @return dataset to query
+   * @throws OWLOntologyStorageException on issue writing ontology to TTL format
+   */
+  public static Dataset loadOntologyAsDataset(OWLOntology ontology, boolean useGraphs)
+      throws OWLOntologyStorageException, UnsupportedEncodingException {
+    OWLOntologyManager manager = ontology.getOWLOntologyManager();
+    Set<OWLOntology> ontologies = new HashSet<>();
+    ontologies.add(ontology);
+    if (useGraphs) {
+      ontologies.addAll(ontology.getImports());
+    }
+    // Instantiate an empty dataset
+    Dataset dataset = TDBFactory.createDataset();
+    TDB.getContext().set(TDB.symUnionDefaultGraph, true);
+    // Load each ontology in the set as a named model
+    for (OWLOntology ont : ontologies) {
+      Model m = ModelFactory.createDefaultModel();
+      ByteArrayOutputStream os = new ByteArrayOutputStream();
+      manager.saveOntology(ont, new TurtleDocumentFormat(), os);
+      String content = new String(os.toByteArray(), "UTF-8");
+      RDFParser.fromString(content).lang(RDFLanguages.TTL).parse(m);
+      // Get the name of the graph as the ontology IRI
+      IRI iri = ont.getOntologyID().getOntologyIRI().orNull();
+      String name;
+      if (iri != null) {
+        name = iri.toString();
+      } else {
+        // If there is no IRI, generate a random ID
+        name = UUID.randomUUID().toString();
+      }
+      dataset.addNamedModel(name, m);
+      logger.info("Named graph added: " + name);
+    }
+    return dataset;
+  }
+
+  /**
+   * Count results.
+   *
+   * @param results The result set to count.
+   * @return the size of the result set
+   */
+  public static int countResults(ResultSet results) {
+    int i = 0;
+    while (results.hasNext()) {
+      results.next();
+      i++;
+    }
+    return i;
+  }
+
   public static Dataset createEmptyDataset() {
-    return DatasetFactory.create(DatasetGraphFactory.createMem());
+    return DatasetFactory.create(DatasetGraphFactory.create());
   }
 
   /**
-   * Given a SPARQL query, return its type as a string: ASK, CONSTRUCT, DESCRIBE, SELECT.
+   * Execute a SPARQL CONSTRUCT query on a graph and return the model.
    *
-   * @param queryString the SPARQL query string
-   * @return the query type string or null
-   * @throws IllegalArgumentException on bad query
+   * @param dsg the graph to construct in
+   * @param query the SPARQL construct query string
+   * @return the result Model
    */
-  public static String getQueryTypeName(String queryString) throws IllegalArgumentException {
-    // TODO: This method is unused
-    QueryExecution qexec = QueryExecutionFactory.create(queryString, createEmptyDataset());
-    Query query = qexec.getQuery();
-    String queryType = null;
-    switch (query.getQueryType()) {
-      case Query.QueryTypeAsk:
-        queryType = "ASK";
-        break;
-      case Query.QueryTypeConstruct:
-        queryType = "CONSTRUCT";
-        break;
-      case Query.QueryTypeDescribe:
-        queryType = "DESCRIBE";
-        break;
-      case Query.QueryTypeSelect:
-        queryType = "SELECT";
-        break;
-      default:
-        throw new IllegalArgumentException(String.format(queryTypeError, query.getQueryType()));
+  public static Model execConstruct(DatasetGraph dsg, String query) {
+    return execConstruct(DatasetFactory.create(dsg), query);
+  }
+
+  /**
+   * Execute a SPARQL CONSTRUCT query on a dataset and return a model.
+   *
+   * @param dataset the Dataset to construct in
+   * @param query the SPARQL construct query string
+   * @return the result Model
+   */
+  public static Model execConstruct(Dataset dataset, String query) {
+    QueryExecution qExec = QueryExecutionFactory.create(query, dataset);
+    return qExec.execConstruct();
+  }
+
+  /**
+   * Execute a SPARQL SELECT query on a graph and return a result set.
+   *
+   * @param dsg the graph to query
+   * @param query the SPARQL query string
+   * @return the result set
+   */
+  public static ResultSet execQuery(DatasetGraph dsg, String query) {
+    return execQuery(DatasetFactory.create(dsg), query);
+  }
+
+  /**
+   * Execute a SPARQL SELECT query on a dataset and return a result set.
+   *
+   * @param dataset the Dataset to query over
+   * @param query the SPARQL query string
+   * @return the result set
+   */
+  public static ResultSet execQuery(Dataset dataset, String query) {
+    QueryExecution qExec = QueryExecutionFactory.create(query, dataset);
+    return qExec.execSelect();
+  }
+
+  /**
+   * Execute a verification. Writes to STDERR.
+   *
+   * @param queriesResults a map from files to query results and output streams
+   * @return true if there are any violations
+   */
+  public static boolean execVerify(
+      Map<File, Tuple<ResultSetRewindable, OutputStream>> queriesResults) throws IOException {
+    boolean isViolation = false;
+    for (File outFile : queriesResults.keySet()) {
+      Tuple<ResultSetRewindable, OutputStream> resultAndStream = queriesResults.get(outFile);
+      ResultSetRewindable results = resultAndStream.left();
+      OutputStream outStream = resultAndStream.right();
+      System.out.println(
+          "Rule " + outFile.getCanonicalPath() + ": " + results.size() + " violation(s)");
+      if (results.size() > 0) {
+        isViolation = true;
+        ResultSetMgr.write(System.err, results, Lang.CSV);
+        results.reset();
+      }
+      System.err.print('\n');
+      ResultSetMgr.write(outStream, results, Lang.CSV);
+    }
+    return isViolation;
+  }
+
+  /**
+   * Execute a SPARQL query and return true if there are any results, false otherwise. Prints
+   * violations to STDERR.
+   *
+   * @param dsg the graph to query over
+   * @param query the SPARQL query string
+   * @return true if the are results, false otherwise
+   */
+  public static boolean execVerify(DatasetGraph dsg, String ruleName, String query) {
+    ResultSetRewindable results = ResultSetFactory.copyResults(execQuery(dsg, query));
+    System.out.println("Rule " + ruleName + ": " + results.size() + " violation(s)");
+    if (results.size() == 0) {
+      System.out.println("PASS Rule " + ruleName + ": 0 violation(s)");
+      return false;
+    } else {
+      ResultSetMgr.write(System.err, results, Lang.CSV);
+      System.out.println("FAIL Rule " + ruleName + ": " + results.size() + " violation(s)");
+      return true;
     }
-    return queryType;
   }
 
   /**
@@ -96,8 +227,8 @@ public class QueryOperation {
    * @return the format name
    */
   public static String getDefaultFormatName(String query) {
-    QueryExecution qexec = QueryExecutionFactory.create(query, createEmptyDataset());
-    return getDefaultFormatName(qexec.getQuery());
+    QueryExecution qExec = QueryExecutionFactory.create(query, createEmptyDataset());
+    return getDefaultFormatName(qExec.getQuery());
   }
 
   /**
@@ -108,7 +239,7 @@ public class QueryOperation {
    * @throws IllegalArgumentException on bad query
    */
   public static String getDefaultFormatName(Query query) throws IllegalArgumentException {
-    String formatName = null;
+    String formatName;
     switch (query.getQueryType()) {
       case Query.QueryTypeAsk:
         formatName = "txt";
@@ -169,58 +300,33 @@ public class QueryOperation {
   }
 
   /**
-   * Run a SPARQL query (ASK, CONSTRUCT, DESCRIBE, SELECT) and return true if there were results,
-   * false otherwise.
+   * Given a SPARQL query, return its type as a string: ASK, CONSTRUCT, DESCRIBE, or SELECT.
    *
-   * @param qexec the SPARQL QueryExecution to run
-   * @param formatName the name of the output format
-   * @param output the OutputStream to write to
-   * @return true if there are any results, false otherwise
+   * @param queryString the SPARQL query string
+   * @return the query type string
    * @throws IllegalArgumentException on bad query
    */
-  public static boolean runSparqlQuery(QueryExecution qexec, String formatName, OutputStream output)
-      throws IllegalArgumentException {
-    boolean result;
-    Query query = qexec.getQuery();
-    if (formatName == null) {
-      formatName = getDefaultFormatName(query);
-    }
-
+  public static String getQueryTypeName(String queryString) {
+    QueryExecution qExec = QueryExecutionFactory.create(queryString, createEmptyDataset());
+    Query query = qExec.getQuery();
+    String queryType;
     switch (query.getQueryType()) {
       case Query.QueryTypeAsk:
-        result = true;
-        writeResult(qexec.execAsk(), output);
+        queryType = "ASK";
         break;
       case Query.QueryTypeConstruct:
-        result = maybeWriteResult(qexec.execConstruct(), formatName, output);
+        queryType = "CONSTRUCT";
         break;
       case Query.QueryTypeDescribe:
-        result = maybeWriteResult(qexec.execDescribe(), formatName, output);
+        queryType = "DESCRIBE";
         break;
       case Query.QueryTypeSelect:
-        result = maybeWriteResult(qexec.execSelect(), formatName, output);
+        queryType = "SELECT";
         break;
       default:
         throw new IllegalArgumentException(String.format(queryTypeError, query.getQueryType()));
     }
-
-    return result;
-  }
-
-  /**
-   * Run a SPARQL query (ASK, CONSTRUCT, DESCRIBE, SELECT) and return true if there were results,
-   * false otherwise.
-   *
-   * @param dsg the graph to query over
-   * @param query the SPARQL query string
-   * @param formatName the name of the output format
-   * @param output the OutputStream to write to
-   * @return true if there are any results, false otherwise
-   */
-  public static boolean runSparqlQuery(
-      DatasetGraph dsg, String query, String formatName, OutputStream output) {
-    QueryExecution qexec = QueryExecutionFactory.create(query, DatasetFactory.create(dsg));
-    return runSparqlQuery(qexec, formatName, output);
+    return queryType;
   }
 
   /**
@@ -230,11 +336,7 @@ public class QueryOperation {
    * @return true if there are results, false otherwise
    */
   public static boolean hasResult(ResultSet result) {
-    if (result.hasNext()) {
-      return true;
-    } else {
-      return false;
-    }
+    return result.hasNext();
   }
 
   /**
@@ -244,11 +346,7 @@ public class QueryOperation {
    * @return true if there are statements, false otherwise
    */
   public static boolean hasResult(Model result) {
-    if (result.isEmpty()) {
-      return false;
-    } else {
-      return true;
-    }
+    return !result.isEmpty();
   }
 
   /**
@@ -272,7 +370,7 @@ public class QueryOperation {
   /**
    * If a model has statements, write to the output stream and return true. Otherwise return false.
    *
-   * @param result the model to write
+   * @param result the Model to write
    * @param formatName the name of the language to write in
    * @param output the output stream to write to
    * @return true if there were statements, false otherwise
@@ -287,172 +385,118 @@ public class QueryOperation {
   }
 
   /**
-   * Write a boolean result to an output stream.
-   *
-   * @param result the boolean to write
-   * @param output the output stream to write to
-   */
-  public static void writeResult(boolean result, OutputStream output) {
-    PrintStream printStream = new PrintStream(output);
-    printStream.print(result);
-  }
-
-  /**
-   * Write a result set to an output stream.
-   *
-   * @param resultSet the results to write
-   * @param format the language to write in
-   * @param output the output stream to write to
-   */
-  public static void writeResult(ResultSet resultSet, Lang format, OutputStream output) {
-    if (format == null) {
-      format = Lang.CSV;
-    }
-    ResultSetMgr.write(output, resultSet, format);
-  }
-
-  /**
-   * Write a result set to an output stream.
-   *
-   * @param resultSet the results to write
-   * @param formatName the name of the language to write in
-   * @param output the output stream to write to
-   */
-  public static void writeResult(ResultSet resultSet, String formatName, OutputStream output) {
-    writeResult(resultSet, getFormatLang(formatName), output);
-  }
-
-  /**
-   * Write a model to an output stream.
-   *
-   * @param model the model to write
-   * @param format the language to write in
-   * @param output the output stream to write to
-   */
-  public static void writeResult(Model model, Lang format, OutputStream output) {
-    if (format == null) {
-      format = Lang.TTL;
-    }
-    RDFDataMgr.write(output, model, format);
-  }
-
-  /**
-   * Write a model to an output stream.
-   *
-   * @param model the model to write
-   * @param formatName the name of the language to write in
-   * @param output the output stream to write to
-   */
-  public static void writeResult(Model model, String formatName, OutputStream output) {
-    writeResult(model, getFormatLang(formatName), output);
-  }
-
-  /**
-   * Execute a SPARQL SELECT query and return a result set.
+   * Run a CONSTRUCT query and write the result to a file.
    *
-   * @param dsg the graph to query over
-   * @param query the SPARQL query string
-   * @return the result set
+   * @param dataset the Dataset to construct in.
+   * @param query The SPARQL construct query string.
+   * @param output The file to write to.
+   * @param outputFormat The file format.
+   * @throws FileNotFoundException if output file is not found
    */
-  public static ResultSet execQuery(DatasetGraph dsg, String query) {
-    QueryExecution qexec = QueryExecutionFactory.create(query, DatasetFactory.create(dsg));
-    return qexec.execSelect();
+  public static void runConstruct(Dataset dataset, String query, File output, Lang outputFormat)
+      throws FileNotFoundException {
+    writeResult(execConstruct(dataset, query), outputFormat, new FileOutputStream(output));
   }
 
   /**
-   * Execute a SPARQL CONSTRUCT query and return a model.
+   * Run a SELECT query on a graph and write the results to a file.
    *
-   * @param dsg The graph to construct in.
-   * @param query The SPARQL construct query string.
-   * @return The result model
+   * @param dsg the graph to query
+   * @param query The SPARQL query string.
+   * @param output The file to write to.
+   * @param outputFormat The file format.
+   * @throws FileNotFoundException if output file is not found
    */
-  public static Model execConstruct(DatasetGraph dsg, String query) {
-    QueryExecution qexec = QueryExecutionFactory.create(query, DatasetFactory.create(dsg));
-    return qexec.execConstruct();
+  public static void runQuery(DatasetGraph dsg, String query, File output, Lang outputFormat)
+      throws FileNotFoundException {
+    runQuery(DatasetFactory.create(dsg), query, output, outputFormat);
   }
 
   /**
-   * Run a SELECT query and write the result to a file.
+   * Run a SELECT query on a dataset and write the result to a file.
    *
-   * @param dsg The graph to query over.
+   * @param dataset The dataset to query over.
    * @param query The SPARQL query string.
    * @param output The file to write to.
    * @param outputFormat The file format.
    * @throws FileNotFoundException if output file is not found
    */
-  public static void runQuery(DatasetGraph dsg, String query, File output, Lang outputFormat)
+  public static void runQuery(Dataset dataset, String query, File output, Lang outputFormat)
       throws FileNotFoundException {
     if (outputFormat == null) {
       outputFormat = Lang.CSV;
     }
-    writeResult(execQuery(dsg, query), outputFormat, new FileOutputStream(output));
+    writeResult(execQuery(dataset, query), outputFormat, new FileOutputStream(output));
   }
 
   /**
-   * Run a CONSTRUCT query and write the result to a file.
+   * Run a SPARQL query and return true if there were results, false otherwise.
    *
-   * @param dsg The graph to construct in.
-   * @param query The SPARQL construct query string.
-   * @param output The file to write to.
-   * @param outputFormat The file format.
-   * @throws FileNotFoundException if output file is not found
+   * @param dsg the graph to query over
+   * @param query the SPARQL query string
+   * @param formatName the name of the output format
+   * @param output the OutputStream to write to
+   * @return true if results, false if otherwise
    */
-  public static void runConstruct(DatasetGraph dsg, String query, File output, Lang outputFormat)
-      throws FileNotFoundException {
-    writeResult(execConstruct(dsg, query), outputFormat, new FileOutputStream(output));
+  public static boolean runSparqlQuery(
+      DatasetGraph dsg, String query, String formatName, OutputStream output) {
+    return runSparqlQuery(DatasetFactory.create(dsg), query, formatName, output);
   }
 
   /**
-   * Execute a verification. Writes to STDERR.
+   * Given a dataset, a query string, a format name, and an output stream, run the SPARQL query over
+   * the named graphs and write the output to the stream.
    *
-   * @param queriesResults a map from files to query results and output streams
-   * @return true if there are any violations
-   * @throws IOException on issue getting file path
+   * @param dataset Dataset to query over
+   * @param queryString query to run
+   * @param formatName format of output
+   * @param output output stream to write to
+   * @return true if successful
    */
-  public static boolean execVerify(
-      Map<File, Tuple<ResultSetRewindable, OutputStream>> queriesResults) throws IOException {
-    boolean isViolation = false;
-    for (File outFile : queriesResults.keySet()) {
-      Tuple<ResultSetRewindable, OutputStream> resultAndStream = queriesResults.get(outFile);
-      ResultSetRewindable results = resultAndStream.left();
-      OutputStream outStream = resultAndStream.right();
-      System.out.println(
-          "Rule " + outFile.getCanonicalPath() + ": " + results.size() + " violation(s)");
-      if (results.size() > 0) {
-        isViolation = true;
-        ResultSetMgr.write(System.err, results, Lang.CSV);
-        results.reset();
-      }
-      System.err.print('\n');
-      ResultSetMgr.write(outStream, results, Lang.CSV);
+  public static boolean runSparqlQuery(
+      Dataset dataset, String queryString, String formatName, OutputStream output) {
+    dataset.begin(ReadWrite.READ);
+    boolean result;
+    try (QueryExecution qExec = QueryExecutionFactory.create(queryString, dataset)) {
+      result = runSparqlQuery(qExec, formatName, output);
+    } finally {
+      dataset.end();
     }
-    return isViolation;
+    return result;
   }
 
   /**
-   * Execute a SPARQL query and return true if there are any results, false otherwise. Prints
-   * violations to STDERR.
+   * Given a query execution, a format name, and an output stream, run the query and write to
+   * output.
    *
-   * @param dsg the graph to query over
-   * @param ruleName name of the rule
-   * @param query the SPARQL query string
-   * @return true if the are results, false otherwise
+   * @param qExec QueryExecution to run
+   * @param formatName format of output
+   * @param output output stream to write to
+   * @return true if there are results
    */
-  public static boolean execVerify(DatasetGraph dsg, String ruleName, String query) {
-    ResultSetRewindable results = ResultSetFactory.copyResults(execQuery(dsg, query));
-    System.out.println("Rule " + ruleName + ": " + results.size() + " violation(s)");
-    if (results.size() == 0) {
-      System.out.println("PASS Rule " + ruleName + ": 0 violation(s)");
-      return false;
-    } else {
-      ResultSetMgr.write(System.err, results, Lang.CSV);
-      System.out.println("FAIL Rule " + ruleName + ": " + results.size() + " violation(s)");
-      return true;
+  public static boolean runSparqlQuery(
+      QueryExecution qExec, String formatName, OutputStream output) {
+    Query query = qExec.getQuery();
+    if (formatName == null) {
+      formatName = getDefaultFormatName(query);
+    }
+    switch (query.getQueryType()) {
+      case Query.QueryTypeAsk:
+        writeResult(qExec.execAsk(), output);
+        return true;
+      case Query.QueryTypeConstruct:
+        return maybeWriteResult(qExec.execConstruct(), formatName, output);
+      case Query.QueryTypeDescribe:
+        return maybeWriteResult(qExec.execDescribe(), formatName, output);
+      case Query.QueryTypeSelect:
+        return maybeWriteResult(qExec.execSelect(), formatName, output);
+      default:
+        throw new IllegalArgumentException(String.format(queryTypeError, query.getQueryType()));
     }
   }
 
   /**
-   * Run a SELECT query and write the result to a file. Prints violations to STDERR.
+   * Run a SELECT query over the graph and write the result to a file. Prints violations to STDERR.
    *
    * @param dsg The graph to query over.
    * @param ruleName name of the rule
@@ -465,10 +509,27 @@ public class QueryOperation {
   public static boolean runVerify(
       DatasetGraph dsg, String ruleName, String query, Path outputPath, Lang outputFormat)
       throws FileNotFoundException {
+    return runVerify(DatasetFactory.create(dsg), ruleName, query, outputPath, outputFormat);
+  }
+
+  /**
+   * Run a SELECT query over the union of named graphs in a dataset and write the result to a file.
+   * Prints violations to STDERR.
+   *
+   * @param dataset The dataset to query over.
+   * @param query The SPARQL query string.
+   * @param outputPath The file path to write to, if there are results
+   * @param outputFormat The file format.
+   * @throws FileNotFoundException if output file is not found
+   * @return true if the are results (so file is written), false otherwise
+   */
+  public static boolean runVerify(
+      Dataset dataset, String ruleName, String query, Path outputPath, Lang outputFormat)
+      throws FileNotFoundException {
     if (outputFormat == null) {
       outputFormat = Lang.CSV;
     }
-    ResultSetRewindable results = ResultSetFactory.copyResults(execQuery(dsg, query));
+    ResultSetRewindable results = ResultSetFactory.copyResults(execQuery(dataset, query));
     if (results.size() == 0) {
       System.out.println("PASS Rule " + ruleName + ": 0 violation(s)");
       return false;
@@ -483,17 +544,63 @@ public class QueryOperation {
   }
 
   /**
-   * Count results.
+   * Write a boolean result to an output stream.
    *
-   * @param results The result set to count.
-   * @return the size of the result set
+   * @param result the boolean to write
+   * @param output the output stream to write to
    */
-  public static int countResults(ResultSet results) {
-    int i = 0;
-    while (results.hasNext()) {
-      results.next();
-      i++;
+  public static void writeResult(boolean result, OutputStream output) {
+    PrintStream printStream = new PrintStream(output);
+    printStream.print(result);
+  }
+
+  /**
+   * Write a result set to an output stream.
+   *
+   * @param resultSet the results to write
+   * @param formatName the name of the language to write in
+   * @param output the output stream to write to
+   */
+  public static void writeResult(ResultSet resultSet, String formatName, OutputStream output) {
+    writeResult(resultSet, getFormatLang(formatName), output);
+  }
+
+  /**
+   * Write a result set to an output stream.
+   *
+   * @param resultSet the results to write
+   * @param format the language to write in (if null, CSV)
+   * @param output the output stream to write to
+   */
+  public static void writeResult(ResultSet resultSet, Lang format, OutputStream output) {
+    if (format == null) {
+      format = Lang.CSV;
     }
-    return i;
+    ResultSetMgr.write(output, resultSet, format);
+  }
+
+  /**
+   * Write a model to an output stream.
+   *
+   * @param model the Model to write
+   * @param formatName the name of the language to write in
+   * @param output the output stream to write to
+   */
+  public static void writeResult(Model model, String formatName, OutputStream output) {
+    writeResult(model, getFormatLang(formatName), output);
+  }
+
+  /**
+   * Write a model to an output stream.
+   *
+   * @param model the Model to write
+   * @param format the language to write in (if null, TTL)
+   * @param output the output stream to write to
+   */
+  public static void writeResult(Model model, Lang format, OutputStream output) {
+    if (format == null) {
+      format = Lang.TTL;
+    }
+    RDFDataMgr.write(output, model, format);
   }
 }
diff --git a/robot-core/src/main/java/org/obolibrary/robot/ReportOperation.java b/robot-core/src/main/java/org/obolibrary/robot/ReportOperation.java
index 9cca07d1..369ad3f6 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/ReportOperation.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/ReportOperation.java
@@ -1,8 +1,5 @@
 package org.obolibrary.robot;
 
-import com.hp.hpl.jena.query.QuerySolution;
-import com.hp.hpl.jena.query.ResultSet;
-import com.hp.hpl.jena.sparql.core.DatasetGraph;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
@@ -24,6 +21,11 @@ import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import org.apache.commons.io.FileUtils;
+import org.apache.jena.query.Dataset;
+import org.apache.jena.query.DatasetFactory;
+import org.apache.jena.query.QuerySolution;
+import org.apache.jena.query.ResultSet;
+import org.apache.jena.sparql.core.DatasetGraph;
 import org.obolibrary.robot.checks.Report;
 import org.obolibrary.robot.checks.Violation;
 import org.semanticweb.owlapi.model.OWLOntology;
@@ -128,7 +130,8 @@ public class ReportOperation {
     Map<String, String> queries = getQueryStrings(profile.keySet());
 
     Report report = new Report();
-    DatasetGraph dsg = QueryOperation.loadOntology(ontology);
+    // Load into dataset without imports
+    Dataset dataset = QueryOperation.loadOntologyAsDataset(ontology, false);
     for (String queryName : queries.keySet()) {
       String fullQueryString = queries.get(queryName);
       String queryString;
@@ -138,7 +141,7 @@ public class ReportOperation {
       } else {
         queryString = fullQueryString;
       }
-      report.addViolations(queryName, profile.get(queryName), getViolations(dsg, queryString));
+      report.addViolations(queryName, profile.get(queryName), getViolations(dataset, queryString));
     }
 
     Integer violationCount = report.getTotalViolations();
@@ -372,15 +375,25 @@ public class ReportOperation {
   }
 
   /**
-   * Given an ontology as a DatasetGraph and a query as a CheckerQuery, return the violations found
-   * by that query.
+   * Given an ontology as a DatasetGraph and a query, return the violations found by that query.
    *
-   * @param dsg the ontology
+   * @param dsg the ontology as a graph
    * @param query the query
    * @return List of Violations
    */
   private static List<Violation> getViolations(DatasetGraph dsg, String query) {
-    ResultSet violationSet = QueryOperation.execQuery(dsg, query);
+    return getViolations(DatasetFactory.create(dsg), query);
+  }
+
+  /**
+   * Given an ontology as a Dataset and a query, return the violations found by that query.
+   *
+   * @param dataset the ontology/ontologies as a dataset
+   * @param query the query
+   * @return List of Violations
+   */
+  private static List<Violation> getViolations(Dataset dataset, String query) {
+    ResultSet violationSet = QueryOperation.execQuery(dataset, query);
 
     Map<String, Violation> violations = new HashMap<>();
     Violation violation;


Test patch:
diff --git a/robot-core/src/test/java/org/obolibrary/robot/QueryOperationTest.java b/robot-core/src/test/java/org/obolibrary/robot/QueryOperationTest.java
index dec1238c..b422955b 100644
--- a/robot-core/src/test/java/org/obolibrary/robot/QueryOperationTest.java
+++ b/robot-core/src/test/java/org/obolibrary/robot/QueryOperationTest.java
@@ -5,21 +5,16 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import com.google.common.collect.Lists;
-import com.hp.hpl.jena.query.ResultSet;
-import com.hp.hpl.jena.query.ResultSetFactory;
-import com.hp.hpl.jena.query.ResultSetRewindable;
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.Property;
-import com.hp.hpl.jena.rdf.model.RDFNode;
-import com.hp.hpl.jena.rdf.model.Resource;
-import com.hp.hpl.jena.rdf.model.ResourceFactory;
-import com.hp.hpl.jena.sparql.core.DatasetGraph;
 import java.io.*;
 import java.util.HashMap;
 import java.util.Map;
+import org.apache.jena.query.Dataset;
+import org.apache.jena.query.ResultSet;
+import org.apache.jena.query.ResultSetFactory;
+import org.apache.jena.query.ResultSetRewindable;
+import org.apache.jena.rdf.model.*;
 import org.junit.Test;
 import org.semanticweb.owlapi.model.OWLOntology;
-import org.semanticweb.owlapi.model.OWLOntologyCreationException;
 import org.semanticweb.owlapi.model.OWLOntologyStorageException;
 
 /**
@@ -33,16 +28,51 @@ public class QueryOperationTest extends CoreTest {
    * Tests a simple query.
    *
    * @throws IOException on IO error
-   * @throws OWLOntologyCreationException on ontology error
    * @throws OWLOntologyStorageException on ontology error
    */
   @Test
-  public void testQuery()
-      throws IOException, OWLOntologyCreationException, OWLOntologyStorageException {
+  public void testQuery() throws IOException, OWLOntologyStorageException {
     OWLOntology ontology = loadOntology("/simple.owl");
-    DatasetGraph dsg = QueryOperation.loadOntology(ontology);
+    Dataset dataset = QueryOperation.loadOntologyAsDataset(ontology);
     String query = "SELECT * WHERE { ?s ?p ?o }";
-    ResultSet results = QueryOperation.execQuery(dsg, query);
+    ResultSet results = QueryOperation.execQuery(dataset, query);
+    assertEquals(6, QueryOperation.countResults(results));
+  }
+
+  /**
+   * Tests a simple query with imports loaded.
+   *
+   * @throws IOException on IO error
+   * @throws OWLOntologyStorageException on ontology error
+   */
+  @Test
+  public void testQueryWithDefaultGraph() throws IOException, OWLOntologyStorageException {
+    OWLOntology ontology = loadOntologyWithCatalog("/import_test.owl");
+    Dataset dataset = QueryOperation.loadOntologyAsDataset(ontology, true);
+    String query =
+        "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
+            + "SELECT * WHERE {\n"
+            + "  ?s ?p ?o .\n"
+            + "  FILTER NOT EXISTS { ?s ?p owl:Ontology }\n"
+            + "}";
+    ResultSet results = QueryOperation.execQuery(dataset, query);
+    assertEquals(6, QueryOperation.countResults(results));
+  }
+
+  /**
+   * Tests a simple query on a named graph.
+   *
+   * @throws IOException on IO error
+   * @throws OWLOntologyStorageException on ontology error
+   */
+  @Test
+  public void testQueryWithNamedGraph() throws IOException, OWLOntologyStorageException {
+    OWLOntology ontology = loadOntologyWithCatalog("/import_test.owl");
+    Dataset dataset = QueryOperation.loadOntologyAsDataset(ontology, true);
+    String query =
+        "PREFIX robot: <https://github.com/ontodev/robot/robot-core/src/test/resources/>\n"
+            + "SELECT * FROM NAMED robot:simple.owl WHERE {?s ?p ?o}";
+    ResultSet results = QueryOperation.execQuery(dataset, query);
     assertEquals(6, QueryOperation.countResults(results));
   }
 
@@ -55,7 +85,7 @@ public class QueryOperationTest extends CoreTest {
   @Test
   public void testConstruct() throws IOException, OWLOntologyStorageException {
     OWLOntology ontology = loadOntology("/bot.owl");
-    DatasetGraph dsg = QueryOperation.loadOntology(ontology);
+    Dataset dataset = QueryOperation.loadOntologyAsDataset(ontology);
     String query =
         "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
             + "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
@@ -69,21 +99,27 @@ public class QueryOperationTest extends CoreTest {
             + "\t\t\t\t\t\towl:onProperty part_of: ;\n"
             + "\t\t\t\t\t\towl:someValuesFrom ?whole ]\n"
             + "}";
-    Model model = QueryOperation.execConstruct(dsg, query);
+    Model model = QueryOperation.execConstruct(dataset, query);
     Resource s = ResourceFactory.createResource("http://purl.obolibrary.org/obo/UBERON_0000062");
     Property p = ResourceFactory.createProperty("http://purl.obolibrary.org/obo/BFO_0000050");
     RDFNode o = ResourceFactory.createResource("http://purl.obolibrary.org/obo/UBERON_0000467");
     assertTrue(model.contains(s, p, o));
   }
 
+  /**
+   * Tests a verify with violations.
+   *
+   * @throws IOException on IO error
+   * @throws OWLOntologyStorageException on ontology error
+   */
   @Test
   public void testExecVerifyWithViolations() throws IOException, OWLOntologyStorageException {
 
     OWLOntology ontology = loadOntology("/simple.owl");
-    DatasetGraph graph = QueryOperation.loadOntology(ontology);
+    Dataset dataset = QueryOperation.loadOntologyAsDataset(ontology);
     String allViolations =
         "SELECT ?s ?p ?o\n" + "WHERE {\n" + "    ?s ?p ?o .\n" + "}\n" + "LIMIT 10";
-    ResultSet resultSet = QueryOperation.execQuery(graph, allViolations);
+    ResultSet resultSet = QueryOperation.execQuery(dataset, allViolations);
     ResultSetRewindable copy = ResultSetFactory.copyResults(resultSet);
     Map<File, Tuple<ResultSetRewindable, OutputStream>> testResults = new HashMap<>();
     ByteArrayOutputStream testOut = new ByteArrayOutputStream();
@@ -94,12 +130,18 @@ public class QueryOperationTest extends CoreTest {
     assertEquals(7, Lists.newArrayList(testOut.toString().split("\n")).size());
   }
 
+  /**
+   * Tests a verify with no violations.
+   *
+   * @throws IOException on IO error
+   * @throws OWLOntologyStorageException on ontology error
+   */
   @Test
   public void testExecVerifyNoViolations() throws IOException, OWLOntologyStorageException {
     OWLOntology ontology = loadOntology("/simple.owl");
-    DatasetGraph graph = QueryOperation.loadOntology(ontology);
+    Dataset dataset = QueryOperation.loadOntologyAsDataset(ontology);
     String allViolations = "SELECT ?s ?p ?o\n" + "WHERE {\n" + "    \n" + "}\n" + "LIMIT 0";
-    ResultSet resultSet = QueryOperation.execQuery(graph, allViolations);
+    ResultSet resultSet = QueryOperation.execQuery(dataset, allViolations);
     ResultSetRewindable copy = ResultSetFactory.copyResults(resultSet);
     Map<File, Tuple<ResultSetRewindable, OutputStream>> testResults = new HashMap<>();
     ByteArrayOutputStream testOut = new ByteArrayOutputStream();