Skip to content

SPARQL

ModelDesk can run SPARQL queries against a model. Where a path expression navigates from element to element, a SPARQL query matches a pattern across the whole model at once — so it can express things a path cannot: “concepts with no broader concept”, “everything reachable through any number of skos:broader steps”, “attributes whose name matches a regular expression”, “concepts with more than three notations”.

Most users never write one. ModelDesk uses SPARQL internally in the mappings that power transformations, and there you only see the results. You read or write one yourself in four situations:

  • The Query page. On a model whose standard supports querying, owned by an organization that has SPARQL enabled, Query in the left navigation answers a whole query as a table of results — see Query a model. There a query is not a selector: several variables, ORDER BY, LIMIT, aggregation and ASK are all what the page is for, so the extra rules for selectors below do not apply there.
  • Mapping rules and links. In the rule editor on the Mappings page, and on a link’s card in the split view, a query box can hold SPARQL. See Choose SPARQL or Core DSL. The rule’s scope and a link’s base elements are selectors. A field’s query is not: it answers the field’s value for the element in $self, as in SELECT ?v WHERE { $self skos:prefLabel ?v }.
  • Feature-specific builders. A few capabilities that ModelDesk can enable for your organization let you write a query to select elements.
  • Working on ModelDesk configuration. If you author or review mappings together with ModelDesk, this page is the reference for the queries in those files.

If none of these applies to you, you can skip this page.

A SPARQL query does not see ModelDesk’s internal structure. It sees the model rendered as RDF, using the vocabulary of the standard the model is written in — so a MIM model is queried with MIM’s own IRIs, an OWL ontology with OWL’s. This is the same rendering an RDF export produces, so a query that works against your exported file works here.

One model is one graph. There is no dataset of several named graphs to choose between, which is why the graph constructs below are out of scope.

SPARQL 1.1 Query, excluding the constructs listed under Not supported. In practice that means all of the following work:

Area Covered
Patterns basic graph patterns, OPTIONAL, UNION, MINUS, FILTER EXISTS / NOT EXISTS, subqueries
Paths sequence /, alternative |, inverse ^, and the closures *, +, ?
Values BIND, VALUES, blank-node patterns, collections
Filters comparison, arithmetic, regex, string / numeric / date functions, casts, IN, COALESCE
Aggregation COUNT, SUM, MIN, MAX, AVG, SAMPLE, GROUP_CONCAT, GROUP BY, HAVING
Answer sequence ORDER BY (including over expressions), DISTINCT, REDUCED, LIMIT, OFFSET
Query forms SELECT, ASK

Conformance is measured against the W3C SPARQL test suite on every change, and the result is published with the source. Two things are checked and must always hold: a query ModelDesk accepts is never answered wrongly, and the two places SPARQL runs — the server and the query editor in your browser — accept, reject and answer identically. A query the editor accepts is a query the server runs, and the other way round.

A variable belongs to the group { … } it is written in. A FILTER constrains its own group, and a BIND sees what is written before it in that same group — nothing from the group around it. This is SPARQL’s rule, not ModelDesk’s, and it surprises people often enough to be worth stating:

SELECT ?c WHERE {
?c skos:notation ?n .
{ FILTER(?n > 3) } # ?n is NOT the ?n above — it is unbound here
}

The inner group is its own group, and nothing in it binds ?n, so the comparison has no value to work with. An expression over an unbound variable is an error, and a FILTER that errors keeps nothing — this query answers with no rows at all. Remove the braces and it filters as you would expect.

The same rule decides what a BIND can read, and it is why an assignment sometimes leaves its variable empty rather than failing: BIND(?n + 1 AS ?m) where ?n is out of scope leaves ?m unbound and keeps every row. Use BOUND(?x) or COALESCE(?x, …) when you want to handle the unbound case yourself.

Two exceptions are worth knowing, because both read naturally and both are what SPARQL prescribes. A FILTER written directly inside an OPTIONAL may read the variables the required part binds — that is what makes ?c skos:notation ?n OPTIONAL { ?c skos:prefLabel ?l . FILTER(?n > 3) } work. And a variable no part of the query mentions is treated as a misspelling: ModelDesk rejects the query by name rather than silently answering nothing.

Two different things are on this list, and the distinction matters when you are deciding how to rewrite a query.

These will not be added. A query using one is rejected with a message saying so.

  • FROM and FROM NAMED. A query is answered over one model, rendered as a single graph, so there is no dataset to select from. The clause is refused rather than ignored — ignoring it would answer over exactly what the query asked to leave out.
  • Named-graph answers. GRAPH <g> { … } around a pattern is accepted and the graph name ignored, since there is only one graph. But a query whose answer depends on graph names is rejected, because the single-graph rendering has no honest answer to give: selecting the graph variable of a GRAPH ?g { … } block, or asking whether a graph exists at all with an empty one.
  • CONSTRUCT and DESCRIBE. These build a graph rather than a table of answers. Use an export to get RDF out of a model.
  • SPARQL Update (INSERT, DELETE, LOAD, …). Models are changed through ModelDesk, not through queries.
  • SERVICE. A query is answered over your model, never by calling out to another endpoint.

