Querying Knowledge Graphs

Wouter Beek (wouter@triply.cc)

Part I: Triple Patterns

The first SPARQL query

select * {
  ?s ?p ?o.
}
limit 25
select *
Projection: the variables whose bindings are returned (result set). * means bindings for all variables are returned.
{ ?s ?p ?o. }
The graph pattern that is matched. Variables start with a question mark.
limit 25
Modifier over the result set: only show the first 25 results.

Query for a specific predicate

select * {
  ?pokemon <http://xmlns.com/foaf/0.1/depiction> ?image.
}
limit 25
<http://xmlns.com/foaf/0.1/depiction>
A specific predicate term.
?pokemon and ?image
Use descriptive names for variables.

Short syntax for IRIs

prefix foaf: <http://xmlns.com/foaf/0.1/>
select * {
  ?pokemon foaf:depiction ?image.
}
limit 25

Change the projection

prefix foaf: <http://xmlns.com/foaf/0.1/>
select ?image {
  ?pokemon foaf:depiction ?image.
}
limit 25
?image
Only return the bindings for this variable.
?pokemon
A hidden variable: one whose bindings are not returned.

Introduce variables with bind

prefix foaf: <http://xmlns.com/foaf/0.1/>
select * {
  ?pokemon foaf:depiction ?image.
  bind(concat('<img src="',?image,'">') as ?widget)
}
limit 25
bind(EXPRESSION) as ?x
The result of evaluating EXPRESSION is bound to ?x.
concat(STRING,…)
Concatenates an arbitrary number of strings.

Change the limit

prefix foaf: <http://xmlns.com/foaf/0.1/>
select * {
  ?pokemon foaf:depiction ?image.
}
limit 250
The implicit limit is 10,000 results. (This is implementation-specific.)

Add an offset

prefix foaf: <http://xmlns.com/foaf/0.1/>
select * {
  ?pokemon foaf:depiction ?image.
}
limit 25
offset 250
offset 250
Skips the first 250 results, returning the 251st through the 260th result.
prefix bag: <http://bag.basisregistraties.overheid.nl/def/bag#>
prefix brt: <http://brt.basisregistraties.overheid.nl/def/top10nl#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix geo: <http://www.opengis.net/ont/geosparql#>
prefix pokemon: <https://demo.triply.cc/academy/pokemon/id/pokemon/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
In the following, we will assume these prefix declarations.

Part II: Graph patterns

Graph pattern (2 triple patterns)

select * {
  ?pokemon foaf:depiction ?image;
           pokemon:cry ?cry.
}
limit 25
?pokemon
A shared variable connects two triple patterns. Connected triple patterns form a graph pattern.
Short syntax can be used
Repeat subject (;), repeat subject and predicate (,).

Graph pattern (3 triple patterns)

select * {
  ?pokemon foaf:depiction ?image;
           pokemon:cry ?cry;
           rdfs:label ?name.
}
limit 25

Predicates from different vocabularies:

http://xmlns.com/foaf/0.1/depicts
Friend of a Friend (FOAF)
http://www.w3.org/2000/01/rdf-schema#
RDF Schema (RDFS)

Graph pattern (5 triple patterns)

select * {
  ?pokemon foaf:depiction ?image;
           pokemon:cry ?cry;
           rdfs:label ?name;
           pokemon:type ?type.
  ?type rdfs:label ?typeName.
}
limit 25

Instantiating variables

select * {
  ?pokemon foaf:depiction ?image;
           pokemon:cry ?cry;
           rdfs:label ?name;
           pokemon:type ?type.
  ?type rdfs:label "Dragon Type"^^xsd:string.
}
limit 25
Instantiating a variable makes the query more specific.

Sort by happiness (lowest first)

select * {
  ?pokemon foaf:depiction ?image;
           rdfs:label ?name;
           pokemon:happiness ?happiness.
}
order by ?happiness
limit 25
order by ?x
Orders results by the bindings of ?x.

Sort by happiness (highest first)

select * {
  ?pokemon foaf:depiction ?image;
           rdfs:label ?name;
           pokemon:happiness ?happiness.
}
order by desc(?happiness)
limit 25
order by desc(?x)
Inversely orders results by the bindings of ?x.

