sparql for the pragmatic ontologist

41
SPARQL for the Pragmatic Ontologist Daniel Yacob Mekonnen Semantic Solutions Architect, TopQuadrant The Washington Semantic Web Meetup 2009-02-12

Upload: phyllis-martinez

Post on 30-Dec-2015

38 views

Category:

Documents


3 download

DESCRIPTION

SPARQL for the Pragmatic Ontologist. The Washington Semantic Web Meetup 2009-02-12. Daniel Yacob Mekonnen Semantic Solutions Architect, TopQuadrant. SPARQL by Application. Q: What is the SQL of RDF?. SPARQL!. Q: What is the XSLT of RDF?. SPARQL!. Q: What is the JavaDoc of RDF?. SPARQL!. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SPARQL for the Pragmatic Ontologist

SPARQL for thePragmatic Ontologist

Daniel Yacob MekonnenSemantic Solutions Architect, TopQuadrant

The Washington Semantic Web Meetup2009-02-12

Page 2: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 2

SPARQL by Application

Q: What is the SQL of RDF?

Q: What is the XSLT of RDF?

Q: What is the Lint of RDF?

Q: What is the JavaDoc of RDF?

Q: What is the Unit Test of RDF?

“Its in the way that you use it”- Eric Clapton

“The query was just the beginning”- Hey, I said that!

Page 3: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 3

Thinking SPARQL

To think in SPARQL, think in patterns– graph patterns or shapes of data– think in graphs– a similar mindset to regular expressions thinking

The pattern language is N3– “Notation 3”– A language for expressing RDF– Learn N3 as you learn SPARQL

Page 4: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 4

SPARQL Vocabulary

ASK BASE BOUND CONSTRUCT DATATYPE DESCRIBE DISTINCT false FILTER FROM FROM NAMED GRAPH isIRI isLITERAL isURI

LANG LANGMATCHES LIMIT OFFSET OPTIONAL ORDER BY PREFIX REDUCED REGEX sameTerm SELECT STR true UNION WHERE

Is SPARQL

READ-ONLY?

Yes. SPARQL 1.0

is READ-ONLY.

Terms I’ve used at least once in 2 years.Terms I use regularly.

Page 5: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 5

SPARQL Vocabulary

Jena ARQ Extensions

CLEAR COUNT CREATE DELETE DELETE DATA DROP GROUP BY HAVING

INSERT INSERT DATA INTO MODIFY SERVICE SILENT

These are read-write extension to SPARQL. Supported by TBC .

Gruff is read-only

(SPARQL 2 Candidates)

Terms I’ve used at least once in the last year.Terms I use regularly.

Page 6: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 6

Exercise Dataset

7 ontologies altogether:– 1 ontology of Airport codes: IANA Code, City,

State, Country.– 1 ontology of National Capitals: Nation, Capital

City, with Latitude and Longitude.– 5 ontologies work as a group providing Country,

City and US States relationships.

These 3 groupings are disjoint –but we can link the data with SPARQL!

Query are executed over ALL ontologies.

Page 7: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 7

Simple Select

“Find the names of all countries”“Find all country instances and their name properties”

Subject

Predicates

Objects

rdf:type

country:name

country:Country

?country ?name“Italy”

I’m a Class!

I’m an Instance!I’m an unknown

instance.

“?” indicates a variable, something unknown.

Triple

Tri

ple

I’m a Property!

I’m an unknown

property value.

I’m a Property value!

Page 8: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 8

‘$’ is also legal(sigil to taste).‘?’ is the sigil in SPARQL.

country:Countryrdf:type

cou

ntr

y:n

ame

?name

?country

SELECT ?country ?nameWHERE { ?country rdf:type country:Country . ?country country:name ?name}

SELECT $country $nameWHERE { $country rdf:type country:Country . $country country:name $name}

SELECT ?country ?nameWHERE { ?country rdf:type country:Country . ?country country:name ?name}

Simple Select

“Find the names of all countries”

Page 9: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 9

SELECT ?country ?nameWHERE { ?country a country:Country . ?country country:name ?name}

SELECT ?country ?nameWHERE { ?country rdf:type country:Country . ?country country:name ?name}

