Ranking managers with a SPARQL query

A SPARQL 1.2 query counts the people under each manager through the reporting line and ranks the top three.

org-chart-rq.rq · data/org-chart-rq.ttl · output · proof · check · playground


The question

Seven people work in a small organization. Everyone but the director reports to one manager. The people under a manager are those who report to them, directly or through others. Which three managers have the most people under them, how many, and who are they?


Historical background

The 2013 SPARQL 1.1 Recommendation added property paths and aggregates to the query language. A reporting hierarchy illustrates how the two additions fit together. A path finds people several reporting levels away, and an aggregate counts them for each manager. W3C, SPARQL 1.1 Query Language


What we tell eyemind

data/org-chart-rq.ttl holds the reporting lines:

:grace :name "Grace" ; :reportsTo :ada .
:alan :name "Alan" ; :reportsTo :grace .
:dennis :name "Dennis" ; :reportsTo :alan .

org-chart-rq.rq follows the reporting line one step or more with the property path :reportsTo+, groups the people by manager, and ranks the groups:

SELECT ?manager (COUNT(?person) AS ?people) (GROUP_CONCAT(?name; SEPARATOR=", ") AS ?names) WHERE {
  ?person :reportsTo+ ?manager ; :name ?name .
}
GROUP BY ?manager
ORDER BY DESC(?people) ?manager
LIMIT 3

What eyemind concludes

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

?manager	?people	?names
<https://example.org/org#ada>	6	"Alan, Barbara, Dennis, Grace, Ken, Linus"
<https://example.org/org#grace>	3	"Alan, Barbara, Dennis"
<https://example.org/org#alan>	1	"Dennis"

Ada has everyone else under her. Alan and Linus each have one person under them; ORDER BY’s second key, the manager, puts Alan first, and LIMIT keeps three rows.

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(['manager', 'people', 'names'], [[iri('https://example.org/org#ada'), 6, literal('Alan, Barbara, Dennis, Grace, Ken, Linus', datatype('http://www.w3.org/2001/XMLSchema#string'))], [iri('https://example.org/org#grace'), 3, literal('Alan, Barbara, Dennis', datatype('http://www.w3.org/2001/XMLSchema#string'))], [iri('https://example.org/org#alan'), 1, literal('Dennis', datatype('http://www.w3.org/2001/XMLSchema#string'))]])

How the query becomes a program

eyemind compiles the query to Logic Script over the dataset’s quads, rdf(S, P, O, G). The path :reportsTo+ becomes two forward rules for a predicate of its own, sparql_path_1(G, X, Y): one step, and a step more from a pair already found. The forward fixpoint makes the pairs distinct, as SPARQL defines +, and would end on a cycle in the reporting line too.

The solutions are collected once with their group key and the arguments of COUNT and GROUP_CONCAT. sparql_groups gathers them per manager; COUNT counts each group’s bound arguments, and GROUP_CONCAT joins the names. The rows are sorted by the ORDER BY keys, in SPARQL’s order of terms, and LIMIT keeps the first three. --program prints the whole program.


What the proof establishes

The saved report ends with verdict('checked'). The proof holds the listing of the path’s derived pairs, which the checker requires to be closed under the two path rules, and a call table for each step the answer rests on: the collection of solutions, the grouping, each aggregate and the sort. The checker expands each table one level against the compiled program and replays the collection, so no person and no manager is missing from the counts. The verdict establishes that the ranking follows from the query and the data.


Try it

From the repository root:

node bin/eyemind.js examples/org-chart-rq.rq examples/data/org-chart-rq.ttl
node bin/eyemind.js --proof examples/org-chart-rq.rq examples/data/org-chart-rq.ttl
node bin/eyemind.js --check-proof examples/proof/org-chart-rq.js examples/org-chart-rq.rq examples/data/org-chart-rq.ttl

Add --to tsv, --to csv or --to sparql-xml for the other result formats.