axiothOntology Hub

了解什么是本体 / 第三部分:查询 · 第 13 课,共 24 课

暂无所选语言的译文,当前显示English内容。

Count, follow and build: aggregates, property paths and CONSTRUCT

Count how many, follow several arrows at once, and get the answer as a graph instead of a table.

请先阅读: 3. Classes, individuals and properties

The queries so far found matching rows and showed them as they were. This lesson groups the rows and counts them, follows several arrows in one step, and turns results into new triples.

Counting: COUNT and GROUP BY

COUNT counts the rows found. A function that summarises many rows as one value like this is called an aggregate function. A computed value needs a variable name given with AS before it appears in the result.

PREFIX ex: <https://example.org/>
SELECT (COUNT(?pet) AS ?n) WHERE {
  ?person ex:keeps ?pet .
}

The pattern finds three rows and the answer is one. All the rows found form a single group, summarised as one count.

To count per person, group the rows with GROUP BY. Rows for the same person form one group, and an aggregate function is computed once per group.

SELECT ?person (COUNT(?pet) AS ?n) WHERE {
GROUP BY ?person

Rows with the same ?person were grouped, and each group gives one row. ?person is named in GROUP BY, so it can appear in the SELECT as it is.

A condition on a group uses HAVING, not FILTER. FILTER applies to rows before grouping, HAVING to the results after it.

HAVING (COUNT(?pet) > 1)

A count is known only after grouping, so this condition cannot be a FILTER. HAVING is checked once per group.

FunctionWhat it does
COUNT(?x), COUNT(*)How many have a value, how many rows
COUNT(DISTINCT ?x)How many different values
SUM, AVGTotal, average
MIN, MAXSmallest value, largest value
GROUP_CONCATJoins the values in a group into one string
SAMPLEAny one value from the group

Following arrows in a row: property paths

Asking "what kinds of animal does Minji keep" means crossing two arrows. You can write two lines joined by a variable in the middle, or put a path in the predicate position and finish in one line.

NotationMeaningExample
p/qFollow p, then qex:keeps/a
p|qp or qrdfs:label|skos:prefLabel
^pFollow p backwards^ex:keeps (who keeps it)
p*Follow p zero or more timesrdfs:subClassOf*
p+Follow p one or more timesex:partOf+
p?Follow p zero times or oncerdfs:subClassOf?
SELECT ?person ?kind WHERE {
  ?person ex:keeps/a ?kind .

Follow ex:keeps, then a (the type). The animal in the middle was not given a variable, so it does not appear in the result.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?pet WHERE {
  ?pet a/rdfs:subClassOf* ex:Animal .

Nowhere does the file say "Nabi is an animal", and nothing was inferred. The query starts at the type and follows rdfs:subClassOf zero or more times until it reaches ex:Animal. Notice that no dashed arrow appeared in the graph.

a/rdfs:subClassOf* is the path seen most often in practice. Even on a store with reasoning turned off it finds "everything belonging to any kind of animal". Unlike inference, it does not create new facts and keep them. It only follows arrows inside this one query.

Getting a graph back: CONSTRUCT

The answer to a SELECT is a table. CONSTRUCT fills a template for each row found, producing new triples, and returns them as a graph. It is used to move data written in one vocabulary into another, or to cut the needed part out of a large graph.

CONSTRUCT {
  ?pet ex:keptBy ?person .
WHERE {
  ?person ex:keeps ?pet .
}

For each row found, the template above is filled in to make one triple. The answer is three ex:keptBy statements pointing the other way. The original graph is unchanged, and the returned graph can be stored and used separately.

The four query forms

FormAnswerUsed for
SELECTA table of variables and valuesMost lookups
ASKTrue or falseChecking that a condition holds, tests
CONSTRUCTA newly built graphConversion, extracting a subgraph
DESCRIBEA graph describing a thingWhat comes back is up to the store
ASK {
  ex:junho ex:keeps/a ex:Dog .

This asks whether anything Junho keeps is a dog. Nothing matches, so the answer is false. Change ex:junho to ex:minji and it becomes true.

常见误解

The * in a property path is the same as inference.
A path follows arrows only while this query looks for its answer. Inference produces new triples that other queries and rules see as well.
COUNT(?x) counts rows.
It counts the rows where ?x has a value. Rows left blank by an OPTIONAL are not counted. COUNT(*) counts rows.
With GROUP BY, any variable can still go in the SELECT.
Only the variables named in GROUP BY and the results of aggregate functions can. A variable with several values in one group has no single value to show.

Aggregates group rows and summarise them, paths follow several arrows in one line, and CONSTRUCT turns the answer back into a graph.