SELECT ?country ?nameWHERE { ?country a country:Country ; country:name ?name}

SELECT ?country ?name { ?country country:name ?name}

SELECT ?country ?name { ?country a country:Country ; country:name ?name}

Simple Select

“Find the names of all countries”

Page 10: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 10

Ordered Select

SELECT ?state ?city

WHERE {

?cityR city:name ?city ;

uscity:state ?stateR .

?stateR state:name ?state

}

“Find the states and their cities”

Page 11: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 11

Ordered Select

SELECT ?state ?city

WHERE {

?cityR city:name ?city ;

uscity:state ?stateR .

?stateR state:name ?state

} ORDER BY ?state ?city

“Find the states and their cities-sorted by state, then city name”

Page 12: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 12

Ordered Select

SELECT ?state ?city

WHERE {

?cityR city:name ?city ;

uscity:state ?stateR .

?stateR state:name ?state

} ORDER BY ASC(?state) DESC(?city)

“Find the states and their cities-sorted by ascending state name, then by descending city name”

Page 13: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 13

Ordered Select

SELECT ?state ?city

WHERE {

?cityR city:name ?city ;

uscity:state [ state:name ?state ]

} ORDER BY ASC(?state) DESC(?city)

Compactified Expression

Found in the wild, but best to avoid –more cryptic and error prone.

Page 14: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 14

bridge across ontologies

Select with OPTIONAL

SELECT ?state ?capital ?airport

WHERE {

?stateR state:name ?state ;

state:capital ?capitalR .

?capitalR city:name ?capital

OPTIONAL {

?airportR airports:city ?capital ;

airports:airport ?airport

}

}

Page 15: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 15

Select with UNION

SELECT ?state

WHERE {

{

?stateR state:borderstate usstate:AL, usstate:TN .

?stateR state:name ?state .

}

UNION

{

?stateR state:borderstate usstate:ID .

?stateR state:name ?state .

}

}

Comma between objects means the subject and predicate are the same.

Page 16: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 16

Select with FILTER

“Find all states that do NOT border another state”

SELECT ?state

WHERE {

?stateR state:name ?state .

OPTIONAL {

?stateR state:borderstate ?borderState .

}

FILTER ( !bound(?borderState) )

}

FILTER means “keep”

With !bound() we effectively look for the absence of a resource

Page 17: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 17

Cast untyped literals into datatype needed by function.

Select with FILTER

“Find all states, and cities, where the city name begins with the letter ‘Y’ ”

SELECT ?state ?city

WHERE {

?cityR uscity:state ?stateR .

?cityR city:name ?city .

?stateR state:name ?state

FILTER( regex( xsd:string(?city), "^Y") )

}

Page 18: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 18

Performance Notes

Filter early, near the top of an expression –if a condition is not met the pattern match aborts

immediately.

Use OPTIONAL sparingly, they will slow down a query, sometimes drastically.

Use ASK when you do not need the results of the match

–ASK will terminate when the first match if found, returns a boolean

DISTINCT and ORDERED BY may also be slow over large result sets

USE LIMIT and OFFSET with large result sets.

Page 19: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 19

Select with DISTINCT

DISTINCT avoids duplicate result sets

SELECT DISTINCT ?state ?city

WHERE {

?person uscity:address ?address .

?address uscity:city ?city .

?city uscity:state ?state

}

Duplicates Explained: ?person is not in the SELECT list, but many, many people will live in a ?city. Without DISTINCT one result set of ?state ?city would appear per ?person!

Without DISTINCT, one SELECT result is returned per graph pattern match.

Page 20: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 20

CONSTRUCT Query

Has anyone thought it strange that we query a graph, with a graph (WHERE clause), but then process a tabular result set?

CONSTRUCT allows us to specify a graph to return.

CONSTRUCTed graphs are not inserted into the queried graph –but some tools allow the constructed graph to be asserted.

CONSTRUCT is extremely useful for transforming graphs.

Page 21: SPARQL for the Pragmatic Ontologist

Now the bad news…

SPARQL Gruff TBC

CONSTRUCT ✖ ✔

INSERT ✖ ✔

DELETE ✖ ✔

