semantic web - sparql - uni-koblenz · semantic web sparql gerd gr oner, matthias thimm...

75
Semantic Web SPARQL Gerd Gr¨ oner , Matthias Thimm {groener,thimm}@uni-koblenz.de Institute for Web Science and Technologies (WeST) University of Koblenz-Landau July 15, 2013 Gerd Gr¨ oner , Matthias Thimm Semantic Web 1 / 56

Upload: dodien

Post on 24-May-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Semantic WebSPARQL

Gerd Groner, Matthias Thimm

{groener,thimm}@uni-koblenz.de

Institute for Web Science and Technologies (WeST)University of Koblenz-Landau

July 15, 2013

Gerd Groner, Matthias Thimm Semantic Web 1 / 56

Page 2: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Agenda for this Week

I SPARQL (2 lectures)

I Semantic Search

I Ontology Engineering

I Pattern-based Ontology Design

I Semantic Web Applications, Closing

Gerd Groner, Matthias Thimm Semantic Web 2 / 56

Page 3: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Outline

1 SPARQL Protocol and RDF Query Language

2 SPARQL Protocol

Gerd Groner, Matthias Thimm Semantic Web 3 / 56

Page 4: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Outline

1 SPARQL Protocol and RDF Query Language

2 SPARQL Protocol

Gerd Groner, Matthias Thimm Semantic Web 4 / 56

Page 5: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Semantic Web Layer Cake

Gerd Groner, Matthias Thimm Semantic Web 5 / 56

Page 6: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Goals of this lecture

I Understanding basic and advanced queries using SPARQL

I Gain knowledge about how to query data sources

Gerd Groner, Matthias Thimm Semantic Web 6 / 56

Page 7: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

SPARQL

I W3C Recommendation 15 January 2008I http://www.w3.org/TR/rdf-sparql-query/

I Standard query language for RDFI Native RDF knowledge basesI knowledge bases viewed as RDF via middleware

I Language for querying for graph patternsI Includes unions, conjunctions and optional patternsI No support for inserts or updates

I Supports extensible testing for values and constraints

Gerd Groner, Matthias Thimm Semantic Web 7 / 56

Page 8: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

SPARQL 1.1

I SPARQL 1.1 (W3C Recommendation 21 March 2013)

I http://www.w3.org/TR/sparql11-query/I SPARQL 1.1 Query - Adds support for aggregates, subqueries, projected

expressions, and negation to the SPARQL query language.

I SPARQL 1.1 Update - Defines an update language for RDF graphs.

I SPARQL 1.1 Protocol - Defines an abstract interface and HTTP bindings for aprotocol to issue SPARQL Query and SPARQL Update statements against aSPARQL endpoint.

I SPARQL 1.1 Service Description - Defines a vocabulary and discoverymechanism for describing the capabilities of a SPARQL endpoint.

I SPARQL 1.1 Uniform HTTP Protocol for Managing RDF Graphs - Describes theuse of the HTTP protocol for managing named RDF graphs on an HTTP server.

I SPARQL 1.1 Entailment Regimes - Defines conditions under which SPARQLqueries can be used with entailment regimes such as RDF, RDF Schema, OWL,or RIF.

I SPARQL 1.1 Property Paths - Defines a more succinct way to write parts ofbasic graph patterns and also extend matching of triple pattern to arbitrarylength paths.

Gerd Groner, Matthias Thimm Semantic Web 8 / 56

Page 9: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

SPARQL Query Types

I SELECTI returns the set of variables bound in a query pattern match

I CONSTRUCTI returns an RDF graph constructed by substituting variables in

a set of triple templates

I DESCRIBEI returns an RDF graph that describes the resources found

I ASKI returns whether a query pattern matches any triples or not

(true / false query)

Gerd Groner, Matthias Thimm Semantic Web 9 / 56

Page 10: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

SPARQL SELECT Query

PREFIX . . .

SELECT . . .

FROM . . .

WHERE { . . . }

schemas used in the query

values to be returned

identify source data to query

triple patterns and otherconditions tomatch the graph

Gerd Groner, Matthias Thimm Semantic Web 10 / 56

Page 11: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

SPARQL ASK Query – Example

Query: Is the river Amazon longer than the river Nile?

PREFIX prop : <h t t p : / / d b p e d i a . org / p r o p e r t y />ASK{<h t t p : / / d b p e d i a . org / r e s o u r c e / Amazon River> prop : l e n g t h ? amazon .<h t t p : / / d b p e d i a . org / r e s o u r c e / N i l e> prop : l e n g t h ? n i l e .

FILTER (? amazon > ? n i l e ) .}

Answer: true

Gerd Groner, Matthias Thimm Semantic Web 11 / 56

Page 12: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

SPARQL CONSTRUCT Query – Example

CONSTRUCT query is used to build an RDF graph.

PREFIX ex : <h t t p : / / example . org / schema/>PREFIX r d f : <h t t p : / /www. w3 . org /1999/02/22− r d f−syntax−ns#>CONSTRUCT{

? p e r s o n r d f : t y p e ex : Adu l t .}WHERE{

? p e r s o n ex : age ? ageFILTER (? age > 17)

}

Result is an RDF graph.

Gerd Groner, Matthias Thimm Semantic Web 12 / 56

Page 13: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

SPARQL DESCRIBE Query – Example

DESCRIBE query is used to provide further information about anRDF resource

DESCRIBE <h t t p : / / d b p e d i a . org / r e s o u r c e / Amazon River>

Result is an extensive description of the resource “Amazon River”.Here is just an excerpt:

@ p r e f i x r d f : <h t t p : / /www. w3 . org /1999/02/22− r d f−syntax−ns#> .@ p r e f i x d b p e d i a : <h t t p : / / d b p e d i a . org / r e s o u r c e /> .@ p r e f i x dbpedia−owl : <h t t p : / / d b p e d i a . org / o n t o l o g y/> .

d b p e d i a : Amazon River r d f : t y p e dbpedia−owl : N a t u r a l P l a c e ,dbpedia−owl : P l a c e .

. . .

Gerd Groner, Matthias Thimm Semantic Web 13 / 56

Page 14: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Basic SPARQL Patterns

I Triple PatternI Similar to an RDF Triple

I subject, predicate, object

I Any component can be a query variableI Any combination of variables in the query is allowed

I Matching patterns in the WHERE clauseI Matching conjunction of triple patternsI Matching a triple pattern to a graph

I Finding bindings between variables and RDF Terms

I Underneath use of reasonersI Inferring triples originally not present in the knowledge base

Gerd Groner, Matthias Thimm Semantic Web 14 / 56

Page 15: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

SPARQL Example

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : page ? page .? p e r s o n f o a f : name ?name

}

?person

?name

?page

foaf:name

foaf:page

Gerd Groner, Matthias Thimm Semantic Web 15 / 56

Page 16: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/ > .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ g r o e n e r> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : homepage ? page .? p e r s o n f o a f : name ?name }

Query Result:

name page“Steffen Staab” <http://www.uni-koblenz.de/~staab>

“Gerd Groener” <http://www.uni-koblenz.de/~groener>

Gerd Groner, Matthias Thimm Semantic Web 16 / 56

Page 17: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Querying for Blank Nodes

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/ > .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ g r o e n e r> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ? p e r s o n ?name ? pageWHERE {

? p e r s o n f o a f : homepage ? page .? p e r s o n f o a f : name ?name }

Query Result:

person name page_:a “Steffen Staab” <http://www.uni-koblenz.de/~staab>

_:b “Gerd Groener” <http://www.uni-koblenz.de/~groener>

Gerd Groner, Matthias Thimm Semantic Web 17 / 56

Page 18: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Filters

I Further constrain graph patterns

I Applied to the whole group of patterns

I FILTER clause:

I Support for AND and OR logic operatorsI Extensive applications for testing literalsI Support for numerical operationsI Support for math equality operators for literals (<,=, >)I Use of regular expressionsI support for data types defined in XSL (e.g., comparision of

dates, time)I possible comparison of resources (equal, not equal)I even possible user extensions

Gerd Groner, Matthias Thimm Semantic Web 18 / 56

Page 19: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

FILTER – Value Constraints

Data:

@ p r e f i x dc : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/> .@ p r e f i x ex : <h t t p : / / example . org / book/> .@ p r e f i x ns : <h t t p : / / example . org / ns#> .ex : book1 dc : t i t l e ‘ ‘SPARQL T u t o r i a l ’ ’ .ex : book1 ns : p r i c e 42 .ex : book2 dc : t i t l e ‘ ‘ The Semant ic Web ’ ’ .ex : book2 ns : p r i c e 23 .

Query:

PREFIX dc : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/>PREFIX ns : <h t t p : / / example . org / ns#>SELECT ? t i t l e ? p r i c eWHERE { ? x ns : p r i c e ? p r i c e .

FILTER ? p r i c e < 30 .? x dc : t i t l e ? t i t l e }

Query Result:

title price“The Semantic Web” 23

Gerd Groner, Matthias Thimm Semantic Web 19 / 56

Page 20: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

FILTER – Value Constraints

Data:

@ p r e f i x dc : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/> .@ p r e f i x ex : <h t t p : / / example . org / book/> .@ p r e f i x ns : <h t t p : / / example . org / ns#> .ex : book1 dc : t i t l e ‘ ‘SPARQL T u t o r i a l ’ ’ .ex : book1 ns : p r i c e 42 .ex : book2 dc : t i t l e ‘ ‘ The Semant ic Web ’ ’ .ex : book2 ns : p r i c e 23 .

Query:

PREFIX dc : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/>PREFIX ns : <h t t p : / / example . org / ns#>SELECT ? t i t l e ? p r i c eWHERE { ? x ns : p r i c e ? p r i c e .

FILTER ? p r i c e < 30 .? x dc : t i t l e ? t i t l e }

Query Result:

title price“The Semantic Web” 23

Gerd Groner, Matthias Thimm Semantic Web 19 / 56

Page 21: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Scope of Filters

I Filters are applied to the whole group of patterns where theyappear.

{ ? x f o a f : name ?name .? x f o a f : homepage ? page .FILTER r e g e x (? name , ‘ ‘ S t e f f e n ’ ’ ) }

{ ? x f o a f : name ?name .FILTER r e g e x (? name , ‘ ‘ S t e f f e n ’ ’ ) .? x f o a f : homepage ? page }

{ FILTER r e g e x (? name , ‘ ‘ S t e f f e n ’ ’ ) .? x f o a f : name ?name .? x f o a f : homepage ? page }

I These patters are equivalent — have the same solution.

