You are on page 1of 53

SPARQL TUTORIAL

BY AMNA BASHARAT

7th October , 2008
FAST‐NU , Islamabad
Self  Reading

ƒ Value Partitions  ‐You Must Conver!
ƒ Covering Axioms 
C i i
ƒ Using the Property Matrix Wizard 
ƒ Cardinality Restrictions
ƒ Datatype Properties
ƒ Creating Individuals
ƒ Namespaces and Ontology Imports
ƒ Enumerated Classes
ƒ Annotation Properties
p
ƒ Open World Reasoning
FAST – NU, Islamabad, Fall 2008
QUIZ!
Electronic Quizzes
ƒ Generally MCQs or True/False
ƒ If it is an open ended question then you are expected to answer it 
on your own.
ƒ Go to www.Quizstar.4teachers.org
ƒ Login using your username and password
L i   i       d  d
à You username and password have been created
à They can be obtained from me or viewed in the course folder
à Your Class is SWT
ƒ Enter your Details
ƒ Change your password!
ƒ Take the Quiz …when assigned!
à Fixed Time
ƒ You can also view answers once the Quiz Expires!
ƒ You may as well be given some assignments
FAST – NU, Islamabad, Fall 2008
Trial Quiz!

ƒ You must take the Trial Quiz today.
ƒ The actual Quiz to be released very soon.
ƒ Would give you essential practice before the 
upcoming Sessional II
à Would require sound logical understanding of 
Ontologies.
O l i
à May Also require you to do things practically.

FAST – NU, Islamabad, Fall 2008


ABOUT  THE PROJECT
Next Deliverable

ƒ Application/ Architecture 
ƒ Ontology
ƒ Reasoning Support

ƒ Due Date : 9
9th October, 2008
,

FAST – NU, Islamabad, Fall 2008


Guidelines for OWL Model
ƒ An OWL Model (well documented) containing the 
ontology developed for the chosen business domain
à The ontology is expected to contain between 40 and 50 
classes with a well‐defined taxonomy (i.e., 
class/subclass hierarchy), properties and conditions. 

à Examples of individual instances must be defined as 
well. 

ƒ Refer to the Guidelines Document for 
Coverage of OWL Constructs

FAST – NU, Islamabad, Fall 2008


Documentation of your Ontology
ƒ The ontology should be complemented by a 
g
documentation containing 
à design considerations and decisions
à The developed Taxonomy 
à comments and assumptions for a better understanding of 
g
the ontology and decisions 
à The document should be written in a clear and concise 
manner.
ƒ List of Scenarios, Competency Questions 
List of Scenarios  Competency Questions 
that your Ontology is capable of answering
ƒ Reasoning Mechanisms, Classifications that 
g ,
you have performed
ƒ Limitations and Recommended 
Modifications/ Enhancement to your 
Ontology
FAST – NU, Islamabad, Fall 2008
SPARQL TUTORIAL
RDF

ƒ RDF is quite committed to Semantic Web.
à Data model
d l
à Serialization by means of XML
à Formal semantics
F l  i
ƒ Still something is missing!
à A standard query language.
à A way for the queries and their respective results 
to be transported between applications / 
services.

FAST – NU, Islamabad, Fall 2008


…result

SPARQL

FAST – NU, Islamabad, Fall 2008


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: <http://inria.fr/schema#>


