SPARQL TUTORIAL BY AMNA BASHARAT
7th October , 2008 FAST‐NU , Islamabad
in a nutshell
Adapted from fabien, gandon, inria
RDF triple model
is the first layer of the semantic web standards FAST – NU, Islamabad, Fall 2008
SPARQL on top...
an RDF query language and data access protocol FAST – NU, Islamabad, Fall 2008
SPARQL stands for
SPARQL Protocol and RDF Query Language FAST – NU, Islamabad, Fall 2008
SPARQL in 3 parts
part 1: query language part 2: result format part 3: access protocol FAST – NU, Islamabad, Fall 2008
SPARQL query SELECT ... FROM ... WHERE { ... } FAST – NU, Islamabad, Fall 2008
SELECT clause
to identify the values to be returned FAST – NU, Islamabad, Fall 2008
FROM clause
to identify the data sources to query q y FAST – NU, Islamabad, Fall 2008
WHERE clause
the triple/graph pattern to be matched against the g triples/graphs of RDF FAST – NU, Islamabad, Fall 2008
WHERE clause a conjunction of triples: { ?x rdf:type ex:Person ?x ex:name ?name }
FAST – NU, Islamabad, Fall 2008
PREFIX
to declare the schema used in the query q y FAST – NU, Islamabad, Fall 2008
example
: persons and their names
PREFIX ex:
SELECT ?person ?name WHERE { ?person rdf:type ex:Person ?person ex:name ?name . }
FAST – NU, Islamabad, Fall 2008
Example or Result <sparql xmlns="http://www.w3.org/2005/sparql-results#" > http://inria.fr/schema#fg gandon
...
FAST – NU, Islamabad, Fall 2008
FILTER
to add constraints to the g graph pattern (e.g., p p ( g, numerical like X>17 ) FAST – NU, Islamabad, Fall 2008
15
Example: persons at least 18‐year old PREFIX ex: SELECT ?person ?name WHERE { ?person rdf:type ex:Person ?person ex:name ?name . ?person ex:age ?age . FILTER (?age > 17) }
FAST – NU, Islamabad, Fall 2008
16
FILTER can use many
operators, functions (e.g., operators functions (e g regular expressions), and even users' extensions FAST – NU, Islamabad, Fall 2008
17
OPTIONAL
to make the matching of a part of the pattern p p optional
FAST – NU, Islamabad, Fall 2008
18
Example:: retrieve the age if available PREFIX ex: e SELECT ?person ?name ?age WHERE { ?person rdf:type ex:Person ?person ex:name ?name . OPTIONAL { ?person ex:age ?age } }
FAST – NU, Islamabad, Fall 2008
19
UNION
to give alternative patterns in a query p q y FAST – NU, Islamabad, Fall 2008
20
Example: explicit or implicit adults
PREFIX ex: SELECT ?name WHERE { ?person ex:name ?name . { { ?person rdf:type ex:Adult } UNION { ?person ex:age ?age FILTER (?age > 17) } } } FAST – NU, Islamabad, Fall 2008
21
Sequence & modify
ORDER BY to sort LIMIT result number OFFSET rank of first result FAST – NU, Islamabad, Fall 2008
22
Example: results 21 to 40 ordered by name
PREFIX ex: SELECT ?person ?name WHERE { ?person rdf:type ex:Person ?person ex:name ?name . } ORDER BY ?name LIMIT 20 OFFSET 20
FAST – NU, Islamabad, Fall 2008
23
UNBOUND test a variable is not bound ; used for negation as failure FAST – NU, Islamabad, Fall 2008
24
Example: persons who are not known authors PREFIX ex: SELECT ?name WHERE { ?person ex:name ?name . OPTIONAL { ?person ex:author ?x } FILTER ( ! b bound(?x)) d( ) }
FAST – NU, Islamabad, Fall 2008
25
NEGATION is tricky and errors can easily be made.
26
PREFIX ex: SELECT ?name WHERE { ?person ex:name ?name . ?person ex:knows ?x FILTER ( ?x != "Java" ) }
?
does this find persons who do not know "java" ? FAST – NU, Islamabad, Fall 2008
27
NO! also persons who know something else ! PREFIX ex: SELECT ?name WHERE { ?person ex:name ?name . ?person ex:knows ?x FILTER ( ?x != "Java" ) } Amna ex:knows "Java" Amna ex:knows "C++" Amna is a answer... FAST – NU, Islamabad, Fall 2008
28
YES!
persons who are not known to know "java" ... negation of an option...
PREFIX ex: SELECT ?name WHERE { ?person ex:name ?name . OPTIONAL { ?person ex:knows ?x FILTER ( ?x = "Java" ) } FILTER ( ! bound(?x) ) }
FAST – NU, Islamabad, Fall 2008
29
ASK
to check just if there is at least one answer ; result is ; "true" or "false"
FAST – NU, Islamabad, Fall 2008
Example: is there a person older than 17 ? PREFIX ex: ASK { ?person ex:age ?age FILTER (? (?age > 17) }
FAST – NU, Islamabad, Fall 2008
CONSTRUCT
return a specific RDF graph for each result g p FAST – NU, Islamabad, Fall 2008
return instances of adults for Example: persons older than 17 PREFIX ex: CONSTRUCT { ?person rdf:type ex:Adult } WHERE { ?person ex:age ?age FILTER (?age > 17) } FAST – NU, Islamabad, Fall 2008
SPARQL protocol
sending queries and their results accross the web FAST – NU, Islamabad, Fall 2008
Example:
with HTTP Binding
GET /sparql/?query=<encoded query> HTTP/1.1 Host: www.inria.fr User-agent: my-sparql-client/0.1
FAST – NU, Islamabad, Fall 2008
Example:
with SOAP Binding
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body> SELECT ?x ?p ?y WHERE {?x ?p ?y} / / p p
FAST – NU, Islamabad, Fall 2008
SPARQL as a Protocol A way of communication between parties
that run SPARQL queries. th t SPARQL i Defining a way of invoking the service. Bindings of a transport protocol for that goal.
FAST – NU, Islamabad, Fall 2008
37
SPARQL Protocol (1) WSDL description file: à Description of the protocol. i i f h l à Not for human understanding.
HTTP binding: bi di à Specify how to encode SPARQL queries in URLs
with GET and POST methods. ith GET d POST th d
SOAP binding: à Specify the SOAP message format S if h SOAP f
(XML message exchange format for queries)
FAST – NU, Islamabad, Fall 2008
38
WSDL Description file WSDL description file: (HTTP binding part)
whttp:inputSerialization= />
FAST – NU, Islamabad, Fall 2008
39
SPARQL Protocol (2) Interface SparqlQuery à Only one operation: query l i For transferring string query à Data types: via XML schema D t t i XML h à Bindings: HTTP / SOAP binding for invokable
operations. operations
A service must support SparqlQuery
interface à support the bindings as described in the WSDL.
FAST – NU, Islamabad, Fall 2008
40
SPARQL Protocol Examples Examples after we cover SPARQL Query
Language for RDF. L f RDF
FAST – NU, Islamabad, Fall 2008
41
SPARQL Query Language A standard query language in the form of
expressive query against the RDF data i i t th RDF d t model… Data access language D t l Graph patterns Powerful than XML queries in some aspects
FAST – NU, Islamabad, Fall 2008
42
SPARQL Query Language (1) SQL: à Internals of DB (tables, fields, data, meaning) à Impossible to query databases on the WILD WILD WEB.
So, what does SPARQL propose? So what does SPARQL propose? à URIs. à Querying databases globally. Querying databases globally à Combining data globally. à Value of data grows exponentially with the ways
you combine it. FAST – NU, Islamabad, Fall 2008
43
The Wild Wild Web SQL XML
FAST – NU, Islamabad, Fall 2008
SPARQL
44
SPARQL Query Language (2) Data structure definitions are being
developed worldwide in a distributed d l d ld id i di t ib t d manner. Common ontologies (Dublin Core, Foaf, C t l i (D bli C F f DOAP, etc.) A database publishes the ontologies it A d b bli h h l i i exports to An application queries it using those ontologies. FAST – NU, Islamabad, Fall 2008
45
Power of SPARQL Illustrated Ask fotograf.com if it has a picture which
matches some constraints such as title, t h t i t h titl date, size, and some other tag… Then ask google for URLs in relation to the Th k l f URL i l ti t th tag we specified. And turn the results of these two A d h l f h uncoordinated data as an RSS feed on your site. site All this in just two‐three SPARQL queries. FAST – NU, Islamabad, Fall 2008
46
Power of SPARQL (2) Ask music cds of price less than 10 You can run this query against
hepsiburada.com, amazon, e‐bay, gittigidiyor.com or any other seller on earth itti idi th ll th who has a website and a database. No seller needs to change their databases. N ll d h h i d b Seller needs: Conversion layer between ontologies and database. Client needs: connectivity framework (like JDBC) for java. FAST – NU, Islamabad, Fall 2008
47
Power of SPARQL (2) Imp. PREFIX dc:
htt // l /d / l t / / PREFIX ns: SELECT ?title ?price SELECT ?titl ? i WHERE { ?x ns:price ?price . ?x ns:price ?price FILTER (?price < 10) . ?x dc:title ?title ?x dc:title ?title . }
FAST – NU, Islamabad, Fall 2008
48
SPARQL Syntax ‐brief‐1 URIs à in < > i à or @PREFIX prefix: à prefix:name for full URI fi f f ll URI
Literals à “Literal“ or “Literal”@language
Blank Node à _:name or [ ] for a Blank Node used just once
FAST – NU, Islamabad, Fall 2008
49
SPARQL Syntax ‐brief‐2 Triples and . à :x :y :z . :t :q :s .
Common predicate and subject: à :x :y :z, :t .which is the same as :x :y :z . :x :y :t .
Common subject: RDF Collections à :x :y ( :z :t :q :s )
which is short for many triples (as lists in LISP)
FAST – NU, Islamabad, Fall 2008
50
A walkthrough example illustrating the power of SPARQL
XML/SQL
FAST – NU, Islamabad, Fall 2008
SPARQL
51
Walkthrough example (1xml) Henry Story <mbox>[email protected] Tim Bray i <mbox>[email protected] Jonathan Story <mbox>[email protected]
FAST – NU, Islamabad, Fall 2008
52
Walkthrough example (1sparql) [ a :Person; :name "Henry Story"; :mbox <mailto:[email protected]>; :knows [ a :Person; :name "Tim Bray"; :mbox <mailto:[email protected] b il b@ ] ]; :knows [ a :Person; :name "Jonathan Story"; name "Jonathan Stor " :mbox <mailto:[email protected]> ]; ] ] . FAST – NU, Islamabad, Fall 2008
53
Graph representation
FAST – NU, Islamabad, Fall 2008
54
Walkthrough example (2) Jonathan Story <mbox>[email protected] France Tim Bray <mbox>[email protected] dd Canada FAST – NU, Islamabad, Fall 2008
55
Walkthrough example (2sparql) [
[[
a :Person; :name "Tim Bray"; :mbox <mailto:[email protected]> :address [ a :Address; :country "Canada"@en ] a :Person; ; :name "Jonathan Story"; :mbox <mailto:[email protected]> :address [ a :Address; :country "France"@en ]
]. FAST – NU, Islamabad, Fall 2008
].
56
Graph representation
FAST – NU, Islamabad, Fall 2008
57
Walkthrough example (2) These graphs can be merged into the
following graph especially if the mbox f ll i h i ll if th b relation is stated as being inverse functional
FAST – NU, Islamabad, Fall 2008
58
Graph representation(merged)
FAST – NU, Islamabad, Fall 2008
59
Walkthrough example (2) "Who does Henry know who lives in Canada,
and what is their e‐mail address?" d h t i th i il dd ?" Can only be answered by aggregating data f from both documents. b th d t Can not be done using the XML query l languages, which can only work hi h l k on the h surface of the document.
FAST – NU, Islamabad, Fall 2008
60
Walkthrough example (2sparql) SELECT ?name ?mail WHERE { [a :Person; :name "Henry Story"; :knows [ [ :name ?name; ; :mbox ?mail; :address [ a :Address; :country "Canada"@en; ] ] ] ] ] ]. } FAST – NU, Islamabad, Fall 2008
61
Walkthrough example (3sparql) Names and websites of contributors to PlanetRDF Pl tRDF PREFIX foaf: SELECT ? SELECT ?name ?website ? b it FROM WHERE { ?person foaf:weblog ?website ; foaf:name ?name . ?website a foaf:Document } FAST – NU, Islamabad, Fall 2008
62
Protocol Example (1) PREFIX foaf: PREFIX dc: SELECT ?who ?g ?mbox FROM FROM NAMED p // y p / FROM NAMED WHERE { ?g dc:publisher ?who . GRAPH ? { ? f f b ? b } } GRAPH ?g { ?x foaf:mbox ?mbox } } FAST – NU, Islamabad, Fall 2008
63
Protocol Example (1) HTTP/1.1 200 OK Date: Wed, 27 Dec 2005 12:48:25 GMT d 8 G Server: Apache/1.3.29 (Unix) PHP/4.3.4 DAV/1.0.3 Connection: closeContent‐Type: application/sparql‐results+xml; charset=utf‐8 l i <sparql xmlns=“…"> … …
FAST – NU, Islamabad, Fall 2008
64
References http://www.w3.org/2004/Talks/17Dec‐
sparql/intro/all.html l/i t / ll ht l http://jena.sourceforge.net/ARQ/Tutorial/ http://blogs.sun.com/roller/page/bblfish http://xmlarmyknife.org/api/rdf/sparql http://xml.com/lpt/a/2005/11/16/introducing ‐sparql‐querying‐semantic‐web‐ tutorial.html http://www.w3.org/2005/Talks/12May‐ p 3 g 5 y SPARQL/all.html FAST – NU, Islamabad, Fall 2008
65
References (2) http://www‐
128.ibm.com/developerworks/xml/library/j‐ 8 ib /d l k / l/lib /j sparql/ http://www.w3.org/TR/rdf‐sparql‐protocol/ htt // /TR/ df l t l/ http://www.w3.org/2004/Talks/17Dec‐ sparql/intro/ex1.rq l/i / http://www.oreillynet.com/pub/wlg/7823
FAST – NU, Islamabad, Fall 2008
66
THANK YOU…
For your attendance and patience 67
ANY QUESTIONS?
68
DUA ONTOLOGY
Taxonomy Dua
Qurani Duas
Duas To Seek K Knowledge l d
DuasToSeekParadise
FAST – NU, Islamabad, Fall 2008
Masnoon Duas
….
RDF Model of Duas containsDua Surah
isRelatedTo Dua
isContainedIn
hasRelatedDua
hasTheme
Theme
FAST – NU, Islamabad, Fall 2008
Prophet
SAMPLE SPARQL QUERIES
Search Dua with a Particular Theme PREFIX dua: SELECT ?D ?theme WHERE { ?D dua:hasTheme ?theme.}
FAST – NU, Islamabad, Fall 2008
SAMPLE SPARQL QUERIES
é é Tested with Protégé 3.3.1
PizzaOntology Querying for Classes
FAST – NU, Islamabad, Fall 2008
SELECT ?subject ?object WHERE { ?subject rdfs:subClassOf ?object }
FAST – NU, Islamabad, Fall 2008
PREFIX p:
ode.org/ontologies/pizza/2005/10/18/pizza.o d / t l i / i / / / 8/ i wl#> SELECT ?subject ?object SELECT ? bj t ? bj t WHERE { ?subject rdfs:subClassOf ?object }
FAST – NU, Islamabad, Fall 2008
PREFIX p:
ode.org/ontologies/pizza/2005/10/18/pizza.o d / t l i / i / / / 8/ i wl#> SELECT ?subject ?object SELECT ? bj t ? bj t WHERE { ?subject rdfs:subClassOf p:Pizza}
FAST – NU, Islamabad, Fall 2008
PREFIX p:
ode.org/ontologies/pizza/2005/10/18/pizza.o d / t l i / i / / / 8/ i wl#> SELECT ?subject ?object SELECT ? bj t ? bj t WHERE { ?subject rdfs:subClassOf p:PizzaTopping. Pi T i }
FAST – NU, Islamabad, Fall 2008
PREFIX p:
ode.org/ontologies/pizza/2005/10/18/pizza.o ode org/ontologies/pizza/2005/10/18/pizza o wl#> SELECT ?subject ?object WHERE { ?subject rdfs:subClassOf p p:DomainConcept. p ?subject owl:disjointWith ?object. }
FAST – NU, Islamabad, Fall 2008
Dua Ontology Querying on Instance Data
FAST – NU, Islamabad, Fall 2008
List all Duas with Corresponding themes PREFIX dua: SELECT ?D ?theme WHERE { ?D dua:hasTheme ?theme.}
FAST – NU, Islamabad, Fall 2008
Example: List of Duas, their Themes which are contained in Surah Taha PREFIX dua:
SELECT ?D ?theme WHERE { ?D dua:hasTheme ?theme. ?D dua:hasSourceSurah dua:Taha.
FAST – NU, Islamabad, Fall 2008
}
List of All Dua’s which have some Theme and have some source Surah PREFIX dua:
SELECT ?D ?theme ?surah WHERE { ?D dua:hasTheme ?theme.
?D dua:hasSourceSurah ?surah.}
FAST – NU, Islamabad, Fall 2008
PREFIX dua:
SELECT ?D ?theme ?surah ?no
WHERE { ?D dua:hasTheme ?theme. WHERE { ?D dua:hasTheme ?theme ?D dua:hasSourceSurah ?surah .
} ORDER BY ?no
?surah dua:hasSurahNo ?no ?surah dua:hasSurahNo ?no.
FAST – NU, Islamabad, Fall 2008
PREFIX dua:
SELECT ?D ?theme ?surah ?no ?ayah
WHERE { ?D dua:hasTheme ?theme. WHERE { ?D dua:hasTheme ?theme ?D dua:hasSourceSurah ?surah ; dua:hasSourceAyah ?ayah. ?surah dua:hasSurahNo ?no ?surah dua:hasSurahNo ?no.
} ORDER BY ?no
FAST – NU, Islamabad, Fall 2008
Notice the change in results with the use of OPTIONAL
PREFIX dua:
SELECT ?D ?theme ?surah ?no ?ayah
WHERE{ ?D dua:hasTheme ?theme. ?D dua:hasSourceSurah ?surah.
OPTIONAL{ ?D dua:hasSourceAyah ?ayah.
}} ORDER BY ?no
?surah dua:hasSurahNo ?no.
FAST – NU, Islamabad, Fall 2008
Use of Regular Expression to retrieve duas that contain the word Rabbi PREFIX dua:
l SELECT ?D ?surah ?no ?text WHERE{ ?D dua:hasSourceSurah ?surah ?D dua:hasSourceSurah ?surah. ?D dua:hasTextAyah ?text.
FILTER regex(?text, "Rabbi") .
?surah dua:hasSurahNo ?no. } FAST – NU, Islamabad, Fall 2008
Use of Regular Expression to retrieve duas that contain the word Rabbana
PREFIX dua:
SELECT ?D ?surah ?no ?text
WHERE{ ?D dua:hasSourceSurah ?surah. ?D dua:hasTextAyah ?text. FILTER regex(?text "Rabbana") FILTER regex(?text, Rabbana ) .
?surah dua:hasSurahNo ?no.
}
FAST – NU, Islamabad, Fall 2008
AN interesting Query!. …Think about what it does!
PREFIX dua:
SELECT ?Dua ?DuaOther ?surah2
WHERE{ ?Dua dua:hasSourceSurah dua:Taha. ?Dua dua:isRelatedTo ?p. ?DuaOther dua:isRelatedTo ?p ?DuaOther dua:isRelatedTo ?p. ?DuaOther dua:hasSourceSurah ?surah2.
}
FAST – NU, Islamabad, Fall 2008
Find other Duas related to the same prophet in a given Surah “Find other duas related with the prophet of a given surah. given surah ”
FAST – NU, Islamabad, Fall 2008