These are gaps rather than decisions, and each is rejected with a message naming the construct. They come from one place: a pattern is compiled into a flat set of conditions, and these shapes cannot be flattened.

  • A UNION or subquery that shares no bound variable with the pattern around it. An OPTIONAL, MINUS or FILTER EXISTS that shares none is fine — it is a question about the model as a whole, and is answered as one.
  • Two constructs in a row that may each leave the same variable unbound. A UNION branch that binds nothing for a variable is fine on its own — the answer simply leaves it unbound, and the pattern around it is matched the way SPARQL matches a missing value. What is refused is a second UNION joined on a variable an earlier one may already have left unbound, since neither side is certain to have a value.
  • An OPTIONAL reading a variable that only another OPTIONAL binds, across a group boundary. An OPTIONAL may bind a variable the pattern around it also binds: the two are matched the way SPARQL matches them, so a row where the optional found nothing is kept rather than dropped. What is still refused is the case where neither side is certain to have a value — a variable one OPTIONAL may leave unbound, read by another that is not directly joined with it.
  • MINUS and FILTER EXISTS whose body itself contains a UNION, a subquery, a property path or VALUES. A body of plain triple patterns, filters and further negations is fine.
  • FILTER EXISTS / NOT EXISTS whose body contains an OPTIONAL, or that reads a variable an OPTIONAL may leave unbound. A MINUS handles both: it is defined by asking whether the two sides agree wherever both have a value, and by whether they have any value in common at all, which is exactly what a missing value needs. FILTER EXISTS is defined differently — the current answer’s values are put into the pattern before it is matched — and there is nothing to put in where the answer has none, so it is refused rather than treated as if the two questions were the same one.
  • An OPTIONAL whose body itself contains VALUES or an aggregating subquery. Everything else may go in an optional’s body — a UNION, an ordinary subquery, a property path, a MINUS or a FILTER EXISTS / NOT EXISTS — and the row is still kept when the body as a whole finds nothing. These two are refused because of how they are answered rather than because of the nesting: inline data supplies rows, and a grouped subquery is answered as a query of its own. Lifting either out of the OPTIONAL and into the pattern around it is usually the same question, written where it can be answered.
  • BIND to a variable a graph pattern also binds. A computed value can be returned, but not matched against the model.

If you hit one of these, the usual fix is to bind the shared variable in every branch, or to lift the nested construct out of the negation and into the enclosing pattern.

A VALUES block over a variable an OPTIONAL may leave unbound is answered the way SPARQL answers it: a row whose value is missing takes the one the block supplies, rather than being dropped. The one case still refused is a group that also reads that variable — in a FILTER, say — because the block is joined last, after everything else in the group has already been matched.

Two more, for a different reason — both would have to range over every predicate the model uses, which a query is not given:

  • A variable in the predicate position (?s ?p ?o). Name the predicate, or list the alternatives with |.
  • A negated property set (!skos:broader), which is the same thing written the other way round.

A selector is a query used by a mapping rule to choose the source elements the rule runs over. It answers with a set of elements, and the mapping engine consumes that set directly — so a selector has to follow four rules the language itself does not impose. A query that breaks one of these is still a perfectly good SPARQL query elsewhere; it just cannot be a selector.

  1. Project exactly one variable. The selector names the source element, so SELECT ?c — not SELECT ?c ?n and not SELECT * over a pattern binding several variables.
  2. No LIMIT or OFFSET. A selector answers with a set, which has no first two. (OFFSET 0 is allowed: it asks for no slice at all.)
  3. No ORDER BY. A set carries no order, so an ordering would be silently discarded.
  4. No aggregation, and no subquery that has to be evaluated on its own — which means no GROUP BY, no aggregate in the projection, and no subquery carrying its own ORDER BY or LIMIT.

A selector nested inside another rule is given the element its parent rule matched, under the name ?self. It must navigate from there:

SELECT ?child WHERE { ?self skos:narrower ?child }

Mentioning ?self only in a FILTER or VALUES does not count as navigating from it, and is reported as an error rather than quietly answering nothing.

Concepts with no broader concept — a shape a path expression cannot state at all:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?c WHERE {
?c a skos:Concept
FILTER NOT EXISTS { ?c skos:broader ?parent }
}

Everything reachable through any number of skos:broader steps:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?ancestor WHERE { ?self skos:broader+ ?ancestor }

Concepts whose notation matches a pattern:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?c WHERE {
?c skos:notation ?n
FILTER(regex(str(?n), "^NL-", "i"))
}

How many notations each concept carries, most first — a query, not a selector, since it projects two variables and aggregates. This is the shape the Query page exists for:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?c (COUNT(?n) AS ?notations) WHERE {
?c skos:notation ?n
}
GROUP BY ?c
HAVING (COUNT(?n) > 1)
ORDER BY DESC(?notations)