Counting species with a query over OWL 2 RL

A SPARQL 1.2 query counts the animals of each class on the graph OWL 2 RL entails from a small taxonomy.

species-rq.rq · data/species-rq.ttl · output · proof · check · playground


The question

A zoo records each animal in its most specific class, and which class lies under which. How many animals belong to each class of the taxonomy, counting an animal in every class above its own?


What we tell eyemind

data/species-rq.ttl holds the taxonomy and the animals:

:Mammal rdfs:subClassOf :Animal .
:Dog rdfs:subClassOf :Mammal .
:rex a :Dog .
:nemo a :Animal .

species-rq.rq counts the animals of each class under :Animal:

SELECT ?class (COUNT(?animal) AS ?animals) WHERE {
  ?class rdfs:subClassOf :Animal .
  ?animal a ?class .
}
GROUP BY ?class
ORDER BY DESC(?animals) ?class

The query asks only for classes declared under :Animal and animals typed with them. --owl2rl adds the OWL 2 RL rules, including RDFS, and the query runs on their working graph.


What eyemind concludes

The command writes SPARQL JSON results; as TSV, with --to tsv:

?class	?animals
<https://example.org/zoo#Animal>	5
<https://example.org/zoo#Mammal>	3
<https://example.org/zoo#Dog>	2
<https://example.org/zoo#Bird>	1
<https://example.org/zoo#Cat>	1

OWL 2 RL makes rdfs:subClassOf transitive and reflexive, so :Dog is under :Animal and :Animal under itself, and types each animal with every class above its own. Without --owl2rl, the same query on the data alone finds only :Bird, the one class declared directly under :Animal with an animal typed by it.

The check report records the answer as one JavaScript expression:

// Checked answers: these follow from the input program and are every answer of its questions.
sparql_select(['class', 'animals'], [[iri('https://example.org/zoo#Animal'), 5], [iri('https://example.org/zoo#Mammal'), 3], [iri('https://example.org/zoo#Dog'), 2], [iri('https://example.org/zoo#Bird'), 1], [iri('https://example.org/zoo#Cat'), 1]])

How the query meets the profile

eyemind compiles the data and --owl2rl to the profile’s Logic Script program, whose working graph rdf(S, P, O, G) holds the data and every entailment, and appends the query compiled to rules over that graph. The SPARQL functions the query and the profile both use are stated once. The query’s solutions are collected after the profile’s rules reach their fixpoint, so the counts include every entailed class membership.


What the proof establishes

The saved report ends with verdict('checked'). One proof covers both parts: the steps of the OWL 2 RL entailments, the listings of the entailed triples, which the checker requires to be closed under the profile’s rules, and the call tables of the query’s collection, grouping and counts. The verdict establishes that the counts follow from the taxonomy, the OWL 2 RL rules and the query, with no animal or class left out.


Try it

From the repository root:

node bin/eyemind.js --owl2rl examples/species-rq.rq examples/data/species-rq.ttl
node bin/eyemind.js --owl2rl --proof examples/species-rq.rq examples/data/species-rq.ttl
node bin/eyemind.js --owl2rl --check-proof examples/proof/species-rq.js examples/species-rq.rq examples/data/species-rq.ttl

Leave out --owl2rl to query the data alone.