OpenHPI 3.2 - How to Query RDF(S)? - SPARQL(2)

Post on 11-May-2015

548 views 2 download

Tags:

Transcript of OpenHPI 3.2 - How to Query RDF(S)? - SPARQL(2)

This file is licensed under the Creative Commons Attribution-NonCommercial 3.0 (CC BY-NC 3.0)

Dr. Harald Sack

Hasso Plattner Institute for IT Systems Engineering

University of Potsdam

Spring 2013

Semantic Web Technologies

Lecture 3: Semantic Web - Basic Architecture II02: SPARQL (2)

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

2

Lecture 3: Semantic Web - Basic Architecture II

Open HPI - Course: Semantic Web Technologies

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

Is RDF(S) sufficient for

Knowledge Representation

in the Semantic Web?

02 How to query RDF(S) ? - SPARQL (2)

Open HPI - Course: Semantic Web Technologies - Lecture 3: Semantic Web Basic Architecture

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

4

SPARQL Query Format• The keyword FILTER specifies constraints for the results

• FILTER expressionscontain operatorsand functions

• FILTER can NOT assign/create new values

# Default Graph (stored at http://example.org/book) @prefix dc: <http://purl.org/dc/elements/1.1/> .@prefix : <http://example.org/book/> .@prefix ns: <http://example.org/ns#> .

:book1 dc:title "SPARQL Tutorial" .:book1 ns:price 42 .:book2 dc:title "The Semantic Web" .:book2 ns:price 23 .

PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX ns: <http://example.org/ns#>SELECT ?title ?priceFROM <http://example.org/book> WHERE { ! ?x ns:price ?price . FILTER (?price < 30.5) ?x dc:title ?title . }

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

5 SPARQL Unary Operators in Constraints

Operator Type(A) Result Type!A xsd:boolean xsd:boolean

+A numeric numeric

-a numeric numeric

BOUND(A) variable xsd:boolean

isURI(A) RDF term xsd:boolean

isBLANK(A) RDF term xsd:boolean

isLITERAL(A) RDF term xsd:boolean

STR(A) literal/URI simple literal

LANG(A) literal simple literal

DATATYPE(A) literal URI

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

6 More SPARQL Operators

• Logical connectives && and || for xsd:boolean

• Comparison operators =, !=, <, >, <=, and >= for numeric datatypes, xsd:dateTime, xsd:string, and xsd:boolean

• Comparison operators = and != for other datatypes

• Arithmetic operators +, -, *, and / for numeric datatypes

• and in addition:

• REGEX(String,Pattern) or REGEX(String,Pattern,Flags)

• sameTERM(A,B)

• langMATCHES(A,B)

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

7 Filter Constraints are Evaluated in 3-valued Logic• true, false, and error

A !A

T F

F T

E E

A B A || B A && B

T T T T

T F T F

F T T F

F F F F

T E T E

E T T E

F E E F

E F E F

E E E E

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

8

SPARQL Query Format• The keyword OPTIONAL selects optional elements from the RDF graph

• complies to a Left Outer Join

# Default Graph (stored at http://example.org/addresses) @prefix foaf: <http://xmlns.com/foaf/0.1/> .@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

_:a rdf:type foaf:Person ._:a foaf:name "Alice" ._:a foaf:mbox <mailto:alice@example.com> ._:a foaf:mbox <mailto:alice@work.example> .

_:b rdf:type foaf:Person ._:b foaf:name "Bob" .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?name ?mboxFROM <http://example.org/addresses>WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox }}

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

8

SPARQL Query Format• The keyword OPTIONAL selects optional elements from the RDF graph

• complies to a Left Outer Join

# Default Graph (stored at http://example.org/addresses) @prefix foaf: <http://xmlns.com/foaf/0.1/> .@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

_:a rdf:type foaf:Person ._:a foaf:name "Alice" ._:a foaf:mbox <mailto:alice@example.com> ._:a foaf:mbox <mailto:alice@work.example> .

_:b rdf:type foaf:Person ._:b foaf:name "Bob" .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?name ?mboxFROM <http://example.org/addresses>WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox }}

Result:Result:Alice mailto:alice@example.com

Alice mailto:alice@work.example

Bob

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

9

SPARQL Query Format• The keyword UNION allows for alternatives (logical disjunction)

# Default Graph (stored at http://example.org/book) @prefix dc10: <http://purl.org/dc/elements/1.0/> .@prefix dc11: <http://purl.org/dc/elements/1.1/> .

