Querying Knowledge Graphs


Wouter Beek (wouter@triply.cc)
Thomas de Groot (thomas.de.groot@triply.cc)


https://triply.cc

SPARQL Forms

4 SPARQL forms

ask
Graph → Y/N
construct
Graph → Graph
describe
IRI → Graph
select
Graph → Table

select

Graph → Table

select query

  • RDF data is stored in a graph.
  • A select query creates a tabular view by matching a pattern against the graph data.

Components of a select query

Projection
Specifies the columns of the table.
Pattern
Specifies how the cells of the table are filled.
Modifier
Specifies additional operations over the table.

select

Triple Patterns

1-1: Our first select query

Projection (columns)
select ?s ?p ?o
Pattern (graph match)
{ ?s ?p ?o. }
Modifier
limit 25

1-2: Table of Pokémon + image links

select ?pokemon ?image {
  ?pokemon <http://xmlns.com/foaf/0.1/depiction> ?image
}
limit 25
<http://xmlns.com/foaf/0.1/depiction>
Match specific arcs in the graph.
?pokemon and ?image
Use descriptive names for variables.

1-3: Abbreviated IRI notation

Abbreviated query notation
foaf:depiction
Abbreviated result notation
Example: id:flareon

1-4: Invert the projection

?image
The first column contains image links.
?pokemon
The second column contains Pokémon IDs.

1-5: Change the projection

?image
Only return the column for image links.
?pokemon
A hidden variable: one whose bindings are not returned.

1-6: The generic projection

select *
Return columns for all variables. Columns appear in unspecified order.

1-7: Introduce a variable

bind(VALUE as VARIABLE)
Add a column with values that are not matched in the graph.

1-8: HTML template

{{VARIABLE}}
Use ?VARIABLE in a template string.

1-9: Limit the number of rows

limit 250
Return at most 250 rows.

1-10: Skip a number of rows

offset 250
Skip the first 250 rows, returning the 251st through the 275th row.

Exercise: Write a SPARQL select query over your own data.

PLDN datasets

Summary

Construct Purpose Examples
Prefix Abbreviate syntax prefix ex: <https://example.com/>
Projection Select columns select ?x ?y
select *
Pattern Match cell values { ?s ?p ?o. }
{ ?s ex:p ?o. }
Binding Introduce new variables bind('Hi!' as ?widget)
Template Return HTML widgets bind('<img src="{{image}}">' as ?widget)
Limit Set a maximum number of rows. limit 10
Offset Skip a number of rows. offset 10

select

Graph Patterns

2-1: Graph Pattern: One Triple Pattern

Graph patterns contain zero or more Triple Patterns.

2-2: Graph Pattern: Two Triple Patterns

?pokemon
A shared variable connects two or more triple patterns.
. (dot)
Marks the end of a Triple Pattern.

2-3: Graph Pattern: Four Triple Patterns

2-4: Graph Pattern: Abbr. notation

; (semi-colon)
Repeat the previous subject term.
, (comma)
Repeat the previous subject and predicate terms.

2-5: Multiple values

filter( … )
A non-graph restriction that is added to the pattern.
X != Y, X < Y, …
X and Y must not be the same, X must be smaller than Y, etc.

2-6: Filter by language

lang(…)
Returns the language of a language-tagged string.
filter( A && B )
Apply filter A ánd filter B.

2-7: Graph pattern: Five Triple Patterns

2-8: Property Paths

P/Q
Sequence: first follow P, then follow Q.
P|Q
Choice: follow P ór follow Q.
P+
Follow P one or more times.
P*
Follow P zero or more times.

2-9: Making the query more specific

Instantiating a variable makes the query more specific.

2-10: Sort rows

order by ?x
Sorts rows from least happy to most happy Pokémon.
order by ?x ?y ?z
It is possible to sort by multiple criteria.

2-11: Inversely sort rows

order by desc(?x)
Inversely sort rows (descending).

SPARQL Gallery


              
'''…'''^^rdf:HTML
An HTML string with unescaped newlines and quotes.
?widget
Widget cards displayed in the gallery.

Exercise: What is the heaviest dragon, and how does it sound?

Pokémon Endpoint

ask

Graph → Y/N

ask query

construct

Graph → Graph

describe

IRI → Graph

GeoSPARQL

Geospatial data model (GeoSPARQL)

A feature can have 2D ánd 3D shapes; it can have; serializations in GML ánd in WKT.

GeoSPARQL: Geometry

geo:hasGeometry and geo:asWKT
geosparql, standardized by the Open Geospatial Consortium (OGC).
?shapeLabel
Popup for the shape bound to ?shape.

GeoSPARQL: Anonymous node syntax

a
Abbreviation for the predicate term rdf:type.
[ P O ]
Anonymous node notation (square brackets, […]) can be used to abbreviate unused subject terms.

Find a Dutch building

bag namespace
Vocabulary of the Dutch Base Registry for Buildings (BAG) by the Dutch Cadastre (Kadaster).

Exercise: Find a house or street in the Netherlands.

Template query

Find a Dutch building: bracketed

S Q [ P O ]
Anonymous nodes are regular nodes that can be used in the object position as well.

JSON ↔ SPARQL

