ST 4
ST 4
Semantic Technologies 4 1
Reminder: RDF literals
• Literals can be
Plain, without a language tag:
geo:berlin geo:name ”Berlin” .
Semantic Technologies 4 2
Reminder: RDF blank nodes
• A blank node can only be used as the subject or object of an RDF triple
:x a geo:City .
:x geo:containedIn geo:germany .
:x geo:name ”Berlin” .
Semantic Technologies 4 3
SPARQL Protocol And RDF Query Language
(pronounced as sparkle)
• SPARQL is a query language for RDF graphs (supported by many graph databases)
• Does not support RDFS, only RDF (SPARQL 1.1 supports RDFS entailment regime)
Tutorials: http://www.ibm.com/developerworks/xml/library/j-sparql/
http://jena.apache.org/tutorials/sparql.html
Semantic Technologies 4 4
Library data in RDF
@prefix lib: <http://www.lib.org/schema#> .
@prefix : <http://www.bremen-lib.org/> .
:library lib:location ”Bremen” .
:jlb lib:name ”Jorge Luis Borges” .
:b1 lib:author :jlb ;
lib:title ”Labyrinths” .
:b2 lib:author :jlb ;
lib:title ”Doctor Brodie’s Report” .
:b3 lib:author :jlb ;
lib:title ”The Garden of Forking Paths” .
:abc lib:name ”Adolfo Bioy Casares” .
:b4 lib:author :abc ;
lib:title ”The Invention of Morel” .
:jc lib:name ”Julio Cortázar” .
:b5 lib:author :jc ;
lib:title ”Bestiario” .
:b6 lib:author :jc ;
lib:title ”Un tal Lucas” .
:jc lib:bornin ”Brussels” .
Semantic Technologies 4 5
SPARQL: simple query
Query over the library RDF document: find the names of authors
variable identifier
Semantic Technologies 4 6
SPARQL: simple query
Query over the library RDF document: find the names of authors
variable identifier
There are three triples having the form of the query pattern:
Semantic Technologies 4 6
SPARQL: simple query
Query over the library RDF document: find the names of authors
variable identifier
There are three triples having the form of the query pattern:
the choice of variable names is arbitrary: for example, you can use ?y in place of ?author
Semantic Technologies 4 6
SPARQL: basic graph pattern
Query over the library RDF document: find the names of authors and
the titles of their books
Semantic Technologies 4 7
SPARQL: basic graph pattern
Query over the library RDF document: find the names of authors and
the titles of their books
SELECT ?author, ?title query variables
WHERE
{
?b lib:author ?a .
?a lib:name ?author . query pattern
?b lib:title ?title . aka basic graph pattern or BGP
}
variable identifiers
Semantic Technologies 4 7
SPARQL: basic graph pattern
Query over the library RDF document: find the names of authors and
the titles of their books
SELECT ?author, ?title query variables
WHERE
{
?b lib:author ?a .
?a lib:name ?author . query pattern
?b lib:title ?title . aka basic graph pattern or BGP
}
Semantic Technologies 4 7
COUNT, LIMIT, DISTINCT
Semantic Technologies 4 8
COUNT, LIMIT, DISTINCT
Semantic Technologies 4 8
COUNT, LIMIT, DISTINCT
Semantic Technologies 4 8
The shape of a SPARQL query
Semantic Technologies 4 9
The shape of a SPARQL query
• ASK query: to check whether there are results at all (without returning any)
• CONSTRUCT query: to build an RDF graph from query results
• DESCRIBE query: to get an RDF graph with additional information on each
query result (application dependent)
Semantic Technologies 4 9
Basic SPARQL syntax
Semantic Technologies 4 10
Basic SPARQL syntax
A variable is a string that begins with ? or $, where the string can consist of
letters (including many non-Latin letters), numbers, and the symbol
The variable name is the string after ? or $, without this leading symbol.
Semantic Technologies 4 10
Basic SPARQL syntax
A variable is a string that begins with ? or $, where the string can consist of
letters (including many non-Latin letters), numbers, and the symbol
The variable name is the string after ? or $, without this leading symbol.
The variables ?var1 and $var1 have the same variable name
(and same meaning across SPARQL).
Semantic Technologies 4 10
Basic Graph Patterns
NB. These are semantic notions, which are not directly defining query syntax.
Triple patterns describe query conditions where we are looking for matching
triples. BGPs are interpreted conjunctively, i.e.,
we are looking for a match that fits all triples at once.
Convention: We will also use triple pattern and basic graph pattern to refer
to any (syntactic) Turtle snippet that specifies such (semantic) patterns.
Semantic Technologies 4 11
Blank nodes in SPARQL
Remember: blank node (bnode) IDs are syntactic aids to allow us serialising
graphs with such nodes. They are not part of the RDF graph.
SELECT ?a ?b SELECT ?a ?b
WHERE WHERE
{ {
?a :predicate :blanknode .
= ?a :predicate ?variable .
:blanknode :otherPredicate ?b . ?variable :otherPredicate ?b .
} }
Semantic Technologies 4 13
Blank node example
Data
:a foaf:name ”Alice” .
:b foaf:name ”Bob” .
SPARQL query
SELECT ?x ?name
WHERE
{
?x foaf:name ?name .
}
Answer
x name
:c ”Alice”
:d ”Bob”
Semantic Technologies 4 14
Answers to BGPs
NB. When no specific order is required, the solutions computed for a SPARQL
query can be represented by a multiset
(= ‘a set with repeated elements’ = ‘an unordered list’).
film cardinality
eg:Arrival 2
eg:Gravity 1
The cardinality of the first solution mapping is 2 since the bnode can be mapped
to two resources, eg:aux1 and eg:aux2, to find a subgraph.
Semantic Technologies 4 16
Example 2
Semantic Technologies 4 17
GROUP, ORDER, FILTER
Semantic Technologies 4 18
GROUP, ORDER, FILTER
Semantic Technologies 4 18
SELECT clauses
SELECT clauses
The keyword DISTINCT can be used after SELECT to remove duplicate solutions
Semantic Technologies 4 19
Solution set modifiers
In Wikidata, find the largest German cities, rank 6 to 15: (see Wikidata identifiers)
Semantic Technologies 4 20
OPTIONAL
Get the names of authors (in the dataset on page 5) and also
the places where they were born, if this information is available
Semantic Technologies 4 21
OPTIONAL
Get the names of authors (in the dataset on page 5) and also
the places where they were born, if this information is available
Answer
author birthplace
”Jorge Luis Borges”
”Adolfo Bioy Casares”
”Julio Cortázar” ”Brussels”
because the triple pattern for birthplace is optional, there is a pattern solution for
the authors who do not have information about their birthplace.
Without OPTIONAL, there would be only one solution: ”Julio Cortázar” ”Brussels”
Semantic Technologies 4 21
UNION
an RDF graph containing information about people’s names from FOAF and vCard
Semantic Technologies 4 22
UNION
an RDF graph containing information about people’s names from FOAF and vCard
The MINUS operator allows us to remove the results of one group graph pattern
from the results of another
SELECT ?person
WHERE {
{ ?person wdt:P106 wd:Q36834 . } # ?person occupation: composer
MINUS
{ ?person wdt:P570 [ ] . } # ?person date of death: some value
}
Semantic Technologies 4 23
Testing For the Absence/Presence of a Pattern
Data
@prefix : <http://example/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name “Alice” .
:bob rdf:type foaf:Person .
Query 1 Answer
SELECT ?person person
WHERE <http://example/bob>
{ ?person rdf:type foaf:Person .
FILTER NOT EXISTS { ?person foaf:name ?name . }
}
Semantic Technologies 4 24
Testing For the Absence/Presence of a Pattern
Data
@prefix : <http://example/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name “Alice” .
:bob rdf:type foaf:Person .
Query 1 Answer
SELECT ?person person
WHERE <http://example/bob>
{ ?person rdf:type foaf:Person .
FILTER NOT EXISTS { ?person foaf:name ?name . }
}
Query 2 Answer
SELECT ?person person
WHERE <http://example/alice>
{ ?person rdf:type foaf:Person .
FILTER EXISTS { ?person foaf:name ?name . }
}
Semantic Technologies 4 24
Filters
Data
:book1 dc:title ”SPARQL Tutorial” .
:book1 ns:price 42 .
:book2 dc:title ”The Semantic Web” .
:book2 ns:price 23 .
SPARQL query that retrieves the titles of books whose price is less than 30.5
Semantic Technologies 4 25
Filters
Data
:book1 dc:title ”SPARQL Tutorial” .
:book1 ns:price 42 .
:book2 dc:title ”The Semantic Web” .
:book2 ns:price 23 .
SPARQL query that retrieves the titles of books whose price is less than 30.5
Semantic Technologies 4 25
Optional and filters
Semantic Technologies 4 26
Optional and filters
‘Composers, and, optionally, their spouses that were born in the same year.’
Semantic Technologies 4 26
Subqueries
Semantic Technologies 4 27
Bound variables
SPARQL query to return the URIs that identify cities of type ‘Cities in Texas’ and
their total population in descending order (i.e., bigger cities first)
Only those cities that do not have a metro population will be returned
At most 10 results will be returned
Semantic Technologies 4 29
Property paths
Semantic Technologies 4 30
Property paths
SELECT ?name
WHERE {
?x foaf:mbox <mailto:alice@example> .
?x foaf:knows/foaf:knows/foaf:name ?name .
}
Semantic Technologies 4 31
Property paths (cont.)
Semantic Technologies 4 31
CONSTRUCT
Semantic Technologies 4 32
CONSTRUCT
Semantic Technologies 4 32
Exercise
Semantic Technologies 4 33
Exercise
What does the following query construct?
Semantic Technologies 4 33
SPARQL endpoint