Slide Semweb 7 Sparql
Slide Semweb 7 Sparql
SPARQL
Sergej Sizov
Semantic Web
Semantic Web Language Layer Cake
Existing standards
WeST Sergej Sizov Semantic Web
[email protected] 2
SPARQL
PREFIX
SELECT Values to be returned
FROM
WHERE { }
SELECT
returns the set of variables bound in a query pattern match
CONSTRUCT
returns an RDF graph constructed by substituting variables
in a set of triple templates
DESCRIBE
returns an RDF graph that describes the resources found
ASK
returns whether a query pattern matches any triples or not
True / False query
Triple Pattern
Similar to an RDF Triple
subject, predicate, object
Any component can be a query variable
Any combination of variables in the query is allowed
foaf:page ?page
?person
foaf:name
?name
name page
FILTER clause
Support for AND and OR logic operators
Extensive applications for testing literals
Support for numerical operations
Support for math equality operators for literals
Less than equal greater than
Use of regular expressions
Support for datatypes defined in XSL
e.g. comparison of dates, time
Possible comparison of resources
Equal or not equal
Even possible user extensions
title price
Query Result
"The Semantic Web" 23
{ ?x foaf:name ?name .
?x foaf:homepage ?page .
FILTER regex(?name, Steffen") }
{ ?x foaf:name ?name .
FILTER regex(?name, Steffen") .
?x foaf:homepage ?page }
OPTIONAL
Include optional triple patterns to the match
Optional is a pattern itself can include further constraints
SELECT
WHERE {
OPTIONAL { }
}
OPTIONAL is left-associative
pattern OPTIONAL { pattern } OPTIONAL { pattern }
is the same as
{ pattern OPTIONAL { pattern } } OPTIONAL { pattern }
name page
name page
Maciej Janik"
UNION
Combining alternative graph patterns
If more than one of the alternatives matches, all the
possible pattern solutions are included in result
SELECT
WHERE {
{ pattern }
UNION
{ pattern }
}
title
ORDER BY
Sort results alphabetically / numerically by specific variable
LIMIT
Limit number of returned results (only top n results)
OFFSET
Skip n top results, and return the rest
isBlank
Testing if bounded variable is a blank node
SELECT ?given ?family
annotation
WHERE { ?annot dc:creator ?c .
OPTIONAL { dc:creator
?c foaf:given ?given .
:_
?c foaf:family ?family }. foaf:given
foaf:family
FILTER isBlank(?c) }
Alan Smith
lang
Accessing the language of a literal
SELECT ?name ?mbox
WHERE { ?x foaf:name ?name .
?x foaf:mbox ?mbox .
FILTER ( lang(?name) = DE ) }
isLiteral
Testing if bounded variable is a literal (not a resource)
SELECT ?name ?mbox
foaf:name Maciej
WHERE { ?x foaf:name ?name .
?x foaf:mbox ?mbox . :_
FILTER isLiteral(?mbox) }
foaf:mbox janik@
str
Converting resource URI to string for regular expression matching
SELECT ?name ?mbox
WHERE { ?x foaf:name ?name .
?x foaf:mbox ?mbox .
FILTER regex(str(?mbox), "@uni-koblenz.de") }
term1 = term2
or
sameTerm(term1, term2)
Returns true, if
terms are of the same type (URI, literal, blank node)
two terms represent URIs are equivalent
two terms represent literals are equivalent
two terms are bound by the same blank node
WeST Sergej Sizov Semantic Web
[email protected] 27
Equal terms
Find people who have the same email address, but use
different names
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice".
_:a foaf:mbox <mailto:[email protected]> .
_:b foaf:name "Ms A. .
_:b foaf:mbox <mailto:[email protected]> .
ancestorOf ancestorOf
daugherOf sonOf
Alice Bob Clare
ancestorOf
Query Result
SELECT ?x Clare
WHERE ?x ancestorOf Alice Bob
PREFIX
CONSTRUCT
{
graph pattern
definition of triples
}
WHERE
{
constraint triple patterns, filters, etc
}
WeST Sergej Sizov Semantic Web
[email protected] 32
Constructing graphs
Data: Query:
@prefix foaf: PREFIX foaf:
<http://xmlns.com/foaf/0.1/> . <http://xmlns.com/foaf/0.1/>
_:a foaf:givenname "Alice" . PREFIX vcard:
_:a foaf:family_name "Hacker" . <http://www.w3.org/2001/vcard-
_:b foaf:firstname "Bob" . rdf/3.0#>
_:b foaf:surname "Hacker" . CONSTRUCT
{
?x vcard:N _:v .
_:v vcard:givenName ?gname .
_:v vcard:familyName ?fname
}
Result: WHERE
@prefix vcard: {
<http://www.w3.org/2001/vcard-rdf/3.0#> { UNION
?x foaf:firstname ?gname }
_:v1 vcard:N _:x . { ?x foaf:givenname ?gname } .
_:x vcard:givenName "Alice" . { ?x foaf:surname ?fname }
_:x vcard:familyName "Hacker" . UNION
_:v2 vcard:N _:z . { ?x foaf:family_name ?fname }
_:z vcard:givenName "Bob" . }
_:z vcard:familyName "Hacker" .
YES
Answer: NO
WeST Sergej Sizov Semantic Web
[email protected] 34
DESCRIBE queries
s:examinee
s:examiner s:in
s:kant/#me s:exam1 s:introCS
s:grade
1.0
s:JonasGraph
s:jonas/#me
s:examinedIn
s:Stud2Graph
s:kant/#me s:introCS
s:examinedBy
s:grade
1.0
SELECT ...
FROM NAMED <http://example.org/alice>
FROM NAMED <http://example.org/bob>
...
WeST Sergej Sizov Semantic Web
[email protected] 40
Relationships between named graphs
# Default graph
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:y foaf:name "Alice" .
_:y foaf:mbox <mailto:[email protected]> .
_:y foaf:mbox <mailto:[email protected]> .
# Graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name Alice" .
_:a foaf:mbox <mailto:[email protected]> .
# Graph: http://example.org/alice_prev
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:[email protected]> .
mbox
mailto:[email protected]