LET ✖ ✔

COUNT ✖ ✔

MODIFY ✖ ✔

DESCRIBE ✔ ✖

Not all SPARQL implementations are created equal

Page 22: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 22

Linkage, equating, of resources.

CONSTRUCT Query

CONSTRUCT {

?countryR owl:sameAs ?nationR

}

WHERE {

?countryR a country:Country;

country:name ?country .

?nationR a capitals:Nation;

rdfs:label ?country;

}?country bridges two ontologies in the same graph.

Page 23: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 23

CONSTRUCT Query

CONSTRUCT {

?airportR wswm:country ?countryR ;

wswm:city ?cityR ;

wswm:state ?stateR

}

WHERE {

?airportR a airports:AirportCode ;

airports:city ?city ;

airports:country ?country .

{

# Link to International City Instances

?cityR a capitals:Capital;

rdfs:label ?city .

?countryR a capitals:Nation ;

rdfs:label ?country ;

capitals:capital ?cityR .

}

UNION

{

# Link to US City Instances

LET ( ?countryR := country:UNITED-STATES )

?airportR airports:state ?state .

?stateR state:code ?state .

?cityR city:name ?city ;

uscity:state ?stateR

}

}

Use LET() to assign variables.

Start comments anywhere with “#”

Construct resource linkages across the 3ontology groups. The data is now linked and we can find geo coordinates for airports(in capital cities only).

Page 24: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 24

SPARQL 2

Page 25: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 25

SPARQL 2

DELETE and INSERT –use together in sequence to move content:

INSERT {

?state rdfs:label ?name

}

WHERE {

?state state:name ?name

}

DELETE {

?state state:name ?name

}

WHERE {

?state state:name ?name

}

Page 26: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 26

Advanced Topics

Named Graphs – useful when the same namespaces are used in multiple graphs:

SELECT ?state ?births

FROM NAMED <http://some.org/births.owl>

WHERE {

?stateR a state:State ;

state:name ?state ;

stats:birthRate ?births

}

Graphs names are automatic with TBC. With Gruff the names must be specified at import time into the Allegograph triplestore.

Page 27: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 27

Advanced Topics

SELECT ?state ?births ?deaths

WHERE {

?stateR a state:State ;

state:name ?state ;

GRAPH <http://some.org/births.owl> {

stats:birthRate ?births

}

GRAPH <http://other.org/deaths.owl> {

stats:deathRate ?deaths

}

}

Page 28: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 28

Advanced Topics

The graph can also be a variable

SELECT ?state ?deaths ?graph

WHERE {

?stateR a state:State ;

state:name ?state ;

GRAPH ?graph {

stats:deathRate ?deaths

}

}

Page 29: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 29

A user defined function to shorted a 9 digit zip code to a 5 digit zip code.

User function to get current temperature at zip code.

Advanced Topics

User function to convert Celsius to

Fahrenheit.

Custom FILTER, LET and Property functions bound to Java code.

SELECT ?town ?temperature

WHERE {

?town a geo:Town;

geo:zipcode ?zipcodePlus4 .

?zipcode my:shortenZip( ?zipcodePlus4 ).

LET ( ?temperatureC := my:weather( ?zipcode ) )

FILTER( my:toFahrenheit(?temperatureC) > 100 )

}

Page 30: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 30

TopBraid Suite Advanced Product Training

TopBraid Suite offers extensive SPARQL support

In-depth SPARQL– full Jena ARQ and TBC extensions– using SPARQL in applications

Next-generation SPARQL– SPARQLMotion Scripts– SPIN (SPARQL Inference Notation)

March 9-12, 2009, Washington, March 9-12, 2009, Washington, DC areaDC area

http://www.topquadrant.com/topbraidtraining.html(first day is optional Semantic Web overview)

Page 31: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 31

SPARQLMotion

Import(Spreadsheets, DBs, XML)

Processing(Editing, querying, transforming)

Export(Converting, browsing, visualizing)

SPARQLMotion is designed to tie the data lifecycle into an automated process

SP

AR

QL

Mo

tio

n

En

gin

e

Page 32: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 32

SPARQLMotion Language

Scripts consist of modules Modules have a type

