querying linked data
TRANSCRIPT
Querying Linked Data
Presented by:Barry Norton
2
Motivation: Music!
Visualization Module
Metadata
Streaming providers
Physical Wrapper
Downloads
Dat
a ac
quis
ition
D2R Transf.LD Wrapper
Musical Content
Appl
icati
on
Analysis & Mining Module
LD D
atas
etAc
cess
LD Wrapper
RDF/ XML
Integrated Dataset
Interlinking CleansingVocabulary Mapping
SPARQL Endpoint
Publishing
RDFa
Other content
EUCLID - Querying Linked Data 3
Agenda
1. Introduction to SPARQL
2. Querying Linked Data with SPARQL
3. SPARQL Algebra
4. Updating Linked Data with SPARQL 1.1
5. SPARQL Protocol
6. Reasoning over Linked Data
4
INTRODUCTION TO SPARQL
EUCLID - Querying Linked Data
5
SPARQL
EUCLID - Querying Linked Data
Semantic Web StackBerners-Lee (2006)
* Protocol and RDF Query Language
Declarative query language
6
SPARQL
EUCLID - Querying Linked Data
• SPARQL Query– Declarative query language for RDF data– http://www.w3.org/TR/rdf-sparql-query/
• SPARQL Algebra– Standard for communication between SPARQL services and clients– http://www.w3.org/2001/sw/DataAccess/rq23/rq24-algebra.html
• SPARQL Update– Declarative manipulation language for RDF data– http://www.w3.org/TR/sparql11-update/
• SPARQL Protocol– Standard for communication between SPARQL services and clients– http://www.w3.org/TR/sparql11-protocol/
7
SPARQL Query 1.1
EUCLID - Querying Linked Data
• SPARQL 1.0 only allows accessing the data (query)
• SPARQL 1.1 introduces:
Query extensions
Updates
• Data management: Insert, Delete, Delete/Insert
• Graph management: Create, Load, Clear, Drop, Copy, Move, Add
• Aggregates, Subqueries, Negation, Expressions in the SELECT clause, Property paths, assignment, short form for CONSTRUCT, expanded set of functions and operators
Federation extension • Service, values, service variables (informative) CH 5
8
SPARQL Basics
EUCLID - Querying Linked Data
• RDF triple: Basic building block, of the form subject, predicate, object. Example:
• RDF triple pattern: Contains one or more variables. Examples:
• RDF quad pattern: Contains graph name: URI or variable. Examples:
dbpedia:The_Beatles foaf:name "The Beatles" .
dbpedia:The_Beatles foaf:made ?album.
?album mo:track ?track .
?album ?p ?o .
GRAPH <:g> {:s :p :o .}GRAPH ?g {dbpedia:The_Beatles foaf:name ?o.}
9
SPARQL Basics
EUCLID - Querying Linked Data
• RDF graph: Set of RDF assertions, manipulated as a labeled directed graph.
• RDF data set: set of RDF triples. It is comprised of:• One default graph• Zero or more named graphs
• SPARQL protocol client: HTTP client that sends requests for SPARQL Protocol operations (queries or updates)
• SPARQL protocol service: HTTP server that services requests for SPARQL Protocol operations
• SPARQL endpoint: The URI at which a SPARQL Protocol service listens for requests from SPARQL clients
10
QUERYING LINKED DATA WITH SPARQL
EUCLID - Querying Linked Data
11
SPARQL Query
EUCLID - Querying Linked Data
Main idea: Pattern matching• Queries describe sub-graphs of the queried graph• Graph patterns are RDF graphs specified in Turtle
syntax, which contain variables (prefixed by either “?” or “$”)
• Sub-graphs that match the graph patterns yield a result
?albumdbpedia: The_Beatles
foaf:made
12
SPARQL Query
EUCLID - Querying Linked Data
?albumdbpedia: The_Beatles
foaf:made
dbpedia: The_Beatlesfoaf:made
<http://musicbrainz.org
/record/...>
<http://musicbrainz.org
/record/...>
foaf:madeData:
Graph pattern: Results:
"Help!" "Let It Be"
dc:title dc:title
<http://musicbrainz.org
/record/...>
"Abbey Road"
dc:title
foaf:made
?album
<http://musicbrainz.org...>
<http://musicbrainz.org...>
<http://musicbrainz.org...>
13
SPARQL Query
EUCLID - Querying Linked Data
?album
dbpedia: The_Beatles
foaf:made
dbpedia: The_Beatlesfoaf:made
<http://musicbrainz.org
/record/...>
<http://musicbrainz.org
/record/...>
foaf:madeData:
Graph pattern: Results:
"Help!" "Let It Be"
dc:title dc:title
<http://musicbrainz.org
/record/...>
"Abbey Road"
dc:title
foaf:made
?album ?title
<http://...> "Help!"
<http://...> "Abbey Road"
<http://...> "Let It Be"?title
dc:title
14
SPARQL Query
EUCLID - Querying Linked Data
?album
dbpedia: The_Beatles
foaf:made
dbpedia: The_Beatlesfoaf:made
<http://musicbrainz.org
/record/...>
<http://musicbrainz.org
/track/...>
foaf:madeData:
Graph pattern: Results:
"Help!" "Help!"
dc:title dc:title
mo:track
a
mo:Record mo:Track
mo:Record
?album
<http://musicbrainz.org...>
15
SPARQL Query: Components
EUCLID - Querying Linked Data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX mo: <http://purl.org/ontology/mo/>
SELECT ?albumFROM <http://musicbrainz.org/20130302>WHERE { dbpedia:The_Beatles foaf:made ?album . ?album a mo:Record ; dc:title ?title} ORDER BY ?title
Prologue:• Prefix definitions• Subtly different from Turtle syntax - the final period is not used
16
SPARQL Query: Components
EUCLID - Querying Linked Data
Query form:• ASK, SELECT, DESCRIBE or CONSTRUCT• SELECT retrieves variables and their bindings as a table
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX mo: <http://purl.org/ontology/mo/>
SELECT ?albumFROM <http://musicbrainz.org/20130302>WHERE { dbpedia:The_Beatles foaf:made ?album . ?album a mo:Record ; dc:title ?title} ORDER BY ?title
17
SPARQL Query: Components
EUCLID - Querying Linked Data
Data set specification:• This clause is optional• FROM or FROM NAMED• Indicates the sources for the data against which to find matches
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX mo: <http://purl.org/ontology/mo/>
SELECT ?albumFROM <http://musicbrainz.org/20130302>WHERE { dbpedia:The_Beatles foaf:made ?album . ?album a mo:Record ; dc:title ?title} ORDER BY ?title
18
SPARQL Query: Components
EUCLID - Querying Linked Data
Query pattern:• Defines patterns to match against the data• Generalises Turtle with variables and keywords – N.B. final period optional
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX mo: <http://purl.org/ontology/mo/>
SELECT ?albumFROM <http://musicbrainz.org/20130302>WHERE { dbpedia:The_Beatles foaf:made ?album . ?album a mo:Record ; dc:title ?title} ORDER BY ?title
19
Solution modifier:• Modify the result set• ORDER BY, LIMIT or OFFSET re-organise rows; • GROUP BY combines them
SPARQL Query: Components
EUCLID - Querying Linked Data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX mo: <http://purl.org/ontology/mo/>
SELECT ?albumFROM <http://musicbrainz.org/20130302>WHERE { dbpedia:The_Beatles foaf:made ?album . ?album a mo:Record ; dc:title ?title} ORDER BY ?title
20
Query Forms
EUCLID - Querying Linked Data
SPARQL supports different query forms:
• ASK tests whether or not a query pattern has a solution. Returns yes/no
• SELECT returns variables and their bindings directly
• CONSTRUCT returns a single RDF graph specified by a graph template
• DESCRIBE returns a single RDF graph containing RDF data about resource
21
Query Form: ASK
EUCLID - Querying Linked Data
• Namespaces are added with the ‘PREFIX’ directive• Statement patterns that make up the graph are
specified between brackets (“{}”)
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX dbpedia-ont: <http://dbpedia.org/ontology/>PREFIX mo: http://purl.org/ontology/mo/ASK WHERE { dbpedia:The_Beatles mo:member dbpedia:Paul_McCartney.}
Is Paul McCartney member of ‘The Beatles’?Query: true
Results:
Is Elvis Presley member of ‘The Beatles’?Query: false
Results: PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX dbpedia-ont: <http://dbpedia.org/ontology/>PREFIX mo: http://purl.org/ontology/mo/ASK WHERE { dbpedia:The_Beatles mo:member dbpedia:Elvis_Presley.}
22
Query Form: SELECT
EUCLID - Querying Linked Data
• The solution modifier projection nominates which components of the matches should be returned
• “*” means all components should be returned
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX mo: <http://purl.org/ontology/mo/>SELECT ?album_name ?track_titleWHERE { dbpedia:The_Beatles foaf:made ?album . ?album dc:title ?album_name ; mo:track ?track . ?track dc:title ?track_title .}
Query: What albums and tracks did ‘The Beatles’ make?
23
Filter expressions• Different types of filters and functions may be used
Query Form: SELECT (2)
EUCLID - Querying Linked Data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX mo: <http://purl.org/ontology/mo/>SELECT ?album_name ?track_title ?date ?durationWHERE { dbpedia:The_Beatles foaf:made ?album . ?album dc:title ?album_name ; mo:track ?track . ?track dc:title ?track_title ;
mo:duration ?duration; FILTER (?duration>300000 && ?duration<400000) }
Query:
Filter: Comparison and logical operatorsRetrieve the albums and tracks recorded by ‘The Beatles’, where the duration of the song is more than 300 secs. and no longer than 400 secs.
24
Query Form: SELECT (3)
EUCLID - Querying Linked Data
Elimination of duplicates
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX mo: <http://purl.org/ontology/mo/>SELECT MODIFIER ?album_nameWHERE { dbpedia:The_Beatles foaf:made ?album . ?album dc:title ?album_name .
mo:track ?track1 . mo:track ?track2 .
FILTER (?track1 != ?track2) }
Retrieve the name of the albums recorded by ‘The Beatles’ which have at least two different songs.Query:
?album
“Revolver”
“Sessions”
“Abbey Road”
?album
“Revolver”
“Revolver”
“Revolver”
“Sessions”
“Abbey Road”
“Abbey Road”
DISTINCT
Results:
REDUCEDMODIFIER= MODIFIER=
25
Aggregates• Calculate aggregate values: COUNT, SUM, MIN, MAX, AVG,
GROUP_CONCAT and SAMPLE• Built around the GROUP BY operator• Prune at group level (cf. FILTER) using HAVING
Query Form: SELECT (4)
EUCLID - Querying Linked Data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX mo: <http://purl.org/ontology/mo/>SELECT ?album (SUM(?track_duration) AS ?album_duration)WHERE { dbpedia:The_Beatles foaf:made ?album . ?album mo:track ?track . ?track mo:duration ?track_duration .} GROUP BY ?album HAVING (SUM(?track_duration) > 3600000)
Retrieve the duration of the albums recorded by ‘The Beatles’.Query:
26
Query Form: DESCRIBE
EUCLID - Querying Linked Data
Takes the resources within the solution, and provides information about them as RDF statements. They can be identified by:• Specifying explicit IRIs
• Bindings of variables in the WHERE clause
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX mo: <http://purl.org/ontology/mo/>DESCRIBE ?memberWHERE { dbpedia:The_Beatles mo:member ?member .}
PREFIX dbpedia: <http://dbpedia.org/resource/>DESCRIBE dbpedia:Paul_McCartney
27EUCLID - Querying Linked Data
• CONSTRUCT WHERE: In order to query for a subgraph, without change, it is no longer necessary to repeat the graph pattern in the template
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX mo: <http://purl.org/ontology/mo/>PREFIX dc: <http://purl.org/dc/elements/1.1/>CONSTRUCT WHERE { dbpedia:The_Beatles foaf:made ?album . ?album mo:track ?track .}
Example:
Query Form: CONSTRUCT
28
Query Form: CONSTRUCT (2a)
EUCLID - Querying Linked Data
dbpedia: The_Beatlesfoaf:made
<http://musicbrainz.org
/record/...>
<http://musicbrainz.org
/record/...>
foaf:madeData:
Query: Result:
"Help!" "Let It Be"
dc:title dc:title
<http://musicbrainz.org
/record/...>
"Abbey Road"
dc:title
foaf:made
CONSTRUCT { ?album dc:creator dbpedia:The_Beatles .}WHERE { dbpedia:The_Beatles foaf:made ?album .}
dbpedia: The_Beatles
<http://musicbrainz
…>
<http://musicbrainz
…><http://
musicbrainz…>
dc:creator
dc:creator
dc:creator
29EUCLID - Querying Linked Data
• Returns RDF statements created from variable bindings
• Template: graph pattern with variables from the query pattern
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX mo: <http://purl.org/ontology/mo/>PREFIX dc: <http://purl.org/dc/elements/1.1/>CONSTRUCT { ?album dc:creator dbpedia:The_Beatles . ?track dc:creator dbpedia:The_Beatles .}WHERE { dbpedia:The_Beatles foaf:made ?album . ?album mo:track ?track .}
Create the dc:creator descriptions for albums and their tracks recorded by ‘The Beatles’.Query:
Query Form: CONSTRUCT (2b)
30
Query Form: CONSTRUCT (3)
EUCLID - Querying Linked Data
Subsets of results• It is possible to combine the query with solution modifiers
(ORDER BY, LIMIT, OFFSET)
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX mo: <http://purl.org/ontology/mo/>PREFIX dc: <http://purl.org/dc/elements/1.1/>CONSTRUCT { ?album dc:creator dbpedia:The_Beatles . ?track dc:creator dbpedia:The_Beatles .}WHERE { dbpedia:The_Beatles foaf:made ?album . ?album mo:track ?track ; dc:date ?date .} ORDER BY DESC(?date) LIMIT 10
Create the dc:creator descriptions for the 10 most recent albums and their tracks recorded by ‘The Beatles’.Query:
31
Union Graph Pattern• Allows the specification of alternatives (disjunctions)
EUCLID - Querying Linked Data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: http://xmlns.com/foaf/0.1/PREFIX dc: <http://purl.org/dc/elements/1.1/>CONSTRUCT { ?album dc:creator dbpedia:The_Beatles . ?track dc:creator dbpedia:The_Beatles .}WHERE { dbpedia:The_Beatles foaf:made ?album . ?album dc:title ?album_name . {?album dbpedia-ont:recordedIn dbpedia:Abbey_Road_Studios .}
UNION {?album dbpedia-ont:recordedIn dbpedia:Trident_Studios .}}
Create the dc:creator descriptions for the albums recorded by ‘The Beatles’ in ‘Abbey Road Studios’ or ‘Trident Studios’ Query:
Query Form: CONSTRUCT (4)
32
Filter expressions• Different types of filters and functions may be used
EUCLID - Querying Linked Data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>CONSTRUCT {?album dc:creator dbpedia:The_Beatles .}WHERE { dbpedia:The_Beatles foaf:made ?album . ?album dc:title ?album_name ; FILTER (REGEX(?album_name, ".*love.*", i)) }
Query:
Filter: Regular expressions over stringsCreate the dc:creator descriptions of the albums recorded by ‘The Beatles’ whose title contains the word ‘love .
Query Form: CONSTRUCT (5)
33
Type of function Function Result type
Functional Forms
boundIFCOALESCENOT EXISTS, EXISTSor, andRDFTerm-equal (=), sameTermIN, NOT IN
xsd:booleanrdfTermrdfTermxsd:booleanxsd:booleanxsd:booleanboolean
Functions on RDF TermsisIRI, isBlank, isLiteral, isNumericstr, lang,datatypeIRIBNODE
xsd:booleansimple literaliriiriblank node
Functions on Numerics ABS, ROUND, CEIL, FLOORRAND
numericxsd:double
Filter expressions
EUCLID - Querying Linked DataSource:http://www.w3.org/TR/sparql11-query/#SparqlOps
Query Form: CONSTRUCT (6)
34
Type of function Function Result type
Functions on Strings STRLENSUBSTR, UCASE, LCASESTRSTARTS, STRENDS, CONTAINSSTRBEFORE, STRAFTERENCODE_FOR_URICONCATlangMatchesREGEXREPLACE
xsd:integerstring literalxsd:booleanliteralsimple literalstring literalxsd:booleanxsd:booleanstring literal
Functions on Dates and Times
nowyear, month, day, hours, minutessecondstimezonetz
xsd:dateTimexsd:integerxsd:decimalxsd:dayTimeDurationsimple literal
Filter expressions
EUCLID - Querying Linked Data
Source:http://www.w3.org/TR/sparql11-query/#SparqlOps
Query Form: CONSTRUCT (7)
35
Optional Graph Pattern• OPTIONAL clause encloses the optional parts• If variables in the construct clause are not bound in the optional,
the triple patterns with these variables are not generated
EUCLID - Querying Linked Data
PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>CONSTRUCT { ?album dc:creator ?artist . ?picture dc:depicts ?artist . }WHERE { ?artist foaf:made ?album . OPTIONAL {?artist foaf:depiction ?picture .}}
Create the dc:creator and dc:depicts descriptions of artists.Query:
Query Form: CONSTRUCT (8)
36
Optional Graph Pattern• Can test if variables are bound in filter expressions• Solutions that meet the OPTIONAL clause can be filtered out by
using the logical filter NOT (!)
EUCLID - Querying Linked Data
PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>CONSTRUCT { ?album dc:creator ?artist . }WHERE { ?artist foaf:made ?album . OPTIONAL {?artist dbpedia-owl:deathPlace ?place_of_death .} FILTER (!BOUND(?place_of_death))}
Create the dc:creator descriptions of those artists who are not dead.
Query:
Query Form: CONSTRUCT (9)
NOT EXISTS {?artist dbpedia-owl:deathPlace ?place_of_death .}
37
Assigning Variables • The value of an expression can be added to a solution mapping by
binding a new variable (which can be further used and returned)• The BIND form allows to assign a value to a variable from a BGP
EUCLID - Querying Linked Data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX mo: <http://purl.org/ontology/mo/>PREFIX dbpedia-ont: <http://dbpedia.org/ontology/>CONSTRUCT { ?track dbpedia-ont:runtime ?secs .} WHERE { dbpedia:The_Beatles foaf:made ?album . ?album mo:track ?track . ?track mo:duration ?duration . BIND((?duration/1000) AS ?secs) .}
Calculate the duration of the tracks from ms to s, and store the value using the dbpedia-ont:runtime property .
Query:
Query Form: CONSTRUCT (10)
38
Sub-queries and Aggregate Values• To combine the CONSTRUCT query form with aggregate values, a
sub-query should be created inside the WHERE clause
EUCLID - Querying Linked Data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX mo: <http://purl.org/ontology/mo/>CONSTRUCT {?album music-ont:duration ?album_duration .}WHERE { SELECT ?album (SUM(?track_duration) AS ?album_duration) { dbpedia:The_Beatles foaf:made ?album . ?album mo:track ?track . ?track mo:duration ?track_duration . } GROUP BY ?album HAVING (SUM(?track_duration) > 3600000)}
Materialize the duration of the albums recorded by ‘The Beatles’.Query:
Query Form: CONSTRUCT (11)
39
UPDATING LINKED DATA WITH SPARQL 1.1
EUCLID - Querying Linked Data
40
Data Management
EUCLID - Querying Linked Data
SPARQL 1.1 provides data update operations:
• INSERT data: adds some triples, given inline in the request, into a graph
• DELETE data: removes some triples, given inline in the request, if the respective graphs contains those
• DELETE/INSERT data: uses in parallel INSERT and DELETE
CH 1
CH 1
41
Data Management (2)
EUCLID - Querying Linked Data
INSERT data
PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>
INSERT DATA { GRAPH { <http://musicbrainz.org/20130302> <http://musicbrainz.org/artist/b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d> foaf:made <http://musicbrainz.org/release/3a685770-7326-34fc-9f18-e5f5626f3dc5> ,
< http://musicbrainz.org/release/cb6f8798-d51e-4fa5-a4d1-2c0602bfe1b6 > .
<http://musicbrainz.org/release/3a685770-7326-34fc-9f18-e5f5626f3dc5> dc:title "Please Please Me".
< http://musicbrainz.org/release/cb6f8798-d51e-4fa5-a4d1-2c0602bfe1b6 >
dc:title "Something New". } }
Insert the following albums recorded by The Beatles into the graphhttp://musicbrainz.org/20130209-004702
CH 1
42
Data Management (3)
EUCLID - Querying Linked Data
DELETE data Delete all the information about the album Casualities of The Beatles.
PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>
DELETE { ?album ?predicate ?object . }WHERE {<http://musicbrainz.org/artist/b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d> foaf:made ?album .?album dc:title "Casualities". ?album ?predicate ?object .}
CH 1
43
Data Management (4)
EUCLID - Querying Linked Data
DELETE/INSERT data
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX db-ont: <http://dbpedia.org/ontology/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>
DELETE { dbpedia:The_Beatles db-ont:currentMember ?x . }INSERT { GRAPH <http://musicbrainz.org/20130209-004702> { dbpedia:The_Beatles db-ont:formerBandMember ?x .} }WHERE { dbpedia:The_Beatles db-ont:currentMember ?x . ?x foaf:name "Peter Best" .}
Delete the status of ‘Peter Best’ as current member of´The Beatles´, and insert his status as former member of the band.
44
Graph Management
EUCLID - Querying Linked Data
SPARQL 1.1 provides graph update operations:
• CREATE: creates an empty graph in the Graph Store
• LOAD: reads the content of a document into a graph in the Graph Store
• CLEAR: removes all triples in one or more graphs
• DROP: removes the graph from the Graph Store
• Other operations: COPY, MOVE, ADD
45
Graph Management (2)
EUCLID - Querying Linked Data
CREATE• Creates a new named graph• Can be used with the DEFAULT and ALL keywords
LOAD• An RDF graph can be loaded from a URL
• LOAD can be used with the SILENT keyword
CREATE GRAPH <http://musicbrainz.org/20130302>
LOAD <http://xmlns.com/foaf/spec/20100809.rdf>
LOAD <http://xmlns.com/foaf/spec/20100809.rdf> INTO <http://xmlns.com/foaf/0.1/>
Named graph
46
Graph Management (3)
EUCLID - Querying Linked Data
CLEAR• Removes all triples in the graph (it is emptied but not deleted!) • The graph(s) can be specified with the following keywords:
DEFAULT, NAMED, ALL, GRAPH • Can be used with the SILENT keyword
DROP• The given graph is removed from the Graph Store, including its
content• Can be used with the DEFAULT and ALL keywords
CLEAR GRAPH <http://musicbrainz.org/20130302>
DROP GRAPH <http://musicbrainz.org/20130302>
47
Graph Management (4)
EUCLID - Querying Linked Data
SPARQL 1.1 provides other graph management operations:
• COPY … TO …
• MOVE … TO …
• ADD … TO …
COPY GRAPH <http://musicbrainz.org/20130302>TO GRAPH <http://musicbrainz.org/20130303>
MOVE GRAPH <http://musicbrainz.org/temp>TO GRAPH <http://musicbrainz.org/20130303>
ADD GRAPH <http://musicbrainz.org/20130302>TO GRAPH <http://musicbrainz.org/20130303>
48
SPARQL PROTOCOL FOR RDF
EUCLID - Querying Linked Data
49
SPARQL 1.1. Protocol
EUCLID - Querying Linked Data
• Consists of two operations: query and update
• An operation defines:• The HTTP method (GET or POST)• The HTTP query string parameters• The message content included in the HTTP request body• The message content included in the HTTP response body
SPARQL Client SPARQL EndpointRequest
alt [no errors]
[else]
Success Response
Failure Response
50EUCLID - Querying Linked Data
• Sends a SPARQL query to a service and receives the results of the query
• The response is:
• May be invoked using HTTP GET or HTTP POST. The method POST with URL encoding is mostly used when the query string is too long
Query Operation
XML, JSON, CSV/TSV from a SELECT query
RDF/XML, Turtle from a CONSTRUCT query
51EUCLID - Querying Linked Data
Example:
PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX dbpedia-ont: <http://dbpedia.org/ontology/>SELECT ?album WHERE { ?album dbpedia-ont:artist dbpedia:The_Beatles .} LIMIT 3
SPARQL Query:
Query Operation (2)
HTTP GET request:
Try this query! (Click on the hurl icon)
52
Update Operation
EUCLID - Querying Linked Data
• Sends a SPARQL update request to a service
• Should be invoked using the HTTP PATCH/POST method
• The response consists of a HTTP response status code, which indicates success or failure of the operation
53
REASONING OVER LINKED DATA
EUCLID - Querying Linked Data
54
Reasoning forLinked Data Integration
EUCLID - Querying Linked Data
• Example: Integration of the MusicBrainz data set and the DBpedia data set
IntegrationData set Data set
55
Reasoning forLinked Data Integration
EUCLID - Querying Linked Data
mo:b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d foaf:name The Beatles; mo:member mo:ba550d0e-adac-4864-b88b-407cab5e76af; mo:member mo:4d5447d7-c61c-4120-ba1b-d7f471d385b9; mo:member mo:42a8f507-8412-4611-854f-926571049fa0; mo:member mo:300c4c73-33ac-4255-9d57-4e32627f5e13.
dbpedia:The_Beatles dbpedia-ont:origin dbpedia:Liverpool; dbpedia-ont:genre dbpedia:Rock_music; foaf:depiction .
IntegrationData set Data set
same
56
Reasoning forLinked Data Integration
EUCLID - Querying Linked Data
mo:b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d foaf:name The Beatles; mo:member mo:ba550d0e-adac-4864-b88b-407cab5e76af; mo:member mo:4d5447d7-c61c-4120-ba1b-d7f471d385b9; mo:member mo:42a8f507-8412-4611-854f-926571049fa0; mo:member mo:300c4c73-33ac-4255-9d57-4e32627f5e13.
dbpedia:The_Beatles dbpedia-ont:origin dbpedia:Liverpool; dbpedia-ont:genre dbpedia:Rock_music; foaf:depiction .
same
SELECT ?m ?g WHERE { dbpedia:The_Beatles dbpedia-ont:genre ?g; mo:member ?m.}
Query: ?m ?g
mo:ba550d0e-adac-4864-b88b-407cab5e76af
dbpedia:Rock_music
mo:4d5447d7-c61c-4120-ba1b-d7f471d385b9
dbpedia:Rock_music
mo42a8f507-8412-4611-854f-926571049fa0;
dbpedia:Rock_music
mo300c4c73-33ac-4255-9d57-4e32627f5e13
dbpedia:Rock_music
Result set:
57
SPARQL 1.1:Entailment Regimes
EUCLID - Querying Linked Data
• SPARQL 1.0 was defined only for simple entailment (pattern matching )
• SPARQL 1.1 is extended with entailment regimes other than simple entailment:– RDF entailment– RDFS entailment– D-Entailment– OWL RL entailment– OWL Full entailment– OWL 2 DL, EL, and QL entailment– RIF entailmentSource: http://www.w3.org/TR/rdf-mt/#RDFSRules
58
RDFS
EUCLID - Querying Linked Data
Resource Description Framework Schema
Semantic Web StackBerners-Lee (2006)
Taxonomies and inferences
59
RDFS Entailment Regimes
EUCLID - Querying Linked Data
• Contains 13 entailment rules denominated rdfsi for inference over RDFS definitions*:– rdfs:Literal (rdfs1, rdfs13)
– rdfs:domain (rdfs2), rdfs:range (rdfs3)
– rdfs:Resource (rdfs4a, rdfs4, rdfs8)
– rdfs:subPropertyOf (rdfs5, rdfs6, rdfs7, rdfs12)
– rdfs:Class (rdfs8, rdfs10)
– rdfs:subClassOf (rdfs9, rdfs10, rdfs11)
– rdfs:ContainerMembershipProperty (rdfs12)
– rdfs:Datatype (rdfs13)* Source: http://www.w3.org/TR/rdf-mt/#RDFSRules
60
rdfs2 – rdfs:domain
EUCLID - Querying Linked Data
dbpedia: The_Beatles
dbpedia: Paul_McCartney
SELECT ?x WHERE { ?x a mo:MusicGroup.}
mo:member rdfs:domain mo:MusicGroup .
?x ?x
dbpedia:The_Beatles …
mo:member
Schema: Query:
Result set: Result set with inference:
dbpedia: John_Lennon
dbpedia: George_Harrison
dbpedia: Ringo_Starr
mo:member mo:member
mo:member
61
rdfs3 – rdfs:range
EUCLID - Querying Linked Data
dbpedia: The_Beatles
dbpedia: Paul_McCartney
SELECT ?x WHERE { ?x a foaf:Agent.}
mo:member rdfs:range foaf:Agent .
?x ?x
dbpedia:Paul_McCartney
dbpedia:John_Lennon
dbpedia:Ringo_Starr
dbpedia:George_Harrison …
dbpedia-ont:bandMember
Schema: Query:
Result set: Result set with inference:
dbpedia: John_Lennon
dbpedia: George_Harrison
dbpedia: Ringo_Starr
dbpedia-ont:bandMember
dbpedia-ont:bandMember
dbpedia-ont:bandMember
62
rdfs7 – rdfs:subPropertyOf
EUCLID - Querying Linked Data
dbpedia: Yesterday
dbpedia: Paul_McCartney
SELECT ?x WHERE { dbpedia:Yesterday mo:performer ?x.}
mo:singer rdfs:subPropertyOf mo:performer .
?x
dbpedia:John_Lennon
dbpedia:Ringo_Starr
dbpedia:George_Harrison
?x
dbpedia:John_Lennon
dbpedia:Ringo_Starr
dbpedia:George_Harrison
dbpedia:Paul_McCartney
mo:singer
Schema: Query:
Result set: Result set with inference:
dbpedia: John_Lennon
dbpedia: George_Harrison
dbpedia: Ringo_Starr
mo:performer mo:performermo:performer
mo:performer
63
rdfs9 – rdfs:subClassOf
EUCLID - Querying Linked Data
dbpedia: The_Beatles
SELECT ?x WHERE { ?x a mo:MusicArtist.}
mo:MusicGroup rdfs:subClassOf mo:MusicArtist .
?x ?x
dbpedia:The_Beatles …
Schema: Query:
Result set: Result set with inference:
mo: MusicArtist
rdf:type
mo: MusicGroup
rdf:type
64
Inference from Schema
EUCLID - Querying Linked Data
• Knowledge encoded in the schema leads to infer new facts
• This is also captured in the set of axiomatic triples, which provide basic meaning for all the vocabulary terms
mo:MusicGroup rdfs:subClassOf mo:MusicArtist .
mo:MusicGroup a rdfs:Class .mo:MusicArtist a rdfs:Class .
Schema:
Inferred
facts:
rdfs:subClassOf rdfs:domain rdfs:Class .
rdfs:subClassOf rdfs:range rdfs:Class .
65
RDFS: Lack of Consistency Check
EUCLID - Querying Linked Data
• It is possible to infer facts that seem incorrect facts, but RDFS cannot prevent this:
Schema: mo:member rdfs:domain mo:MusicGroup ;
rdfs:range foaf:Agent .
Existing :PaulMcCartney a :SoloMusicArtist ; facts: :member :TheBeatles .
Inferred :PaulMcCartney a :MusicGroup . facts:
No contradiction!:The mis-modeling is not diagnosed
rdfs2
66
• We might wish further inferences, but these are beyond the entailment rules implemented by RDFS
RDFS: Inference Limitations
EUCLID - Querying Linked Data
foaf:knows rdfs:domain foaf:Person ; rdfs:range foaf:Person .
foaf:made rdfs:domain foaf:Agent . :PaulMcCartney foaf:made :Yesterday ; foaf:knows :RingoStarr .
:PaulMcCartney a foaf:Agent ; a foaf:Person .:RingoStarr a foaf:Person .
Schema:
Existing fact:
Inferred
facts:
:Yesterday dc:creator :PaulMcCartney.:RingoStarr foaf:knows :PaulMcCartney . These inferences require OWL!
NOT inferre
d:
Cannot model with RDFS that ‘x knows y’ implies ‘y knows x’
Cannot model with RDFS that if ‘x makes y’ implies that ‘the creator of y is x’
67
OWL
EUCLID - Querying Linked Data
Web Ontology Language
Semantic Web StackBerners-Lee (2006)
Ontologies and inferences
68
Introduction to OWL
EUCLID - Querying Linked Data
• Provides more ontological constructs and avoids some of the potential confusion in RDFS
• OWL 2 is divided into sub-languages denominated profiles:– OWL 2 EL: Limited to basic classification,
but with polynomial-time reasoning
– OWL 2 QL: Designed to be translatable to relational database querying
– OWL 2 RL: Designed to be efficiently implementable in rule-based systems
• Most triple stores concentrate on the use of RDFS with a subset of OWL features, called OWL-Horst or RDFS++
More restrictive than OWL DL
69
OWL Properties
EUCLID - Querying Linked Data
OWL distinguishes between two types of properties:• OWL ObjectProperties: resources as values• OWL DatatypeProperties: literals as values
:plays rdf:type owl:ObjectProperty;rdfs:domain :Musician;
rdfs:range :Instrument .
:hasMembers rdf:type owl:DatatypeProperty; rdfs:domain :MusicGroup
rdfs:range xsd:int .
70
Property Axioms
EUCLID - Querying Linked Data
• Property axioms include those from RDF Schema• OWL allows for property equivalence. Example:EquivalentObjectProperties(dbpedia-ont:bandMember mo:member)
dbpedia-ont:bandMember owl:equivalentProperty mo:member.
≡
dbpedia: The_Beatles
dbpedia: Paul_McCartney
mo:member
dbpedia: John_Lennon
dbpedia: George_Harrison
dbpedia: Ringo_Starr
mo:member
mo:member
mo:member
SELECT ?x {dbpedia:The_Beatles dbpedia-ont:bandMember ?x.}
Query:
?xResult set:
?x
dbpedia:Paul_McCartney
dbpedia:John_Lennon
dbpedia:Ringo_Starr
dbpedia:George_Harrison
Result set with inference:
71
Property Axioms
EUCLID - Querying Linked Data
• Property axioms include those from RDF Schema• OWL allows for property equivalence. Example:EquivalentObjectProperties(dbpedia-ont:bandMember mo:member)
dbpedia-ont:bandMember owl:equivalentProperty mo:member.
• OWL allows for property disjointness. Example: DisjointObjectProperty(dbpedia-ont:length mo:duration)
dbpedia-ont:length owl:propertyDisjointWith mo:duration.
• There is no standard for implementing inconsistency reports under SPARQL
≡
≡
72
Property Axioms (2)
EUCLID - Querying Linked Data
OWL allows the definition of property characteristics to infer new facts relating to instances and their properties
• Symmetry
• Transitivity
• Inverse
• Functional
• Inverse Functional
73
Property Axioms: Symmetry
EUCLID - Querying Linked Data
dbpedia: The_Beatles
dbpedia: Billy_Preston
dbpedia: Plastic_Ono_
Band :associatedMusicalArtist a owl:SymmetricProperty .
?genre
dbpedia:Plastic_Ono_Band
?genre
dbpedia:Plastic_Ono_Band
dbpedia:Billy_Preston
:associatedMusicalArtist
Schema:
Result set: Result set with inference:
SELECT ?x WHERE { dbpedia:The_Beatles :associatedMusicalArtist ?x.}
Query:
:associatedMusicalArtist
74
Property Axioms: Transitivity
EUCLID - Querying Linked Data
:Rock
:Heavy_ metal
:Black_ metal
:Punk_ rock SELECT ?genre WHERE {
:Rock :subgenre ?genre .}
:subgenre a owl:TransitiveProperty .
?genre
:Heavy_metal
:Punk_rock
?genre
:Heavy_metal
:Punk_rock
:Black_metal
:subgenre :subgenre
:subgenre :subgenre
Schema:
Query:
Result set: Result set with inference:
75
Property Axioms: Inverse
EUCLID - Querying Linked Data
SELECT ?x WHERE { ?x mo:member_of dbpedia:The_Beatles .}
mo:member_of owl:inverseOf mo:member.
?x
dbpedia:John_Lennon
dbpedia:George_Harrison
?x
dbpedia:John_Lennon
dbpedia:George_Harrison
dbpedia:Paul_McCartney
dbpedia:Ringo_Starr
Schema:
Query:
Result set: Result set with inference:
dbpedia: The_Beatles
dbpedia: Paul_McCartney
mo:member_of
dbpedia: John_Lennon
dbpedia: George_Harrison
dbpedia: Ringo_Starr
mo:member
mo:member_of
mo:member
mo:member_of mo:member_of
76
Example: Every artist primarily plays only one musical instrument
mo:primary_instrument rdf:type owl:FunctionalProperty .
dbpedia:Jimi_Hendrix mo:primary_instrument dbpedia:Electric_Guitar.dbpedia:Jimi_Hendrix mo:primary_instrument dbpedia:E-Guitar.
Conclusion dbpedia:Electric_Guitar
owl:sameAs dbpedia:E-Guitar .
Property Axioms:Functional
EUCLID - Querying Linked Data
It refers to a property that can have only one (unique) value for each instance
r2
mo:primary_
instrument
same
r1
mo:primary_ instrument
77
Example: Every recording has a unique ISRC(International Standard Recording Code)
mo:isrc rdf:type owl:InverseFunctionalProperty .
mo:21047249-7b3f-4651-acca-246669c081fd mo:isrc "GBAYE6300412" .dbpedia:She_Loves_You mo:isrc "GBAYE6300412" .
Conclusion mo:21047249-7b3f-4651-acca-246669c081fd
owl:sameAs :dbpedia:She_Loves_You .
Property Axioms:Inverse Functional
EUCLID - Querying Linked Data
It is useful for specifying unique properties identifying an individual
r2
mo:isrc
mo:isrcsam
e
r1
78
Individual Axioms
EUCLID - Querying Linked Data
OWL Individuals represent instances of classes. They are related to their class by the rdf:type property
• We can state that two individuals are the same SameIndividual(<artist/ba550d0e-adac-4864-b88b-407cab5e76af#_> dbpedia:PaulMcCartney)
<artist/ba550d0e-adac-4864-b88b-407cab5e76af#_> owl:sameAs dbpedia:PaulMcCartney .
• We can state that two individuals are different DifferentIndividuals(:TheBeatles_band :TheBeatles_TVseries)
:TheBeatles_band owl:differentFrom :TheBeatles_Tvseries .
≡
≡
79
Class Axioms
EUCLID - Querying Linked Data
Axioms declare general statements about concepts which are used in logical inference (reasoning). Class axioms:
• Sub-class relationship (from RDF Schema)
• Equivalent relationship: classes have the same individuals EquivalentClass(:Musician :MusicArtist)
:Musician owl:equivalentClass :MusicArtist .
• Disjointness: classes have no shared individuals DisjointClasses(:SoloMusicArtist :MusicGroup)
:SoloMusicArtist owl:disjointWith :MusicGroup .≡
≡
80
Class Construction
EUCLID - Querying Linked Data
• OWL classes are defined by the OWL term owl:Class
• OWL classes can be subclassed as in RDFS:
• OWL classes may be combined with class constructs to build new classes
Music Artist
Artist:MusicArtist rdfs:subClassOf :Artist .
Class Construction (2)
EUCLID - Querying Linked Data 81
These class constructs are available in OWL, not in RDFS The class of female music artists ObjectIntersectionOf(:Female :MusicArtist) [a owl:Class; owl:intersectionOf(:Female :MusicArtist)]
The class of music artists ObjectUnionOf(:SoloMusicArtist :MusicGroup) [a owl:Class; owl:unionOf(:SoloMusicArtist :MusicGroup)]
Everything that’s not instrumental music ObjectComplementOf(:InstrumentalMusic) [a owl:Class; owl:complementOf(:InstrumentalMusic)]
Female
Music Artist
Solo
Group
Instrumental
≡
≡
≡NOTE: Anonymous classes!
82
Naming Class Constructions
EUCLID - Querying Linked Data
• Direct naming can be achieved via owl:equivalentClass
• This construction provides necessary and sufficient conditions for class membership
• Class naming can be also achieved using rdfs:subClassOf, it provides a necessary but insufficient condition for class membership
Music ArtistSolo
Group
EquivalentClass(:MusicArtist ObjectUnionOf(:SoloMusicArtist :MusicGroup))
:MusicArtist owl:equivalentClass [owl:unionOf (:SoloMusicArtist :MusicGroup)]
≡
83
Summary
EUCLID - Querying Linked Data
• Basic concepts: triple patterns, graph patterns, SPARQL endpoint ...
• SPARQL Query:
• Query forms: ASK, SELECT, DESCRIBE, CONSTRUCT
• Query patterns: BGP, UNION, OPTIONAL, FILTER
• Sequence modifiers: DISTINCT, REDUCED, ORDER BY, LIMIT, OFFSET
• SPARQL 1.1 Update:
• Data management: INSERT, DELETE; DELETE/INSERT
• Graph management: LOAD, CLEAR, CREATE, DROP, COPY/MOVE/ADD
• SPARQL Protocol: query operation, update operation
Qu
ery
ing
Lin
ked
Data
In this chapter we studied:
84
Summary (2)
EUCLID - Querying Linked Data
• Reasoning over Linked Data:
• SPARQL 1.1 entailment regimes
• RDFS: entailment regimes, lacks of consistency check, inference limitations
• OWL: properties, property axioms (symmetry, transitivity, inverse, functional, inverse functional), individual axioms, class axioms, class constructions, naming classes …
Reason
ing
over
Lin
ked
DataIn this chapter we studied:
EUCLID - Providing Linked Data 85
For exercises, quiz and further material visit our website:
@euclid_project euclidproject euclidproject
http://www.euclid-project.eu
Other channels:
eBook Course
86
Acknowledgements
• Alexander Mikroyannidis• Alice Carpentier• Andreas Harth• Andreas Wagner• Andriy Nikolov• Barry Norton• Daniel M. Herzig• Elena Simperl• Günter Ladwig• Inga Shamkhalov• Jacek Kopecky• John Domingue
• Juan Sequeda• Kalina Bontcheva• Maria Maleshkova• Maria-Esther Vidal• Maribel Acosta• Michael Meier• Ning Li• Paul Mulholland• Peter Haase• Richard Power• Steffen Stadtmüller
People who have contributed to creating Euclid training content: