102
Toronto Semantic Web Meetup Group June 12, 2013 Dr. Scott Henninger VP, Technology Adoption, EVN Product Manager Technical Support Manager TopQuadrant, Inc. What Can SPARQL Do For You?

Toronto semanticmeetup 2013-05-12

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Toronto semanticmeetup 2013-05-12

Toronto Semantic Web Meetup Group

June 12, 2013

Dr. Scott HenningerVP, Technology Adoption, EVN Product Manager Technical Support Manager

TopQuadrant, Inc.

What Can SPARQL Do For You?

Page 2: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 2

Query languages

SQL (databases) views data as tablesIncludes table operations – e.g., “JOIN”

XQuery (XML) views data as treesIncludes tree operations – e.g., “path expressions”

SPARQL (RDF) views data as graphsIncludes graph operations – e.g., “Graph Pattern”

Page 3: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 3

SPARQL

Query language for RDF…and more

Common misperception: inference is what you do with RDF datacan process data through queries

SPARQL Protocol and RDF Query Languagequery graph patterns

• triple patterns• variable bindings

protocol for creating WSDL services• SPARQL endpoints

results in XML form• …with Composer you will never have to worry about this

Page 4: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 4

Query Forms (SPARQL 1)

SELECT returns matches for specified variables

CONSTRUCT returns a graph that includes triples constructed from specified

variables ASK

true if a match to the graph pattern is found DESCRIBE

returns a graph (instead of selecting “columns”) for a resource• format determined by service, usually ?rsc ?p ?o + bnode trees

Page 5: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 5

SPARQL and SPARQL 1.1

SPARQL W3C recommendation – 2008-01-15specifies a query language for RDFhttp://www.w3.org/TR/rdf-sparql-query/

SPARQL 1.1 recommendation 2013-03-21adds update, aggregates, subqueries, projected expressions,

and negationmay also include property pathsall already supported in TBC

• a few details remain, such as MINUS (negation form)• may be some syntax changes – INSERT INTO vs INSERT USING, BIND

vs. LET http://www.w3.org/TR/sparql11-query/

Page 6: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 6

SPARQL

SPARQL query languageSPARQL is to RDF graphs what SQL is to relational modelsunderlying model is an RDF graph – i.e. triples

• …not relational tablese.g. no notion of “join” because there are no tables to join

• instead use triple patterns• match patterns to data

…yet object properties are basically joins Not just queries

various query language manipulationsCONSTRUCT, INSERT functions for manipulating data

• string manipulation, data types, logical and math operations, etc.

Page 7: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 7

New Features in SPARQL 1.1 Projected expressions

allow query results to contain function calls for post-processing results Aggregates

aggregates: COUNT, MIN, MAX, AVG, SUM, GROUP_CONCA, SAMPLE Update

language for directly updating RDF data Subqueries

embed a query in another Negation

NOT EXISTS or MINUS instead of OPTIONAL/!bound() idiom Property Paths

specify a path from one node to another with properties Basic Federated Queries

federate queries against data from multiple endpoints Functional variable bindings

BIND statement Uniform HTTP Protocol

defines RDF mapping to HTTP (POST/GET/PUT/…) for RESTful services Service description

discovery mechanism for SPARQL endpoints

Page 8: Toronto semanticmeetup 2013-05-12

Slide 8

SPARQL – a graph pattern matching language

Page 9: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 9

SPARQL in Brief

SPARQL is a query language for graph models…just as SQL is a query language for relational models

subset of graph representing John Kennedy Jr. and Maria Shriver’s uncles

Page 10: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 10

SPARQL Example

SELECT ?xWHERE{ :CarolineKennedy :parent ?x .}

Who was Caroline Kennedy father?

?x

John Kennedy

Jacqueline Bouvier

Page 11: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 11

SPARQL ExampleWho was Caroline Kennedy father?

SELECT ?fatherWHERE{ :CarolineKennedy :parent ?father . ?father :gender :male .}

We have specified a graph pattern to match

:CarolineKennedy ?father :male:parent :gender

?father

John Kennedy

Page 12: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 12

SPARQL Example Example queries

Find grandparents of John Kennedy JrSELECT ?gparentWHERE { :JohnKennedyJr :parent ?parent . ?parent :parent ?gparent .}

Page 13: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 13

SPARQL ExampleWho is a mother of whom?

?mom ?kid

:female

:child :gender

SELECT ?mom ?kidWHERE{ ?mom :child ?kid . ?mom :gender :female} ?mom ?kid

Rose Fitzgerald Rose Kennedy

Rose Fitzgerald Joseph Kennedy Jr

Rose Fitzgerald John Kennedy

Rose Fitzgerald Robert Kennedy

Rose Fitzgerald Edward Kennedy

Rose Fitzgerald Eunice Kennedy

Rose Fitzgerald Jean Kennedy

Rose Fitzgerald KathleenAKennedy

Rose Fitzgerald Patricia Kennedy

Eunice Kennedy Maria Shriver

Eunice Kennedy …

Jean Kennedy Kym Smith

Jean Kennedy …

Patricia Kennedy Victoria Lafword

Patricia Kennedy …

Page 14: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 14

SPARQL ExampleWho are Maria Shriver’s Uncles?

?uncle

John Kennedy

Joseph Kennedy Jr

Robert Kennedy

Edward Kennedy

SELECT DISTINCT ?uncleWHERE{ :MariaShriver :parent ?parent . ?parent :parent ?gparent . ?gparent :child ?uncle . ?uncle :gender :male}

Page 15: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 15

SPARQL ExampleWho are Maria Shriver’s Aunts?

?aunt

Jean Kennedy

KathleenAKennedy

Patricia Kennedy

Rose Kennedy

SELECT DISTINCT ?auntWHERE{ :MariaShriver :parent ?parent . ?parent :parent ?gparent . ?gparent :child ?aunt . ?aunt :gender :female . FILTER (?aunt != ?parent)} ORDER BY ?aunt

Trickier – Your mom is not your Aunt

Page 16: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 16

Finding Properties

Do not need to now the properties to query for dataFind everything we know about John Kennedy

PREFIX k: <http://topbraid.org/examples/kennedys#>SELECT ?p ?o WHERE { k:JohnKennedy ?p ?o .}

?p ?ok:almaMater k:Harvard

