The SPARQL View can be used to run queries on the current model. SPARQL is a W3C standard recommendation, and supported by the Jena framework. The SPARQL View serves both as a query editor and to display the query results. In the example below, the query asks for all bindings for the variable dest, to find all destinations that have a hotel with three star rating. Note that in contrast to the rest of Composer, resources from the default namespace need to be entered starting with a ':'
The following types of SPARQL queries are currently supported in Composer.
CONSTRUCT queries are a very powerful mechanism for various purposes. In particular, they can be used to define mappings between classes in the same (or separate) ontology. The result of a CONSTRUCT query is a set of triples, and these triples can be interpreted as inferred triples, using the SHACL or SPIN inference engines.
Note that the query editor of the SPARQL View remembers the most recently used query for the next time that it is being opened. If you right-click on the editor's text area, you can find menu actions to reset the text to some SELECT or CONSTRUCT default queries.
Query Processing and Inferencing
In its default configuration, SPARQL queries will only operate on the
asserted statements and the currently inferred statements in your model.
No additional reasoning or inferencing is performed. This means, for
example, that queries for the rdfs:subClassOf
relationship
will only operate on directly adjacent classes, but not on transitive
neighbors. In order to execute queries on a model with inferencing
turned on, check the "Use Currently Configured Inferences"
button. In this mode, the queries will execute over the inference model
defined for the current file. For example, you can execute SPARQL over
a SHACL inference model, so that the SPARQL engine will see additional
triples. Note that in most cases, queries on an inference graph will
execute significantly slower than on the default graph only, but they
will also return more results.
In many cases, users only need simple types of inferences such as
getting the transitive superclasses and superproperties. The Jena
SPARQL engine provides a mechanism to run "local" inferences while
the query is executing, using the * notation. For example, the
pattern ?subClass rdfs:subClassOf* owl:Thing
will
bind ?subClass with all subclasses of owl:Thing
, including
the sub-classes of the sub-classes.
Query Processing on the base model
The option Run on base model only can have a dramatic
impact on the query performance when the underlying triple store provides
native SPARQL optimizations. If activated, the query will only operate on the base
model and therefore not see any system triples, imported triples or
inferred triples (except those natively inferred by the databases).
Neither will the uncommitted changes be visible, because they are only
stored in an in-memory graph by TopBraid but not in the database yet.
Also note that the expressiveness of a native SPARQL engine may be different than
what is offered by TopBraid's built-in Jena engine. For example,
OPTIONAL
patterns may not be supported on all native engines.
Also, SPARQL extension functions will not be visible.
Continuous Execution of Queries
When you activate the "link" button in the SPARQL view then the variable
?this will be treated differently from other variables:
The system will automatically bind the variable ?this
with the
currently selected resource. Moreover, if the query contains this
variable, then the query will be automatically executed whenever the
selected resource changes. This allows you to install a convenient viewer
that repeatedly runs arbitrary queries. For example, enter the query
SELECT ?instance ?label WHERE { ?instance rdf:type ?this ; rdfs:label ?label }
and the SPARQL results window will automatically show names and labels of all instances of the currently selected class.
Query Libraries
In many cases, it makes sense to systematically organize queries so that they become more readily available or can be executed together. This is particularly interesting with CONSTRUCT queries for ontology mapping and inferencing. If you have two ontologies and want to convert resources from one ontology into classes from the other ontology, then you can use a collection of CONSTRUCT queries to define the transformation rules.
In order to use the Query Library feature, your model must import
the SPIN ontology which defines a
property spin:query
.
You will find the file spin.ttl
in the SPIN folder in the
TopBraid project in your workspace.
Drag this file into the Imports View
to get the correct property declaration. Once the spin:query
property is defined, you can edit values for it and its sub-properties
on the resource forms.
The Query Library tab of the SPARQL View displays all values
of spin:query
and sub-properties thereof in your model.
Pressing the execute query buttons will apply to all selected queries.
Double-clicking the query in the library's list will move to the
resource where the query is defined.
Visualizing Queries
The Class Diagrams panel on the resource editor can be used to visualize SPARQL queries in the context of the properties and classes that are mentioned in the query. If the user expands a class node that is connected to a query, then the query will be displayed as a yellow node as shown in the screenshot below.
SPARQL Functions
TopBraid provides several functions to be used in SPARQL queries. In addition to the built-in functions from Jena and the SPARQLMotion Function Library, you can define your own SPARQL functions without any programming using SPIN or SPARQLMotion. The "tops" property functions are described here.
There are several mechanisms for extending SPARQL by means of user-defined functions and property functions. See Adding SPARQL Functions to TopBraid.
SPARQL Debugger and Profiler
TopBraid Composer includes a sophisticated SPARQL Debugger and Profiler that can be used to visualize a query at execution time, to see intermediate variable bindings and analyze performance. This feature can be used by pressing the in the tool bar of the SPARQL view. This will open the Debugger for the currently edited SPARQL query.