Semantic Web Chapter 7 SPARQL Query Language for
Semantic Web Chapter 7
SPARQL: Query Language for RDF PREFIX ex: <http: //example. org/> SELECT ? title ? author WHERE { ? book ex: published. By <someone>. ? book ex: title ? title. ? book ex: author ? author }
Example result (Select result) title author "title 1" John Writer "title 2" John Doe
Group patterns { {? pattern that. Is <uri/simple> } {} ? code using. Pattern ? pattern }
OPTIONAL pattern Not required for a match Creates bindings if it occurs. OPTIONAL { ? book ex: author ? author} Unbound fields are left empty in the result.
UNION pattern {group} UNION {group} Match if either group (or both) is a match. {? book ex: author ? author. } UNION {? book ex: writer ? author. }
Data values Untyped, untyped and typed forms of "a string" is different. Some types may have semantics to evaluate equality between them. "041"^int == "41"^double
Filters Extra conditions for accepting a particular variable assignment. Boolean statements (or error). FILTER (? price < 100) Scope of rule is the containing group.
Comparison operators =, >, <=. . etc Works according to the semantics of the compared types. Gives errors for incompatible comparisons.
Special operators BOUND is. URI is. BLANK is. LITERAL STR LANG DATATYPE same. TERM(A, B) lang. MACHES(A, B) REGEX(A; B)
Boolean and arithmetic operators &&, ||, !, +, -, *, / These are quite familiar.
Result formats SELECT => table CONSTRUCT => rdf graph DESCRIBE => for browsing ASK => boolean
Modifiers ORDER BY => sorting LIMIT OFFSET => result pruning DISTINCT => removes duplicates
Semantics and Algebra of SPARQL • SPARQL Algebra: o • "a system of clearly computational operations which can be used to calculate the results of a query" VERY similar to SQL
Translating queries to algebra • • We distinguish operators that describe graph patterns from those describing solution sequence modifiers Graph pattern operators: o o • o Basic Graph Patterns (BGP) Join (Conjunctions) Left. Join (Optional Condition) Filter Union Each returns the result of the subquery it describes
Translating queries to algebra • An example query: { ? book ex: price ? price. FILTER(? price < 15) OPTIONAL {? book ex: title ? title. } { ? book ex: author ex: Shakespeare. } UNION { ? book ex: author ex: Marlowe. } }
Translating queries to algebra • • • Step one: Expand all abbreviations, and all simple graph patterns (triples) are expressed with BGP Step two: Occurrences of UNION are marked with the operator Union Step three: Group graph patterns are resolved from inside to outside: Join, Filter and Left. Join o If Sub-Expressions are OPTIONAL Filter(F, A) § R : = Left. Join(R, A, F) o if SE is OPTIONAL A § R : = Left. Join(R, A, true) o Otherwise § R : = Join(R, SE) o Z denotes no condition or variable binding § Our BGP-s are denoted Z, for example
Translating queries to algebra • Applying these rules on example: Filter((? price < 15) Join( Left. Join(Z, BGP(? book <example. org/price> ? price. )), Join(Z, BGP(? book <example. org/title> ? title. )), true ), Union(Join(Z, BGP(? book <example. org/author> <example. org/Shakespeare>. )), Join(Z, BGP(? book <example. org/author> <example. org/Marlowe>. ))) ))
Calculations in SPARQL Algebra • All these operators return query results These partial results are denoted with µ-s. o Read the book for understanding calculations with these. . . o One result is that Z is neutral with respect to Join o
Calculations in SPARQL algebra • Which simplifies a bit: Filter((? price < 15) Join( Left. Join( BGP(? book <example. org/price> ? price. ), BGP(? book <example. org/title> ? title. ), true ), Union(BGP(? book <example. org/author> <example. org/Shakespeare>. ), BGP(? book <example. org/author> <example. org/Marlowe>. ))) ))
Operators for modifiers • Order By, Project, Distinct, Slice can only be done on the final result
Conjunctive Queries for OWL DL • SPARQL: Query language for RDF graphs o • RDFS and OWL not directly supported OWL DL can be interpreted as graphs, BUT: OWL DL Ontologies describe many interpretations o OWL DL interpretations may comprise an infinite number of elements o
Conjunctive Queries for OWL DL • Conjunctive queries is a query formalism for MIGHT OWL DL that serve as a basis for a SPARQL for OWL extension
Conjunctive queries • • Mainly follows first-order logic syntax: o Allowed atoms: o o o • Book(x) ^ published. By(x, CRCPress) ^ Author(x, y) C(e) or ¬C(e). C is a class, e is a variable R(e, f). R is a property name, e/f variables/name Note: ¬R(e, f) not allowed Applying negation: o o o Adding axiom to knowledge base: ¬R(e, f) ≡ ¬(∃R{e})(f) Book(x)^¬(∃published. By{CRCPress})(x)^Author(x, y)
Variables • Distinguished Variables: o o • Free variables that we know must exist, but we may not have the knowledge of what/who it is Book(a) (a is a book) Book⊆ ∃author. ⊤ (every book has an author) § This knowledge base has no solution for Book(x) ^ author(x, y) § We know of no y-s that can be returned Non-Distinguished Variables: o o Bound variables, useful for querying concrete instances, or asking for the existence of an element ∃x. (author(x, y) ^ Book(x))
Conjunctive Queries and SPARQL • Different, different, but same: Consider the atom R(e, f) as a triple <e, R, f> o conjunction of atoms describe a graph structure o
Conjunctive Queries and SPARQL • . . . but different CQs lack features like OPTIONAL and UNION o Only SPARQL allows disjunction o SPARQL allows variables in place of property names, CQ have syntactic restrictions of first order logic o
Conjunctive Queries and SPARQL • Different usage of variables SPARQL represents placeholders in queries by variables or blank nodes o Not all variables need to be part of result in SPARQL o CQ use Distinguished and Non-Distinguished variables o • Summarized by two features: Anonymous values: Can the variable take values for which no concrete ID is known? o Output: Will the variable appear in the result? o
Conjunctive Queries and SPARQL Anonymous Values Output Distinguished Variable - Yes Non-Distinguished Variable Yes - Blank Node Yes - SPARQL Variable Yes Non-Selected SPARQL var. Yes - • Major difference is between SPARQL variables and Distinguished Variables Latter is restricted to elements with concrete names o Thus, semantic approach from SPARQL leads to all entailed elements having to be returned o Strongest technical reason as to why no official extension from SPARQL to OWL DL exists o
- Slides: 29