SPARQL Query Language for RDF

50
SPARQL QUERY LANGUAGE FOR RDF

description

SPARQL Query Language for RDF. Agenda. Introduction Graph Patterns Query Execution and Ordering Query Forms Testing Values SPARQL Support. The XML approach is to "wrap" each data item in start/end tags. 14.8 meters - PowerPoint PPT Presentation

Transcript of SPARQL Query Language for RDF

Page 1: SPARQL Query Language for RDF

SPARQLQUERY LANGUAGE FOR RDF

Page 2: SPARQL Query Language for RDF

Agenda

• Introduction

• Graph Patterns

• Query Execution and Ordering

• Query Forms

• Testing Values

• SPARQL Support

Page 3: SPARQL Query Language for RDF

The XML approach is to "wrap" each data item in start/end tags

<Aircraft> <wingspan>14.8 meters</wingspan> <weight>512 kilograms</weight> <cruise-speed>70 knots</cruise-speed> <range>400 nautical miles</range> <description> medium-altitude, long-endurance unmanned aerial vehicle </description></Aircraft>

RQ-1.xml

Page 4: SPARQL Query Language for RDF

Why use OWL?

Page 5: SPARQL Query Language for RDF

Why use OWL?

• The purpose of this document is to describe the role that OWL plays in data interoperability. [Note: this is not the only use of OWL, but it is an important one.]

• Contents:– Understanding Syntax versus Semantics– An example that shows why standardizing syntax is

necessary but not sufficient– Migrating from defining semantics on a per-

application basis to standardized semantics

Page 6: SPARQL Query Language for RDF

Syntax versus Semantics

• Syntax: the structure of your data– e.g., XML mandates that you structure your data by "wrapping"

each data item within a start tag and an end tag pair, with the end tag being preceded by / and both tags in <…> brackets.

– That is, XML specifies the syntax of your data. • Semantics: the meaning of your data

• Two conditions necessary for interoperability:

1. Adopt a common syntax: this enables applications to parse the data. XML provides a common syntax, and thus is a critical first step. 2. Adopt a means for understanding the semantics: this enables applications to use the data. OWL provides a standard way of expressing the semantics.

Page 7: SPARQL Query Language for RDF

What is this XML snippet talking about, i.e., what are the semantics?

<Predator> …</Predator>

What is a Predator?

Page 8: SPARQL Query Language for RDF

Predator - which one?

• Predator: a medium-altitude, long-endurance unmanned aerial vehicle system.

• Predator : one that victimizes, plunders, or destroys, especially for one's own gain.

• Predator : an organism that lives by preying on other organisms.• Predator: a company which specializes in camouflage attire.• Predator: a video game.• Predator: software for machine networking.• Predator: a chain of paintball stores.

Page 9: SPARQL Query Language for RDF

Meaning (semantics) applied on a per-application basis

<Predator> …</Predator>

application

Semantics: A Predator is type ofAircraft.Actions: These actions must be performed on the Predator data: - identify ground control station. - determine onboard sensors. - determine ordnance.

Page 10: SPARQL Query Language for RDF

Meaning (semantics) applied on a per-application basis

XML

app#1Semantics: Code to interpret the dataAction: Code to process the data

app#2Semantics: Code to interpret the dataAction: Code to process the data

Page 11: SPARQL Query Language for RDF

Problem with attaching semantics on a per-application basis

applicationSemantics: Code to interpret the dataAction: Code to process the data

Problems with burying semantic definitionswithin each application:- Duplicate effort - Each application must express the semantics- Variability of interpretation - Each application can take its own interpretation - Example: Mars probe disaster - one application interpreted the data in inches, another application interpreted the data in centimeters.- No ad-hoc discovery and exploitation - Applications have the semantics pre-wired. Thus, when new data (e.g., new type of aircraft) is encountered an application may not be able to effectively process it. This makes for brittle applications.

What's a better approach?

Page 12: SPARQL Query Language for RDF

Better approach:(1) Extricate semantic definitions from applications (2) Express semantic definitions in a standard vocabulary XML

app#1Action: Code to process the data

app#2Action: Code to process the data

OWL DocumentSemantic Definitions

Page 13: SPARQL Query Language for RDF