Example queries

Dutch Chamber of Commerce (KvK)

Part III: GeoSPARQL

A Simple GeoSPARQL Query

select * {
  ?monument geo:hasGeometry ?geometry.
  ?geometry geo:asWKT ?shape.
}
limit 1000
geo:hasGeometry and geo:asWKT
GeoSPARQL, standardized by the Open Geospatial Consortium (OGC).

Property Path notation

select * {
  ?monument geo:hasGeometry/geo:asWKT ?shape.
}
limit 1000
p/q
Means follow property p and then property q (and do not bind the node in between).

Federated query

select * {
  {
    select * {
      ?monument geo:hasGeometry/geo:asWKT ?shape1;
                owl:sameAs ?pand.
    }
    limit 10
  }
  service <https://data.pdok.nl/sparql> {
    ?pand geo:hasGeometry/geo:asWKT ?shape2.
  }
}
Sub-select
First the inner query is performed, and then the outer query is performed.
service <IRI> { QUERY }
Execute QUERY on endpoint IRI.

Complex data model

select ?shape {
  service <https://data.pdok.nl/sparql> {
    ?woonplaats rdfs:label "Amsterdam"@nl.
    ?openbareRuimte bag:bijbehorendeWoonplaats ?woonplaats;
                    bag:naamOpenbareRuimte "De Boelelaan".
    ?nummeraanduiding bag:bijbehorendeOpenbareRuimte ?openbareRuimte;
                      bag:huisnummer 1105;
                      bag:postcode "1081HV".
    ?verblijfsobject bag:hoofdadres ?nummeraanduiding;
                     bag:pandrelatering ?pand.
    ?pand geo:hasGeometry/geo:asWKT ?shape.
  }
}
limit 1
Data model of the Dutch Base Registry for Buildings (BAG).

Large-scale queries

select ?shape ?shapeLabel {
  ?verblijfsobject bag:hoofdadres/bag:bijbehorendeOpenbareRuimte/bag:bijbehorendeWoonplaats/bag:naamWoonplaats "Apeldoorn"^^xsd:string;
                   bag:pandrelatering ?pand.
  ?pand bag:geometriePand/geo:asWKT ?shape;
        bag:oorspronkelijkBouwjaar ?shapeLabel.
}
order by asc(?shapeLabel)
limit 50
Show the 50 oldest buildings in Apeldoorn.

Geo-3D query

  • Living space
  • Shopping
  • Offices
  • Education
  • Health
  • Sport

Average building values (WOZ)

Querying 4 datasets

Kadaster
BAG
Kamer van Koophandel (KvK)
Bedrijfsvestigingen
Rijksdienst voor het Cultureel Erfgoed (RCE)
Monumentenregister & Beeldbank
Rijksdienst voor Ondernemend Nederland (RVO)
Energielabels

Part IV: Querying the LOD Cloud

Linked Open Data Cloud

Dutch municipality → DBpedia 🕸

select ?shape ?shapeLabel {
  ?place1 brt:isBAGwoonplaats true;
          geo:hasGeometry/geo:asWKT ?shape;
          rdfs:label "Swalmen"^^xsd:string.
  service <https://dbpedia.org/sparql> {
    ?place2 foaf:depiction ?vlag;
            rdfs:label "Swalmen"@nl.
  }
}
limit 1

service <URL> { A } means that subquery A is executed on a different SPARQL endpoint. The results are received from that endpoint, and integrated within the overall query results.

Apeldoorn does not have a flag

select ?shape ?shapeLabel {
  ?place1 brt:isBAGwoonplaats true;
          geo:hasGeometry/geo:asWKT ?shape;
          rdfs:label "Apeldoorn"^^xsd:string.
  service <https://dbpedia.org/sparql> {
    ?place2 rdfs:label "Apeldoorn"@nl.
    optional { ?place2 foaf:depiction ?vlag. }
  }
}
limit 1

When you query the web, not all information is there all the time. optional { A } makes the query resilient against missing information.

Population density

Thank you for your attention!