Gerd Groner, Matthias Thimm Semantic Web 20 / 56

Page 22: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Scope of Filters

I Filters are applied to the whole group of patterns where theyappear.

{ ? x f o a f : name ?name .? x f o a f : homepage ? page .FILTER r e g e x (? name , ‘ ‘ S t e f f e n ’ ’ ) }

{ ? x f o a f : name ?name .FILTER r e g e x (? name , ‘ ‘ S t e f f e n ’ ’ ) .? x f o a f : homepage ? page }

{ FILTER r e g e x (? name , ‘ ‘ S t e f f e n ’ ’ ) .? x f o a f : name ?name .? x f o a f : homepage ? page }

I These patters are equivalent — have the same solution.

Gerd Groner, Matthias Thimm Semantic Web 20 / 56

Page 23: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

FILTER – Regular Expressions

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ g r o e n e r> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : homepage ? page .? p e r s o n f o a f : name ?name .FILTER r e g e x (? name , ‘ ‘ S t e f f e n ’ ’ )}

Query Result:

name page“Steffen Staab” <http://www.uni-koblenz.de/~staab>

Gerd Groner, Matthias Thimm Semantic Web 21 / 56

Page 24: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

FILTER – Regular Expressions

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ g r o e n e r> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : homepage ? page .? p e r s o n f o a f : name ?name .FILTER r e g e x (? name , ‘ ‘ i ’ ’ , ‘ ‘ g r o e n e r ’ ’ )}

Query Result: case insensitive

name page“Gerd Groener” <http://www.uni-koblenz.de/~groener>

Gerd Groner, Matthias Thimm Semantic Web 22 / 56

Page 25: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Optional Patterns

I include optional triple patterns to match

I optional is a pattern itself — can include further constraints

SELECTWHERE {

. . .OPTIONAL { . . . }

}

I OPTIONAL is left-associative (i.e., operators are groupedfrom left to right)pattern OPTIONAL {pattern } OPTIONAL { pattern }is the same as{ pattern OPTIONAL { pattern } } OPTIONAL { pattern }

Gerd Groner, Matthias Thimm Semantic Web 23 / 56

Page 26: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : mbox <g r o e n e r @ u n i−k o b l e n z . de> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : name ?name .? p e r s o n f o a f : homepage ? page .}Query Result:

name page“Steffen Staab” <http://www.uni-koblenz.de/~staab>

Gerd Groner, Matthias Thimm Semantic Web 24 / 56

Page 27: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : mbox <g r o e n e r @ u n i−k o b l e n z . de> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : name ?name .? p e r s o n f o a f : homepage ? page .}Query Result:

name page“Steffen Staab” <http://www.uni-koblenz.de/~staab>

Gerd Groner, Matthias Thimm Semantic Web 24 / 56

Page 28: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example — OPTIONAL

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : mbox <g r o e n e r @ u n i−k o b l e n z . de> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : name ?name .OPTIONAL ? p e r s o n f o a f : homepage ? page .}Query Result:

name page“Steffen Staab” <http://www.uni-koblenz.de/~staab>

“Gerd Groener”

Gerd Groner, Matthias Thimm Semantic Web 25 / 56

Page 29: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example — OPTIONAL

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : mbox <g r o e n e r @ u n i−k o b l e n z . de> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : name ?name .OPTIONAL ? p e r s o n f o a f : homepage ? page .}Query Result:

name page“Steffen Staab” <http://www.uni-koblenz.de/~staab>

“Gerd Groener”

Gerd Groner, Matthias Thimm Semantic Web 25 / 56

Page 30: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Union of Graph Pattern

UNION

I combining alternative graph patterns

I if more than one of the alternatives matches, all the possiblepattern solutions are included in result

SELECTWHERE {

{ p a t t e r n }UNION{ p a t t e r n }

}

Gerd Groner, Matthias Thimm Semantic Web 26 / 56

Page 31: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example — UNION

Data:

@ p r e f i x dc10 : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.0/> .@ p r e f i x dc11 : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/> .book1 dc10 : t i t l e ‘ ‘SPARQL T u t o r i a l ’ ’ .book1 dc10 : c r e a t o r ‘ ‘ A l i c e ’ ’ .book2 dc11 : t i t l e ‘ ‘ The Semant ic Web’ ’ .book2 dc11 : c r e a t o r ‘ ‘ Robert ’ ’ .

Query:

PREFIX dc10 : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.0/>PREFIX dc11 : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/>SELECT ? t i t l eWHERE { { ? x dc10 : t i t l e ? t i t l e }

UNION{ ? x dc11 : t i t l e ? t i t l e } }

Query Result: title“SPARQL Tutorial”“The Semantic Web”

Gerd Groner, Matthias Thimm Semantic Web 27 / 56

Page 32: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example — UNION

Data:

@ p r e f i x dc10 : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.0/> .@ p r e f i x dc11 : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/> .book1 dc10 : t i t l e ‘ ‘SPARQL T u t o r i a l ’ ’ .book1 dc10 : c r e a t o r ‘ ‘ A l i c e ’ ’ .book2 dc11 : t i t l e ‘ ‘ The Semant ic Web’ ’ .book2 dc11 : c r e a t o r ‘ ‘ Robert ’ ’ .

Query:

PREFIX dc10 : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.0/>PREFIX dc11 : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/>SELECT ? t i t l eWHERE { { ? x dc10 : t i t l e ? t i t l e }

UNION{ ? x dc11 : t i t l e ? t i t l e } }