OWL provides an agreed-upon vocabulary for expressing semantics

A Sampling of the OWL Vocabulary:

subClassOf: this OWL element is used to assert that one class of items is a subset of another class of items. Example: Predator is a subClassOf Aircraft.

FunctionalProperty: this OWL element is used to assert that a property has a unique value. Example: sensorID is a FunctionalProperty, i.e., sensorID has a unique value.

equivalentClass: this OWL element is used to assert that one Class is equivalent to another Class. Example: Platform is an equivalentClass to Aircraft.

Page 14: SPARQL Query Language for RDF

OWL Enables Machines to Understand Data!

XML/DTD/XML Schemas

OWL

Syntax

Semantics

OWL enables machine-processable semantics!

Page 15: SPARQL Query Language for RDF

Ontology (definition)

• An Ontology is the collection of semantic definitions for a domain.

• Example: an Aircraft Ontology is the set of semantic definitions for the Aircraft domain, e.g.,– Predator is a subClassOf Aircraft.– sensorID is a FunctionalProperty.

– Platform is an equivalentClass to Aircraft.

Page 16: SPARQL Query Language for RDF

Why use OWL?

• Benefits to application developers:– Less code to write (save $$$).

– Less chance of misinterpretation (save $$$).

• Benefits to community at large:– Everyone can understand each other's data's semantics,

since they are in a common language.

– OWL uses the XML syntax to express semantics, i.e., it builds on an existing technology.

• Don't have to learn new syntax.

• Common XML tools (e.g., parsers) can work on OWL.

– OWL will soon be a W3C recommendation.

Page 17: SPARQL Query Language for RDF

XQuery Databases

• XQuery is an XML query language. • It can be used to efficiently and easily extract information

from Native XML Databases (NXD).• It can be used to query XML views of relational data.

Page 18: SPARQL Query Language for RDF

Database Systems supporting XQuery

• The following database systems offer XQuery support: – Native XML Databases:

• Berkeley DB XML• eXist• MarkLogic• Software AG Tamino• Raining Data TigerLogic• Documentum xDb (X-Hive/DB)

– Relational Databases: • IBM DB2• Microsoft SQL Server• Oracle

Page 19: SPARQL Query Language for RDF

Native XML Database (NXD)

• Native XML databases have an XML-based internal model, i.e., their fundamental unit of storage is XML.

Page 20: SPARQL Query Language for RDF

Copyright 2004 Digital Enterprise Research Institute. All rights reserved.

www.deri.org

OWL Tutorial

Page 21: SPARQL Query Language for RDF

Introduction

• RDF – flexible and extensible way to represent information about WWW resources

• SPARQL - query language for getting information from RDF graphs. It provides facilities to:– extract information in the form of URIs, blank nodes, plain and typed

literals.

– extract RDF subgraphs.

– construct new RDF graphs based on information in the queried graphs

• matching graph patterns • variables – global scope; indicated by ‘?‘ or ‘$‘ • query terms – based on Turtle syntax• terms delimited by "<>" are relative URI references • data description format - Turtle

Page 22: SPARQL Query Language for RDF

Graph Patterns

Basic Graph Pattern – set of Triple Patterns

Group Pattern - a set of graph patterns must all match

Value Constraints - restrict RDF terms in a solution

Optional Graph Patterns .- additional patterns may extend the solution

Alternative Graph Pattern – two or more possible patterns are tried

Patterns on Named Graphs - patterns are matched against named graphs

Page 23: SPARQL Query Language for RDF

Basic Graph Pattern

• Set of Triple Patterns– Triple Pattern – similar to an RDF Triple (subject, predicate,

object), but any component can be a query variable; literal subjects are allowed

– Matching a triple pattern to a graph: bindings between variables and RDF Terms

• Matching of Basic Graph Patterns– A Pattern Solution of Graph Pattern GP on graph G is any

substitution S such that S(GP) is a subgraph of G.

x v

rdf:type rdf:Property

rdf:type rdf:type rdf:PropertySELECT ?x ?v WHERE { ?x ?x ?v }

?book dc:title ?title

Page 24: SPARQL Query Language for RDF

Basic Graph Pattern - Multiple Matches

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?mbox

WHERE

{ ?x foaf:name ?name .

?x foaf:mbox ?mbox }

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