k:almaMater k:Princeton

k:birthYear 1917

k:child k:JohnKennedyJr

k:child k:PatrickBKennedy

k:deathYear 1963

k:firstName “John”

k:gender k:male

k:lastName “Kennedy”

k:middleName “Fitzgerald”

k:name “John Kennedy”

k:parent k:JosephKennedy

k:parent k:RoseFitzgerald

… …

Same thing, but return triples

PREFIX k: <http://topbraid.org/examples/kennedys#>DESCRIBE k:JohnKennedy

Subject Predicate Objectk:JohnKennedy k:almaMater k:Harvard

k:JohnKennedy k:almaMater k:Princeton

k:JohnKennedy k:birthYear 1917

k:JohnKennedy k:child k:JohnKennedyJr

… … …

Page 17: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 17

Filters

Given matching graphs, can filter resultsfilter criteria for bound variables

SELECT ?name ?bDate WHERE { ?person :birthYear ?bDate . ?person :name ?name . FILTER (?bDate < 1960)} ORDER BY DESC(?bDate)

SELECT ?name ?college WHERE { ?person :almaMater ?college . ?person :name ?name . FILTER (?college = :Harvard)}

All birth dates before 1960 - list in descending order

Find all Harvard Graduates

SPARQL FILTERs restrict solutions to those for which the filter expression evaluates to TRUE.

Better version of this query! – This is a frequent SPARQL anti-patternBetter version of this query! – This is a frequent SPARQL anti-pattern

SELECT ?nameWHERE { ?person :almaMater :Harvard . ?person :name ?name .}

Page 18: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 18

Optional used when a pattern may fail to match i.e. if some results do not have the value sought

Optional

SELECT ?childName ?childPhotoWHERE { ?person :almaMater :Harvard . ?person :child ?child . ?child rdfs:label ?childName . OPTIONAL {?child :photo ?childPhoto}} ORDER BY ASC (?childName)

Name all of the Kennedys whose parents graduated from Harvard and show their photos, if they exist.

Basically a left-join

Page 19: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 19

Negation in SPARQL 1.x

Negation as failureSELECT ?personWHERE { ?person :gender :female . OPTIONAL {?person :child ?child} . FILTER (!bound(?child))} ORDER BY ASC(?person)

Name all Kennedy women that do not have children(order by name)

Just using optional – find all females

Just using optional – find all females

The ?child variable is unbound for these matches

The key pattern - to test that a graph pattern does not exist:

- specify an optional graph (not all person instances will define :child) that introduces a variable

- test whether the variable is bound

Page 20: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 20

Negation in SPARQL – NOT EXISTS

NOT EXISTSfilter results for whether a

pattern exists, given current bindings

SELECT ?personWHERE { ?person :gender :female . FILTER NOT EXISTS {?person :child ?child} .} ORDER BY ASC(?person)

Page 21: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 21

Negation in SPARQL

Some difference in corner casesbasically, NOT EXISTS depends on bindings

Which to use?corner cases are marginal, but sometimes ariseuse NOT EXISTS by convention

SELECT * { ?s ?p ?o NOT EXISTS { :a :b :c } }

SELECT * { ?s ?p ?o MINUS { :a :b :c } }

No bindings, no results no solutions to eliminate,result is ?s = :a, ?p = :b, ?o = :c

@prefix : <http://example/> . :a :b :c .

Data

Page 22: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 22

Negation in SPARQL – Example

Common example: find roots of a tree

SELECT ?clsWHERE { ?cls a owl:Class . { NOT EXISTS {?cls rdfs:subClassOf ?parent .} } UNION { ?cls rdfs:subClassOf owl:Thing .} FILTER (!isBlank(?cls))}

rdfs:subClassOf tree: find all roots of a tree, noting that roots may or may not assert being subclasses of owl:Thing

SELECT DISTINCT ?btermWHERE { ?nterm skos:broader ?bterm . NOT EXISTS {?bterm skos:broader ?x}} broader terms that do not have

broader terms are toots

Page 23: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 23

Summary: Combining Triple Patterns

WHERE{ ?person a k:Person . ?person k:almaMater ?college . }

WHERE{ { ?person a k:Person . } UNION { ?person k:almaMater ?college . }}

WHERE{ ?person a k:Person . OPTIONAL {?person k:almaMater ?college .} . }

?person

?collegek:almaMater

k:Personrdf:type

?person

?collegek:almaMater

k:Personrdf:type

?person

?collegek:almaMater

k:Personrdf:type

?person

Triple pattern is a SPO triple: “?s ?p ?o”, “?daughter a :Person”…

“.” (conjunction)

UNION (disjunction)

OPTIONAL similar to a relational left outer join

Page 24: Toronto semanticmeetup 2013-05-12

Slide 24

SPARQL CONSTRUCT

Page 25: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 25

CONSTRUCT

Building new sets of triples based on patterns…and asserting those triples

CONSTRUCT {?person a :Mother}WHERE { ?person :gender :female . ?person :child ?child .}

Any woman with children is a Mother

Returns an RDF graph…but ontology (file) is not changed

What about the globe icon for Mother?

Page 26: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 26

CONSTRUCT

Building new sets of triples based on patterns…and asserting those triples

CONSTRUCT {?person rdf:type :Mother}WHERE { ?person :gender :female . ?person :child ?child .}

Any woman with children is a Mother

Choose desired triplesAssert into ontology

Page 27: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 27

CONSTRUCT

Building new sets of triples based on patterns…and asserting those triples

Any woman with children is a Matriarch

CONSTRUCT {?person rdf:type :Mother}WHERE { ?person :gender :female . ?person :child ?child .}

Page 28: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 28

Another Inference with SPARQL

If someone went to a college in a state, then they lived in that state

CONSTRUCT { ?person kennedys:livedIn ?state .}WHERE{ ?person kennedys:almaMater ?college . ?college rdfs:label ?cl . ?dbuniv world:state ?state . ?dbuniv rdfs:label ?dbul . FILTER (regex (?dbul, ?cl)) .}

Page 29: Toronto semanticmeetup 2013-05-12

Slide 29

SPARQL INSERT/DELETE

Page 30: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 30

INSERT/DELETE DATA

Insert triples this syntax only allows triples with named URIs

• no WHERE clause• no variables allowed