{
  "hoofdadres": {
    "bijbehorendeOpenbareRuimte": {
      "bijbehorendeWoonplaats": { "label": "Amsterdam" },
      "naamOpenbareRuimte": "De Boelelaan"
    },
    "huisnummer": 1105,
    "postcode": "1081HV"
  }
}
[
  bag:hoofdadres [
    bag:bijbehorendeOpenbareRuimte [
      bag:bijbehorendeWoonplaats/rdfs:label "Amsterdam"@nl;
      bag:naamOpenbareRuimte "De Boelelaan"
    ];
    bag:huisnummer 1105;
    bag:postcode "1081HV"
  ]
]

GeoSPARQL: 3D geometries

?shapeColor
Color of the shape bound to ?shape.
?shapeHeight
Height of the shape bound to ?shape.
?shapeLabel
Label for the shape bound to ?shape.
values (?var … ?var) { (?term … ?term) … (?term … ?term) }
Specify multiple bindings.

Color Schemes


              
Color names
CSS Color Values, HDL Color Codes, RGB Color Codes
Color gradients
colormap & Color Brewer

GeoSPARQL + modifiers: order by

Show the 25 oldest buildings in Apeldoorn.

Federation

Federation

service <URL> { Q }
Run SPARQL select query Q on the SPARQL endpoint located at URL.
https://dbpedia.org/sparql
SPARQL endpoint over the Linked Data version of Wikipedia.

Exercise: Write a federative query

Start at your own SPARQL endpoint, and federate to an endpoint of one of your colleagues. Or federate to some other endpoint, e.g., DBpedia

For this you need to share at least one term: an IRI or a literal.

Aggregation

What is aggregation?

One or more functions that are applied to groups of values.

The groups are generated for each unique combination of values for a specified set of variables.

An example of groups

?pokemon?name
id:abomasnow"ABOMASNOW"@it-it
id:abomasnow"ABOMASNOW"@es-es
id:abomasnow"ABOMASNOW"@en-us
id:abomasnow"BLIZZAROI"@fr-fr
id:abomasnow"REXBLISAR"@de-de
id:abomasnow"ユキノオー"@ja-ja
id:abra"ABRA"@it-it
id:abra"ABRA"@fr-fr
id:abra"ABRA"@es-es
id:abra"ABRA"@de-de
id:abra"ABRA"@en-us
id:abra"ケーシィ"@ja-ja

The set of variables is {?pokemon}.

The groups are the sets of names per Pokémon.

3-2: Count function

count(…)
Applies the count function to each group of names.
group by ?pokemon
Explicit grouping criterion.

3-2: Count function + implicit grouping

Implicit grouping
When there is at least one aggregation function (e.g., count) and there is no group by clause.

3-2: Implicit grouping gone wrong

Implicit grouping
The implicitly grouped-by variables are the ones that are (1) visible, and that (2) are not input for an aggregation function.

3-3: Concatenation function

concat(…)
Concatenate all arguments into one new string.
group_concat(…;separator=…)
Concatenate all bindings, interspersed with separators, into one new string.

3-4: Sum function

Using data from the Dutch Chamber of Commerce (KvK).

(sum(?employees) as ?employees)
Summate the number of employees per company (?id).
(sample(?name) as ?name)
Since a company (?id) can have multiple names (?name), select one arbitrarily.
Implicitly grouped by ?id
Could be made explicit with grouped by ?id.

Hierarchies

Transitive predicates

Hierarchy: Org Chart

Uses the Historical International Standard Classification of Occupations (HISCO) dataset.

Hierarchy: TreeMap

Uses the Historical International Standard Classification of Occupations (HISCO) dataset.

DataCube

DataCube: Observation

observation:0007ddade4 a qb:Observation;
  qb:dataSet dataset:countries;
  dimension:location country:Netherlands;
  dimension:year "2002"^^xsd:gYear;
  measure:lifeExpectancy 7.9696e1.

DataCube: Dataset

dataset:countries a qb:DataSet;
  qb:structure dsd:countries;
  sdmx-attribute:unitMeasure dbr:Year.

DataCube: Data Structure Definition

dsd:countries a qb:DataStructureDefinition;
  qb:component
    [ qb:dimension dimension:location ],
    [ qb:dimension dimension:year ],
    [ qb:measure measure:lifeExpectancy ],
    [ qb:attribute sdmx-attribute:unitMeasure;
      qb:componentAttachment qb:Dataset ].

dimension:location a qb:DimensionProperty;
  qb:concept sdmx-concept:refArea;
  rdfs:range vocab:Country;
  rdfs:subPropertyOf sdmx-dimension:refArea.

dimension:year a qb:DimensionProperty;
  qb:concept sdmx-concept:refPeriod;
  rdfs:range xsd:gYear;
  rdfs:subPropertyOf sdmx-dimension:refPeriod.

measure:lifeExpectancy a qb:MeasureProperty;
  rdfs:range xsd:double;
  rdfs:subProperty sdmx-measure:obsValue.

Plot a measure for one dimension


              
Fixed dimension
dimension:year "2007"^^xsd:gYear
Plotted dimension
dimension:location/rdfs:label ?country
Plotted measure
measure:lifeExpectancy ?value

Plot a measure for one dimension


              
  • Column 1: plotted dimension
  • Column 2: plotted measure
  • Column 3: coordinate label
  • Column 4: tooltip

Post-processing


              
  • Caption, axes, legend
  • Linear/polynomial trend
  • Error bars
  • Log scale

Thank you for your attention!


Wouter Beek (wouter@triply.cc)
Thomas de Groot (thomas.de.groot@triply.cc)


https://triply.cc