SPARQL Tutorial II & III
SPARQL Tutorial II & III
BY AMNA BASHARAT
7th October , 2008
FAST‐NU , Islamabad
in a nutshell
PREFIX ex:
e #>
<http://inria.fr/schema#
htt //i i f / h
SELECT ?person ?name ?age
WHERE {
?person rdf:type ex:Person
?person ex:name ?name .
OPTIONAL { ?person ex:age ?age }
}
26
PREFIX ex: <http://inria.fr/schema#>
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 !
<soapenv:Body>
<query-request
<query request xmlns
xmlns="http://www.w3.org/2005/09/sparql-
http://www.w3.org/2005/09/sparql
protocol-types/#">
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.
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)
WSDL description file: (HTTP binding part)
<binding name="queryHttp"
bi di " Htt "
interface="tns:SparqlQuery“
… > >
<fault name="MalformedQuery" whttp:zode="400"/>
<!‐‐ the GET binding for query operation ‐‐>
<operation ref="tns:query" whttp:method="GET"
whttp:inputSerialization=“…" />
whttp:inputSerialization= />
</binding>
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.
Examples after we cover SPARQL Query
Language for RDF.
L f RDF
A standard query language in the form of
expressive query
i against the RDF data
i t th RDF d t
model…
Data access language
D t l
Graph patterns
Powerful than XML queries in some aspects
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 SPARQL
XML
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.
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.
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:
<http://purl.org/dc/elements/1.1/>
htt // l /d / l t / / PREFIX
ns: <http://example.org/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 .
}
URIs
à <URI> in < >
i
à or @PREFIX prefix: <http://....>
à 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
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)
XML/SQL SPARQL
[ 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
[ 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
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
"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.
SELECT ?name ?mail
WHERE {
[a :Person;
:name "Henry Story";
:knows [
[ :name ?name; ;
:mbox ?mail;
:address [ a :Address;
:country "Canada"@en;
] ] ]
] ] ].
} 61
FAST – NU, Islamabad, Fall 2008
Walkthrough example (3sparql)
Names and websites of contributors to
PlanetRDF
Pl tRDF
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?
SELECT ?name ?website
? b it
FROM <http://planetrdf.com/bloggers.rdf>
WHERE {
?person foaf:weblog ?website ;
foaf:name ?name .
?website a foaf:Document }
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?who ?g ?mbox
FROM <http://my.example/publishers>
FROM NAMED <http://my.example/alice>
p // y p /
FROM NAMED <http://my.example/bob>
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
d Dec 2005 12:48:25 GMT
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
<?xml version="1.0"?>
l i
<sparql xmlns=“…"> … …
</sparql>
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
For your attendance
and patience
67
ANY QUESTIONS?
68
DUA ONTOLOGY
Taxonomy
Dua
Duas To Seek
DuasToSeekParadise ….
K
Knowledge
l d
containsDua isRelatedTo
Surah Dua
Prophet
isContainedIn
hasRelatedDua
hasTheme
Theme
PREFIX dua:
<http://www.semanticweb.org/ontologies/2
htt // ti b / t l i /
008/9/DuaOntology.owl#>
SELECT ?D ?theme
WHERE { ?D dua:hasTheme ?theme.}
é é
Tested with Protégé 3.3.1
PizzaOntology
Querying for Classes
}
}
Querying on Instance Data
PREFIX dua:
<http://www.semanticweb.org/ontologies/2
htt // ti b / t l i /
008/9/DuaOntology.owl#>
SELECT ?D ?theme
WHERE { ?D dua:hasTheme ?theme.}
SELECT ?D ?theme
WHERE { ?D dua:hasTheme ?theme.
?D dua:hasSourceSurah dua:Taha. }
SELECT ?D ?theme ?surah
WHERE { ?D dua:hasTheme ?theme.
?D dua:hasSourceSurah ?surah.}
SELECT ?D ?theme ?surah ?no
WHERE { ?D dua:hasTheme ?theme.
WHERE { ?D dua:hasTheme ?theme
?D dua:hasSourceSurah ?surah .
?surah dua:hasSurahNo ?no
?surah dua:hasSurahNo ?no.
}
ORDER BY ?no
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
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
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.
}
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.
}
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.
}