_:a foaf:name "Johnny Lee Outlaw" .

_:a foaf:mbox <mailto:[email protected]> .

_:b foaf:name "Peter Goodguy" .

_:b foaf:mbox <mailto:[email protected]> .

name mbox

"Johnny Lee Outlaw"

<mailto:[email protected]>

"Peter Goodguy" <mailto:[email protected]>

Group Graph Pattern (set of graph patterns) also!

Data

Query

Query Result

Page 25: SPARQL Query Language for RDF

Basic Graph Pattern - Blank Nodes

PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?x ?name

WHERE { ?x foaf:name ?name }

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

_:a foaf:name "Alice" .

_:b foaf:name "Bob" .

x name

_:c “Alice“

_:d “Bob”

Data

Query

Query Result

Page 26: SPARQL Query Language for RDF

Graph Patterns

Basic Graph Pattern – set of Triple Patterns

Group Pattern - a set of graph patterns must all match

Value Constraints - restrict RDF terms in a solution

Optional Graph Patterns .- additional patterns may extend the solution

Alternative Graph Pattern – two or more possible patterns are tried

Patterns on Named Graphs - patterns are matched against named graphs

Page 27: SPARQL Query Language for RDF

Group Pattern

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?mbox

WHERE

{ ?x foaf:name ?name .

?x foaf:mbox ?mbox }

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?mbox

WHERE

{ {?x foaf:name ?name;

foaf:mbox ?mbox }

}

Page 28: SPARQL Query Language for RDF

Graph Patterns

Basic Graph Pattern – set of Triple Patterns

Group Pattern - a set of graph patterns must all match

Value Constraints - restrict RDF terms in a solution

Optional Graph Patterns .- additional patterns may extend the solution

Alternative Graph Pattern – two or more possible patterns are tried

Patterns on Named Graphs - patterns are matched against named graphs

Page 29: SPARQL Query Language for RDF

Value Constraints

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

PREFIX ns: <http://example.org/ns#>

SELECT ?title ?price

WHERE { ?x ns:price ?price .

FILTER ?price < 30 .

?x dc:title ?title . }

@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 .

title price

"The Semantic Web" 23

Data

Query

Query Result

Page 30: SPARQL Query Language for RDF

Graph Patterns

Basic Graph Pattern – set of Triple Patterns

Group Pattern - a set of graph patterns must all match

Value Constraints - restrict RDF terms in a solution

Optional Graph Patterns .- additional patterns may extend the solution

Alternative Graph Pattern – two or more possible patterns are tried

Patterns on Named Graphs - patterns are matched against named graphs

Page 31: SPARQL Query Language for RDF

Optional graph patterns

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

PREFIX ns: <http://example.org/ns#>

SELECT ?title ?price

WHERE { ?x dc:title ?title .

OPTIONAL { ?x ns:price ?price .

FILTER ?price < 30 }}

@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 .

title price

“SPARQL Tutorial“

"The Semantic Web" 23

Data

Query

Query Result

Page 32: SPARQL Query Language for RDF

Multiple Optional Blocks

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?mbox ?hpage

WHERE { ?x foaf:name ?name .

OPTIONAL { ?x foaf:mbox ?mbox }. OPTIONAL { ?x foaf:homepage ?hpage } }

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

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

_:a foaf:name "Alice" .

_:a foaf:homepage <http://work.example.org/alice/> .

_:b foaf:name "Bob" .

_:b foaf:mbox <mailto:[email protected]> .

Data

Query

Query Result

name Mbox hpage

“Alice“ <http://work.example.org/alice/>

“Bob“ <mailto:[email protected]>

Page 33: SPARQL Query Language for RDF

Graph Patterns

Basic Graph Patterns – set of Triple Patterns

Group Patterns - a set of graph patterns must all match

Value Constraints - restrict RDF terms in a solution

Optional Graph Patterns .- additional patterns may extend the solution

Alternative Graph Patterns – two or more possible patterns are tried

Patterns on Named Graphs - patterns are matched against named graphs

Page 34: SPARQL Query Language for RDF

Alternative Graph Patterns

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

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

SELECT ?x ?y

WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } }

@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" .

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

_:c dc10:title "SPARQL" .

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