Query Result: title“SPARQL Tutorial”“The Semantic Web”

Gerd Groner, Matthias Thimm Semantic Web 27 / 56

Page 33: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Modification of the Result Set

Result of SPARQL query can be further modified

I ORDER BYI sort results alphabetically / numerically by specific variable

I LIMITI Limit number of returned results (only top n results)

I OFFSETI Skip n top results and return the rest

These expressions can be combined in a query.

Gerd Groner, Matthias Thimm Semantic Web 28 / 56

Page 34: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Sequencing and Limiting Results

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : homepage ? page .? p e r s o n f o a f : name ?name

}ORDER BY ?nameLIMIT 20OFFSET 10

Gerd Groner, Matthias Thimm Semantic Web 29 / 56

Page 35: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Bound Variables

I one of the FILTER expressions

I supports testing if a variable in a query can be bound to an instancein the knowledge base

I mostly used for negation as failure

PREFIX f o a f : < h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?nameWHERE {

? p e r s o n f o a f : name ?name .OPTIONAL { ? p e r s o n f o a f : knows ? x . }FILTER ( ! bound (? x ) )

}

Gerd Groner, Matthias Thimm Semantic Web 30 / 56

Page 36: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Bound Variables – Example

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : mbox <g r o e n e r @ u n i−k o b l e n z . de> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : name ?name .OPTIONAL { ? p e r s o n f o a f : homepage ? page .}FILTER ( bound (? page ) )

}Query Result:

name page“Steffen Staab” <http://www.uni-koblenz.de/~staab>

Gerd Groner, Matthias Thimm Semantic Web 31 / 56

Page 37: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Bound Variables – Example

Data:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ S t e f f e n Staab ’ ’ .: a f o a f : homepage <h t t p : / /www. uni−k o b l e n z . de /˜ staab> .: b f o a f : name ‘ ‘ Gerd Groener ’ ’ .: b f o a f : mbox <g r o e n e r @ u n i−k o b l e n z . de> .

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name ? pageWHERE {

? p e r s o n f o a f : name ?name .OPTIONAL { ? p e r s o n f o a f : homepage ? page .}FILTER ( bound (? page ) )

}Query Result:

name page“Steffen Staab” <http://www.uni-koblenz.de/~staab>

Gerd Groner, Matthias Thimm Semantic Web 31 / 56

Page 38: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Tricky Negation

Find people who do not know Steffen.First attempt:

PREFIX f o a f : < h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?nameWHERE {

? p e r s o n f o a f : name ?name .? p e r s o n f o a f : knows ? x .FILTER ( ? x != ‘ ‘ S t e f f e n ’ ’ ) }

Data:... we know that ...

‘ ‘ Paul ’ ’ f o a f : knows ‘ ‘ S t e f f e n ’ ’‘ ‘ Paul ’ ’ f o a f : knows ‘ ‘ S e r g e j ’ ’

... so “Paul” is still a valid answer (do you know why?)

... and we do not want this!

Gerd Groner, Matthias Thimm Semantic Web 32 / 56

Page 39: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Negation with bound

Find people who do not know Steffen.

I now the correct way using bound expression and optional graphpattern

PREFIX f o a f : h t t p : / / xmlns . com/ f o a f / 0 . 1 /SELECT ?nameWHERE {

? p e r s o n f o a f : name ?name .OPTIONAL { ? p e r s o n f o a f : knows ? x .

FILTER ( ? x = ‘ ‘ S t e f f e n ’ ’ ) }FILTER ( ! bound (? x ) )

}

Gerd Groner, Matthias Thimm Semantic Web 33 / 56

Page 40: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Other SPARQL Filter Expressions

Testing if a bounded variable is a blank node.

annotation

:_

Alan Smith

dc:creator

foaf:given

foaf:family

Gerd Groner, Matthias Thimm Semantic Web 34 / 56

Page 41: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Other SPARQL Filter Expressions (2)

I isBlank: Testing if a bounded variable is a blank node.

SELECT ? g i v e n ? f a m i l yWHERE { ? a n n o t a t i o n dc : c r e a t o r ? c .

OPTIONAL {? c f o a f : g i v e n ? g i v e n .? c f o a f : f a m i l y ? f a m i l y } .

FILTER i s B l a n k (? c ) }

I lang: Accessing the language of a literal

SELECT ?name ?mboxWHERE { ? x f o a f : name ?name .

? x f o a f : mbox ?mbox .FILTER ( l a n g (? name ) = ‘ ‘DE’ ’ ) }

Gerd Groner, Matthias Thimm Semantic Web 35 / 56

Page 42: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Other SPARQL Filter Expressions (3)

Testing if a bounded variable is a literal (not a resource).

:_

“Gerd”

<[email protected]>

foaf:name

foaf:mbox

Gerd Groner, Matthias Thimm Semantic Web 36 / 56

Page 43: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Other SPARQL Filter Expressions (4)

I isLiteral: Testing if a bounded variable is a literal (not a resource)

SELECT ?name ?mboxWHERE { ? x f o a f : name ?name .

? x f o a f : mbox ?mbox .FILTER i s L i t e r a l (? mbox ) }

I str: Converting resource URI to string for regular expression matching

