Upload
hakiet
View
216
Download
2
Embed Size (px)
Citation preview
SPARQL
Philippe Genoud ([email protected])
Ce cours est inspiré en partie du tutorial
ARQ : http://jena.sourceforge.net/ARQ/Tutorial
1
SPARQL : introduction
• RDF
– Resource Description Framework
– Flexible and extensible way to represent information about resources of the web
• SPARQL
– SPARQL Protocol And RDF Query Language
– a request language to access a RDF graph
– a protocol to submit request through HTTP GET, HTTP POST or SOAP
– an XML format for the results
2
SPARQL Status
• W3C Recommendations, 15 January 2008
– SPARQL Query Language for RDF
• http://www.w3.org/TR/rdf-sparql-query/
– SPARQL Protocol for RDF • http://www.w3.org/TR/rdf-sparql-protocol/
– SPARQL Query Results XML Format • http://www.w3.org/TR/2008/REC-rdf-sparql-XMLres-20080115/
• W3C Proposed Recommendation, 08 November 2012
– SPARQL 1.1 Query Language
• http://www.w3.org/TR/sparql11-query/
4
A simple query with SPARQL
@prefix vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <#> .
<http://somewhere/MattJones/>
vCard:FN "Matt Jones" ;
vCard:N [ vCard:Family "Jones" ;
vCard:Given "Matthew"
] .
<http://somewhere/RebeccaSmith/>
vCard:FN "Becky Smith" ;
vCard:N [ vCard:Family "Smith" ;
vCard:Given "Rebecca"
] .
<http://somewhere/JohnSmith/>
vCard:FN "John Smith" ;
vCard:N [ vCard:Family "Smith" ;
vCard:Given "John"
] .
<http://somewhere/SarahJones/>
vCard:FN "Sarah Jones" ;
vCard:N [ vCard:Family "Jones" ;
vCard:Given "Sarah"
]
• RDF data graph in Turtle notation
6
A simple query with SPARQL
@prefix vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://somewhere/MattJones/> vCard:FN "Matt Jones" .
<http://somewhere/MattJones/> vCard:N _:b0 .
_:b0 vCard:Family "Jones" .
_:b0 vCard:Given "Matthew" .
<http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" .
<http://somewhere/RebeccaSmith/> vCard:N _:b1 .
_:b1 vCard:Family "Smith" .
_:b1 vCard:Given "Rebecca" .
<http://somewhere/JohnSmith/> vCard:FN "John Smith" .
<http://somewhere/JohnSmith/> vCard:N _:b2 .
_:b2 vCard:Family "Smith" .
_:b2 vCard:Given "John" .
<http://somewhere/SarahJones/> vCard:FN "Sarah Jones" .
<http://somewhere/SarahJones/> vCard:N _:b3 .
_:b3 vCard:Family "Jones" .
_:b3 vCard:Given "Sarah" .
• or even more explicitly as triples:
7
@prefix vcard: <http://www.w3.org/2001/vcard
@prefix rdf: <http://www.w3.org/1999/02/22
<http://somewhere/JohnSmith/> vcard:FN "John Smith";
vcard:N _:A0.
<http://somewhere/MattJones/> vcard:FN "Matt Jones";
vcard:N _:A2.
<http://somewhere/RebeccaSmith/> vcard:FN
vcard:N _:A3.
<http://somewhere/SarahJones/> vcard:FN "Sarah Jones";
vcard:N _:A1.
_:A0 vcard:Family "Smith";
vcard:Given "John".
_:A1 vcard:Family "Jones";
vcard:Given "Sarah".
_:A2 vcard:Family "Jones";
vcard:Given "Matthew".
_:A3 vcard:Family "Smith";
vcard:Given "Rebecca".
A simple query with SPARQL
SELECT ?x
WHERE {
?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith"
}
variable
match the triple pattern in
the WHERE clause
against the triples in the
RDF graph
triple patern
(Subject Property Object)
---------------------------------
| x |
=================================
| <http://somewhere/JohnSmith/> |
--------------------------------- possible bindings for variable x 8
----------------------------------------------------
| x | name |
====================================================
| <http://somewhere/RebeccaSmith/> | "Becky Smith" |
| <http://somewhere/SarahJones/> | "Sarah Jones" |
| <http://somewhere/JohnSmith/> | "John Smith" |
| <http://somewhere/MattJones/> | "Matt Jones" |
----------------------------------------------------
Another simple query with SPARQL
SELECT ?x ?fname
WHERE {
?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> ?fname
}
It is possible to have more than one variable in the triple pattern
9
A third simple SPARQL request
SELECT ?givenName
WHERE {
?y <http://www.w3.org/2001/vcard-rdf/3.0#Family> "Smith" .
?y <http://www.w3.org/2001/vcard-rdf/3.0#Given> ?givenName .
}
-------------
| givenName |
=============
| "John" |
| "Rebecca" |
------------- 10
matches when the triple patterns
all match with the same value
used each time the variable with
the same name is used
Basic pattern : set of triple patterns
Exemples simples de requêtes SPARQL SELECT ?givenName WHERE { ?y <http://www.w3.org/2001/vcard-rdf/3.0#Family> "Smith" . ?y <http://www.w3.org/2001/vcard-rdf/3.0#Given> ?givenName . }
results in XML format
-------------
| givenName |
=============
| "John" |
| "Rebecca" |
-------------
11
<?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="givenName"/> </head> <results> <result> <binding name="givenName"> <literal>Rebecca</literal> </binding> </result> <result> <binding name="givenName"> <literal>John</literal> </binding> </result> </results> </sparql>
Q(ualified)Names
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?givenName
WHERE
{ ?y vcard:Family "Smith" .
?y vcard:Given ?givenName .
}
There is shorthand mechanism for writing long URIs using prefixes
12
------------- | givenName | ============= | "John" | | "Rebecca" | -------------
Prefix IRI
rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdfs: http://www.w3.org/2000/01/rdf-schema#
xsd: http://www.w3.org/2001/XMLSchema# « standards » prefixes
SELECT ?givenName
WHERE {
?y <http://www.w3.org/2001/vcard-rdf/3.0#Family> "Smith" .
?y <http://www.w3.org/2001/vcard-rdf/3.0#Given> ?givenName .
}
other simplifications (similars to Turtle)
?x c:firstName ?y .
?x c:lastName ?z .
?x c:firstName ?y ;
c:lastName ?z . triples with same subject
triples with same subject
and same predicate ?x c:name “Olivier” .
?x c:name “Laurent” .
?x c:name “Olivier” ,
“Laurent” .
Filters
• Filters : allow to restrict the values in a solution
– boolean expression the request solutions must satisfy.
– rich expression language based on Xpath, XQuery and special operators defined by SPARQL.
see section 11 of SPARQL specification document http://www.w3.org/TR/rdf-sparql-query/#tests
• Relational operators: <, >, =, <=, >=, !=
• Boolean operators: &&, ||, !
• Arithmetic operator: + * - /
• variable binding tests: isURI(?x), isBlank(?x), isLiteral(?x), bound(?x)
• regular expressions regex(?x, "A.*")
• Accès aux attributs/valeur lang(), datatype(), str()
• Fonctions de (re-)typage (casting) xsd:integer(?x)
• Fonctions externes / extensions
13
Filters: Testing Values
14
PREFIX info: <http://somewhere/peopleInfo#>
SELECT ?resource
WHERE
{
?resource info:age ?age .
FILTER (?age >= 24)
}
---------------------------------
| resource |
=================================
| <http://somewhere/JohnSmith/> |
---------------------------------
<http://somewhere/RebeccaSmith/>
info:age "23"^^xsd:integer ;
vCard:FN "Becky Smith" ;
vCard:N [ vCard:Family "Smith" ;
vCard:Given "Rebecca" ].
lets add an age property
to some persons
The arithmetic expression must be in parentheses (round brackets)
23
25
>= 24 ?
Filters: String matching
15
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?givenName
WHERE
{ ?y vcard:Given ?givenName .
FILTER regex(?givenName, "r", "i")
}
Regular expressions to test strings (like LIKE in SQL request)
given names with an "r"
or "R" in them -------------
| givenName |
=============
| "Rebecca" |
| "Sarah" |
-------------
FILTER regex(?x, "pattern" [, "flags"]) syntax
optional
XQuery regular expression language (similar toperl)
http://www.w3.org/TR/xpath-functions/#regex-syntax
case insensitive
OPTIONALs • SPARQL has a the ability to query for data but not to fail when that data does not
exist
• the OPTIONAL part of a query can extend the information found in the query solution but the non-optional information is returned anyway
16 PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name ?age
WHERE
{
?person vcard:FN ?name .
?person info:age ?age .
}
-----------------------
| name | age |
=======================
| "Becky Smith" | 23 |
| "John Smith" | 25 |
-----------------------
------------------------
| name | age |
=======================
| "Becky Smith" | 23 |
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name ?age
WHERE
{
?person vcard:FN ?name .
OPTIONAL { ?person info:age ?age } }
the info:age property must now be present in a solution
the triple pattern for the age is optional, there is a pattern solution for
the people who don't have age information
PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name ?age
WHERE
{
?person vcard:FN ?name .
?person info:age ?age
}
the info:age
property must be
present in a
solution
OPTIONALs
17
23
25
-----------------------
| name | age |
=======================
| "Becky Smith" | 23 |
| "John Smith" | 25 |
-----------------------
PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name ?age
WHERE
{
?person vcard:FN ?name .
OPTIONAL { ?person info:age ?age } }
the triple pattern for
the age is optional,
there is a pattern
solution for the people
who don't have age
information
OPTIONALs
18
23
25
------------------------
| name | age |
=======================
| "Becky Smith" | 23 |
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
?
OPTIONALs with FILTERs • The optional pattern can be any group pattern and may involve any SPARQL
pattern types, e.g. you can use OPTIONAL clauses.
19
PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name ?age
WHERE
{
?person vcard:FN ?name .
OPTIONAL { ?person info:age ?age . FILTER ( ?age > 24 ) }
}
------------------------
| name | age |
=======================
| "Becky Smith" | |
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
OPTIONAL is a binary operator that
combines two graph patterns. If the
group matches, the solution is extended,
if not, the original solution is given
?person vcard:FN ?name .
----------------------------------------------------
| person | name |
====================================================
| <http://somewhere/RebeccaSmith/> | "Becky Smith" |
| <http://somewhere/SarahJones/> | "Sarah Jones" |
| <http://somewhere/JohnSmith/> | "John Smith" |
| <http://somewhere/MattJones/> | "Matt Jones" |
----------------------------------------------------
?person info:age ?age . FILTER ( ?age > 24 )
------------------------------------------
| person | age |
==========================================
| <http://somewhere/JohnSmith/> | 25 |
------------------------------------------
Name of all the people and
age of those older than 24
------------------------
| name | age |
=======================
| "Becky Smith" | |
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
?
OPTIONALs with FILTERs
20
PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name ?age
WHERE
{
?person vcard:FN ?name .
OPTIONAL { ?person info:age ?age }
}
?person vcard:FN ?name .
----------------------------------------------------
| person | name |
====================================================
| <http://somewhere/RebeccaSmith/> | "Becky Smith" |
| <http://somewhere/SarahJones/> | "Sarah Jones" |
| <http://somewhere/JohnSmith/> | "John Smith" |
| <http://somewhere/MattJones/> | "Matt Jones" |
----------------------------------------------------
?person info:age ?age .
------------------------------------------
| person | age |
==========================================
| <http://somewhere/RebeccaSmith/> | 23 |
| <http://somewhere/JohnSmith/> | 25 |
------------------------------------------
------------------------
| name | age |
=======================
| "Becky Smith" | 23 |
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
. FILTER (?age > 24) }
( ?age > 24 )
FILTER is performed
after the OPTIONAL
variable age is
unbound
evaluation
exception
FAILURE !
?
OPTIONALs with FILTERs
21
PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name ?age
WHERE
{
?person vcard:FN ?name .
OPTIONAL { ?person info:age ?age }
}
?person vcard:FN ?name .
----------------------------------------------------
| person | name |
====================================================
| <http://somewhere/RebeccaSmith/> | "Becky Smith" |
| <http://somewhere/SarahJones/> | "Sarah Jones" |
| <http://somewhere/JohnSmith/> | "John Smith" |
| <http://somewhere/MattJones/> | "Matt Jones" |
----------------------------------------------------
?person info:age ?age .
------------------------------------------
| person | age |
==========================================
| <http://somewhere/RebeccaSmith/> | 23 |
| <http://somewhere/JohnSmith/> | 25 |
------------------------------------------
------------------------
| name | age |
=======================
| "Becky Smith" | 23 |
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
. FILTER (?age > 24) }
FILTER ( ?age > 24 )
FILTER is performed
after the OPTIONAL
variable age is
unbound
evaluation
exception
FAILURE !
----------------------------------------------------
| person | name |
====================================================
| <http://somewhere/RebeccaSmith/> | "Becky Smith" |
| <http://somewhere/SarahJones/> | "Sarah Jones" |
| <http://somewhere/JohnSmith/> | "John Smith" |
| <http://somewhere/MattJones/> | "Matt Jones" |
----------------------------------------------------
------------------------------------------
| person | age |
==========================================
| <http://somewhere/RebeccaSmith/> | 23 |
| <http://somewhere/JohnSmith/> | 25 |
------------------------------------------
------------------------
| name | age |
=======================
| "Becky Smith" | 23 |
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
?
OPTIONALs with FILTERs • bound operator (like PHP isset) allows the use of unbound variables in FILTERs
22
PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name ?age
WHERE
{
?person vcard:FN ?name .
OPTIONAL { ?person info:age ?age .}
FILTER ( ?age > 24 )
}
------------------------
| name | age |
=======================
| "Sarah Jones" | |
| "John Smith" | 25 |
| "Matt Jones" | |
-----------------------
FILTER
name of all people whose age is undefined or older than 24
FILTER ( !bound(?age) || ?age > 24)
?person vcard:FN ?name .
?person info:age ?age .
!bound(?age) || ?age > 24
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name
WHERE
{
?x a foaf:Person .
OPTIONAL { ?x foaf:name ?name }
OPTIONAL { ?x vCard:FN ?name }
}
OPTIONALs and Order Dependent Queries is you use the same variable in two or more optional clauses order is relevant
23
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
_:a a foaf:Person ;
foaf:name "Matt Jones" .
_:b a foaf:Person ;
foaf:name "Sarah Jones";
vcard:FN "S. Jones“ .
_:c a foaf:Person ;
vcard:FN "Becky Smith" .
_:d a foaf:Person ;
vcard:FN "John Smith" .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name
WHERE
{
?x a foaf:Person .
OPTIONAL { ?x vCard:FN ?name }
OPTIONAL { ?x foaf:name ?name }
}
-------------------------|
| x | name |
==========================
| A11556 | "John Smith" |
--------------------------
-------------------------|
| x | name |
==========================
| A11556 | "John Smith" |
| A11554 | "Becky Smith" |
--------------------------
OPTIONALs and Order Dependent Queries
24
-------------------------|
| x | name |
==========================
| A11556 | |
| A11555 | |
| A11554 | "Sarah Jones" |
| A11553 | "Matt Jones" |
--------------------------
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name
WHERE
{
?x a foaf:Person .
}
?x a foaf:Person .
OPTIONAL { ?x foaf:name ?name } ?x vCard:FN ?name
A11553 vCard:FN "Matt Jones"
A11556 vCard:FN ?name
A11556 vCard:FN ?name
A11556 vCard:FN "Sarah Jones"
-----------------
| name |
=================
| "John Smith" |
| "Becky Smith" |
| "Sarah Jones" |
| "Matt Jones" |
-----------------
OPTIONAL { ?x vCard:FN ?name }
OPTIONAL { ?x foaf:name ?name }
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name
WHERE
{
?x a foaf:Person .
}
OPTIONAL { ?x vCard:FN ?name }
OPTIONAL { ?x foaf:name ?name }
OPTIONALs and Order Dependent Queries
25
-------------------------|
| x | name |
==========================
| A11556 | "John Smith" |
| A11555 | "Becky Smith" |
| A11554 | "S. Jones" |
| A11553 | |
--------------------------
?x a foaf:Person .
OPTIONAL { ?x vCard:FN ?name } ?x foaf:name ?name
A11553 foaf:name ?name
A11556 foaf:name "John Smith"
A11556 foaf:name "Becky Smith"
A11556 foaf:name "S. Jones"
-------------------------|
| x | name |
==========================
| A11553 | "Matt Jones" |
--------------------------
-----------------
| name |
=================
| "John Smith" |
| "Becky Smith" |
| "S. Jones" |
| "Matt Jones" |
-----------------
Union
26
-----------------
| name |
=================
| "Sarah Jones" |
| "Matt Jones" |
-----------------
[] foaf:name ?name [] vCard:FN ?name
-----------------
| name |
=================
| "John Smith" |
| "Becky Smith" |
| "S. Jones" |
-----------------
-----------------
| name |
=================
| "Sarah Jones" |
| "Matt Jones" |
| "John Smith" |
| "Becky Smith" |
| "S. Jones" |
-----------------
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name
WHERE {
{ [] foaf:name ?name } UNION { [] vCard:FN ?name }
}
pattern to query for one of a number of possibilities
---------------------------------
| name1 | name2 |
=================================
| "Sarah Jones" | |
| "Matt Jones" | |
---------------------------------
Union
27
[] foaf:name ?name1 [] vCard:FN ?name2
If different variables are used in each branch, the application can discover which sub-pattern caused the match
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name1 ?name2
WHERE {
{ [] foaf:name ?name1 } UNION { [] vCard:FN ?name2 }
}
SELECT ?name1 ?name2 SELECT ?name1 ?name2
---------------------------------
| name1 | name2 |
=================================
| | "John Smith" |
| | "Becky Smith" |
| | "S. Jones" |
---------------------------------
---------------------------------
| name1 | name2 |
=================================
| "Sarah Jones" | |
| "Matt Jones" | |
| | "John Smith" |
| | "Becky Smith" |
| | "S. Jones" |
---------------------------------
---------------------------------
| name1 | name2 |
=================================
| "Sarah Jones" | |
| "Matt Jones" | |
---------------------------------
Union
28
[] foaf:name ?name1 [] vCard:FN ?name2
If different variables are used in each branch, the application can discover which sub-pattern caused the match
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name1 ?name2
WHERE {
{ [] foaf:name ?name1 } UNION { [] vCard:FN ?name2 }
}
SELECT ?name1 ?name2 SELECT ?name1 ?name2
---------------------------------
| name1 | name2 |
=================================
| | "John Smith" |
| | "Becky Smith" |
| | "S. Jones" |
---------------------------------
---------------------------------
| name1 | name2 |
=================================
| "Sarah Jones" | |
| "Matt Jones" | |
| | "John Smith" |
| | "Becky Smith" |
| | "S. Jones" |
---------------------------------
---------------------------------
| x | name1 | name2 |
==================================
| A11556 | | |
| A11555 | | |
| A11554 | "Sarah Jones" | |
| A11553 | "Matt Jones" | |
----------------------------------
Union ≠ OPTIONALs
29
?x foaf:name ?name1 ?x vCard:FN ?name2
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?name1 ?name2
WHERE {
?x a foaf:Person
OPTIONAL { ?x foaf:name ?name1 }
OPTIONAL { ?x vCard:FN ?name2 }
}
OPTIONAL OPTIONAL
---------------------------------
| name1 | name2 |
=================================
| | "John Smith" |
| | "Becky Smith" |
| "Sarah Jones" | "S. Jones" |
| "Matt Jones" | |
---------------------------------
-------------------------|
| x | name1 | name2 |
==========================
| A11556 | | |
| A11555 | | |
| A11554 | | |
| A11553 | | |
--------------------------
?x a foaf:Person .
SELECT ?name1 ?name2
-----------------------------------------
| x | name1 | name2 |
=========================================
| A11556 | | "John Smith" |
| A11555 | | "Becky Smith"|
| A11554 | "Sarah Jones" | "S. Jones" |
| A11553 | "Matt Jones" | |
-----------------------------------------
Datasets • an RDF Dataset is the unit that is queried by a SPARQL query. It consists of
a default graph, and a number of named graphs.
• graph matching operations (basic patterns, OPTIONALs, and UNIONs) work on one RDF graph. – by default …. the default graph
– it can be changed by the GRAPH keyword.
30
GRAPH uri | ?var { ... pattern ... }
– uri : the pattern will be matched against the graph in the dataset with that name
– ?var : all the named graphs (not the default graph) are tried The variable may be used elsewhere so that if, during execution, it's value is already known for a solution, only the specific named graph is tried.
prefix foaf: <http://xmlns.com/foaf/0.1/> SELECT * where { ?x rdf:type foaf:Person. GRAPH <http://www.ujf-grenoble.fr/data> { ?x foaf:name ?name } GRAPH ?g { ?x foaf:knows ?y } }
names must come from the graph <http://www.ujf-grenoble.fr/>
to know the source of the result for each contact found
Datasets • a (very simple) dataset that might occur for an RDF aggregator
of book details – two small graphs describing some books
– a default graph which records when these graphs were last read
31
@prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <ds-ng-1.ttl> dc:date "2005-07-14T03:18:56+0100"^^xsd:dateTime . <ds-ng-2.ttl> dc:date "2005-09-22T05:53:05+0100"^^xsd:dateTime .
@prefix dc: <http://purl.org/dc/elements/1.1/> . [] dc:title "Harry Potter and the Philospher's Stone". [] dc:title "Harry Potter and the Chamber of Secrets".
@prefix dc: <http://purl.org/dc/elements/1.1/> . [] dc:title "Harry Potter and the Sorcerer's Stone" . [] dc:title "Harry Potter and the Chamber of Secrets".
Harry Potter and the Philospher's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Named graph <ds-ng-1.ttl>
Harry Potter and the Sorcerer's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Named graph <ds-ng-2.ttl>
Dataset
<ds-ng-1.ttl> 2005-07-14 ...
2005-09-22 ... <ds-ng-2.ttl> dc:date
dc:date Default graph
ds-dft.ttl
ds-ng-1.ttl
ds-ng-2.ttl
Datasets
32
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT * WHERE { ?s ?p ?o }
<ds-ng-1.ttl> 2005-07-14 ...
2005-09-22 ... <ds-ng-2.ttl> dc:date
dc:date
Harry Potter and the Philospher's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Harry Potter and the Sorcerer's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Named graph <ds-ng-1.ttl>
Named graph <ds-ng-2.ttl>
Default graph
Dataset
---------------------------------------------------------------------- | s | p | o | ====================================================================== | :ds-ng-2.ttl | dc:date | "2005-09-22T05:53:05+01:00"^^xsd:dateTime | | :ds-ng-1.ttl | dc:date | "2005-07-14T03:18:56+01:00"^^xsd:dateTime | ----------------------------------------------------------------------
only the default
graph is queried
Datasets
33
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT * WHERE{ { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }
<ds-ng-1.ttl> 2005-07-14 ...
2005-09-22 ... <ds-ng-2.ttl> dc:date
dc:date
Harry Potter and the Philospher's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Harry Potter and the Sorcerer's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Named graph <ds-ng-1.ttl>
Named graph <ds-ng-2.ttl>
Default graph
Dataset
--------------------------------------------------------------------------------------- | s | p | o | g | ======================================================================================= | :ds-ng-2.ttl | dc:date | "2005-09-22T05:53:05+01:00"^^xsd:dateTime | | | :ds-ng-1.ttl | dc:date | "2005-07-14T03:18:56+01:00"^^xsd:dateTime | | | _:b0 | dc:title | "Harry Potter and the Sorcerer's Stone" | :ds-ng-2.ttl | | _:b1 | dc:title | "Harry Potter and the Chamber of Secrets" | :ds-ng-2.ttl | | _:b2 | dc:title | "Harry Potter and the Chamber of Secrets" | :ds-ng-1.ttl | | _:b3 | dc:title | "Harry Potter and the Philospher's Stone" | :ds-ng-1.ttl | ---------------------------------------------------------------------------------------
the default graph
and the named
graphs are queried
Datasets
34
PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?title { GRAPH :ds-ng-2.ttl { ?b dc:title ?title } }
<ds-ng-1.ttl> 2005-07-14 ...
2005-09-22 ... <ds-ng-2.ttl> dc:date
dc:date
Harry Potter and the Philospher's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Harry Potter and the Sorcerer's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Named graph <ds-ng-1.ttl>
Named graph <ds-ng-2.ttl>
Default graph
Dataset
--------------------------------------------- | title | ============================================= | "Harry Potter and the Sorcerer's Stone" | | "Harry Potter and the Chamber of Secrets" | ---------------------------------------------
only the named graph ds-ng-2.ttl
is queried
Datasets
35
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?date ?title WHERE { ?g dc:date ?date . FILTER (?date > "2005-08-01T00:00:00Z"^^xsd:dateTime) GRAPH ?g { ?b dc:title ?title } }
<ds-ng-1.ttl> 2005-07-14 ...
2005-09-22 ... <ds-ng-2.ttl> dc:date
dc:date
Harry Potter and the Philospher's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Harry Potter and the Sorcerer's Stone
dc:title
Harry Potter and the Chamber of Secrets
dc:title
Named graph <ds-ng-1.ttl>
Named graph <ds-ng-2.ttl>
Default graph
Dataset
----------------------------------------------------------------------------------------- | date | title | ========================================================================================= | "2005-09-22T05:53:05+01:00"^^xsd:dateTime | "Harry Potter and the Sorcerer's Stone" | | "2005-09-22T05:53:05+01:00"^^xsd:dateTime | "Harry Potter and the Chamber of Secrets" | -----------------------------------------------------------------------------------------
The name of the graph to be queried is determined
with the query itself.
FROM and FROM NAMED
• FROM and FROM NAMED allow to describe the RDF dataset used for a query – FROM url the graphs defining the default data set
• There can be more than one FROM clause and the default graph is the RDF merge of the individual graphs
– FROM NAMED url the named graphs used to solve the request • The graph is given the name url and the data is read from that location.
• Multiple FROM NAMED clauses cause multiple graphs to be added to the dataset.
36
prefix foaf: <http://xmlns.com/foaf/0.1/> FROM NAMED <http://www.ujf-grenoble.fr/data> FROM NAMED <http://www.cnrs.fr/base> SELECT * where { GRAPH ?g { ?x foaf:name ?name } }
prefix foaf: <http://xmlns.com/foaf/0.1/> FROM <http://www.ujf-grenoble.fr/data> FROM <http://www.cnrs.fr/base> SELECT * where { ?x foaf:knows ?y } }
RDF 1.1 will provide a standard way to name graphs
Solution Modifiers • The set of solution produced by graphs pattern matching can be modified
in various ways: – Projection - keep only selected variables
– OFFSET/LIMIT - chop the number solutions (best used with ORDER BY)
• OFFSET the start index,
• LIMIT the number of solutions to be returned.
– Using LIMIT alone useful to ensure not too many solutions are returned, to restrict the effect of some unexpected situation
– ORDER BY - sorted results
– DISTINCT - yield only one row for one combination of variables and values.
• The solution modifiers OFFSET/LIMIT and ORDER BY always apply to all result forms
37
prefix foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT * where { ?x foaf:name ?name; faof:age ?age. } ORDER BY ?name DESC(?age)
example
ASK
• ASK – ask a boolean query. – to verify that there is at least one response.
– Is there a student over 30 years?
38
PREFIX ufrimag: <http://www.ufrimag.fr#> ASK { ?etudiant ufrimag:inscrit ?x . ?x ufrimag:siteweb <http://www.ufrimag.fr> . ?etudiant ufrimag:age ?age . FILTER (?age > 30) }
<sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> … </head> <boolean> true </boolean> </sparql>
the boolean result
CONSTRUCT
• SELECT returns a flat list of variables bindings – the application program is in charge of processing these bindings
(often by converting solution tuples into triples and adding them to a RDF graph)
• CONSTRUCT allows you to directly product a RDF graph containing the variables values – the WHERE and FILTER clause works the same way as the SELECT form
– bindings of the variables are inserted into a new graph constructed from template triples specified in the CONSTRUCT clause (which replace the SELECT clause).
39
Construct
40
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
CONSTRUCT { ?x foaf:firstName ?y .
?x foaf:lastName ?z .
}
FROM <vca-db-1rd.rdf>
WHERE
{ ?x vcard:N ?u .
?u vcard:Given ?y .
?u vcard:Family ?z.
}
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
<http://somewhere/RebeccaSmith/>
foaf:firstName "Rebecca" ;
foaf:lastName "Smith" .
<http://somewhere/MattJones/>
foaf:firstName "Matthew" ;
foaf:lastName "Jones" .
<http://somewhere/SarahJones/>
foaf:firstName "Sarah" ;
foaf:lastName "Jones" .
<http://somewhere/JohnSmith/>
foaf:firstName "John" ;
foaf:lastName "Smith" .
CONSTRUCT avec un graph-gabarit à
un seul sujet et deux triplets
D’après : http://pagesperso-systeme.lip6.fr/Jean-Francois.Perrot/inalco/XML/RDF/SPARQL/IntroSPARQL.html
CONSTRUCT
42
@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> . @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://inalco/M2-Trad/poetes#Théodore_de_BANVILLE> foaf:firstName "Théodore" ; foaf:lastName "de BANVILLE" ; foaf:pastProject [ dc:publisher "Paris : M. Levy, 1859." ; dc:title "Odes funambulesques" ] .
PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> CONSTRUCT { ?auteur foaf:pastProject _:oeuvre . _:oeuvre dc:title ?nom . _:oeuvre dc:publisher ?editeur . ?auteur foaf:firstName ?p . ?auteur foaf:lastName ?n . } FROM <http://pagesperso-systeme.lip6.fr/Jean-Francois.Perrot/inalco/XML/RDF/Frantext/RRb.rdf> WHERE { ?oe dc:creator ?auteur . ?oe dc:title ?nom . ?oe dc:publisher ?editeur . ?auteur vcard:N ?vc . ?vc vcard:Family ?n . ?vb vcard:Given ?p . }
DESCRIBE
• DESCRIBE – Returns an RDF graph, based on what the query processor is configured to return.
– SPARQL specification says : "the useful information the service has about a resource"
– in theory this should help you understand the context of the resources returned… but there is no warranty.
43
DESCRIBE <http://example.com/fish> ?x WHERE { ?x ?y <http://example.com/fish> }
PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX ex: <http://example.com/> DESCRIBE ex:karen ?friend { ex:karen foaf:knows ?friend . }
asks for a description of fish, and any resource directly related to fish.
asks for a description of karen ad her friends
New functionalities of SPARQL 1.1
• Aggregation of the results (min, max, average, etc.)
• Path expressions (a bit like regular expressions)
• support for CRUD
– Creation
– Update
– Deletion
• New serialization format for Query results (JSON…)
44
Jean API for SPARQL
• ARQ : query engine for Jena that supports the SPARQL RDF Query language.
• javadoc – http://jena.apache.org/documentation/javadoc/arq/index.html
• com.hp.hpl.jena.query package for ARQ API
45
com.hp.hpl.jena.query Key classes
• Query represents the application query.
– QueryFactory : to create Query instances
• QueryExecution - represents one execution of a query.
– QueryExecutionFactory - a place to get QueryExecution instances
• Dataset, a collection of named graphs and a default graph (background graph or or unnamed graph).
– DatasetFactory - a place to make datasets
46
com.hp.hpl.jena.query Key classes
• For SELECT queries:
– QuerySolution - A single solution to the query
– ResultSet - An iterator over all the the QuerySolutions.
– ResultSetFormatter : turns a ResultSet into various forms
• text,
• Model (an RDF graph)
• plain XML
• …
47
SELECT queries
48
import com.hp.hpl.jena.query.* ; Model model = ... ;
String queryString = " ...SELECT... " ;
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
try { ResultSet results = qexec.execSelect() ; for ( ; results.hasNext() ; ) { QuerySolution soln = results.nextSolution() ; RDFNode x = soln.get("varName") ; Resource r = soln.getResource("varR") ; Literal l = soln.getLiteral("varL") ; } } finally { qexec.close() ; }
import for ARQ classes better to have explicit imports import com.hp.hpl.jena.query.Query
construct the SPARQL query String
construct the SPARQL query String
produce an instance of QueryExecution
Preparing the request
can be reduced to one step in some common cases: QueryExecution qexec = QueryExecutionFactory.create(queryString, model) ;
SELECT queries
49
import com.hp.hpl.jena.query.* ; Model model = ... ;
String queryString = " ...SELECT... " ;
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
try { ResultSet results = qexec.execSelect() ; for ( ; results.hasNext() ; ) { QuerySolution soln = results.nextSolution() ; RDFNode x = soln.get("varName") ; Resource r = soln.getResource("varR") ; Literal l = soln.getLiteral("varL") ; } } finally { qexec.close() ; }
execute the query
Executing the request and exploiting the results
it's an ARQ Resultset not a java.sql.ResultSet !
lteration loop to traverse QuerySolutions
ResultSet presents the results of a SELECT query in table-like manner.
Each row (QuerySolution) corresponds to a set of bindings which fulfill the conditions of the query.
SELECT queries
50
for ( ; results.hasNext() ; ) { QuerySolution soln = results.nextSolution() ; RDFNode n = soln.get("varName") ; Resource r = soln.getResource("varR") ; Literal l = soln.getLiteral("varL") ; }
Executing the request and exploiting the results (continued)
Instead of a loop to deal with each row in the result set, the application can call an operation of the ResultSetFormatter.
process results to produce a simple text presentation
If you need to test the thing returned
ResultSetFormatter fmt = new ResultSetFormatter(results, query) ; fmt.printAll(System.out) ;
ResultSetFormatter.out(System.out, results, query) ;
or simply
CONSTRUCT and DESCRIBE queries
• CONSTRUCT
51
import com.hp.hpl.jena.query.* ; Model model = ... ;
String queryString = " ...CONSTRUCT... " ;
Query query = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(query, model) ; Model resultModel = qexec.execConstruct() ; qexec.close() ;
CONSTRUCT queries return a single RDF graph
• DESCRIBE import com.hp.hpl.jena.query.* ; Model model = ... ;
String queryString = " ...DESCRIBE... " ;
Query query = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(query, model) ; Model resultModel = qexec.execDescribe() ; qexec.close()
DESCRIBE queries return a single RDF graph
ASK queries
52
• ASK import com.hp.hpl.jena.query.* ; Model model = ... ;
String queryString = " ...ASK... " ;
Query query = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(query, model) ; boolean result = qexec.execAsk() ; qexec.close() ;
ASK queries return a boolean
Datasets • A SPARQL query is made on a dataset : a default graph and zero
or more named graph
53
String dftGraphURI = "file:default-graph.ttl" ; List namedGraphURIs = new ArrayList() ; namedGraphURIs.add("file:named-1.ttl") ; namedGraphURIs.add("file:named-2.ttl") ; Query query = QueryFactory.create(queryString) ; Dataset dataset = DatasetFactory.create(dftGraphURI, namedGraphURIs) ; QueryExecution qExec = QueryExecutionFactory.create(query, dataset) ; try { ... } finally { qExec.close() ; }
• Already existing models can also be used DataSet dataSource = DatsetFactory.createMem() ; dataSource.setDefaultModel(model) ; dataSource.addNamedModel("http://example/named-1", modelX) ; dataSource.addNamedModel("http://example/named-2", modelY) ; QueryExecution qExec = QueryExecutionFactory.create(query, dataSource) ;
// Creates an in-memory, modifiable Dataset
Ressources SPARQL
• http://thefigtrees.net/lee/sw/sparql-faq SPARQL Frequently Asked Questions
• http://www.w3.org/wiki/SparqlImplementations SPARQL implementations - community maintained list of open-source and commercial SPARQL engines
• http://www.w3.org/wiki/SparqlEndpoints Public SPARQL endpoints - community maintained list
54
• Goal: “expose” open datasets in RDF
• Set RDF links among the data items from different datasets
• Set up, if possible, query endpoints
Linked Open Data Project
• DBpedia is a community effort to
– extract structured (“infobox”) information from Wikipedia
– provide a query endpoint to the dataset
– interlink the DBpedia dataset with other datasets on the Web
Example data source: DBpedia
Extracting structured data from Wikipedia
@prefix dbpedia <http://dbpedia.org/resource/>.
@prefix dbterm <http://dbpedia.org/property/>.
dbpedia:Amsterdam
dbterm:officialName "Amsterdam" ;
dbterm:longd "4" ;
dbterm:longm "53" ;
dbterm:longs "32" ;
dbterm:website <http://www.amsterdam.nl> ;
dbterm:populationUrban "1364422" ;
dbterm:areaTotalKm "219" ;
...
dbpedia:ABN_AMRO
dbterm:location dbpedia:Amsterdam ;
...
Automatic links among open datasets
<http://dbpedia.org/resource/Amsterdam>
owl:sameAs <http://rdf.freebase.com/ns/...> ;
owl:sameAs <http://sws.geonames.org/2759793> ;
...
<http://sws.geonames.org/2759793>
owl:sameAs <http://dbpedia.org/resource/Amsterdam>
wgs84_pos:lat "52.3666667" ;
wgs84_pos:long "4.8833333";
geo:inCountry <http://www.geonames.org/countries/#NL> ;
...
Processors can switch automatically from one to the other…
• It provides a core set of data that Semantic Web applications can build on
– stable references for “things”, • e.g., http://dbpedia.org/resource/Amsterdam
– many many relationships that applications may reuse • e.g., the BBC application!
– a “nucleus” for a larger, semantically enabled Web!
• For many, publishing data may be the first step into the world of Semantic Web
The importance of Linked Data
• Publish your data first, care about sexy user interfaces later!
– the “raw data” can become useful on its own right and others may use it
– you can add your added value later by providing nice user access
• If possible, publish your data in RDF but if you cannot, others may help you in conversions
– trust the community…
• Add links to other data. “Just” publishing isn’t enough…
Some things to remember if you publish data