Data

Query

Query Result

x y

"SPARQL (updated)"

"SPARQL Protocol Tutorial"

"SPARQL"

"SPARQL Query Language Tutorial"

Page 35: SPARQL Query Language for RDF

Graph Patterns

Basic Graph Pattern – set of Triple Patterns

Group Pattern - a set of graph patterns must all match

Value Constraints - restrict RDF terms in a solution

Optional Graph Patterns .- additional patterns may extend the solution

Alternative Graph Pattern – two or more possible patterns are tried

Patterns on Named Graphs - patterns are matched against named graphs

Page 36: SPARQL Query Language for RDF

RDF Dataset

• RDF data stores may hold multiple RDF graphs:– record information about each graph– queries that involve information from more than one graph – RDF Dataset in SPARQL terminology– the background graph, which does not have a name, and zero or

more named graphs, identified by URI reference

• the relationship between named and background graphs:– (i) to have information in the background graph that includes

provenance information about the named graphs (the application is not directly trusting the information in the named graphs )

– (ii) to include the information in the named graphs in the background graph as well.

Page 37: SPARQL Query Language for RDF

RDF Dataset- The Relationship between Named and Background Graphs (I)

# Background graph

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

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

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

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

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

_:a foaf:name "Bob" .

_:a foaf:mbox <mailto:[email protected]> .

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

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

_:a foaf:name "Alice" .

_:a foaf:mbox <mailto:[email protected]> .

Page 38: SPARQL Query Language for RDF

RDF Dataset- The Relationship between Named and Background Graphs (II)

# Background graph

@prefix foaf: <http://xmlns.com/foaf/0.1/> . _:x foaf:name "Bob" .

_:x foaf:mbox <mailto:[email protected]> .

_:y foaf:name "Alice" .

_:y foaf:mbox <mailto:[email protected]> .

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

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

_:a foaf:name "Bob" .

_:a foaf:mbox <mailto:[email protected]> .

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

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

_:a foaf:name "Alice" .

_:a foaf:mbox <mailto:[email protected]> .

Page 39: SPARQL Query Language for RDF

Querying the Dataset

# Graph: http://example.org/foaf/aliceFoaf

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

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

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

_:a foaf:name "Alice" .

_:a foaf:mbox <mailto:[email protected]> .

_:a foaf:knows _:b .

_:b rdfs:seeAlso <http://example.org/foaf/bobFoaf> .

<http://example.org/foaf/bobFoaf> rdf:type foaf:PersonalProfileDocument .

_:b foaf:name "Bob" .

_:b foaf:mbox <mailto:[email protected]> .

_:b foaf:age 32 . # Graph: http://example.org/foaf/bobFoaf

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

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

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

_:1 foaf:mbox <mailto:[email protected]> .

_:1 rdfs:seeAlso <http://example.org/foaf/bobFoaf> .

_:1 foaf:age 35 .

<http://example.org/foaf/bobFoaf> rdf:type foaf:PersonalProfileDocument .

Page 40: SPARQL Query Language for RDF

Querying the Dataset - Accessing Graph Labels

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?src ?bobAge

WHERE { GRAPH ?src

{ ?x foaf:mbox <mailto:[email protected]> .

?x foaf:age ?bobAge }

}

src bobAge

<http://example.org/foaf/aliceFoaf> 32

<http://example.org/foaf/bobFoaf> 35

Page 41: SPARQL Query Language for RDF

Querying the Dataset - Restricting by Graph Label

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

PREFIX data: <http://example.org/foaf/>

SELECT ?age

WHERE

{

GRAPH data:bobFoaf {

?x foaf:mbox <mailto:[email protected]> .

?x foaf:age ?age }

}

age

35

Page 42: SPARQL Query Language for RDF

Querying the Dataset - Restricting via Query Pattern

PREFIX data: <http://example.org/foaf/>

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

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

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?mbox ?age ?ppd

WHERE

{ GRAPH data:aliceFoaf

{ ?alice foaf:mbox <mailto:[email protected]> ;

foaf:knows ?whom .

?whom foaf:mbox ?mbox ;

rdfs:seeAlso ?ppd .

?ppd a foaf:PersonalProfileDocument . } .

GRAPH ?ppd { ?w foaf:mbox ?mbox ;

foaf:age ?age } }