SELECT ?name ?mboxWHERE { ? x f o a f : name ?name .

? x f o a f : mbox ?mbox .FILTER r e g e x ( s t r (? mbox ) , ‘ ‘ @uni−k o b l e n z . de ’ ’ ) }

Gerd Groner, Matthias Thimm Semantic Web 37 / 56

Page 44: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Equal Terms and Same Terms

I check if two terms are equal or if they describe the same entity

term1 = term2 orsameTerm ( term1 , term2 )

I returns true if

I terms are of the same type (URI, literal, blank node)I two terms represent URIs are equivalentI two terms represent literals are equivalentI two terms are bound by the same blank node

I same entity can have even different URIs, but connected withowl:sameAs

Gerd Groner, Matthias Thimm Semantic Web 38 / 56

Page 45: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Equal Terms

Find people who have the same email address, but use different names

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example> .: b f o a f : name ‘ ‘Ms A . ’ ’ .: b f o a f : mbox <m a i l t o : a l i c e @ w o r k . example> .

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>SELECT ?name1 ?name2WHERE {

? x f o a f : name ?name1 .? x f o a f : mbox ?mbox1 .? y f o a f : name ?name2 .? y f o a f : mbox ?mbox2 .FILTER ( sameTerm (? mbox1 , ?mbox2 ) && ?name1 != ?name2 ) }

Gerd Groner, Matthias Thimm Semantic Web 39 / 56

Page 46: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

User-defined Functions

FILTER enables using user-defined expressions

PREFIX aGeo : <h t t p : / / example . org / geo#>SELECT ? n e i g h b o r WHERE {

? a aGeo : placeName ‘ ‘ Koblenz ’ ’ .? a aGeo : l o c a t i o n ? axLoc .? a aGeo : l o c a t i o n ? ayLoc .?b aGeo : placeName ? n e i g h b o r .?b aGeo : l o c a t i o n ? bxLoc .?b aGeo : l o c a t i o n ? byLoc .FILTER

( aGeo : d i s t a n c e (? axLoc , ? ayLoc , ? bxLoc , ? byLoc ) < 5 )}

Definition of user function geometric distance between two points described by (x, y)coordinates:

xsd:double aGeo:distance (numeric x1, numeric y1, numeric x2, numeric y2)

Gerd Groner, Matthias Thimm Semantic Web 40 / 56

Page 47: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

User-defined Functions

FILTER enables using user-defined expressions

PREFIX aGeo : <h t t p : / / example . org / geo#>SELECT ? n e i g h b o r WHERE {

? a aGeo : placeName ‘ ‘ Koblenz ’ ’ .? a aGeo : l o c a t i o n ? axLoc .? a aGeo : l o c a t i o n ? ayLoc .?b aGeo : placeName ? n e i g h b o r .?b aGeo : l o c a t i o n ? bxLoc .?b aGeo : l o c a t i o n ? byLoc .FILTER

( aGeo : d i s t a n c e (? axLoc , ? ayLoc , ? bxLoc , ? byLoc ) < 5 )}

Definition of user function geometric distance between two points described by (x, y)coordinates:

xsd:double aGeo:distance (numeric x1, numeric y1, numeric x2, numeric y2)

Gerd Groner, Matthias Thimm Semantic Web 40 / 56

Page 48: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Querying for Inferred Knowledge

I SPARQL does not have specific constructs for accessing inferred knowledge

I Underlying knowledge base is responsible for supportinginference, e.g.,

I Class hierarchyI Property hierarchyI Transitive or symmetric propertiesI OWL restrictionsI Defining classes by unions and/or intersections

I Different knowledge bases can offer different level of support

I Same knowledge in different knowledge bases may returndifferent results for the same query, depending on supportedentailment

Gerd Groner, Matthias Thimm Semantic Web 41 / 56

Page 49: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example

Alice Bob

Clare

daughterOf

sonOf

ancestorOf

ancestorOf

ancestorOf

ancestorOf = owl:transitiveProperty ∧ union(inverse(daughterOf), inverse(sonOf))

Find ancestors of Alice:

Query:

SELECT ? xWHERE { ? x a n c e s t o r O f A l i c e }

Gerd Groner, Matthias Thimm Semantic Web 42 / 56

Page 50: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example

Alice Bob

Clare

daughterOf

sonOf

ancestorOf

ancestorOf

ancestorOf

ancestorOf = owl:transitiveProperty ∧ union(inverse(daughterOf), inverse(sonOf))

Find ancestors of Alice:

Query:

SELECT ? xWHERE { ? x a n c e s t o r O f A l i c e }

Gerd Groner, Matthias Thimm Semantic Web 42 / 56

Page 51: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Query Example

Alice Bob

Clare

daughterOf

sonOf

ancestorOf

ancestorOf

ancestorOf

ancestorOf = owl:transitiveProperty ∧ union(inverse(daughterOf), inverse(sonOf))

Find ancestors of Alice:

Query:

SELECT ? xWHERE { ? x a n c e s t o r O f A l i c e }

Gerd Groner, Matthias Thimm Semantic Web 42 / 56

Page 52: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

CONSTRUCT Queries

I Special type of query to construct a new RDF graph from the existingknowledge base

PREFIX

CONSTRUCT{

. . . graph p a t t e r n . . .

. . . d e f i n i t i o n o f t r i p l e s . . .}

WHERE{

c o n s t r a i n t t r i p l e p a t t e r n s ,f i l t e r s , e t c .

}

