RDF and SPARQL
RDF and SPARQL
Miao Qiao
SEAT
Massey University
1
Data Model: RDF
Why we need RDF? Semantic Web.
“The Semantic Web is an extension of the current web in which
information is given well-defined meaning, better enabling com-
puters and people to work in cooperation.”
(Tim Berners-Lee et al. 2001.)
Specific goals:
• A descriptive language with standard semantics.
Make semantics machine-processable and understandable.
• Incorporate logical infrastructure to reason about resources.
• W3C proposals: Resource Description Framework (RDF) and its
query language SPARQL.
2
RDF and SPARQL
• Data Model: RDF
• Query Language: SPARQL
3
Data Model: RDF
Resource Description Framework (RDF)
RDF is a graph-based model for representing the web as a graph of
• Resources and their properties, using
• Internationalized Resource Identifiers (IRIs) a and literal values.
IRI: http://example.org/123
Literal: 438sd:int
aA generalized Unified Resource Identifier that permits more characters.
The graph is a collection of subject-predicate-object triples (RDF triples).
4
http://example.org/123
Data Model: RDF
• RDF Statement : an RDF triple.
• An RDF triple:
Subject :IRI or blank node.
Predicate : IRI.
Object : IRI, blank node or literal.
5
Data Model: RDF
• RDF Statement : an RDF triple.
• An RDF triple:
Subject :IRI or blank node.
Predicate : IRI.
Object : IRI, blank node or literal.
5
Data Model: RDF
One can write RDF in different syntaxes:
• RDF/XML: respent RDF data in XML.
• Turtle: a compact presentation of RDF data in RDF triples.
6
Data Model: RDF
One can write RDF in different syntaxes:
• RDF/XML: respent RDF data in XML.
• Turtle: a compact presentation of RDF data in RDF triples.
6
Data Model: RDF
One can write RDF in different syntaxes:
• RDF/XML: respent RDF data in XML.
• Turtle: a compact presentation of RDF data in RDF triples.
6
Data Model: RDF
RDF document can be presented in different syntaxes.
• RDF/XML: respent RDF data in XML.
• Turtle: a compact presentation of RDF data in RDF triples.
@prefix rdf:
@prefix dc:
@prefix ex:
dc:title “RDF/XML Syntax” ;
ex:editors [
ex:fullname “Dave Beckett”;
ex:homePage
] .
7
Data Model: RDF
RDF document can be presented in different syntaxes.
• RDF/XML: respent RDF data in XML.
• Turtle: a compact presentation of RDF data in RDF triples.
Next, we shall mainly use the Turtle syntax to describe an RDF document.
8
Data Model: RDF
RDF Schema
RDF Schema standardizes RDF vocabulary for describing classes and
properties
• using object-oriented types + domain/range
• subclasses (sc), subproperties (sp), domain/range, · · ·
9
Data Model: RDF
RDF Schema
RDF Schema standardizes RDF vocabulary for describing classes and
properties
• using object-oriented types + domain/range
• subclasses (sc), subproperties (sp), domain/range, · · ·
Range and domain:
• A rdfs:domain B
A is an instance of the class rdf:Property.
B is a instance of the class rdfs:Class.
The subjects of triples whose predicate is A are the instances of B.
• A rdfs:range B
A is an instance of the class rdf:Property.
B is a instance of the class rdfs:Class.
The objects of triples whose predicate is A are the instances of B.
9
Data Model: RDF
RDF Schema
RDF Schema standardizes RDF vocabulary for describing classes and
properties
• using object-oriented types + domain/range
• subclasses (sc), subproperties (sp), domain/range, · · ·
9
Data Model: RDF
RDF Schema
RDF Schema standardizes RDF vocabulary for describing classes and
properties
• using object-oriented types + domain/range
• subclasses (sc), subproperties (sp), domain/range, · · ·
The semantics can be used as an inference system.
• Sub-property:
(A, sp,B) + (B, sp,C) => (A, sp,C)
(A, sp,B) + (X ,A,Y ) => (X ,B,Y )
• Subclass:
(A, sc,B) + (B, sc,C) => (A, sc,C)
(A, sc,B) + (X , type,A) => (X , type,B)
• Typing:
(A, dom,B) + (X ,A,Y ) => (X , type,B)
(A, range,B) + (X ,A,Y ) => (Y , type,B)
9
Data Model: RDF
prefix Namespace IRI RDF vocabulary
rdfs http://www.w3.org/2000/01/rdf-schema# The RDF Schema vocabulary
rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# The RDF built-in vocabulary
rdfs
is used to define
=⇒ rdf
rdfs
rdf
}
are used to define
=⇒ other vocabularies such as dc, owl, · · ·
10
http://www.w3.org/2000/01/rdf-schema#
http://www.w3.org/1999/02/22-rdf-syntax-ns#
Data Model: RDF
prefix Namespace IRI RDF vocabulary
rdfs http://www.w3.org/2000/01/rdf-schema# The RDF Schema vocabulary
rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# The RDF built-in vocabulary
rdfs
is used to define
=⇒ rdf
rdfs
rdf
}
are used to define
=⇒ other vocabularies such as dc, owl, · · ·
10
http://www.w3.org/2000/01/rdf-schema#
http://www.w3.org/1999/02/22-rdf-syntax-ns#
Data Model: RDF
prefix Namespace IRI RDF vocabulary
rdfs http://www.w3.org/2000/01/rdf-schema# The RDF Schema vocabulary
rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# The RDF built-in vocabulary
rdfs
is used to define
=⇒ rdf
rdfs
rdf
}
are used to define
=⇒ other vocabularies such as dc, owl, · · ·
10
http://www.w3.org/2000/01/rdf-schema#
http://www.w3.org/1999/02/22-rdf-syntax-ns#
Data Model: RDF
prefix Namespace IRI RDF vocabulary
rdfs http://www.w3.org/2000/01/rdf-schema# The RDF Schema vocabulary
rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# The RDF built-in vocabulary
rdfs
is used to define
=⇒ rdf
rdfs
rdf
}
are used to define
=⇒ other vocabularies such as dc, owl, · · ·
10
http://www.w3.org/2000/01/rdf-schema#
http://www.w3.org/1999/02/22-rdf-syntax-ns#
Data Model: RDF
• Namespace URI: the common substring of the URIs in an RDF
vocabulary.
prefix Namespace IRI RDF vocabulary
rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# The RDF built-in vocabulary
rdfs http://www.w3.org/2000/01/rdf-schema# The RDF Schema vocabulary
xsd http://www.w3.org/2001/XMLSchema# The RDF-compatible XSD types
dc http://purl.org/dc/elements/1.1# “Dublin Core Metadata Element Set
eg http://example.org/ the IRI defined locally.
11
http://www.w3.org/1999/02/22-rdf-syntax-ns#
http://www.w3.org/2000/01/rdf-schema#
http://www.w3.org/2001/XMLSchema#
http://purl.org/dc/elements/1.1#
http://example.org/
Data Model: RDF
• Namespace URI: the common substring of the URIs in an RDF
vocabulary.
prefix Namespace IRI RDF vocabulary
rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# The RDF built-in vocabulary
rdfs http://www.w3.org/2000/01/rdf-schema# The RDF Schema vocabulary
xsd http://www.w3.org/2001/XMLSchema# The RDF-compatible XSD types
dc http://purl.org/dc/elements/1.1# “Dublin Core Metadata Element Set
eg http://example.org/ the IRI defined locally.
11
http://www.w3.org/1999/02/22-rdf-syntax-ns#
http://www.w3.org/2000/01/rdf-schema#
http://www.w3.org/2001/XMLSchema#
http://purl.org/dc/elements/1.1#
http://example.org/
Data Model: RDF
Ontology = ontos(being) + logos(word).
• An ontology is a systematic explanation of existence (400BC).
• Grube (1998)
Formal: machine readable
Explicit: specifications of concepts, properties, functions, axioms are
defined.
Shared: consensual knowledge
Conceptual: abstract model of some phenomena in the world.
Example: Define the concept of “MusicalArtist”. http://dbpedia.org/fct/
12
http://dbpedia.org/fct/
Data Model: RDF
Ontology = ontos(being) + logos(word).
• An ontology is a systematic explanation of existence (400BC).
• Grube (1998)
Formal: machine readable
Explicit: specifications of concepts, properties, functions, axioms are
defined.
Shared: consensual knowledge
Conceptual: abstract model of some phenomena in the world.
Example: Define the concept of “MusicalArtist”. http://dbpedia.org/fct/
12
http://dbpedia.org/fct/
RDF and SPARQL
• Data Model: RDF
• Query Language: SPARQL
13
Querying RDF: SPARQL
An SQL query retrieves data from a database under the relational model.
SELECT name data needed
FROM student data source
WHERE age > 20 data constraint
SPARQL
• a recursive acronym that stands for
SPARQL Protocol and RDF Query Language
• a graph-matching query language for RDF graphs.
• recommended by W3C
• Like SQL, SPARQL consists of three components:
select: the entities to be returned
from: the data source (RDF graph)
where: the pattern to be matched against the RDF graph
optional prologue: namespace
14
Querying RDF: SPARQL
Example: find 10 films with the keyword of “Paddington” in thier labels.
prefix rdfs:
prefix dbpedia-owl:
prefix movie:
select distinct ?film where {
{ ?film a movie:Film } union
{ ?film a dbpedia-owl:Film }
?film rdfs:label ?label .
filter regex( str(?label), “Paddington”, “i”)
}
limit 10
Try this on the SPARQL endPoint of dbpedia
https://dbpedia.org/sparql.
15
https://dbpedia.org/sparql
Query Language: SPARQL
The WHERE clause specifies the graph pattern which :
• is an RDF graph
• involves variables
• has the basic pattern of triple pattern
• can be complex
Semantic: for a pattern P and an RDF graph G , we denote by [P]G the
set of matchings of P, that are, subgraphs of G that match P.
16
Query Language: SPARQL
Triple pattern
Example: A triple pattern can have one/more variables.
• ?film rdfs:label ?label
• ?film a movie:film
Note: a is the abbreviation of rdfs:type.
select ?film where {
?film a dbpedia-owl:Film
} limit 10
https://dbpedia.org/sparql.
17
https://dbpedia.org/sparql
Query Language: SPARQL
A larger pattern is assembled with triple patterns.
The queries below are equivalent.
select ?film
where {
{?film a movie:Film }
{?film rdfs:label ?label }
}
select ?film
where {
?film a movie:Film .
?film rdfs:label ?label .
}
Semantics: [P1 AND P2]G = [P1]G [P2]G
18
Query Language: SPARQL
Alternative Graphs: Union
select ?film
where {
{?film a movie:Film } UNION
{?film rdfs:label ?label}
}
Return the films that EITHER satisfy the pattern of
• ?film a movie:Film or
• ?film rdfs:label ?label
Semantic: [P1UNIONP2] = [P1] ∪ [P2].
19
Query Language: SPARQL
Optional pattern:
select ?film ?label
where {
{?film a movie:Film } OPTIONAL
{?film rdfs:label ?label}
}
Return the films that satisfy the pattern of
• ?film a movie:Film
and return its labels if there are any.
Semantics: [P1 OPT P2]G = [P1]G [P2]G
20
Query Language: SPARQL
Filter clause: a boolean expression consists of
• RDF-model related operators
is Literal(?aNode)
isURI(?aNode)
· · ·
• Numeric values comparison
• Literal: regex (from XQuery)
select ?film where {
?film rdfs:label ?label .
filter regex( str(?label), “Paddington”, “i”)
}
• xsd:boolean REGEX (string literal text, simple literal pattern, flags)
• flags
i: case-insensitive
See https://www.w3.org/TR/xpath-functions/#regex-syntax for more information.
21
https://www.w3.org/TR/xpath-functions/#regex-syntax
Query Language: SPARQL
Filter clause: a boolean expression consists of
• RDF-model related operators
is Literal(?aNode)
isURI(?aNode)
· · ·
• Numeric values comparison
• Literal: regex (from XQuery)
select ?film where {
?film rdfs:label ?label .
filter regex( str(?label), “Paddington”, “i”)
}
Semantics: [P FILTER R]G = {µ ∈ [P]G | µ |= R}
21
Query Language: SPARQL
The pattern of a SPARQL query can be complicated.
Let P1 to P9 be triple patterns and R be a regular expression in XQuery.
• Grouping
• Optional parts
• Nesting
• Union of patterns
• Filtering
{
{ P1
P2 }
OPTIONAL { P5 } }
{ P3
P4
OPTIONAL { P7
OPTIONAL { P8 } } }
}
UNION
{ P9
FILTER ( R ) }
22
Query Language: SPARQL
The pattern of a SPARQL query can be complicated.
Let P1 to P9 be triple patterns and R be a regular expression in XQuery.
• Grouping
• Optional parts
• Nesting
• Union of patterns
• Filtering
{ { P1
P2 }
OPTIONAL { P5 } }
{ P3
P4 }
OPTIONAL { P7
OPTIONAL { P8 } } }
}
UNION
{ P9
FILTER ( R ) }
22
Query Language: SPARQL
The pattern of a SPARQL query can be complicated.
Let P1 to P9 be triple patterns and R be a regular expression in XQuery.
• Grouping
• Optional parts
• Nesting
• Union of patterns
• Filtering
{ { P1
P2
OPTIONAL { P5 } }
{ P3
P4
OPTIONAL { P7 } }
OPTIONAL { P8 } } }
}
UNION
{ P9
FILTER ( R ) }
22
Query Language: SPARQL
The pattern of a SPARQL query can be complicated.
Let P1 to P9 be triple patterns and R be a regular expression in XQuery.
• Grouping
• Optional parts
• Nesting
• Union of patterns
• Filtering
{ { P1
P2
OPTIONAL { P5 } }
{ P3
P4
OPTIONAL { P7
OPTIONAL { P8 } } }
}
UNION
{ P9
FILTER ( R ) }
22
Query Language: SPARQL
The pattern of a SPARQL query can be complicated.
Let P1 to P9 be triple patterns and R be a regular expression in XQuery.
• Grouping
• Optional parts
• Nesting
• Union of patterns
• Filtering
{ { P1
P2
OPTIONAL { P5 } }
{ P3
P4
OPTIONAL { P7
OPTIONAL { P8 } } }
}
UNION
{ P9 }
FILTER ( R ) }
22
Query Language: SPARQL
The pattern of a SPARQL query can be complicated.
Let P1 to P9 be triple patterns and R be a regular expression in XQuery.
• Grouping
• Optional parts
• Nesting
• Union of patterns
• Filtering
{ { P1
P2
OPTIONAL { P5 } }
{ P3
P4
OPTIONAL { P7
OPTIONAL { P8 } } }
}
UNION
{ P9
FILTER ( R ) }
22
Query Language: SPARQL
Let P1 and P9 be triple patterns and R be a regular expression in XQuery.
• Graph patterns: full parenthesized algebra
{ P1 P2 } (P1 AND P2 )
{ P1 OPTIONAL { P2 }} (P1 OPT P2 )
{ P1 } UNION { P2 } (P1 UNION P2 )
{ P1 FILTER ( R ) } (P1 FILTER R )
original SPARQL syntax algebraic syntax
23
Query Language: SPARQL
Semantics of SPARQL
Given an RDF graph G .
Let t be a triple pattern and P1 and P2 be two patterns
Definition
[t]G =
the subgraphs of G that matches t.
[P1 AND P2]G =
[P1]G [P2]G
[P1 UNION P2]G =
[P1]G ∪ [P2]G
[P1 OPT P2]G =
[P1]G [P2]G
24
Query Language: SPARQL
Semantics of SPARQL
Given an RDF graph G .
Let t be a triple pattern and P1 and P2 be two patterns
Definition
[t]G = the subgraphs of G that matches t.
[P1 AND P2]G = [P1]G [P2]G
[P1 UNION P2]G = [P1]G ∪ [P2]G
[P1 OPT P2]G = [P1]G [P2]G
24
Query Language: SPARQL
Solution Modifiers.
• ORDER BY
• LIMIT
• DISTINCT
• PROJECTION
• OFFSET: control where the solutions start from in the overall
sequence of solutions
• REDUCED: permit any non-unique solutions to be eliminated
prefix rdfs:
prefix dbpedia-owl:
prefix movie:
select distinct ?film where {
{ ?film a movie:Film } union
{ ?film a dbpedia-owl:Film }
?film rdfs:label ?label .
filter regex( str(?label), “Paddington”, “i”)
}
order by ?film
limit 10
25
Query Language: SPARQL
SPARQL can be used to:
• select
• construct an RDF graph construct{a triple} where{}
• test whether a query pattern has a solution ask{}
• describe a resource with an RDF graph describe aResource
26
Query Language: SPARQL
Example: find 10 films with the keyword of “Paddington” in their labels.
prefix rdfs:
prefix dbpedia-owl:
prefix movie:
select distinct ?film where {
{ ?film a movie:Film } union
{ ?film a dbpedia-owl:Film }
?film rdfs:label ?label .
filter regex( str(?label), “Paddington”, “i”)
}
limit 10
27
Query Language: SPARQL
Example of AND , UNION : find 10 films either with the keyword of
“Paddington” in their labels or acted by Tom Cruise.
prefix rdfs:
prefix dbpedia-owl:
prefix movie:
select distinct ?film where {
{ ?film a movie:Film } union
{ ?film a dbpedia-owl:Film }
{?film rdfs:label ?label .
filter regex( str(?label), “Paddington”, “i”)} union
{?film dbo:starring ?actor
filter regex( str(?actor), “Tom_Cruise”, “i”)}
}
limit 10
28
Query Language: SPARQL
Example of OPT : find 10 films with the keyword of “Paddington” in
thier labels and show if they are acted by Hugh Grant.
prefix rdfs:
prefix dbpedia-owl:
prefix movie:
select distinct ?film ?actor where {
{ ?film a movie:Film } union
{ ?film a dbpedia-owl:Film }
{?film rdfs:label ?label .
filter regex( str(?label), “Paddington”, “i”)} optional
{?film dbo:starring ?actor
filter regex( str(?actor), “Hugh_Grant”, “i”)}
}
limit 10
29
RDF and SPARQL
• Data Model: RDF
• Query Language: SPARQL
Resources:
• Prefixes: http://dbpedia.org/sparql?help=nsdecl
• DBpedia: http://dbpedia.org/sparql
• W3C: https://www.w3.org/TR/rdf-sparql-query/
30
http://dbpedia.org/sparql?help=nsdecl
http://dbpedia.org/sparql
https://www.w3.org/TR/rdf-sparql-query/
References
• http://www.iro.umontreal.ca/~lapalme/ift6281/sparql-1_
1-cheat-sheet.pdf
• http://webkr.cs.vu.nl/slides/rdfs.pdf
• http://marenas.sitios.ing.uc.cl/talks/pods11.pdf
31
http://www.iro.umontreal.ca/~lapalme/ift6281/sparql-1_1-cheat-sheet.pdf
http://www.iro.umontreal.ca/~lapalme/ift6281/sparql-1_1-cheat-sheet.pdf
http://webkr.cs.vu.nl/slides/rdfs.pdf
http://marenas.sitios.ing.uc.cl/talks/pods11.pdf