mbox age ppd

<mailto:[email protected]> 35 <http://example.org/foaf/bobFoaf>

Page 43: SPARQL Query Language for RDF

Query Execution and Ordering

• Optional-1: an optional pattern that has a common variable with a(more) basic graph pattern(s) must be executed after the basic graph pattern(s)

• Optional-2: there can't be two optionals with a common variable, if that variable does not occur in a basic graph pattern as well

• Constraint: constraints are evaluated after variables are assigned values

Page 44: SPARQL Query Language for RDF

Query forms:

– SELECT

• returns all, or a subset of the variables bound in a query pattern match

• formats : XML or RDF/XML

– CONSTRUCT • returns an RDF graph constructed by substituting variables in a

set of triple templates

– DESCRIBE• returns an RDF graph that describes the resources found.

– ASK• returns whether a query pattern matches or not.

Page 45: SPARQL Query Language for RDF

CONSTRUCT Examples(I)

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

_:a foaf:name "Alice" .

_:a foaf:mbox <mailto:[email protected]> .

@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>.

<http://example.org/person#Alice> vcard:FN "Alice" .

PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> CONSTRUCT { <http://example.org/person#Alice> vcard:FN ?name } WHERE { ?x foaf:name ?name }

#extracting a whole graph from the target RDF dataset

CONSTRUCT { ?s ?p ?o }

WHERE { GRAPH <http://example.org/myGraph> { ?s ?p ?o } . }

Page 46: SPARQL Query Language for RDF

CONSTRUCT Examples(II)

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

PREFIX app: <http://example.org/ns#>

CONSTRUCT { ?s ?p ?o }

WHERE { GRAPH ?g { ?s ?p ?o } .

{ ?g dc:publisher <http://www.w3.org/> } .

{ ?g dc:date ?date } .

FILTER app:myDate(?date) > "2005-02-8T00:00:00Z"^^xsd:dateTime. }

accesing a graph conditional on other information contained in the metadata about named graphs in the dataset

Page 47: SPARQL Query Language for RDF

DESCRIBE

PREFIX ent: <http://myorg.example/employees#> DESCRIBE ?x

WHERE { ?x ent:employeeId "1234" }

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

@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0> .

@prefix myOrg: <http://myorg.example/employees#> .

_:a myOrg:employeeId "1234" ;

foaf:mbox_sha1sum "ABCD1234" ;

vcard:N [ vcard:Family "Smith" ;

vcard:Given "John" ] .

foaf:mbox_sha1sum rdf:type owl:InverseFunctionalProperty .

Page 48: SPARQL Query Language for RDF

ASK

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

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

_:a foaf:name "Alice" .

_:a foaf:homepage <http://work.example.org/alice/> .

_:b foaf:name "Bob" .

_:b foaf:mbox <mailto:[email protected]> .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

ASK { ?x foaf:name "Alice" } .

Page 49: SPARQL Query Language for RDF

Testing Values

• Named functions and syntactically constructed operations: – operands: subset of XML Schema DataTypes {xsd:string,

xsd:decimal, xsd:double, xsd:dateTime} and types derived from xsd:decimal.

• Subset of XPath functions and operators– Operands: xs:string, xs:double, xs:float, xs:decimal, xs:integer, xs:dateTime – additional operators: sop:RDFterm-equal, sop:bound , sop:isURI,

sop:isBlank, sop:isLiteral, sop:str , sop:lang, sop:datatype, sop:logical-or, sop:logical-and

• Type Promotion : xs:double, xs:float, xs:decimal– each of the numeric types is promoted to any type higher in the

above list when used as an argument to function expecting that higher type.

Page 50: SPARQL Query Language for RDF

Support for SPARQL

• SPARQL and Jena– module called ARQ that implements SPARQL; also parses

queries expressed in RDQL or its own internal language.– not yet part of the standard Jena distribution; availbale from

either Jena‘s CVS repository or as a self-contained download

• Twinkle – simple Java interface that wraps the ARQ SPARQL Processor

library (the add-on to Jena).

• Redland– set of free software packages that provide support for RDF,

including querying with RDQL and SPARQL using the Rasqal RDF Query Library. .