naming the graph is not required (can apply to default graph)INSERT DATA{ GRAPH <http://topbraid.org/examples/kennedys> { kennedys:JohnKennedy kennedys:parent kennedys:JosephKennedy . kennedys:CarolineKennedy kennedys:parent kennedys:JohnKennedy }}

DELETE DATA{ GRAPH <http://topbraid.org/examples/kennedys> { kennedys:JohnKennedy kennedys:name "John Kennedy"^^xsd:string . }}

Page 31: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 31

INSERT/DELETE Insert a triple

same syntax as CONSTRUCT triple is directly applied to default graph

Let’s move the label from :name to rdfs:label for each member of :Person

Change History provides details of changes…

INSERT {?person rdfs:label ?name .}WHERE { ?person a kennedys:Person . ?person kennedys:name ?name .}

DELETE {?person kennedys:name ?name .}WHERE { ?person a kennedys:Person . ?person kennedys:name ?name .}

Page 32: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 32

MODIFY

Note that INSERT will only add triples to modify something, need to couple a delete/insert

DELETE {?person kennedys:name ?name .}INSERT {?person rdfs:label ?name .}WHERE { ?person a kennedys:Person . ?person kennedys:name ?name .}

previous queries can be done in one modify the label to different name format

DELETE {?person rdfs:label ?label .}INSERT {?person rdfs:label ?name .}WHERE { ?person a kennedys:Person . ?person rdfs:label ?label . ?person kennedys:firstName ?fname . ?person kennedys:lastName ?lname . BIND (fn:concat(?lname, ?fname) AS ?name) .}

What’s does “BIND” do? (more in a few slides)

Page 33: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 33

SPARQL Update

More commonly seen with GRAPH keywordprevious slides assumed a graph context

• …such as in TBC’s SPARQL viewDELETE { GRAPH <http://topbraid.org/examples/kennedys> { ?person rdfs:label ?label . }}INSERT{ GRAPH <http://topbraid.org/examples/kennedys> { ?person rdfs:label ?name . }}WHERE {GRAPH <http://topbraid.org/examples/kennedys> { ?person a kennedys:Person . ?person rdfs:label ?label . ?person kennedys:firstName ?fname . ?person kennedys:lastName ?lname . BIND (fn:concat(?lname, ?fname) AS ?name) .} }

Page 34: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 34

WITH and Graphs

Can specify what graph modification applies toWITH defines the context for WHERE, DELETE and INSERT

clausesPREFIX k: <http://topbraid.org/examples/kennedys#>WITH <http://topbraid.org/examples/kennedys>DELETE { ?person rdfs:label ?label .}INSERT{ ?person rdfs:label ?name .}WHERE { ?person a k:Person . ?person rdfs:label ?label . ?person k:firstName ?fname . ?person k:lastName ?lname . BIND (fn:concat(?lname, ?fname) AS ?name) .}

Get data and apply changes to

kennedys model

Get data and apply changes to

kennedys model

Page 35: Toronto semanticmeetup 2013-05-12

Slide 35

Projected Expressions and BIND

Page 36: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 36

BIND Expressions

Can perform general expressions

SELECT ?age ?personWHERE{ ?person a kennedys:Person . ?person kennedys:birthYear ?byear . ?person kennedys:deathYear ?dyear . BIND (?dyear - ?byear AS ?age)}

Age of all Kennedys at time of death

Ages of living Kennedys

SELECT ?age ?personWHERE{ ?person a kennedys:Person . ?person kennedys:birthYear ?byear . OPTIONAL {?person kennedys:deathYear ?dyear .} . FILTER (!bound(?dyear)) . BIND (2008 - ?byear AS ?age)}

Page 37: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 37

Creating a URI

IRI creates a URI, CONSTRUCT { kennedys:NewPerson a owl:Class . ?newrsc a kennedys:NewPerson . ?newrsc rdfs:label ?name .}WHERE { ?person a kennedys:Person . ?person kennedys:firstName ?fname . ?person kennedys:lastName ?lname . OPTIONAL{?person kennedys:middleInitial ?mi .} . BIND (fn:concat(?lname, ?fname) AS ?name) . BIND (IRI(fn:concat("http://topbraid.org/examples/kennedys#", ?lname, "_", ?mi, "_", ?fname)) AS ?newrsc) .}

IRI will create a resource• for example, given ?lname = “John” ?mi=“Fitzgerald” and ?lname=“Kenendy”• will create kennedys:Kennedy_Fitzgerald_John• full URI: <http://topbraid.org/examples/kennedys#Kennedy_Fitzgerald_John>

Page 38: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 38

Creating URIs with qnames

spif:buildURI converts qnames to URIssame as IRI, but can specify the prefix in the string

CONSTRUCT { :NewPerson a owl:Class . ?newrsc a kennedys:NewPerson . ?newrsc rdfs:label ?name .}WHERE { ?person a kennedys:Person . ?person kennedys:firstName ?fname . ?person kennedys:lastName ?lname . OPTIONAL{?person kennedys:middleInitial ?mi .} . BIND (fn:concat(?lname, ?fname) AS ?name) . BIND (spif:buildURI(fn:concat("kennedys:", ?lname, "_", ?mi, "_", ?fname)) AS ?newrsc) .}

Same result:• kennedys:Kennedy_Fitzgerald_John• full URI: <http://topbraid.org/examples/kennedys#Kennedy_Fitzgerald_John>

From TopBraid libraryFrom TopBraid library

Page 39: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 39

SPARQL Regular expressions

regex() follows XQuery 1.0/XPath 2.0 standardhttp://www.w3.org/TR/xpath-functions/#regex-syntaxsimilar to that used in Perl, JavaScript, PHP, etc.

also available as a SPARQLMotion functionconvenient for string manipulation

BIND ("avg.FlowingTemp(F)” AS ?name)BIND (smf:regex(?colName, "([A-z]+)\\.([A-z]+)\\(([A-z])\\)", "$1$2$3") AS ?colName)

SELECT *WHERE { ?person kennedys:name ?name . FILTER regex(?name, "^ma", "i")}

find all names starting with “ma”, ignore case

string pattern matchreplace string

Same regular expression language defined in XQuery 1.0 and XPath 2.0 Functions• http://www.w3.org/TR/xpath-functions/#regex-syntax

Page 40: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 40

Projected Expressions

Expressions can be placed in SELECT statementbind to variable with AS

SELECT ?p (fn:concat(?lname, ", ", ?fname) AS ?name)WHERE { ?p kennedys:firstName ?fname . ?p kennedys:lastName ?lname .} SELECT ?p ?name

WHERE { ?p kennedys:firstName ?fname . ?p kennedys:lastName ?lname . BIND (fn:concat(?lname, ", ", ?fname) AS ?name)}

This is an equivalent query using BIND

In TBC these resolve to the same query:(project (?p ?name) (assign ((?name (<http://www.w3.org/2005/xpath-functions#concat> ?lname ", " ?fname))) (bgp (triple ?p kennedys:firstName ?fname) (triple ?p kennedys:lastName ?lname) )))

Page 41: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 41

Projected Expressions

Various expressions supported

SELECT (?dyear-?byear as ?age) ?personWHERE { ?person a kennedys:Person . ?person kennedys:deathYear ?dyear . ?person kennedys:birthYear ?byear .}

age at death

alternative (more explicit expression)

SELECT ?age ?personWHERE { ?person a kennedys:Person . ?person kennedys:deathYear ?dyear . ?person kennedys:birthYear ?byear . BIND (?dyear - ?byear AS ?age)}

Page 42: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 42

Projected Expressions

Any SPARQL expression can be used including library functions, SPIN functions, etc.

SELECT (IF(?isIvy, "Ivy League", ”Non-Ivy") as ?ivy) ?personWHERE { ?college a kennedys:College . ?college kennedys:ivyLeague ?isIvy}

return “Ivy League” if value is true

Page 43: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 43

SPARQL Aggregates

Expressions applied over groups of solutionsSPARQL 1.1 defines:

• COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT, and SAMPLE

SELECT (count(?x) as ?count){ ?x a owl:Class}

SELECT (count(?photo) as ?photos) WHERE { ?person a kennedys:Person . OPTIONAL {?person kennedys:photo ?photo}}

How many photos of Persons do we have?

Page 44: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 44

SELECT Expressions

Aggregates

SELECT (sum(?age)/count(*) as ?x)WHERE { ?person kennedys:deathYear ?dyear . ?person kennedys:birthYear ?byear . BIND (?dyear - ?byear AS ?age)}

average age of death

Same as (avg(?age) as ?x)

SELECT min(?byear)WHERE { ?person kennedys:birthYear ?byear .}

Earliest birth year

Page 45: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 45

Result Set Processing Manipulating how result sets are returned

“Order By” already seenGROUP BY changes result set so that a row for each unique set

of variables is returned

SELECT (count (?c) as ?n)WHERE { ?p a kennedys:Person . ?p kennedys:hasChild ?c .}

An example: find out how many children each person has.

Now group by ?p to get a unique row for each match

SELECT ?p (count (?c) as ?n)WHERE { ?p a kennedys:Person . ?p kennedys:hasChild ?c .} GROUP BY ?p

This only finds the full count…

Page 46: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 46

GROUP BY

Groups a variable for a match value

SELECT ?person (count (?spouse) as ?n)WHERE { ?person a kennedys:Person . ?person kennedys:spouse ?spouse .} GROUP BY ?person ORDER BY DESC(?n)

number of times married, descending order

SELECT ?name (count (?uncle) as ?n)WHERE { ?nephewNiece kennedys:parent ?parent . ?parent kennedys:parent ?grandparent . ?grandparent kennedys:child ?uncle . ?uncle kennedys:gender kennedys:male .OPTIONAL {?nephewNiece kennedys:name ?name}.OPTIONAL {?uncle kennedys:name ?uncleName}.FILTER (?parent != ?uncle)} GROUP BY ?name ORDER BY DESC(?n)

Who has the most uncles?

Page 47: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 47

CONSTRUCT and Sub-Selectcreate a “number of children” property

CONSTRUCT {?person kennedys:numberOfChildren ?n}WHERE{ { SELECT ?person (count (?child) as ?n) WHERE { ?person kennedys:child ?child . } GROUP BY ?person }}

Page 48: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 48

Having

Apply expressions to aggregatessimilar to FILTER, but applied to aggregated variables

SELECT ?person (count (?child) as ?n)WHERE { ?person kennedys:child ?child .} GROUP BY ?person HAVING (?n > 4)

CONSTRUCT {?person a kennedys:LargeFamily}WHERE{ { SELECT ?person (count (?child) as ?n) WHERE { ?person kennedys:child ?child . } GROUP BY ?person HAVING (?n > 4) }}

Returns only those whose number of

children is > 4

Returns only those whose number of

children is > 4

…again, can use the aggregate as an inference for new data

Page 49: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 49

GroupConcat

Define a string that concatenates bindings from all matches

SELECT ?parent (GROUP_CONCAT (?fname ; separator = ", ") AS ?children)WHERE { ?x kennedys:parent ?parent . ?x kennedys:firstName ?fname .} GROUP BY ?parent ORDER BY ?fname

For each parent, get a string of all

first names

For each parent, get a string of all

first names

Page 50: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 50

Using bnodes to Traverse Graphs

It is common to need to traverse a graph path

If you don’t want to bother to create a name for the variable, can use a bnodesometimes seen in the wild, but syntax not so desirable

SELECT ?person ?gparentWHERE { ?person :parent ?parent . ?parent :parent ?gparent .}

SELECT ?person ?gparentWHERE { ?person :parent [:parent ?gparent] .}

Page 51: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 51

Specifying Graphs in Queries

RDF Dataset – a collection of graphsFROM keyword designates default graph

• Composer applies to graph defined in open file if more than one FROM used, query the union of all graphsFROM NAMED clauses

specify a named graph

GRAPH keyword specifies which graph a pattern is applied toanything outside GRAPH is applied to default graph

see http://www.w3.org/TR/sparql11-query/#rdfDataset

SELECT . . . FROM <contact.ttl> FROM NAMED <aliceFoaf.ttl> FROM NAMED <bobFoaf.ttl> WHERE { . . . }

Page 52: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 52

Specifying Graphs in Queries (2) A SPARQL query can specify

which graph to query in Composer, “base graph” is

the graph opened in tab by default query is executed

across base graph and all sub-graphs (e.g. SPARQL View)

NAMED and FROM NAMED specify specific graphs to query

PREFIX kennedys: <http://topbraid.org/examples/kennedys#> PREFIX region: <http://www.topbraid.org/owl/geo/region#>SELECT *WHERE{ GRAPH <http://topbraid.org/examples/kennedys> { ?college a kennedys:College . ?college rdfs:label ?c_label } GRAPH <http://www.topbraid.org/owl/geo/US-states> { ?state a region:State . ?state rdfs:label ?s_label . } FILTER regex(?c_label, ?s_label, "i")}

Find all states from US-stated that

appear in labels from kennedys

Find all states from US-stated that

appear in labels from kennedys

PREFIX kennedys: <http://topbraid.org/examples/kennedys#> SELECT *FROM NAMED <http://topbraid.org/examples/kennedys> FROM NAMED <http://topbraid.org/examples/kennedysExtendedData>WHERE{ GRAPH ?g { ?child kennedys:parent kennedys:CarolineKennedy . }}

Find all children of Caroline Kennedy, and the graph they are defined in - note ?g is a variable

Find all children of Caroline Kennedy, and the graph they are defined in - note ?g is a variable

Page 53: Toronto semanticmeetup 2013-05-12

Slide 53

SPARQL Property Paths

Page 54: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 54

Property Paths

Property paths define possible routes through a graph between two graph nodes

SELECT ?person ?gparentWHERE { ?person kennedys:parent / kennedys:parent ?gparent .}

The “property” in this case is the path between the subject (?person) and object (?gparent)

SELECT ?person ?gparentWHERE { ?person kennedys:parent ?parent . ?parent kennedys:parent ?gparent .}

Page 55: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 55

Property Paths

Viewing queries from the properties i.e. specify possible pathsgood for data exploration

SELECT *WHERE{ ?person a kennedys:Person . ?person kennedys:name | rdfs:label ?name .}

SELECT *WHERE{ ?person a :Person . ?person kennedys:child / kennedys:child / (kennedys:name | rdfs:label) ?gchild .}

match either :name or rdfs:label

follow child properties to grandchild, then get either the name or label

Page 56: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 56

Property Paths and Transitivity Any path (relationship) can be viewed as transitive

different than inference – the query finds all solutions

# transitive closure of subClassOfSELECT ?clsWHERE { ?cls rdfs:subClassOf* owl:Thing}

# transitive relationships of subClassOfSELECT ?clsWHERE { ?cls rdfs:subClassOf+ owl:Thing}

# all subclasses of owl:ThingSELECT ?clsWHERE { ?cls rdfs:subClassOf owl:Thing}

Page 57: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 57

Property Path Syntax

Full syntax of property paths

http://www.w3.org/TR/sparql11-property-paths/

Syntax Form Matches

elt a path element, which may itself be composed of path syntax constructs.

^elt Inverse path (object to subject).

(elt) A group path elt, brackets control precedence.

elt1 / elt2 A sequence path of elt1, followed by elt2

elt1 ^ elt2 Shorthand for elt1 / ^elt2, that is elt1 followed by the inverse of elt2.

elt1 | elt2 A alternative path of elt1, or elt2 (all possibilities are tried).

elt* A path of zero or more occurrences of elt.

elt+ A path of one or more occurrences of elt.

elt? A path of zero or one elt.

elt{n,m} A path between n and m occurrences of elt.

elt{n} Exactly n occurrences of elt. A fixed length path.

elt{n,} n or more occurrences of elt.

elt{,n} Between 0 and n occurrences of elt.

Precedence: • URI, prefixed names• Groups• Unary operators *, ?, + and {} forms• Unary ^ inverse links• Binary operators / and ^• Binary operator |

Precedence is left-to-right within groups.

Page 58: Toronto semanticmeetup 2013-05-12

Slide 58

SPARQL Endpoints

Page 59: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 59

SPARQL Endpoint

SPARQL Endpoint is part of the SPARQL Protocol or RDF spec http://www.w3.org/TR/rdf-sparql-protocol/ specifies standard interface for

• sending SPARQL queries• representing SPARQL results in an XML

“table” (…and JSON)

Example SPARQL endpoint response …using kennedys model

<?xml version="1.0" encoding="UTF-8"?><sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="college"/> <variable name="name"/> </head> <results> <result> <binding name="college"> <uri>http://topbraid.org/examples/kennedys#Princeton</uri> </binding> <binding name="name"> <literal>Princeton</literal> </binding> </result> <result> <binding name="college"> <uri>http://topbraid.org/examples/kennedys#Yale</uri> </binding> <binding name="name"> <literal>Yale</literal> </binding> </result>…

SELECT *WHERE{ ?college a kennedys:College . ?college rdfs:label ?name .}

Page 60: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 60

SPARQL Endpoint Client in Composer

Use SPARQL SERVICE this can be done in any SPARQL query

one use: ApplyConstruct to materialize a set of triples for further processing

SELECT ?childWHERE{ ?person kennedys:firstName ?fname . ?person kennedys:lastName ?lname . BIND (IRI(fn:concat("http://dbpedia.org/resource/", ?fname, "_", ?lname)) AS ?uri) SERVICE <http://dbpedia.org/sparql> { ?uri <http://dbpedia.org/property/children> ?child . }}

Page 61: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 61

TBS SPARQL Endpoint Server

TBS includes a SPARQL Endpoint servlet ‘sparql’ that take a ‘query’ parametercaveat: it server the workspace

• therefore must specify which graph the query operates on

http://localhost:8083/tbl/sparql?query=SELECT DISTINCT ?p WHERE {GRAPH <http://topbraid.org/countries> {?s ?p ?o} }

Calls sparql endpoint, passes query argument

Graph query is applied to (any base URI in workspace)

POST works as well

Page 62: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 62

Accessing a SPARQL Endpoint

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Call Personal Server SPARQL endpoint</title></head><body><p>Enter a SPARQL Query:</p><form action="http://localhost:8083/tbl/sparql" method=”post"><textarea name="query" rows="18" cols="100"></textarea><br \>Execute against Named Graph: <select name ="default-graph-uri"><option value="http://topbraid.org/examples/kennedys">Kennedys</value><option value="http://www.topbraidcomposer.org/owl/2006/07/geotravel.owl">Geo Travel</value><option value="urn:x-evn-master:geo">Geography</value><option value="urn:x-evn-master:iptc">IPTC</value></select> <br />Return type: <select name="format"> <option value="application/sparql-results+xml">SPARQL XML</option> <option value="application/sparql-results+json">SPARQL JSON</option> <option value="text/csv">Comma separated values</option> <option value="text/tab-separated-values">Tab separated values</option> <option value="application/rdf+xml">RDF/XML (CONSTRUCT)</option> <option value="text/turtle">Turtle (CONSTRUCT)</option></select> <br /><p><input type="submit" value="Submit query"/></p></form></body></html>

A web-based SPARQL editor very simple HTML post (or get)

Page 63: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 63

Creating Custom SPARQL Endpoints

Can create custom SPARQL endpoint with SPARQLMotion (gets into some materials we cover later)

Returns in SPARQL endpoint format

Returns in SPARQL endpoint format

Data the endpoint operates on

Data the endpoint operates on

Some data not exposed through

endpoint

Some data not exposed through

endpoint

http://localhost:8083/tbl/sparqlmotion?id=fnt:FindSubTopicsOf&topic=education

Page 64: Toronto semanticmeetup 2013-05-12

Slide 64

SPIN

Page 65: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 65

SPARQL Rules SPARQL Rules (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 Librarysmall set of frequently needed SPARQL queries

Page 66: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 66

SPIN and W3C

SPIN is a W3C Submissionbased on SPARQLuses class/subclass features of RDFSfills the missing rules layer

where RIF cannot

Page 67: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 67

SPARQL Rules Framework

SPARQL RDF syntax RDF syntax for SPARQL queries

SPARQL Functions functions, defined in SPARQL, returning a value

SPARQL Templates parameterized functions

SPARQL Rules SPARQL Inference: SPARQL-based reasoner (includes OWL 2 RL) SPARQL Constraints: constraint violation warnings SPARQL Constructors: execute rule when creating an instance

SPARQL Magic Properties property defined as a SPARQL query, return multiple bindings

SPARQL Web Pages (SWP) formerly UISPIN, text formatting embedding SPARQL queries

SPARQL Result Sets collection of resources for processing result set tables

SPARQL JavaScript Extensions interface to JavaScript for SPARQL Functions

SPARQL Rules is built on SPARQL standardsSPARQL Rules is built on SPARQL standards

Page 68: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 68

SPARQL RDF Syntax

Problem: SPARQL is represented as a stringcan add as a property value to an ontology…ala that query librarybut what if you wanted to associate a query with

a specific resource?

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

kennedys:Person spin:constraint [ a sp:Ask ; rdfs:comment "must be at least 18"^^xsd:string ; sp:where ([ sp:object [ sp:varName "age"^^xsd:string ] ; sp:predicate kspin:age ; sp:subject spin:_this ] [ a sp:Filter ; sp:expression [ a sp:ge ; sp:arg1 [ sp:varName "age"^^xsd:string ] ; sp:arg2 18 ] ]) ] .

Query represented as RDF nodes (not for human consumption)

Page 69: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 69

SPIN RDF Syntax

SPARQL queries represented in RDFspin:query, spin:rule, spin:constraint, etc.SPARQL query resources are URIs

• i.e. represented as a unique resource

Example, change a property name

change ‘birthYear’ to ‘yearOfBirth’• qname in query

automatically changes

Page 70: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 70

SPIN Modeling Vocabulary Constraints

link classes with SPARQL ASK or CONSTRUCT queriese.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 Rulesbut applied only when instance is created

#Must be 18 to be a parentASK WHERE { ?this my:age ?age . FILTER (?age < 18) . }

Page 71: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 71

SPIN Meta-Modeling Vocabulary

Reusable SPARQL queriescan use in other contexts

• particularly Rules and Constraintsencapsulate SPARQL query templates

Templatescreate a template class with SPARQL querycreate instance of template

Functionsdefine new functions to be used in FILTER or LET clauses

Page 72: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 72

Sample SPARQL Query

A running examplequery finds ages of all living Kennedys in our data store run in SPARQL tab to startmany issues with the query that we will use

to demonstrate SPIN

CONSTRUCT { ?person kennedys:age ?age .}WHERE{ ?person kennedys:birthYear ?bYear . NOT EXISTS {?person kennedys:deathYear ?dYear .} . BIND (2010 - ?bYear AS ?age) .}

Page 73: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 73

?this is a reserved variable refers to whatever resource is currently chosen… still in SPARQL tab

Understanding ‘?this’

select to put in Resource form

query applied with ?this bound to chosen resource

Page 74: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 74

SPIN Inferencing

SPIN applies the query directly to the classquery becomes part of the class representation i.e. rule and model integrated

• TopSPIN applies query to each instance

• ?this bound to an instance

definition applies to all instances of Person

Page 75: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 75

SPIN is an Object-Oriented Model

Unlike RDFS, inheritance appliesdeath age of kennedysWhere are Joe and Rose?

• no rdfs inferencing unless inferences turned on

In SPIN, query will apply to “subclasses” i.e. all those with subclassOf

props i.e. SPIN rule applies to

Matriarch and Patriarch

CONSTRUCT { ?this kennedys:deathage ?age .}WHERE { ?this kennedys:birthYear ?bYear . ?this kennedys:deathYear ?dYear . BIND ((?dYear - ?bYear) AS ?age) .}

Page 76: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 76

SPARQL Rules (SPIN) vs. other Rules

SPIN SWRL (etc) JESS, Jena, etc.Standards-based (SPARQL is a W3C Recommendation)

Standard in progress Proprietary

Available Now In progress Available now

Basic engine available in most triples stores

Translate rules into executable form

Engines available in some stores

Extensible with programming

Extensions in standards process

Extensible with programming

Object-oriented semantics

OWL-compliant semantics (OWL-Safe Rules)

Some have object system connections

No optimized chaining N/A (translate to other systems)

Elaborate chaining optimizations

Page 77: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 77

SPIN Constraints

Constraints are used to check data if a constraint is violated, flag a warningas with rules, attach to class and apply to all members

of the classmust turn on warnings

# age must be within a realistic rangeASK WHERE { ?this :age ?age . FILTER ((?age < 0) || (?age > 102)) .}

spin:constraint for :Person•Note this is an ASK query•If it returns true, the constraint has been violated

Page 78: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 78

SPIN Constraint Warnings

Warnings are displayed on applied resources(changed Jean Kennedy’s birth date)

Two warnings

Either kennedys:spouse or kennedys:gender could be the problem

Page 79: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 79

Constraint Violations

Use spin:ConstraintViolation to target where warning is placeduse CONSTRUCT to build constraint warning triples

CONSTRUCT { _:b0 a spin:ConstraintViolation . _:b0 spin:violationRoot ?this . _:b0 spin:violationPath kennedys:spouse . _:b0 rdfs:label "Always blame it on the spouse" .}WHERE { ?this kennedys:spouse ?spouse . ?this kennedys:gender ?gender . ?spouse kennedys:gender ?spouseGender . FILTER (?gender = ?spouseGender) .}

gender no longer displays a warning

Page 80: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 80

Defining SPARQL Functions in SPIN Define functions using SPARQL queries

use in FILTER and LET clause from previous example, create a getCurrentYear function

Body of getCurrentYear

CONSTRUCT { ?this :age ?age . }WHERE { ?this kennedys:birthYear ?birthYear . BIND (:getCurrentYear() AS ?currentYear) . BIND ((?currentYear - ?birthYear) AS ?age) .}

SELECT ?yearWHERE { BIND (xsd:string(afn:now()) AS ?str) . BIND (fn:substring(?str, 0, 4) AS ?sub) . BIND (xsd:integer(?sub) AS ?year) .}

• using afn:now() as before• returns ?year• will return first bound

Page 81: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 81

SPIN Functions

Define the function getCurrentYear each SPIN function is a

subclass of spin:Function

Can now use kspin:getCurrentYear in any SPARQL query

Can now use kspin:getCurrentYear in any SPARQL query

Page 82: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 82

SPIN Functions

…including SPIN rules

• note syntax: getCurrentYear() is defined in default namespace

• i.e. the function is referenced by a URI

CONSTRUCT { ?this kennedys:age ?age .}WHERE { ?this kennedys:birthYear ?bYear . OPTIONAL { ?this kennedys:deathYear ?dYear . } . FILTER (!bound(?dYear)) . BIND (kspin:getCurrentYear() AS ?currentYear) BIND (?currentYear - ?bYear AS ?age) .}

Page 83: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 83

Passing Parameters to SPIN Functions

Define an argument for a functiondrag-drop on spin:constraints to get wizard

Page 84: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 84

Passing Parameters to SPIN Functions

Argument name used in querycan work with multiple queriesargument order determined by local name of property

argument (?arg1) used in query

note that functions can call functions

Example of invoking this function:Example of invoking this function:

SELECT *WHERE { ?this kennedys:birthYear ?byear . BIND (kspin:getAge(?byear) AS ?age}

Page 85: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 85

Defining SPIN Templates

Define a subclass of spin:Templates

Define argumentssame as with functions

arguments

body

label template

Note that local name of arguments are the variable names

Page 86: Toronto semanticmeetup 2013-05-12

Slide 86

SPARQL and Inference

Page 87: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 87

SPARQL Rules and OWL

Inference: inferring factsgiven a matching pattern of facts, define a new facteach fact defined as a triple

Example: infer that anyone that went to a college lived in the state the college is in

CONSTRUCT {?person :livedIn ?state}WHERE{ ?person a :Person . ?person :almaMater ?college . ?college :state ?state .}

create new fact, place in graph of new factscreate new fact, place in graph of new facts

members of class :Person that have

an :almaMater

members of class :Person that have

an :almaMater

colleges have a state propertycolleges have a state property

Page 88: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 88

Inferring Facts

:CarolineKennedy a kennedys:Person ; :almaMater :Harvard , :Columbia ; :birthYear 1957 ; :firstName "Caroline"^^xsd:string ; :gender :female ; :lastName "Kennedy"^^xsd:string ; :middleName "Bouvier"^^xsd:string ; :name "Caroline Kennedy"^^xsd:string ; :parent :JacquelineBouvier , :JohnKennedy ; :profession :non-profit ; :spouse :EdwinSchlossberg .

kennedys:Harvard a kennedys:College ; rdfs:label "Harvard University" ; :state states:MA ; :ivyLeague "true"^^xsd:boolean .

kennedys:Columbia a kennedys:College ; rdfs:label "Columbia University" ; :state states:NY ; kennedys:ivyLeague "true"^^xsd:boolean .

1. graph match binds ?person to :CarolineKennedy

1. graph match binds ?person to :CarolineKennedy

CONSTRUCT {?person :livedIn ?state}WHERE{ ?person a :Person . ?person :almaMater ?college . ?college :state ?state .}

3. match the state property for each, bind to ?state

3. match the state property for each, bind to ?state

2. two matches on ?college2. two matches on ?college

4. Infer two new triples:

:CarolineKennedy :livedIn states:NY :CarolineKennedy :livedIn states:MA

Page 89: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 89

OWL Inferences

OWL supports classification inferencesnotion of consistency within a subset of First-Order Logic“If x lives with y and y lives with z, then we can add a new fact:

x lives with x”“if all members of the CarSedan class have 4 wheels, and

myCar is a member of CarSedan, then we can assume myCar has for wheels”

Classification inferences make up a small part of the solution spacewill always need to augment with rules, code, etc.

Page 90: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 90

OWL 2 RL in SPARQL

OWL is not the only possible rule language…just the one on the current Semantic Web stackother languages are possible

OWA has a huge impact on OWL semantics reasonable inferences cannot be madeeven OWL advocates are beginning to shy away from this

OWL 2: RL profilescalable, but not sacrificing expressive powersubset of OWL 1 with some extensionsW3C recommendation, October 2009

• http://www.w3.org/TR/owl2-profiles/#OWL_2_RL

Page 91: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 91

OWL 2 RL in SPARQL using SPIN

OWL 2 RL designed for rule-based technologies i.e. not full DL (But does that matter?)current approaches to the problem are scattered

• Jena, OWLIM, Oracle, Allegro• all use some subset of OWL

OWL 2 RL in ComposerOWL 2 can be designed in SPIN import http://topbraid.org/spin/owlrl-all

• proof-of-concept based on current working draft

SPARQL & SPIN are rule languagesmore flexible, create-your-own semantics

see http://composing-the-semantic-web.blogspot.com/2009/01/owl-2-rl-in-sparql-using-spin.html

Page 92: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 92

OWL Inferences in SPIN OWL 2 RL profile implemented in full as a set of SPARQL rules

TopBraid/TBC/owlrl.owl (templates for all OWL rules) TopBraid/TBC/owlrl-all.owl (all rules instantiated on owl:Thing) both rules and constraints

Definition found at: http://www.w3.org/TR/owl2-profiles/#Reasoning_in_OWL_2_RL_and_RDF_Graphs_using_Rules

(or Google “OWL 2 profiles”)

Page 93: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 93

Creating Rules from OWL Standard Suppose you need to implement some subset of OWL

not interested in all rules interested in a subset, but want standard rules for all RDFS/OWL reasoners: can’t do that

• no way to pick-and-choose rules In SPIN, import owlrl.owl

create rules from templatesOWL RL templates,

indexed by names in recommendation

OWL RL templates, indexed by names in

recommendation

SPARQL implementation of rule

SPARQL implementation of rule

Page 94: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 94

SPIN vs. OWL

OWL has specific semanticsmotivated by description logic

Note that inference engines add triples i.e. give a pattern/rule, insert some triplesSPARQL CONSTRUCT inserts triples

SPIN builds on SPARQLuses SPARQL + OO principles as the level of abstraction

• SPARQL queries, OO inheritancemore flexible, build queries you need

Model-for vs. Model-aroundOWL: learn types of inferences, build model around thatSPIN: create model and target inferences

Page 95: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 95

Example owl:complementOf

A minor league player is a player that is not a major league player i.e. intersection of :Player and not :MajorLeaguePlayer reasoners will not make this inferenceOWA – because you didn’t say someone is

a :MajorLeaguePlayer doesn’t mean they aren’t one Major League SPIN

rule on :Player

From Allemang, Hendler pp 226-7

:MinorLeaguePlayer owl:intersectionOf ([ a owl:Class ; owl:intersectionOf (:Player [ a owl:Class ; owl:complementOf :MajorLeaguePlayer ]) ]) .

CONSTRUCT { ?this a :MinorLeaguePlayer .}WHERE { ?major a :MajorLeaguePlayer . FILTER (?this != ?major) .}

Page 96: Toronto semanticmeetup 2013-05-12

Slide 96

SPIN and Rule Systems

Page 97: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 97

Execution Order of Rules

Ordered sequences of rules e.g. make sure :age is computed before rules using that value some scenarios are covered by SPIN iterations

spin:nextRuleProperty defines the next rule property to execute create sub-properties of spin:rule

• (any prop that encodes SPIN RDF) define an ordering between them

Want to run rule sets 1 and 2 before spin:rulespin:rs1 subPropertyOf spin:rule .spin:rs2 subPropertyOf spin:rule .spin:rs1 spin:nextRuleProperty spin:rs2 spin:rs2 spin:nextRuleProperty spin:rule

Page 98: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 98

Execution Order of Rules

Can also order within rulescomment of a rule orders rules insert comments such as "# Step 1: ...", "# Step 2: ..." etc.

Max iterationsattach spin:rulePropertyMaxIterationCount to any

sub-property of spin:rule instructs the engine to execute the rules n times

• each iteration runs rules in execution order• repeat n times• can specify max iterations for each rule

Page 99: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 99

Rule Sets

Creating sets of rules rule sets defined by creating sub-properties of spin:rule.

Ordering rule sets spin:nextRuleProperty to define the order.

General procedure1. Create sub-properties of spin:rule (e.g. spin:rs1, spin:rs2)2. Order spin:nextRuleProperty, e.g. the following would set rule to run

a rs1, rs2, rule: spin:rs1 spin:nextRuleProperty spin:rs2 spin:rs2 spin:nextRuleProperty spin:rule

3. add rules to the properties as needed spin:rulePropertyMaxIterationCount set the maximum

number of iterations terminates when no new triples are created or reaches max count

Page 100: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 100

Rule Sets and Max Iterations

Interaction between spin:nextRuleProperty and spin:rulePropertyMaxIterationCount

Want to run rule sets 1 and 2 before spin:rule, only run twice

spin:rs1 subPropertyOf spin:rule .spin:rs2 subPropertyOf spin:rule .spin:rs1 spin:nextRuleProperty spin:rs2 . spin:rs2 spin:nextRuleProperty spin:rule .spin:rs1 spin:rulePropertyMaxIterationCount  2 .

Rules executed as:1. Pass 1: a. execute all rules in spin:rs1 b. execute all rules in spin:rs2 c. execute all rules in spin:rule2. Pass 2: a. execute all rules in spin:rs1 b. execute all rules in spin:rs2 c. execute all rules in spin:rule

If also have: spin:rs2 spin:rulePropertyMaxIterationCount 2 ....then 2b would be skipped

Page 101: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 101

Rule Sets and Ordering

spin:nextRuleProperty sets ordering between rule setswithin rule sets, order is alphanumerically ordered…basically the order shown in Composer UIbest way to ensure ordering is through a comment at the

beginning of the query statement

Triples Rule#spin:rs1 subPropertyOf spin:rule

spin:rs2 subPropertyOf spin:rule

spin:rs1 spin:nextRuleProperty spin:rs2

spin:rs2 spin:nextRuleProperty spin:rule

<x> spin:rs1 “#Step 1 CONSTRUCT …” 1

<x> spin:rs1 “#Step 2 CONSTRUCT …” 2

<x> spin:rs2 “#Step 1 CONSTRUCT …” 3

<x> spin:rs2 “#Step 2 CONSTRUCT …” 4

<x> spin:rule “CONSTRUCT …” 5

Rules will be executed in the order shown in the Rule # column:• rule sets via nextRuleProperty• within sets alphanumeric

order

Page 102: Toronto semanticmeetup 2013-05-12

Toronto SW Meetup, Slide 102

Summary

SPARQL is a query language for RDFquerying named relationships between entitiesquerying data attributesquerying both schema and data relationships

SPARQL is more than a query languagedata processing, aggregation, etc. reduces the need for programming support

SPARQL and rule-based inferencesCONSTRUCT creates new factsSPIN defines a model-based rule engine

• define rules, constraints on the class, apply to instances