_:a dc10:title "SPARQL Query Language Tutorial" ._:a dc10:creator "Alice" .

_:b dc11:title "SPARQL Protocol Tutorial" ._:b dc11:creator "Bob" .

_:c dc10:title "SPARQL" ._:c dc11:title "SPARQL (updated)" .

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

SELECT ?titleFROM <http://example.org/books>WHERE { { ?book dc10:title ?title } UNION { ! ?book dc11:title ?title } }

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

9

SPARQL Query Format• The keyword UNION allows for alternatives (logical disjunction)

# Default Graph (stored at http://example.org/book) @prefix dc10: <http://purl.org/dc/elements/1.0/> .@prefix dc11: <http://purl.org/dc/elements/1.1/> .

_:a dc10:title "SPARQL Query Language Tutorial" ._:a dc10:creator "Alice" .

_:b dc11:title "SPARQL Protocol Tutorial" ._:b dc11:creator "Bob" .

_:c dc10:title "SPARQL" ._:c dc11:title "SPARQL (updated)" .

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

SELECT ?titleFROM <http://example.org/books>WHERE { { ?book dc10:title ?title } UNION { ! ?book dc11:title ?title } }

Result: "SPARQL Query Language Tutorial" "SPARQL Protocol Tutorial" "SPARQL"

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

10 SPARQL Query Format• Negation in SPARQL

• (complies to ,NOT EXISTS‘ in SQL)

@prefix foaf: <http://xmlns.com/foaf/0.1/> .@prefix dc: <http://purl.org/dc/elements/1.1/> .@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

_:a foaf:givenName "Alice".

_:b foaf:givenName "Bob" ._:b dc:date "2005-04-04T04:04:04Z"^^xsd:dateTime .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>SELECT ?name WHERE { ?x foaf:givenName ?name . OPTIONAL { ?x dc:date ?date } . FILTER (!bound(?date)) }

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

10 SPARQL Query Format• Negation in SPARQL

• (complies to ,NOT EXISTS‘ in SQL)

@prefix foaf: <http://xmlns.com/foaf/0.1/> .@prefix dc: <http://purl.org/dc/elements/1.1/> .@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

_:a foaf:givenName "Alice".

_:b foaf:givenName "Bob" ._:b dc:date "2005-04-04T04:04:04Z"^^xsd:dateTime .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>SELECT ?name WHERE { ?x foaf:givenName ?name . OPTIONAL { ?x dc:date ?date } . FILTER (!bound(?date)) }

Result: "Alice"

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

11 SPARQL Query Format•SPARQL queries are executed over an RDF dataset

•one (or more) default RDF graph

•zero or more named RDF graphs

•Named Graphs can explicitely addressed via keyword GRAPH and the URI of the named graph

GRAPH <http://example.org/graph1.rdf> { ! ?x foaf:mbox ?mbox }

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

12PREFIX hpi: ...SELECT ...FROM hpi:g1FROM hpi:g4FROM NAMED hpi:g1FROM NAMED hpi:g2FROM NAMED hpi:g3WHERE { ... A ... GRAPH hpi:g3 { ... B ... } GRAPH ?g { ... C ... }}

hpi:g1

hpi:g4

Default Graph

hpi:g1

hpi:g2

hpi:g3

Default Graph

Named Graphs

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

13 SPARQL Query Format• Example for Named Graphs

@prefix dc: <http://purl.org/dc/elements/1.1/> .

<http://example.org/bob> dc:publisher "Bob Hacker" .<http://example.org/alice> dc:publisher "Alice Hacker" .

Default Graph (stored at http://example.org/dft.ttl)

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Bob" ._:a foaf:mbox <mailto:bob@oldcorp.example.org> .

Named Graph: http://example.org/bob

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" ._:a foaf:mbox <mailto:alice@work.example.org> .

Named Graph: http://example.org/alice

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

14 SPARQL Query Format• Example for Named Graphs

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?g ?mbox ?whoFROM <http://example.org/dft.ttl>FROM NAMED <http://example.org/alice>FROM NAMED <http://example.org/bob>WHERE{ ?g dc:publisher ?who . GRAPH ?g { ?x foaf:mbox ?mbox }}

Default Graph

Named Graph

Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität Potsdam

Is RDF(S) sufficient for

Knowledge Representation

in the Semantic Web?

03 How to query RDF(S) ? - SPARQL (3)

Open HPI - Course: Semantic Web Technologies - Lecture 3: Semantic Web Basic Architecture