(e.g. LoadXML) The output of one module is

the input to its successors Branching and merging

supported Drag & drop modules to create

a script step, fill out required attributes, connect

Page 33: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 33

SPARQLMotion Script

Script define data processing steps– properties define relationships between

modules– ‘next’ means result triples from on

module sent to next Graph view

– shows processing pipeline Form view

– modules are instances of (SPARQLMotion) classes

Page 34: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 34

SPARQLMotion Example

Page 35: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 35

SPIN

SPIN - SPARQL Inferencing Notation– define constraints and inference rules on Semantic Web models– http://spinrdf.org

Specification for representing SPARQL with RDF– RDF syntax for SPARQL queries

Modeling vocabulary– constraints, constructors, rules– templates, functions

Standard Modules Library– small set of frequently needed SPARQL queries

Page 36: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 36

SPIN Syntax

Problem: SPARQL is represented as a string– can add as a property value to an ontology– …ala that query library– but what if you wanted to associate a query with a specific

resource? # must be at least 18 years old ASK WHERE { ?this my:age ?age . FILTER (?age >= 18) . }

[ a sp:Ask ; rdfs:comment "must be at least 18 years old"^^xsd:string ; sp:where ([ sp:object sp:_age ; sp:predicate my:age ; sp:subject spin:_this ] [ a sp:Filter ; sp:expression [ sp:arg1 sp:_age ; sp:arg2 18 ; a sp:ge ] ])]

Query represented as RDF nodes (not for human consumption)

Page 37: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 37

Using SPIN and Composer-ME

Incremental inferencing (Composer)– compute values when editing ontology– can turn incremental inferencing on and off, run on command, etc.

Calculate the value of a property based on other properties– age of a person as a difference between today's date and person's

birthday– computation is declared in SPIN property

Perform constraint checking with closed world semantics – e.g. raise inconsistency flags when currently available information

does not fit specified integrity constraints– constraints specified in SPIN

Isolate a set of rules to be executed under certain conditions– e.g. class constructors– initialize certain values when a resource is first created, or to drive

interactive applications

Page 38: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 38

SPIN Modeling Vocabulary

Constraints– link classes with SPARQL ASK or CONSTRUCT queries– e.g. link Parents to query

• when true, constraint is violated• (note ‘<‘ not ‘>=’)

– CONSTRUCT will generate triples when constraint is violated Rules

– link SPARQL CONSTRUCT queries to instances of classes• …and all subclasses

– apply the rule to all instances Constructors

– same as Rules– but applied only when instance is created

ASK WHERE { ?this my:age ?age . FILTER (?age < 18) . }

Page 39: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 39

SPIN Meta-Modeling Vocabulary

Reusable SPARQL queries– can use in other contexts

• particularly Rules and Constraints

– encapsulate SPARQL query templates Templates

– create a template class with SPARQL query– create instance of template

Functions– define new functions to be used in FILTER or LET clauses

Page 40: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 40

SPIN Standard Modules Library

Set of frequently used SPARQL queries Functions

– spl:hasValue– spl:hasValueOfType– spl:instanceOf– spl:objectCount

Templates– spl:Argument– spl:Attribute– spl:ConstructDefaultValues

spl:hasValueOfType(rdfs:Class, rdfs:label, xsd:string) # truespl:hasValueOfType(rdf:Class, rdfs:label, xsd:int) # false

Page 41: SPARQL for the Pragmatic Ontologist

© Copyright 2009- TopQuadrant Inc. Slide 41

Additional Information SPARQL Guide

http://www.dajobe.org/2005/04-sparql/

SPARQL FAQhttp://thefigtrees.net/lee/sw/sparql-faq

Jena (ARQ) Property & Filter Functionshttp://jena.sourceforge.net/ARQ/library-propfunc.html

http://jena.sourceforge.net/ARQ/library-function.html

My Delicious Bookmarks – sparql and semantic tagshttp://del.icio.us/yacob

SPARQLPedia – a query repositoryhttp://sparqlpedia.org/

SPARQLMotion – scripting of SPARQL http://sparqlmotion.org/

SPIN – stored procedures for SPARQL http://spinrdf.org/