Gerd Groner, Matthias Thimm Semantic Web 43 / 56

Page 53: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Constructing Graphs

Data:

@ p r e f i x f o a f :<h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : g ivenname ” A l i c e ” .: a f o a f : f a m i l y n a m e ” Hacker ” .: b f o a f : f i r s t n a m e ”Bob” .: b f o a f : surname ” Hacker ” .

Result:

@ p r e f i x v c a r d : <h t t p : / /www. w3 . org /2001/ vcard−r d f /3.0#>

: v1 v c a r d :N : x .: x v c a r d : givenName

” A l i c e ” .: x v c a r d : familyName

” Hacker ” .: v2 v c a r d :N : z .: z v c a r d : givenName ”Bob” .

: z v c a r d : familyName” Hacker ” .

Query:

PREFIX f o a f :<h t t p : / / xmlns . com/ f o a f /0.1/>

PREFIX v c a r d : <h t t p : / /www. w3 . org /2001/ vcard−r d f /3.0#>

CONSTRUCT{? x v c a r d :N : v .

: v v c a r d : givenName ?gname .: v v c a r d : familyName ? fname

}WHERE{{ ? x f o a f : f i r s t n a m e ?gname }

UNION{ ? x f o a f : g ivenname ?gname } .{ ? x f o a f : surname ? fname }

UNION{ ? x f o a f : f a m i l y n a m e ? fname }}

Gerd Groner, Matthias Thimm Semantic Web 44 / 56

Page 54: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Constructing Graphs

Data:

@ p r e f i x f o a f :<h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : g ivenname ” A l i c e ” .: a f o a f : f a m i l y n a m e ” Hacker ” .: b f o a f : f i r s t n a m e ”Bob” .: b f o a f : surname ” Hacker ” .

Result:

@ p r e f i x v c a r d : <h t t p : / /www. w3 . org /2001/ vcard−r d f /3.0#>

: v1 v c a r d :N : x .: x v c a r d : givenName

” A l i c e ” .: x v c a r d : familyName

” Hacker ” .: v2 v c a r d :N : z .: z v c a r d : givenName ”Bob” .

: z v c a r d : familyName” Hacker ” .

Query:

PREFIX f o a f :<h t t p : / / xmlns . com/ f o a f /0.1/>

PREFIX v c a r d : <h t t p : / /www. w3 . org /2001/ vcard−r d f /3.0#>

CONSTRUCT{? x v c a r d :N : v .

: v v c a r d : givenName ?gname .: v v c a r d : familyName ? fname

}WHERE{{ ? x f o a f : f i r s t n a m e ?gname }

UNION{ ? x f o a f : g ivenname ?gname } .{ ? x f o a f : surname ? fname }

UNION{ ? x f o a f : f a m i l y n a m e ? fname }}

Gerd Groner, Matthias Thimm Semantic Web 44 / 56

Page 55: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Constructing Graphs

Data:

@ p r e f i x f o a f :<h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : g ivenname ” A l i c e ” .: a f o a f : f a m i l y n a m e ” Hacker ” .: b f o a f : f i r s t n a m e ”Bob” .: b f o a f : surname ” Hacker ” .

Result:

@ p r e f i x v c a r d : <h t t p : / /www. w3 . org /2001/ vcard−r d f /3.0#>

: v1 v c a r d :N : x .: x v c a r d : givenName

” A l i c e ” .: x v c a r d : familyName

” Hacker ” .: v2 v c a r d :N : z .: z v c a r d : givenName ”Bob” .

: z v c a r d : familyName” Hacker ” .

Query:

PREFIX f o a f :<h t t p : / / xmlns . com/ f o a f /0.1/>

PREFIX v c a r d : <h t t p : / /www. w3 . org /2001/ vcard−r d f /3.0#>

CONSTRUCT{? x v c a r d :N : v .

: v v c a r d : givenName ?gname .: v v c a r d : familyName ? fname

}WHERE{{ ? x f o a f : f i r s t n a m e ?gname }

UNION{ ? x f o a f : g ivenname ?gname } .{ ? x f o a f : surname ? fname }

UNION{ ? x f o a f : f a m i l y n a m e ? fname }}

Gerd Groner, Matthias Thimm Semantic Web 44 / 56

Page 56: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

ASK Queries

I True / false queries — check if given set of triple patterns hasat least one match in the knowledge base

I Does not include ORDER BY, LIMIT or OFFSET

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ” A l i c e ” .: a f o a f : homepage <h t t p : / / work . example . org / a l i c e /> .: b f o a f : name ”Bob” .: b f o a f : mbox <m a i l t o : bob@work . example>

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>ASK { ? x f o a f : name ” A l i c e ” .

? x f o a f : mbox ? y }

Answer: NO

Gerd Groner, Matthias Thimm Semantic Web 45 / 56

Page 57: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

ASK Queries

I True / false queries — check if given set of triple patterns hasat least one match in the knowledge base

I Does not include ORDER BY, LIMIT or OFFSET

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ” A l i c e ” .: a f o a f : homepage <h t t p : / / work . example . org / a l i c e /> .: b f o a f : name ”Bob” .: b f o a f : mbox <m a i l t o : bob@work . example>

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>ASK { ? x f o a f : name ” A l i c e ” .

? x f o a f : mbox ? y }

Answer: NO