SELECT ?person ?name
WHERE {
?person rdf:type ex:Person
?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">
<result>
<binding name="person">
<uri>http://inria.fr/schema#fg</uri>
</binding>
<binding name="name">
<literal>gandon</literal>
</binding>
</result>
<result> ...

FAST – NU, Islamabad, Fall 2008


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: <http://inria.fr/schema#>


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: <http://inria.fr/schema#>


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

<?xml version="1.0" encoding="UTF-8"?>


<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>
<query-request
<query request xmlns
xmlns="http://www.w3.org/2005/09/sparql-
http://www.w3.org/2005/09/sparql
protocol-types/#">

<query>SELECT ?x ?p ?y WHERE {?x ?p ?y}</query>


</query-request>
/
</soapenv:Body>
</soapenv:Envelope>
/ p p

FAST – NU, Islamabad, Fall 2008


TAKE‐AWAY

summary of SPARQL
SPARQL is...

... a query language ...
... a result format ...
    lt f t 
... an access protocol ...
p

FAST – NU, Islamabad, Fall 2008


for RDF 
... 
SPARQL query language
based on the triple model   ?x
b d   th  t i l   d l   ? ?p
? ?y
?
filters to add constraints
optional parts and alternative parts

FAST – NU, Islamabad, Fall 2008


SOME MORE EXAMPLES
SPARQL

SPARQL =
Query Language + Protocol + XML Results
Format

• Access and query RDF graphs


• Product of the RDF Data Access Working Group
• Status: W3C Candidate Recommendation

FAST – NU, Islamabad, Fall 2008


SPARQL Query: example

PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?title2
WHERE
{
?doc dc:title "SPARQL at speed" .
?doc dc:creator ?c .
?docOther dc:creator ?c .
?docOther dc:title ?title2

• On an abstracts/papers database:
p p y g p p
“Find other papers by the authors of a given paper.”

FAST – NU, Islamabad, Fall 2008


SPARQL Query

PREFIX dc: <http://purl.org/dc/elements/1.1/>


PREFIX foaf:
f f <http://xmlns.com/foaf/0.1/>
<htt // l /f f/0 1/>
PREFIX shop: <http://example/shop#>

SELECT ?title2
WHERE
{ ?doc dc:title ?title .
FILTER regex(?title, "SPARQL") .
?doc dc:creator ?c .
?c foaf:name ?name .
OPTIONAL
{ ?doc shop:price ?price }
}

• “Find books with ‘SPARQL’ in the title. Get the authors’ name and the 
price (if available).”
price (if available).
• Multiple vocabularies
FAST – NU, Islamabad, Fall 2008
Inference
• An RDF graph may be backed by inference
− OWL, RDFS, application, rules
OWL  RDFS  application  rules
:x rdf:type :C .
:C rdfs:subClassOf :D .

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>


SELECT ?type
WHERE
{
?x rdf:type ?type .
}

--------
| type |
========
| :C |
| :D |
--------

FAST – NU, Islamabad, Fall 2008


DATA VIRTUALIZATION VIA 
SPARQL
SPARQL : Data Virtualization

ƒ SPARQL as integrator 
à Data remains where it is
i h i i
à Existing applications untouched
à data appears as RDF, remap query to native form
d      RDF         i  f
ƒ SPARQL to SQL
‚ Direct mapping of tables
i i f bl
 Semi‐automatic generation of mapping
 SquirrelRDF (also does LDAP)
‚ Modelled: D2RQ
 High‐quality mapping, manually developed

FAST – NU, Islamabad, Fall 2008


Federated Query
Single Point of Access
g

SPARQL
Que
• Inputs:
Inp ts

ery
– Service Description
– Information 
Query Broker
Directory
SPARQL => SPARQL
– Request

SPA

SPA
SP
• Outputs:
Query

Query
Query
Q
PARQL
ARQL

ARQL
– Unified results

Corp Doc
RDF
LDAP DB

FAST – NU, Islamabad, Fall 2008


Access Multiple Sources
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ldap: <http://ldap.hp.com/people#>
PREFIX foaf:

SELECT ?name ?name2


{
?doc dc:title ?title .
FILTER regex(?title, “SPARQL”) .
?doc dc:creator ?reseacher .
?researcher ldap:email ?email .
?researcher ldap:name ?name
}

• “Find the name and email addresses of authors of a paper”
• Query tech reports first, then go to LDAP for more information.
• SQL database; LDAP server
FAST – NU, Islamabad, Fall 2008
Access Multiple Sources

PREFIX dc: <http://purl.org/dc/elements/1.1/>


p
PREFIX ldap: p p p p p
<http://ldap.hp.com/people#>
PREFIX foaf:

SELECT ?name ?name2


{
?doc dc:title ?title .
?doc dc:creator ?reseacher .
?researcher ldap:email <mailto:andy.seaborne@hp.com> .
}

• “Find the reports written by the person with
“Fi d th   t   itt  b  th     ith
email address  andy.seaborne@hp.com
• Go to LDAP first  then the reports database
Go to LDAP first, then the reports database

FAST – NU, Islamabad, Fall 2008


SPARQL : Protocol

SPARQL = 
Query Language + Protocol + XML Results Format

• One request type expressed in WSDL
• HTTP and SOAP
• Results: fixed XML form for further transformation

FAST – NU, Islamabad, Fall 2008


Links

ƒ Jena: Java and .Net Semantic Web Framework
à http://jena.sourceforge.net/ 
à RDF, SPARQL, OWL, Rules
à Open source: BSD‐license
O    BSD li
‚ Commercial and non‐commercial use
ƒ SPARQL Query
à http://jena.sourceforge.net/ARQ
ƒ SPARQL Protocol
à http://www.joseki.org
ƒ SquirrelRDF: Access legacy SQL:
à http://jena.sourceforge.net/SquirrelRDF
FAST – NU, Islamabad, Fall 2008
SPARQL Update

INSERT
{ book:newBook dc:title Q
“More about SPARQL” .
book:newBook dc:creator
[ foaf:mbox <mailto:alex@example> ;
foaf:name “Alex Author” ]
}
DELETE
{ ?book dc:title ?title ;
dc:creator ?c }
WHERE
{ ?book dc:title ?title ;
dc:creator ?c
FILTER regex(?title,
regex(?title “first
first edition”)
edition )
}

• Future HP proposal to W3C
F t  HP  l t  W C
FAST – NU, Islamabad, Fall 2008
DUA ONTOLOGY
Taxonomy

Dua

Qurani Duas Masnoon Duas

Duas To Seek 
DuasToSeekParadise ….
K
Knowledge
l d

FAST – NU, Islamabad, Fall 2008


RDF Model of Duas

isRelatedTo

Prophet

Dua hasRelatedDua

hasTheme
containsDua isContainedIn

Surah Theme

FAST – NU, Islamabad, Fall 2008


SAMPLE SPARQL QUERIES
Search Dua with a Particular 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.}

FAST – NU, Islamabad, Fall 2008

You might also like