Download - SPARQL Tutorial II & III
SPARQL TUTORIALSPARQL TUTORIALBY AMNA BASHARAT
7thOctober , 2008FAST‐NU , Islamabad
in a nutshellin a nutshellin a nutshellin a nutshell
Adapted from fabien, gandon, inria
RDF triple model is the first layer of the is the first layer of the semantic web standards
FAST – NU, Islamabad, Fall 2008
SPARQL on top...an RDF query language an RDF query language and data access protocol
FAST – NU, Islamabad, Fall 2008
SPARQLstands forSPARQL Protocol and SPARQL Protocol and RDF Query Language
FAST – NU, Islamabad, Fall 2008
SPARQL in 3 partspart 1: query languagepart 1: query languagepart 2: result format
FAST – NU, Islamabad, Fall 2008
part 3: access protocol
SPARQL querySELECTSELECT ...FROM ...
FAST – NU, Islamabad, Fall 2008
WHERE { ... }
SELECT clause
to identify the values to to identify the values to be returned
FAST – NU, Islamabad, Fall 2008
FROM clause
to identify the data to identify the data sources to query
FAST – NU, Islamabad, Fall 2008
q y
WHERE clause
the triple/graph pattern the triple/graph pattern to be matched against the
FAST – NU, Islamabad, Fall 2008
gtriples/graphs of RDF
WHEREclauseWHEREclausea conjunction of triples: {?x rdf:type ex:Person?x ex:name ?name }}
FAST – NU, Islamabad, Fall 2008
PREFIX
to declare the schema to declare the schema used in the query
FAST – NU, Islamabad, Fall 2008
q y
example : persons and their names
PREFIX ex: <http://inria.fr/schema#>SELECT ?person ?nameWHERE {WHERE {?person rdf:type ex:Person?person ex:name ?name?person ex:name ?name .}
FAST – NU, Islamabad, Fall 2008
Example or Result
<?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#" ><head><variable name="person"/>
<variable name="name"/> </head><results ordered="false" distinct="false"><results ordered="false" distinct="false"><result><binding name="person">
<uri>http://inria.fr/schema#fg</uri>
</binding>
<binding name="name">
<literal>gandon</literal><literal>gandon</literal>
</binding></result>
<result>
FAST – NU, Islamabad, Fall 2008
<result> ...
FILTER
to add constraints to the to add constraints to the graph pattern (e.g.,
FAST – NU, Islamabad, Fall 2008
g p p ( g ,numerical like X>17 )
15
Example: persons at least 18‐year old
PREFIX ex: <http://inria fr/schema#>PREFIX ex: <http://inria.fr/schema#>SELECT ?person ?nameWHERE {?person rdf:type ex:Person?person ex:name ?name .
?person ex:age ?age .
FILTER (?age > 17)FILTER (?age > 17)}
FAST – NU, Islamabad, Fall 2008 16
FILTER can use many operators functions (e g operators, functions (e.g., regular expressions), and
FAST – NU, Islamabad, Fall 2008
even users' extensions17
OPTIONAL
to make the matching of a to make the matching of a part of the pattern
FAST – NU, Islamabad, Fall 2008
p poptional
18
Example:: retrieve the age if available
PREFIX e <htt //i i f / h #>PREFIX ex: <http://inria.fr/schema#>SELECT ?person ?name ?ageWHERE {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 to give alternative patterns in a query
FAST – NU, Islamabad, Fall 2008
p q y
20
Example: explicit or implicit adults
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE {WHERE {?person ex:name ?name .
{{ ?person rdf:type ex:Adult }UNION{ ?person ex:age ?age{ ?person ex:age ?age FILTER (?age > 17) }
}}
FAST – NU, Islamabad, Fall 2008 21
}
Sequence & modify
ORDER BY to sortORDER BY to sortLIMIT result number
FAST – NU, Islamabad, Fall 2008
OFFSET rank of first result22
Example: results 21 to 40 ordered by name
PREFIX ex: <http://inria.fr/schema#>SELECT ?person ?nameWHERE {?person rdf:type ex:Person?person ex:name ?name .}
ORDER BY ?nameLIMIT 20OFFSET 20
FAST – NU, Islamabad, Fall 2008 23
UNBOUND
test a variable is not bound ; 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 <htt //i i f / h #>PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE {?person ex:name ?name .
OPTIONAL { ?person ex:author ?x } ! b d( )FILTER ( ! bound(?x))
}
FAST – NU, Islamabad, Fall 2008 25
NEGATION
is tricky and errors can easily be made.
26
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE {WHERE {?person ex:name ?name .
?person ex:knows ?x?person ex:knows ?x FILTER ( ?x != "Java" )}
FAST – NU, Islamabad, Fall 2008? does this find persons who do not know "java" ?
27
NO! also persons who know something else !
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE {?person ex:name ?name .
?person ex:knows ?x FILTER ( ?x != "Java" )}
Amna ex:knows "Java"
Amna ex:knows "C++"
FAST – NU, Islamabad, Fall 2008 28
Amna is a answer...
YES! persons who are not known to know "java" ... negation of an option...
PREFIX ex: <http://inria.fr/schema#>SELECT ?nameWHERE {?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 to check just if there is at least one answer ; result is
FAST – NU, Islamabad, Fall 2008
;"true" or "false"
Example: is there a person older than 17 ?
PREFIX ex: <http://inria.fr/schema#>ASK
{?person ex:age ?age FILTER (? > 17)FILTER (?age > 17) }
FAST – NU, Islamabad, Fall 2008
CONSTRUCT
return a specific RDF return a specific RDF graph for each result
FAST – NU, Islamabad, Fall 2008
g p
Example: return instances of adults for persons older than 17
PREFIX ex: <http://inria.fr/schema#>CONSTRUCTCONSTRUCT
{
?person rdf:type ex:Adult
}
WHERE
{?person ex:age ?age FILTER (?age > 17) }
FAST – NU, Islamabad, Fall 2008
}
SPARQL protocol
sending queries and their 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
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelopexmlns: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>
<query-request xmlns="http://www.w3.org/2005/09/sparql-<query request xmlns http://www.w3.org/2005/09/sparqlprotocol-types/#">
<query>SELECT ?x ?p ?y WHERE {?x ?p ?y}</query>
/</query-request>
</soapenv:Body>
</soapenv:Envelope>
FAST – NU, Islamabad, Fall 2008
/ p p
SPARQL as a Protocol
A way of communication between parties th t SPARQL ithat run SPARQL queries.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:i i f h lDescription of the protocol.
Not for human understanding.
bi diHTTP binding:Specify how to encode SPARQL queries in URLs ith GET d POST th d with GET and POST methods.
SOAP binding:S if h SOAP f Specify the SOAP message format (XML message exchange format for queries)
FAST – NU, Islamabad, Fall 2008 38
WSDL Description file
WSDL description file: (HTTP binding part)bi di " Htt " <binding name="queryHttp" interface="tns:SparqlQuery“ >… >
<fault name="MalformedQuery" whttp:zode="400"/> <!‐‐ the GET binding for query operation ‐‐><!‐‐ the GET binding for query operation ‐‐><operation ref="tns:query" whttp:method="GET" whttp:inputSerialization=“ " />whttp:inputSerialization= … />
</binding>
FAST – NU, Islamabad, Fall 2008 39
SPARQL Protocol (2)
Interface SparqlQueryl iOnly one operation: query
For transferring string query
D t t i XML hData types: via XML schemaBindings: HTTP / SOAP binding for invokable operationsoperations.
A service must support SparqlQuery interfaceinterface
support the bindings as described in the WSDL.
FAST – NU, Islamabad, Fall 2008 40
SPARQL Protocol Examples
Examples after we cover SPARQL Query L f RDFLanguage for RDF.
FAST – NU, Islamabad, Fall 2008 41
SPARQL Query Language
A standard query language in the form of i i t th RDF d t expressive query against the RDF data
model…D t lData access languageGraph patternsPowerful than XML queries in some aspects
FAST – NU, Islamabad, Fall 2008 42
SPARQL Query Language (1)
SQL:SQL:Internals of DB (tables, fields, data, meaning)Impossible to query databases on the Impossible to query databases on the WILDWILDWEB.
So what does SPARQL propose?So, what does SPARQL propose?URIs.Querying databases globallyQuerying databases globally.Combining data globally.Value of data grows exponentially with the ways
FAST – NU, Islamabad, Fall 2008 43
Value of data grows exponentially with the ways you combine it.
The Wild Wild Web
SQL SPARQLSQL SPARQLXML
FAST – NU, Islamabad, Fall 2008 44
SPARQL Query Language (2)
Data structure definitions are being d l d ld id i di t ib t d developed worldwide in a distributed manner.C t l i (D bli C F f Common ontologies (Dublin Core, Foaf, DOAP, etc.)A d b bli h h l i i A database publishes the ontologies it exports toAn 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 t h t i t h titl matches some constraints such as title,
date, size, and some other tag…Th k l f URL i l ti t th Then ask google for URLs in relation to the tag we specified.A d h l f h And turn the results of these two uncoordinated data as an RSS feed on your sitesite.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 10You can run this query against hepsiburada.com, amazon, e‐bay, itti idi th ll th gittigidiyor.com or any other seller on earth
who has a website and a database.N ll d h h i d bNo seller needs to change their databases.Seller needs: Conversion layer between ontologies and database.Client needs: connectivity framework (like
FAST – NU, Islamabad, Fall 2008
JDBC) for java.47
Power of SPARQL (2) Imp.
PREFIX dc: htt // l /d / l t / / PREFIX <http://purl.org/dc/elements/1.1/> PREFIX
ns: <http://example.org/ns#>SELECT ?titl ? i SELECT ?title ?price 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
URIsi<URI> in < >
or@PREFIX prefix: <http://....>fi f f ll URIprefix:name for full 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 SPARQL
FAST – NU, Islamabad, Fall 2008 51
Walkthrough example (1xml)
<Person><name>Henry Story</name><mbox>[email protected]</mbox><knows>
<Person>i<name>Tim Bray</name>
<mbox>[email protected]</mbox>
</Person><Person>
<name>Jonathan Story</name> <mbox>[email protected]</mbox>
</Person> </knows>
</Person>
FAST – NU, Islamabad, Fall 2008 52
Walkthrough example (1sparql)
[ a :Person; :name "Henry Story"; :mbox <mailto:[email protected]>;
:knows [ a :Person; :name "Tim Bray";
b il b@ ] :mbox <mailto:[email protected] ]; :knows [ a :Person;
name "Jonathan Stor " :name "Jonathan Story"; :mbox <mailto:[email protected]> ];
]
FAST – NU, Islamabad, Fall 2008
] .
53
Graph representation
FAST – NU, Islamabad, Fall 2008 54
Walkthrough example (2)
<AddressBook><Person>
<name>Jonathan Story</name> <name>Jonathan Story</name> <mbox>[email protected]</mbox><address>
<Country>France</Country></address>
</Person> <Person> <Person>
<name>Tim Bray</name> <mbox>[email protected]</mbox>
dd<address><Country>Canada</Country>
</address>
FAST – NU, Islamabad, Fall 2008 55
</Person></AddressBook>
Walkthrough example (2sparql)
[ a :Person;
:name "Tim Bray"; :name "Tim Bray";
:mbox <mailto:[email protected]>
:address [ a :Address; :address [ a :Address;
:country "Canada"@en ] ].
[ a :Person; [ ;
:name "Jonathan Story";
:mbox <mailto:[email protected]> :address [
a :Address;
FAST – NU, Islamabad, Fall 2008
:country "France"@en ]
]. 56
Graph representation
FAST – NU, Islamabad, Fall 2008 57
Walkthrough example (2)
These graphs can be merged into the f ll i h i ll if th b following graph especially if the mbox 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, d h t i th i il dd ?" and what is their e‐mail address?"
Can only be answered by aggregating data f b th d t from both documents. Can not be done using the XML query l hi h l k h languages, which can only work on the 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;:address [ a :Address;
:country "Canada"@en; ] ] ]
FAST – NU, Islamabad, Fall 2008
] ] ].}
61
Walkthrough example (3sparql)
Names and websites of contributors to Pl tRDFPlanetRDF
PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ? ? b it SELECT ?name ?website FROM <http://planetrdf.com/bloggers.rdf> WHERE { WHERE {
?person foaf:weblog ?website ;foaf:name ?name .
?website a foaf:Document }
FAST – NU, Islamabad, Fall 2008 62
Protocol Example (1)
PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>SELECT ?who ?g ?mboxFROM <http://my.example/publishers>FROM NAMED <http://my.example/alice>p // y p /FROM NAMED <http://my.example/bob>WHERE { WHERE { ?g dc:publisher ?who . GRAPH ? { ? f f b ? b } }
FAST – NU, Islamabad, Fall 2008
GRAPH ?g { ?x foaf:mbox ?mbox } }
63
Protocol Example (1)
HTTP/1.1 200 OKd 8 GDate: Wed, 27 Dec 2005 12:48:25 GMT
Server: Apache/1.3.29 (Unix) PHP/4.3.4 DAV/1.0.3Connection: closeContent‐Type:
application/sparql‐results+xml; charset=utf‐8l i<?xml version="1.0"?>
<sparql xmlns=“…">… …</sparql>
FAST – NU, Islamabad, Fall 2008 64
References
http://www.w3.org/2004/Talks/17Dec‐l/i t / ll ht lsparql/intro/all.html
http://jena.sourceforge.net/ARQ/Tutorial/http://blogs.sun.com/roller/page/bblfishhttp://xmlarmyknife.org/api/rdf/sparqlhttp://xml.com/lpt/a/2005/11/16/introducing‐sparql‐querying‐semantic‐web‐tutorial.htmlhttp://www.w3.org/2005/Talks/12May‐
FAST – NU, Islamabad, Fall 2008
p 3 g 5 ySPARQL/all.html
65
References (2)
http://www‐8 ib /d l k / l/lib /j128.ibm.com/developerworks/xml/library/j‐
sparql/htt // /TR/ df l t l/http://www.w3.org/TR/rdf‐sparql‐protocol/http://www.w3.org/2004/Talks/17Dec‐
l/i /sparql/intro/ex1.rqhttp://www.oreillynet.com/pub/wlg/7823
FAST – NU, Islamabad, Fall 2008 66
THANK YOU…
For your attendance and patience
67
ANY QUESTIONS?ANY QUESTIONS?
68
DUAONTOLOGYDUAONTOLOGY
Taxonomy
DuaDua
Qurani Duas Masnoon DuasQurani Duas Masnoon Duas
DuasTo Seek K l d DuasToSeekParadise
FAST – NU, Islamabad, Fall 2008
Knowledge DuasToSeekParadise ….
RDF Model of Duas
containsDua isRelatedTo
DuaSurahProphet
isRelatedTo
isContainedInhasRelatedDua
hasTheme
Theme
FAST – NU, Islamabad, Fall 2008
SAMPLE SPARQLQUERIESSAMPLE SPARQLQUERIES
Search Duawith a Particular Theme
PREFIX dua: htt // ti b / t l i /<http://www.semanticweb.org/ontologies/2
008/9/DuaOntology.owl#>
SELECT ?D ?themeWHERE { ?D dua:hasTheme ?theme.}
FAST – NU, Islamabad, Fall 2008
SAMPLE SPARQL QUERIESSAMPLE SPARQL QUERIES
é éTested with Protégé 3.3.1
PizzaOntology
Querying for Classes
FAST – NU, Islamabad, Fall 2008
SELECT ?subject ?objectWHERE { ?subject rdfs:subClassOf ?object }
FAST – NU, Islamabad, Fall 2008
PREFIX p: <http://www.co‐d / t l i / i / / / 8/ iode.org/ontologies/pizza/2005/10/18/pizza.o
wl#>SELECT ? bj t ? bj tSELECT ?subject ?objectWHERE { ?subject rdfs:subClassOf ?object }
FAST – NU, Islamabad, Fall 2008
PREFIX p: <http://www.co‐d / t l i / i / / / 8/ iode.org/ontologies/pizza/2005/10/18/pizza.o
wl#>SELECT ? bj t ? bj tSELECT ?subject ?objectWHERE { ?subject rdfs:subClassOf p:Pizza}
FAST – NU, Islamabad, Fall 2008
PREFIX p: <http://www.co‐d / t l i / i / / / 8/ iode.org/ontologies/pizza/2005/10/18/pizza.o
wl#>SELECT ? bj t ? bj tSELECT ?subject ?objectWHERE { ?subject rdfs:subClassOf Pi T ip:PizzaTopping.
}
FAST – NU, Islamabad, Fall 2008
PREFIX p: <http://www.co‐ode org/ontologies/pizza/2005/10/18/pizza oode.org/ontologies/pizza/2005/10/18/pizza.owl#>SELECT ?subject ?objectSELECT ?subject ?objectWHERE { ?subject rdfs:subClassOf p:DomainConcept.p 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: htt // ti b / t l i /<http://www.semanticweb.org/ontologies/2
008/9/DuaOntology.owl#>
SELECT ?D ?themeWHERE { ?D dua:hasTheme ?theme.}
FAST – NU, Islamabad, Fall 2008
Example: List of Duas, their Themes which are contained in Surah Taha
PREFIX dua: htt // ti b / t l i /<http://www.semanticweb.org/ontologies/20
08/9/DuaOntology.owl#>
SELECT ?D ?themeWHERE { ?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: htt // ti b / t l i /<http://www.semanticweb.org/ontologies/20
08/9/DuaOntology.owl#>
SELECT ?D ?theme ?surahWHERE { ?D dua:hasTheme ?theme.
?D dua:hasSourceSurah ?surah.}
FAST – NU, Islamabad, Fall 2008
PREFIX dua: <http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?D ?theme ?surah ?no
WHERE { ?D dua:hasTheme ?themeWHERE { ?D dua:hasTheme ?theme.?D dua:hasSourceSurah ?surah .
?surah dua:hasSurahNo ?no?surah dua:hasSurahNo ?no.
}}ORDER BY ?no
FAST – NU, Islamabad, Fall 2008
PREFIX dua: <http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?D ?theme ?surah ?no ?ayah
WHERE { ?D dua:hasTheme ?themeWHERE { ?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 OPTIONALof OPTIONAL
PREFIX dua: <http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?D ?theme ?surah ?no ?ayah
WHERE{ ?D dua:hasTheme ?theme.?D dua:hasSourceSurah ?surah.
?surah dua:hasSurahNo ?no.
OPTIONAL{ ?D dua:hasSourceAyah ?ayah.
}}ORDER BY ?no
FAST – NU, Islamabad, Fall 2008
Use of Regular Expression to retrieve duas that contain the word Rabbithat contain the word RabbiPREFIX dua: <http://www.semanticweb.org/ontologies/2008/9/DuaOntology.
lowl#>
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 Rabbanathat contain the word Rabbana
PREFIX dua: <http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?D ?surah ?no ?text
WHERE{ 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!what it does!
PREFIX dua: <http://www.semanticweb.org/ontologies/2008/9/DuaOntology.owl#>
SELECT ?Dua ?DuaOther ?surah2
WHERE{ 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 Surahprophet in a given Surah
� “Find other duas related with the prophet of a given surah ”given surah.
FAST – NU, Islamabad, Fall 2008