Gerd Groner, Matthias Thimm Semantic Web 45 / 56

Page 58: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

ASK Queries

I True / false queries — check if given set of triple patterns hasat least one match in the knowledge base

I Does not include ORDER BY, LIMIT or OFFSET

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ” A l i c e ” .: a f o a f : homepage <h t t p : / / work . example . org / a l i c e /> .: b f o a f : name ”Bob” .: b f o a f : mbox <m a i l t o : bob@work . example>

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>ASK { ? x f o a f : name ” A l i c e ” .

? x f o a f : mbox ? y }

Answer: NO

Gerd Groner, Matthias Thimm Semantic Web 45 / 56

Page 59: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

ASK Queries

I True / false queries — check if given set of triple patterns hasat least one match in the knowledge base

I Does not include ORDER BY, LIMIT or OFFSET

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .: a f o a f : name ” A l i c e ” .: a f o a f : homepage <h t t p : / / work . example . org / a l i c e /> .: b f o a f : name ”Bob” .: b f o a f : mbox <m a i l t o : bob@work . example>

Query:

PREFIX f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/>ASK { ? x f o a f : name ” A l i c e ” .

? x f o a f : mbox ? y }

Answer: NO

Gerd Groner, Matthias Thimm Semantic Web 45 / 56

Page 60: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

DESCRIBE Queries

I Return a graph that includes description of specific resourcesI Results of DESCRIBE queries reveal meta-information not

returned by standard SELECT queriesI Type of bounded resourcesI Types of relationships used in query pattern

I Exact description of resources is determined by the queryservice

I No common standard of descriptionI Can even include information about related resources

Gerd Groner, Matthias Thimm Semantic Web 46 / 56

Page 61: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

DESCRIBE Query Example

PREFIX e n t : <h t t p : / / org . example . com/ employees#>DESCRIBE ? xWHERE { ? x e n t : e m p l o y e e I d ”1234” }

Result:

@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .@ p r e f i x v c a r d : <h t t p : / /www. w3 . org /2001/ vcard−r d f /3.0> .@ p r e f i x exOrg : <h t t p : / / org . example . com/ employees#> .@ p r e f i x r d f : <h t t p : / /www. w3 . org /1999/02/22− r d f−syntax−ns#> .@ p r e f i x owl : <h t t p : / /www. w3 . org /2002/07/ owl#>

: a exOrg : e m p l o y e e I d ”1234” ;f o a f : mbox sha1sum ”ABCD1234” ;v c a r d :N

[ v c a r d : Fami ly ” Smith ” ;v c a r d : Given ” John ” ] .

f o a f : mbox sha1sum r d f : t y p e owl : I n v e r s e F u n c t i o n a l P r o p e r t y

Gerd Groner, Matthias Thimm Semantic Web 47 / 56

Page 62: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Named Graphs

I RDF data stores may hold multiple RDF graphs:I record information about each graphI queries that involve information from more than one graphI default graph (does not have a name)I multiple named graphs (identified by URI reference)I direct implementation for reification

I Accessing named graphsI FROM

I access knowledge in default graph

I FROM NAMEDI access information from specific named graph

Gerd Groner, Matthias Thimm Semantic Web 48 / 56

Page 63: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Remember: Ad hoc reification (direct)

→ remember: there are several alternatives for reification in RDF(modeling choices)

Repetition: Reification (statement on a statement):

Example:“Kant” examined “Jonas” in “Introduction to CS” and gave himgrade “1.0”

How can we model this in RDF?

Gerd Groner, Matthias Thimm Semantic Web 49 / 56

Page 64: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Ad hoc reification (direct)

“Kant” examined “Jonas” in “Introduction to CS” and gave himgrade “1.0”

s:kant/#me s:exam1 s:introCS

1.0

s:jonas/#me

s:examiner

s:examinee

s:grade

s:in

Gerd Groner, Matthias Thimm Semantic Web 50 / 56

Page 65: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Named Graph for reification

“Kant” examined “Jonas” in “Introduction to CS” and gave himgrade “1.0”

s:JonasGraph

s:kant/#me s:exam1 s:introCS

1.0

s:examiner

s:grade

s:in

s:Stud2Graph

Gerd Groner, Matthias Thimm Semantic Web 51 / 56

Page 66: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Named Graphs — Example

Data:

# D e f a u l t graph ( h t t p : / / example . org / f r i e n d s )@ p r e f i x dc : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/> .<h t t p : / / example . org /bob> dc : p u b l i s h e r ‘ ‘ Bob ’ ’ .<h t t p : / / example . org / a l i c e > dc : p u b l i s h e r ‘ ‘ A l i c e ’ ’ .

# Bob ’ s Graph : h t t p : / / example . org /bob@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : name ‘ ‘ Bob ’ ’ .: a f o a f : mbox <m a i l t o : bob@oldcorp . example . org> .

# A l i c e ’ s Graph : h t t p : / / example . org / a l i c e@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

Query:

SELECT . . .FROM NAMED <h t t p : / / example . org / a l i c e >FROM NAMED <h t t p : / / example . org /bob>. . .

Gerd Groner, Matthias Thimm Semantic Web 52 / 56

Page 67: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Named Graphs — Example

Data:

# D e f a u l t graph ( h t t p : / / example . org / f r i e n d s )@ p r e f i x dc : <h t t p : / / p u r l . o rg / dc / e l e m e n t s /1.1/> .<h t t p : / / example . org /bob> dc : p u b l i s h e r ‘ ‘ Bob ’ ’ .<h t t p : / / example . org / a l i c e > dc : p u b l i s h e r ‘ ‘ A l i c e ’ ’ .

# Bob ’ s Graph : h t t p : / / example . org /bob@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : name ‘ ‘ Bob ’ ’ .: a f o a f : mbox <m a i l t o : bob@oldcorp . example . org> .

# A l i c e ’ s Graph : h t t p : / / example . org / a l i c e@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

Query:

SELECT . . .FROM NAMED <h t t p : / / example . org / a l i c e >FROM NAMED <h t t p : / / example . org /bob>. . .

Gerd Groner, Matthias Thimm Semantic Web 52 / 56

Page 68: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Relationships between Named Graphs

# D e f a u l t graph@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .

: y f o a f : name ‘ ‘ A l i c e ’ ’ x .: y f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .: y f o a f : mbox <m a i l t o : a l i c e @ o l d c o r p . org> .

# Graph : h t t p : / / example . org / a l i c e@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

# Graph : h t t p : / / example . org / a l i c e p r e v@ p r e f i x f o a f : <h t t p : / / xmlns . com/ f o a f /0.1/> .

: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ o l d c o r p . org> .

Gerd Groner, Matthias Thimm Semantic Web 53 / 56

Page 69: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Accessing the Name of Named Graphs

Data:

Query:

Result:

# Graph : h t t p : / / example . org / a l i c e: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

# Graph : h t t p : / / example . org / a l i c e p r e v: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ o l d c o r p . org> .

SELECT ? s r c ?mboxWHERE {

GRAPH ? s r c{ ? x f o a f : name ‘ ‘ A l i c e ’ ’ .

? x f o a f : mbox ?mbox} }

src mboxhttp://example.org/alice mailto:[email protected]

http://example.org/alice_prev mailto:[email protected]

Gerd Groner, Matthias Thimm Semantic Web 54 / 56

Page 70: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Accessing the Name of Named Graphs

Data:

Query:

Result:

# Graph : h t t p : / / example . org / a l i c e: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

# Graph : h t t p : / / example . org / a l i c e p r e v: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ o l d c o r p . org> .

SELECT ? s r c ?mboxWHERE {

GRAPH ? s r c{ ? x f o a f : name ‘ ‘ A l i c e ’ ’ .

? x f o a f : mbox ?mbox} }

src mboxhttp://example.org/alice mailto:[email protected]

http://example.org/alice_prev mailto:[email protected]

Gerd Groner, Matthias Thimm Semantic Web 54 / 56

Page 71: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Accessing the Name of Named Graphs

Data:

Query:

Result:

# Graph : h t t p : / / example . org / a l i c e: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

# Graph : h t t p : / / example . org / a l i c e p r e v: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ o l d c o r p . org> .

SELECT ? s r c ?mboxWHERE {

GRAPH ? s r c{ ? x f o a f : name ‘ ‘ A l i c e ’ ’ .

? x f o a f : mbox ?mbox} }

src mboxhttp://example.org/alice mailto:[email protected]

http://example.org/alice_prev mailto:[email protected]

Gerd Groner, Matthias Thimm Semantic Web 54 / 56

Page 72: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Restricting Access by Graph Name

Data:

# Graph : h t t p : / / example . org / a l i c e: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

# Graph : h t t p : / / example . org / a l i c e p r e v: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ o l d c o r p . org> .

Query:

PREFIX ex : <h t t p : / / example . org/>SELECT ?mboxWHERE {

GRAPH ex : a l i c e{ ? x f o a f : mbox ?mbox }

}

Result: mboxmailto:[email protected]

Gerd Groner, Matthias Thimm Semantic Web 55 / 56

Page 73: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Restricting Access by Graph Name

Data:

# Graph : h t t p : / / example . org / a l i c e: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

# Graph : h t t p : / / example . org / a l i c e p r e v: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ o l d c o r p . org> .

Query:

PREFIX ex : <h t t p : / / example . org/>SELECT ?mboxWHERE {

GRAPH ex : a l i c e{ ? x f o a f : mbox ?mbox }

}

Result: mboxmailto:[email protected]

Gerd Groner, Matthias Thimm Semantic Web 55 / 56

Page 74: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Restricting Access by Graph Name

Data:

# Graph : h t t p : / / example . org / a l i c e: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ w o r k . example . org> .

# Graph : h t t p : / / example . org / a l i c e p r e v: a f o a f : name ‘ ‘ A l i c e ’ ’ .: a f o a f : mbox <m a i l t o : a l i c e @ o l d c o r p . org> .

Query:

PREFIX ex : <h t t p : / / example . org/>SELECT ?mboxWHERE {

GRAPH ex : a l i c e{ ? x f o a f : mbox ?mbox }

}

Result: mboxmailto:[email protected]

Gerd Groner, Matthias Thimm Semantic Web 55 / 56

Page 75: Semantic Web - SPARQL - Uni-Koblenz · Semantic Web SPARQL Gerd Gr oner, Matthias Thimm fgroener,thimmg@uni-koblenz.de Institute for Web Science and Technologies (WeST) University

Outline

1 SPARQL Protocol and RDF Query Language

2 SPARQL Protocol

Gerd Groner, Matthias Thimm Semantic Web 56 / 56