Package org.obolibrary.robot
Class QueryOperation
- java.lang.Object
-
- org.obolibrary.robot.QueryOperation
-
public class QueryOperation extends Object
Query an ontology using SPARQL.- Author:
- James A. Overton
-
-
Constructor Summary
Constructors Constructor Description QueryOperation()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static org.semanticweb.owlapi.model.OWLOntology
convertModel(org.apache.jena.rdf.model.Model model)
Deprecated.static org.semanticweb.owlapi.model.OWLOntology
convertModel(org.apache.jena.rdf.model.Model model, IOHelper ioHelper, String catalogPath)
Given a Model, an IOHelper, and a path to an XML catalog, convert the model to an OWLOntology object.static org.semanticweb.owlapi.model.OWLOntology
convertModel(org.apache.jena.rdf.model.Model model, IOHelper ioHelper, String catalogPath, boolean useTemporaryFile)
Given a Model, an IOHelper, and a path to an XML catalog, convert the model to an OWLOntology object.static int
countResults(org.apache.jena.query.ResultSet results)
Count results.static org.apache.jena.query.Dataset
createEmptyDataset()
static org.apache.jena.rdf.model.Model
execConstruct(org.apache.jena.query.Dataset dataset, String query)
Execute a SPARQL CONSTRUCT query on a dataset and return a model.static org.apache.jena.rdf.model.Model
execConstruct(org.apache.jena.sparql.core.DatasetGraph dsg, String query)
Deprecated.useexecConstruct(Dataset, String)
instead.static org.apache.jena.query.ResultSet
execQuery(org.apache.jena.query.Dataset dataset, String query)
Execute a SPARQL SELECT query on a dataset and return a result set.static org.apache.jena.query.ResultSet
execQuery(org.apache.jena.sparql.core.DatasetGraph dsg, String query)
Deprecated.useexecQuery(Dataset, String)
instead.static void
execUpdate(org.apache.jena.rdf.model.Model model, String updateString)
Given a Model and a SPARQL update (as string), update the model.static boolean
execVerify(Map<File,Tuple<org.apache.jena.query.ResultSetRewindable,OutputStream>> queriesResults)
Deprecated.previously used as test method.static boolean
execVerify(org.apache.jena.query.Dataset dataset, String ruleName, String query)
Given a dataset to query, a rule name, and the query string, execute the query over the dataset.static boolean
execVerify(org.apache.jena.sparql.core.DatasetGraph dsg, String ruleName, String query)
Deprecated.useexecVerify(Dataset, String, String)
insteadstatic String
getDefaultFormatName(String query)
Given a SPARQL query, return its default file format as a string.static String
getDefaultFormatName(org.apache.jena.query.Query query)
Given a SPARQL query, return its default file format as a string.static org.apache.jena.riot.Lang
getFormatLang(String formatName)
Convert a format name string to a language code.static String
getQueryTypeName(String queryString)
Given a SPARQL query, return its type as a string: ASK, CONSTRUCT, DESCRIBE, or SELECT.static boolean
hasResult(org.apache.jena.query.ResultSet result)
If a result set has results, return true, otherwise false.static boolean
hasResult(org.apache.jena.rdf.model.Model result)
If a model has statements, return true, otherwise false.static org.apache.jena.sparql.core.DatasetGraph
loadOntology(org.semanticweb.owlapi.model.OWLOntology ontology)
Deprecated.useloadOntologyAsDataset(OWLOntology)
instead.static org.apache.jena.query.Dataset
loadOntologyAsDataset(org.semanticweb.owlapi.model.OWLOntology ontology)
Given an ontology, return a dataset containing that ontology as the only graph.static org.apache.jena.query.Dataset
loadOntologyAsDataset(org.semanticweb.owlapi.model.OWLOntology ontology, boolean useGraphs)
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.static org.apache.jena.rdf.model.Model
loadOntologyAsModel(org.semanticweb.owlapi.model.OWLOntology ontology)
Given an OWLOntology, return the Model representation of the axioms.static org.apache.jena.query.Dataset
loadTriplesAsDataset(String inputPath, String tdbDir)
Deprecated.static boolean
maybeWriteResult(org.apache.jena.query.ResultSet result, String formatName, OutputStream output)
If a result set has results, write to the output stream and return true.static boolean
maybeWriteResult(org.apache.jena.rdf.model.Model result, String formatName, OutputStream output)
If a model has statements, write to the output stream and return true.static void
runConstruct(org.apache.jena.query.Dataset dataset, String query, File output, org.apache.jena.riot.Lang outputFormat)
Run a CONSTRUCT query and write the result to a file.static void
runQuery(org.apache.jena.query.Dataset dataset, String query, File output, org.apache.jena.riot.Lang outputFormat)
Run a SELECT query on a dataset and write the result to a file.static void
runQuery(org.apache.jena.sparql.core.DatasetGraph dsg, String query, File output, org.apache.jena.riot.Lang outputFormat)
Deprecated.userunQuery(Dataset, String, File, Lang)
instead.static boolean
runSparqlQuery(org.apache.jena.query.Dataset dataset, String queryString, String formatName, OutputStream output)
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.static boolean
runSparqlQuery(org.apache.jena.query.QueryExecution qExec, String formatName, OutputStream output)
Given a query execution, a format name, and an output stream, run the query and write to output.static boolean
runSparqlQuery(org.apache.jena.sparql.core.DatasetGraph dsg, String query, String formatName, OutputStream output)
Deprecated.static void
runUpdate(org.apache.jena.rdf.model.Model model, String updateString, File output, org.apache.jena.riot.Lang outputFormat)
Given a Model, a SPARQL update string, an output stream, and the output format, update the model and write it to the output.static boolean
runVerify(org.apache.jena.query.Dataset dataset, String ruleName, String query, Path outputPath, org.apache.jena.riot.Lang outputFormat)
Run a SELECT query over the union of named graphs in a dataset and write the result to a file.static boolean
runVerify(org.apache.jena.sparql.core.DatasetGraph dsg, String ruleName, String query, Path outputPath, org.apache.jena.riot.Lang outputFormat)
Deprecated.userunVerify(Dataset, String, String, Path, Lang)
instead.static void
writeResult(boolean result, OutputStream output)
Write a boolean result to an output stream.static void
writeResult(org.apache.jena.query.ResultSet resultSet, String formatName, OutputStream output)
Write a result set to an output stream.static void
writeResult(org.apache.jena.query.ResultSet resultSet, org.apache.jena.riot.Lang format, OutputStream output)
Write a result set to an output stream.static void
writeResult(org.apache.jena.rdf.model.Model model, String formatName, OutputStream output)
Write a model to an output stream.static void
writeResult(org.apache.jena.rdf.model.Model model, org.apache.jena.riot.Lang format, OutputStream output)
Write a model to an output stream.
-
-
-
Method Detail
-
loadOntology
@Deprecated public static org.apache.jena.sparql.core.DatasetGraph loadOntology(org.semanticweb.owlapi.model.OWLOntology ontology) throws org.semanticweb.owlapi.model.OWLOntologyStorageException
Deprecated.useloadOntologyAsDataset(OWLOntology)
instead.Load an ontology into a DatasetGraph. The ontology is not changed.- Parameters:
ontology
- The ontology to load into the graph- Returns:
- A new DatasetGraph with the ontology loaded into the default graph
- Throws:
org.semanticweb.owlapi.model.OWLOntologyStorageException
- rarely
-
loadOntologyAsDataset
public static org.apache.jena.query.Dataset loadOntologyAsDataset(org.semanticweb.owlapi.model.OWLOntology ontology) throws org.semanticweb.owlapi.model.OWLOntologyStorageException
Given an ontology, return a dataset containing that ontology as the only graph.- Parameters:
ontology
- ontology to query- Returns:
- dataset to query
- Throws:
org.semanticweb.owlapi.model.OWLOntologyStorageException
- on issue converting ontology Jena model
-
loadOntologyAsDataset
public static org.apache.jena.query.Dataset loadOntologyAsDataset(org.semanticweb.owlapi.model.OWLOntology ontology, boolean useGraphs) throws org.semanticweb.owlapi.model.OWLOntologyStorageException
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.- Parameters:
ontology
- ontology to queryuseGraphs
- if true, load imports as separate graphs- Returns:
- dataset to query
- Throws:
org.semanticweb.owlapi.model.OWLOntologyStorageException
- on issue converting ontology Jena model
-
loadOntologyAsModel
public static org.apache.jena.rdf.model.Model loadOntologyAsModel(org.semanticweb.owlapi.model.OWLOntology ontology) throws org.semanticweb.owlapi.model.OWLOntologyStorageException
Given an OWLOntology, return the Model representation of the axioms.- Parameters:
ontology
- OWLOntology to convert to Model- Returns:
- Model of axioms (imports ignored)
- Throws:
org.semanticweb.owlapi.model.OWLOntologyStorageException
- on issue rendering ontology to triples
-
loadTriplesAsDataset
@Deprecated public static org.apache.jena.query.Dataset loadTriplesAsDataset(String inputPath, String tdbDir)
Deprecated.Given a path to an RDF/XML or TTL file and a RDF language, load the file as the default model of a TDB dataset backed by a directory to improve processing time. Return the new dataset.WARNING - this creates a directory at given tdbDir location!
- Parameters:
inputPath
- input path of RDF/XML or TTL filetdbDir
- location to put TDB mappings- Returns:
- Dataset instantiated with triples
-
convertModel
@Deprecated public static org.semanticweb.owlapi.model.OWLOntology convertModel(org.apache.jena.rdf.model.Model model) throws org.semanticweb.owlapi.model.OWLOntologyCreationException
Deprecated.Given a Model, return the OWLOntology representation of the axioms.- Parameters:
model
- Model to convert to OWLOntology- Returns:
- OWLOntology of axioms
- Throws:
org.semanticweb.owlapi.model.OWLOntologyCreationException
- on issue loading ontology from byte array
-
convertModel
public static org.semanticweb.owlapi.model.OWLOntology convertModel(org.apache.jena.rdf.model.Model model, IOHelper ioHelper, String catalogPath) throws IOException
Given a Model, an IOHelper, and a path to an XML catalog, convert the model to an OWLOntology object.- Parameters:
model
- Model to convert to OWLOntologyioHelper
- IOHelper to load ontologycatalogPath
- String path to XML catalog- Returns:
- OWLOntology object version of model
- Throws:
IOException
- on issue loading ontology
-
convertModel
public static org.semanticweb.owlapi.model.OWLOntology convertModel(org.apache.jena.rdf.model.Model model, IOHelper ioHelper, String catalogPath, boolean useTemporaryFile) throws IOException
Given a Model, an IOHelper, and a path to an XML catalog, convert the model to an OWLOntology object.- Parameters:
model
- Model to convert to OWLOntologyioHelper
- IOHelper to load ontologycatalogPath
- String path to XML cataloguseTemporaryFile
- whether to use a temporary file to store intermediate results or to keep them in memory.- Returns:
- OWLOntology object version of model
- Throws:
IOException
- on issue loading ontology
-
countResults
public static int countResults(org.apache.jena.query.ResultSet results)
Count results.- Parameters:
results
- The result set to count.- Returns:
- the size of the result set
-
createEmptyDataset
public static org.apache.jena.query.Dataset createEmptyDataset()
-
execConstruct
@Deprecated public static org.apache.jena.rdf.model.Model execConstruct(org.apache.jena.sparql.core.DatasetGraph dsg, String query)
Deprecated.useexecConstruct(Dataset, String)
instead.Execute a SPARQL CONSTRUCT query on a graph and return the model.- Parameters:
dsg
- the graph to construct inquery
- the SPARQL construct query string- Returns:
- the result Model
-
execConstruct
public static org.apache.jena.rdf.model.Model execConstruct(org.apache.jena.query.Dataset dataset, String query)
Execute a SPARQL CONSTRUCT query on a dataset and return a model.- Parameters:
dataset
- the Dataset to construct inquery
- the SPARQL construct query string- Returns:
- the result Model
-
execQuery
@Deprecated public static org.apache.jena.query.ResultSet execQuery(org.apache.jena.sparql.core.DatasetGraph dsg, String query) throws IOException
Deprecated.useexecQuery(Dataset, String)
instead.Execute a SPARQL SELECT query on a graph and return a result set.- Parameters:
dsg
- the graph to queryquery
- the SPARQL query string- Returns:
- the result set
- Throws:
IOException
- on query parse error
-
execQuery
public static org.apache.jena.query.ResultSet execQuery(org.apache.jena.query.Dataset dataset, String query) throws IOException
Execute a SPARQL SELECT query on a dataset and return a result set.- Parameters:
dataset
- the Dataset to query overquery
- the SPARQL query string- Returns:
- the result set
- Throws:
IOException
- on query parse error
-
execUpdate
public static void execUpdate(org.apache.jena.rdf.model.Model model, String updateString)
Given a Model and a SPARQL update (as string), update the model.- Parameters:
model
- Model to updateupdateString
- SPARQL update
-
execVerify
@Deprecated public static boolean execVerify(Map<File,Tuple<org.apache.jena.query.ResultSetRewindable,OutputStream>> queriesResults) throws IOException
Deprecated.previously used as test method.Execute a verification. Writes to STDERR.- Parameters:
queriesResults
- a map from files to query results and output streams- Returns:
- true if there are any violations
- Throws:
IOException
- on file issues
-
execVerify
@Deprecated public static boolean execVerify(org.apache.jena.sparql.core.DatasetGraph dsg, String ruleName, String query) throws IOException
Deprecated.useexecVerify(Dataset, String, String)
insteadExecute a SPARQL query and return true if there are any results, false otherwise.- Parameters:
dsg
- the graph to query overruleName
- name of rule to verifyquery
- the SPARQL query string- Returns:
- true if the are results, false otherwise
- Throws:
IOException
- on query parse error
-
execVerify
public static boolean execVerify(org.apache.jena.query.Dataset dataset, String ruleName, String query) throws IOException
Given a dataset to query, a rule name, and the query string, execute the query over the dataset.- Parameters:
dataset
- Dataset to queryruleName
- name of rule to verifyquery
- the SPARQL query string- Returns:
- true if there are results, false otherwise
- Throws:
IOException
- on query parse error
-
getDefaultFormatName
public static String getDefaultFormatName(String query)
Given a SPARQL query, return its default file format as a string.- Parameters:
query
- the SPARQL query string- Returns:
- the format name
-
getDefaultFormatName
public static String getDefaultFormatName(org.apache.jena.query.Query query) throws IllegalArgumentException
Given a SPARQL query, return its default file format as a string.- Parameters:
query
- the SPARQL query- Returns:
- the format name
- Throws:
IllegalArgumentException
- on bad query
-
getFormatLang
public static org.apache.jena.riot.Lang getFormatLang(String formatName)
Convert a format name string to a language code.- Parameters:
formatName
- the format name as a string- Returns:
- the format language code or null
-
getQueryTypeName
public static String getQueryTypeName(String queryString)
Given a SPARQL query, return its type as a string: ASK, CONSTRUCT, DESCRIBE, or SELECT.- Parameters:
queryString
- the SPARQL query string- Returns:
- the query type string
- Throws:
IllegalArgumentException
- on bad query
-
hasResult
public static boolean hasResult(org.apache.jena.query.ResultSet result)
If a result set has results, return true, otherwise false.- Parameters:
result
- the results to write- Returns:
- true if there are results, false otherwise
-
hasResult
public static boolean hasResult(org.apache.jena.rdf.model.Model result)
If a model has statements, return true, otherwise false.- Parameters:
result
- the results to write- Returns:
- true if there are statements, false otherwise
-
maybeWriteResult
public static boolean maybeWriteResult(org.apache.jena.query.ResultSet result, String formatName, OutputStream output)
If a result set has results, write to the output stream and return true. Otherwise return false.- Parameters:
result
- the results to writeformatName
- the name of the language to write inoutput
- the output stream to write to- Returns:
- true if there were results, false otherwise
-
maybeWriteResult
public static boolean maybeWriteResult(org.apache.jena.rdf.model.Model result, String formatName, OutputStream output)
If a model has statements, write to the output stream and return true. Otherwise return false.- Parameters:
result
- the Model to writeformatName
- the name of the language to write inoutput
- the output stream to write to- Returns:
- true if there were statements, false otherwise
-
runConstruct
public static void runConstruct(org.apache.jena.query.Dataset dataset, String query, File output, org.apache.jena.riot.Lang outputFormat) throws FileNotFoundException
Run a CONSTRUCT query and write the result to a file.- Parameters:
dataset
- the Dataset to construct in.query
- The SPARQL construct query string.output
- The file to write to.outputFormat
- The file format.- Throws:
FileNotFoundException
- if output file is not found
-
runQuery
@Deprecated public static void runQuery(org.apache.jena.sparql.core.DatasetGraph dsg, String query, File output, org.apache.jena.riot.Lang outputFormat) throws IOException
Deprecated.userunQuery(Dataset, String, File, Lang)
instead.Run a SELECT query on a graph and write the results to a file.- Parameters:
dsg
- the graph to queryquery
- The SPARQL query string.output
- The file to write to.outputFormat
- The file format.- Throws:
IOException
- if output file is not found or query cannot be parsed
-
runQuery
public static void runQuery(org.apache.jena.query.Dataset dataset, String query, File output, org.apache.jena.riot.Lang outputFormat) throws IOException
Run a SELECT query on a dataset and write the result to a file.- Parameters:
dataset
- The dataset to query over.query
- The SPARQL query string.output
- The file to write to.outputFormat
- The file format.- Throws:
IOException
- if output file is not found or query cannot be parsed
-
runSparqlQuery
@Deprecated public static boolean runSparqlQuery(org.apache.jena.sparql.core.DatasetGraph dsg, String query, String formatName, OutputStream output) throws IOException
Deprecated.Run a SPARQL query and return true if there were results, false otherwise.- Parameters:
dsg
- the graph to query overquery
- the SPARQL query stringformatName
- the name of the output formatoutput
- the OutputStream to write to- Returns:
- true if results, false if otherwise
- Throws:
IOException
- on issue parsing query
-
runSparqlQuery
public static boolean runSparqlQuery(org.apache.jena.query.Dataset dataset, String queryString, String formatName, OutputStream output) throws IOException
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.- Parameters:
dataset
- Dataset to query overqueryString
- query to runformatName
- format of outputoutput
- output stream to write to- Returns:
- true if successful
- Throws:
IOException
- on issue parsing query
-
runSparqlQuery
public static boolean runSparqlQuery(org.apache.jena.query.QueryExecution qExec, String formatName, OutputStream output)
Given a query execution, a format name, and an output stream, run the query and write to output.- Parameters:
qExec
- QueryExecution to runformatName
- format of outputoutput
- output stream to write to- Returns:
- true if there are results
-
runUpdate
public static void runUpdate(org.apache.jena.rdf.model.Model model, String updateString, File output, org.apache.jena.riot.Lang outputFormat) throws FileNotFoundException
Given a Model, a SPARQL update string, an output stream, and the output format, update the model and write it to the output.- Parameters:
model
- Model to updateupdateString
- SPARQL updateoutput
- output stream to write tooutputFormat
- the file format- Throws:
FileNotFoundException
- if output file cannot be found
-
runVerify
@Deprecated public static boolean runVerify(org.apache.jena.sparql.core.DatasetGraph dsg, String ruleName, String query, Path outputPath, org.apache.jena.riot.Lang outputFormat) throws IOException
Deprecated.userunVerify(Dataset, String, String, Path, Lang)
instead.Run a SELECT query over the graph and write the result to a file. Prints violations to STDERR.- Parameters:
dsg
- The graph to query over.ruleName
- name of the rulequery
- The SPARQL query string.outputPath
- The file path to write to, if there are resultsoutputFormat
- The file format.- Returns:
- true if the are results (so file is written), false otherwise
- Throws:
IOException
- if output file is not found or query cannot be parsed
-
runVerify
public static boolean runVerify(org.apache.jena.query.Dataset dataset, String ruleName, String query, Path outputPath, org.apache.jena.riot.Lang outputFormat) throws IOException
Run a SELECT query over the union of named graphs in a dataset and write the result to a file. Prints violations to STDERR.- Parameters:
dataset
- The dataset to query over.ruleName
- The name of the rule to verify.query
- The SPARQL query string.outputPath
- The file path to write to, if there are resultsoutputFormat
- The file format.- Returns:
- true if the are results (so file is written), false otherwise
- Throws:
IOException
- if output file is not found or query cannot be parsed
-
writeResult
public static void writeResult(boolean result, OutputStream output)
Write a boolean result to an output stream.- Parameters:
result
- the boolean to writeoutput
- the output stream to write to
-
writeResult
public static void writeResult(org.apache.jena.query.ResultSet resultSet, String formatName, OutputStream output)
Write a result set to an output stream.- Parameters:
resultSet
- the results to writeformatName
- the name of the language to write inoutput
- the output stream to write to
-
writeResult
public static void writeResult(org.apache.jena.query.ResultSet resultSet, org.apache.jena.riot.Lang format, OutputStream output)
Write a result set to an output stream.- Parameters:
resultSet
- the results to writeformat
- the language to write in (if null, CSV)output
- the output stream to write to
-
writeResult
public static void writeResult(org.apache.jena.rdf.model.Model model, String formatName, OutputStream output)
Write a model to an output stream.- Parameters:
model
- the Model to writeformatName
- the name of the language to write inoutput
- the output stream to write to
-
writeResult
public static void writeResult(org.apache.jena.rdf.model.Model model, org.apache.jena.riot.Lang format, OutputStream output)
Write a model to an output stream.- Parameters:
model
- the Model to writeformat
- the language to write in (if null, TTL)output
- the output stream to write to
-
-