• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
The Semantics of SPARQL
 

The Semantics of SPARQL

on

  • 3,843 views

I used these slides for an introductory lecture (90min) to a seminar on SPARQL. This slideset introduces the semantics of the RDF query language SPARQL.

I used these slides for an introductory lecture (90min) to a seminar on SPARQL. This slideset introduces the semantics of the RDF query language SPARQL.

Statistics

Views

Total Views
3,843
Views on SlideShare
3,833
Embed Views
10

Actions

Likes
4
Downloads
180
Comments
0

1 Embed 10

http://www.slideshare.net 10

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    The Semantics of SPARQL The Semantics of SPARQL Presentation Transcript

    • SPARQL - Querying the Web of Data Seminar WS 2008/2009 The Semantics of SPARQL Olaf Hartig hartig@informatik.hu-berlin.de
    • Overview Algebra-based approach to define the SPARQL semantics ● Definition of “correct behavior for evaluation of graph ● patterns and solution modifiers” [SPARQL Spec.] Series of steps: ● Parsing a SPARQL query into an abstract syntax tree ● Converting the abstract syntax tree to an abstract query ● (i.e. an algebra expression) Evaluating the abstract query on an RDF dataset ● Each symbol in the abstract query is associated with an ● algebra operator of the same name. The Semantics of SPARQL 2
    • Abstract Syntax SPARQL parser creates an abstract syntax tree (AST) ● Extract of the SPARQL grammar (AST symbols): ● WhereClause ::= 'WHERE'? GroupGraphPattern GroupGraphPattern ::= '{' TriplesBlock? ( ( GraphPatternNotTriples | Filter ) '.'? TriplesBlock? )* '}' TriplesBlock ::= TriplesSameSubject ( '.' TriplesBlock? )? GraphPatternNotTriples ::= OptionalGraphPattern | GroupOrUnionGraphPattern | GraphGraphPattern OptionalGraphPattern ::= 'OPTIONAL' GroupGraphPattern GraphGraphPattern ::= 'GRAPH' VarOrIRIref GroupGraphPattern GroupOrUnionGraphPattern ::= GroupGraphPattern ( 'UNION' GroupGraphPattern )* Filter ::= 'FILTER' Constraint The Semantics of SPARQL 3
    • Converting to an Abstract Query A SPARQL Abstract Query is a tuple (E, DS, R) where: ● E is a SPARQL algebra expression ● DS is an RDF dataset – DS = { G , (<u1>,G1) , ... , (<un>,Gn) } ● R is a query form ● Converting an AST to an algebra expression: ● Step 1: Expand abbreviations for URIs and triple patterns ● Step 2: Recursively translate the query pattern ● Step 3: Translate the solution modifiers ● The Semantics of SPARQL 4
    • Converting to an Abstract Query Step 1: Expand abbreviations for URIs and triple patterns ● ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name . ?v <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Volcano> ; <http://www.w3.org/2000/01/rdf-schema#label> ?name . ?v <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Volcano> . ?v <http://www.w3.org/2000/01/rdf-schema#label> ?name . The Semantics of SPARQL 5
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● PROCEDURE Transform ( AST Symbol ) IF the AST Symbol is TriplesBlock THEN RETURN BGP( list of triple patterns ); IF the AST Symbol is GroupOrUnionGraphPattern THEN LET A := undefined; FOR EACH G in the AST Symbol DO IF A is undefined THEN A := Transform( G ); ELSE A := Union( A, Transform(G) ); RETURN A; IF the AST Symbol is GraphGraphPattern THEN ... The Semantics of SPARQL 6
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { ?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;. OPTIONAL { ?x <http://example.org/p3> ?y. } } UNION { ?x <http://example.org/p1> <http://example.org/B>. } ... The Semantics of SPARQL 7
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { ?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;. OPTIONAL { ?x <http://example.org/p3> ?y. } } UNION { ?x <http://example.org/p1> <http://example.org/B>. } ... The Semantics of SPARQL 8
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;. ) OPTIONAL { ?x <http://example.org/p3> ?y. } } UNION { ?x <http://example.org/p1> <http://example.org/B>. } ... The Semantics of SPARQL 9
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) OPTIONAL { BGP(?x <http://example.org/p3> ?y.) } } UNION { BGP(?x <http://example.org/p1> <http://example.org/B>.) } ... The Semantics of SPARQL 10
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) OPTIONAL { BGP(?x <http://example.org/p3> ?y.) } } UNION { BGP(?x <http://example.org/p1> <http://example.org/B>.) } ... The Semantics of SPARQL 11
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { LeftJoin( BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) , BGP(?x <http://example.org/p3> ?y.) ) } UNION { BGP(?x <http://example.org/p1> <http://example.org/B>.) } ... The Semantics of SPARQL 12
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { LeftJoin( BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) , BGP(?x <http://example.org/p3> ?y.) ) } UNION { BGP(?x <http://example.org/p1> <http://example.org/B>.) } ... The Semantics of SPARQL 13
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● Union( LeftJoin( BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) , BGP(?x <http://example.org/p3> ?y.) ), BGP(?x <http://example.org/p1> <http://example.org/B>.) ) ... The Semantics of SPARQL 14
    • Converting to an Abstract Query Step 2: Recursively translate the query pattern ● Union LeftJoin BGP(?x <http://example.org/p3> ?y.) BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) BGP(?x <http://example.org/p1> <http://example.org/B>.) The Semantics of SPARQL 15
    • Converting to an Abstract Query Step 3: Translate the solution modifiers ● 1. M := ToList( algebra expression of the query pattern ) 2. IF query has ORDER BY THEN M := OrderBy( M, order comparators ) 3. M := Project( M, vars ) where vars is the set of variables in SELECT 4. IF query has DISTINCT THEN M := Distinct( M ) ... Finally, M becomes the algebra expression E of the query ● The Semantics of SPARQL 16
    • Evaluation eval( DG , a ) defines the evaluation of ● an algebra expression a ● on an RDF dataset D ● with the active graph G ● eval( DG, BGP(bgp) ) = multiset of solutions of bgp eval( DG, Union(a1 , a2) ) = Union( eval(DG , a1) , eval(DG , a2) ) eval( DG, Filter(e,a) ) = Filter( e , eval(DG , a) ) eval( DG, Graph(uri,a) ) = eval( DD(uri) , a ) ... eval( D, ToList(a) ) = eval( DD(dflt) , a ) ... The Semantics of SPARQL 17
    • Evaluation of BGPs Query SELECT ?name WHERE { _:x rdf:type umbel-sc:Volcano ; rdfs:label ?name . } Abstract Query BGP( _:x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Volcano> . _:x <http://www.w3.org/2000/01/rdf-schema#label> ?name . ) ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 18
    • Evaluation of BGPs Data dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label quot;Etnaquot; . dbpedia:Mount_Baker rdf:type umbel-sc:Volcano . dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label quot;Beerenbergquot;@en . ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 19
    • Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker ?name ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 20
    • Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker ?name quot;Etnaquot; ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 21
    • Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker ? No matching ?name quot;Etnaquot; subgraph ! ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 22
    • Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker ?name quot;Etnaquot; quot;Beerenbergquot;@en ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 23
    • Basic Graph Pattern Matching Definition: A solution mapping, µ, is a partial function µ: V  T. Definition: A pattern instance mapping, P, is the combination of an RDF instance mapping, σ, and solution mapping, μ. P(x) = μ(σ(x)) Definition: Let bgp be a basic graph pattern and let G be an RDF graph. μ is a solution for bgp from G when there is a pattern instance mapping P such that P(bgp) is a subgraph of G and μ is the restriction of P to the query variables in bgp. The Semantics of SPARQL 24
    • Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker σ = { _:x → dbpedia:Mount_Etna } μ = { ?name → quot;Etnaquot; } ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 25
    • Evaluation of BGPs Evaluation of a BGP yields a multiset Ω of solution ● mappings μ where each μ is a solution for the BGP. card[Ω](μ) denotes the cardinality of each μ in Ω ● Definition: Let bgp be a basic graph pattern and let G be an RDF graph. card[Ω](μ) is the number of distinct RDF instance map- pings, σ, such that P(x) = μ(σ(x)) is a pattern instance mapping and ● P(bgp) is a subgraph of G. ● SPARQL algebra operators operate on multisets Ω (or ● sequences) of solution mappings The Semantics of SPARQL 26
    • Algebra Operators Union( Ω1 , Ω2 ) = { μ | μ is in Ω1 or μ is in Ω2 } card[ Union( Ω1 , Ω2 ) ](μ) = card[Ω1](μ) + card[Ω2](μ) ● ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Mount_Baker 1880 Union ?v ?name ?v ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Mount_Baker 1880 dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru dbpedia:Beerenberg quot;Beerenbergquot;@en The Semantics of SPARQL 27
    • Algebra Operators Filter( e , Ω ) = { μ | μ is in Ω and e(μ) evaluates to true } ● card[ Filter( e , Ω ) ](μ) = card[Ω](μ) ?v ?name dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Beerenbergquot;@en Filter STR(?name) = quot;Beerenbergquot; ?v ?name dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru dbpedia:Beerenberg quot;Beerenbergquot;@en The Semantics of SPARQL 28
    • Algebra Operators Join( Ω1 , Ω2 ) = { merge(μ1 , μ2) | μ1 is in Ω1 and μ2 is in Ω2 and μ1 and μ2 are compatible } Definition: Two solution mappings μ1 , μ2 are compatible if μ1(v) = μ2(v) holds for every variable v in dom(μ1) and v in dom(μ2). Definition: Let μ1 and μ2 compatible solution mappings. merge(μ1 , μ2) = μ1 U μ2 Example: μ1 = { ?x → ex:A , ?y → quot;abcquot; } μ2 = { ?x → ex:A , ?z → ex:B } merge(μ1 , μ2) = { ?x → ex:A , ?y → quot;abcquot; , ?z → ex:B } The Semantics of SPARQL 29
    • Algebra Operators Join( Ω1 , Ω2 ) = { merge(μ1 , μ2) | μ1 is in Ω1 and μ2 is in Ω2 and μ1 and μ2 are compatible } Σ card[ Join( Ω1 , Ω2 ) ](μ) = card[Ω1](μ1) · card[Ω2](μ2) ● (μ1 , μ2) where μ1 is in Ω1 and μ2 is in Ω2 and μ1 and μ2 are compatible The Semantics of SPARQL 30
    • Algebra Operators Join( Ω1 , Ω2 ) = { merge(μ1 , μ2) | μ1 is in Ω1 and μ2 is in Ω2 and μ1 and μ2 are compatible } Σ card[ Join( Ω1 , Ω2 ) ](μ) = card[Ω1](μ1) · card[Ω2](μ2) ● ?v ?name ?le dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 Join ?v ?name dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru ?v ?name ?le dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 The Semantics of SPARQL 31
    • Algebra Operators Diff( Ω1 , Ω2 , e ) = { μ | μ is in Ω1 such that for all μ' in Ω2 either μ and μ' are not compatible or e(merge(μ,μ')) evaluates to false } ● card[ Diff( Ω , Ω , e ) ](μ) = card[Ω ](μ) 1 2 1 ?v ?name dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru Diff STR(?name) = quot;Beerenbergquot; ?v ?name ?v ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Mount_Baker 1880 dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg 1985 dbpedia:Beerenberg quot;Бееренбергquot;@ru The Semantics of SPARQL 32
    • Algebra Operators LeftJoin( Ω1 , Ω2 , e ) = Filter( e, Join(Ω1 , Ω2) ) U Diff(Ω1,Ω2,e) card[ LeftJoin(Ω1,Ω2,e) ](μ) = card[Filter( e, Join(Ω1,Ω2) )](μ) ● + card[Diff(Ω1,Ω2,e)](μ) ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 LeftJoin ?v ?name ?v ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Mount_Baker 1880 dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg 1985 dbpedia:Beerenberg quot;Бееренбергquot;@ru The Semantics of SPARQL 33
    • Algebra Operators ToList( Ω ) = Ψ where Ψ is a sequence of solution mappings μ in Ω ● card[ ToList( Ω ) ](μ) = card[Ω](μ) ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 ToList ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 The Semantics of SPARQL 34
    • Algebra Operators OrderBy( Ψ , c ) = [ μ | μ in Ψ and the sequence satisfies c ] where c is an ordering condiction ● card[ OrderBy(Ψ,c) ](μ) = card[Ψ](μ) ?v ?name ?le dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 dbpedia:Mount_Etna quot;Etnaquot; OrderBy DESC(?le) ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 The Semantics of SPARQL 35
    • Algebra Operators Further operators: ● Project( Ψ , PVars ) ● Distinct( Ψ ) ● Reduced( Ψ ) ● Slice( Ψ , start, length ) ● The Semantics of SPARQL 36
    • Summary Parsing a SPARQL query into an abstract syntax tree (AST) ● e.g.: GroupOrUnionGraphPattern ::= GroupGraphPattern ( 'UNION' GroupGraphPattern )* Converting the AST to a SPARQL Abstract Query (with an ● algebra expression) e.g.: GroupOrUnionGraphPattern → Union(a1 , a2) Evaluating the abstract query on an RDF dataset ● Each symbol in the abstract query is associated with an ● algebra operator of the same name. e.g.: Union( a1 , a2 ) → Union( Ω1 , Ω2 ) Evaluation semantics defined by the definition of solu- ● tions for BGPs, the algebra operators, and eval( DG , a ) The Semantics of SPARQL 37
    • Example Data dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label quot;Etnaquot; . dbpedia:Mount_Baker rdf:type umbel-sc:Volcano . dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label quot;Beerenbergquot;@en ; rdfs:label quot;Бееренбергquot;@ru . Question: What volcanos are not called “Beerenberg”? ● ?v Query SELECT ?v WHERE { dbpedia:Mount_Etna ?v rdf:type umbel-sc:Volcano . dbpedia:Mount_Baker OPTIONAL { ?v rdfs:label ?name . FILTER (STR(?name) = quot;Beerenbergquot;) } FILTER ( ! BOUND(?name) ) Negation as Failure } The Semantics of SPARQL 38
    • Example Query SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano . OPTIONAL { ?v rdfs:label ?name . FILTER (STR(?name) = quot;Beerenbergquot;) } FILTER ( ! BOUND(?name) ) } The Semantics of SPARQL 39
    • Example Query SELECT ?v WHERE { Filter ?v rdf:type umbel-sc:Volcano . ! BOUND(?name) OPTIONAL { ?v rdfs:label ?name . FILTER (STR(?name) = quot;Beerenbergquot;) } LeftJoin FILTER ( ! BOUND(?name) ) } Filter STR(?name) = quot;Beerenbergquot; BGP( ?v rdf:type umbel-sc:Volcano ) BGP( ?v rdfs:label ?name ) The Semantics of SPARQL 40
    • Example Data dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label quot;Etnaquot; . Filter ! BOUND(?name) dbpedia:Mount_Baker rdf:type umbel-sc:Volcano . dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; LeftJoin rdfs:label quot;Beerenbergquot;@en ; rdfs:label quot;Бееренбергquot;@ru . ?v dbpedia:Mount_Etna Filter dbpedia:Beerenberg STR(?name) = quot;Beerenbergquot; dbpedia:Mount_Baker ?v ?name BGP( ?v rdf:type umbel-sc:Volcano ) dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru BGP( ?v rdfs:label ?name ) The Semantics of SPARQL 41
    • ?v ?name Example dbpedia:Mount_Etna dbpedia:Mount_Baker ?v ?name dbpedia:Mount_Etna Filter dbpedia:Beerenberg quot;Beerenbergquot;@en ! BOUND(?name) dbpedia:Mount_Baker LeftJoin ?v ?name dbpedia:Beerenberg quot;Beerenbergquot;@en ?v dbpedia:Mount_Etna Filter dbpedia:Beerenberg STR(?name) = quot;Beerenbergquot; dbpedia:Mount_Baker ?v ?name BGP( ?v rdf:type umbel-sc:Volcano ) dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru BGP( ?v rdfs:label ?name ) The Semantics of SPARQL 42
    • Further Reading SPARQL Query Language for RDF, W3C Recommendation ● http://www.w3.org/TR/rdf-sparql-query/ SPARQL interface for dbpedia: http://dbpedia.org/snorql/ ● The Semantics of SPARQL 43