290 likes | 467 Views
Semantic Web. Chapter 7. SPARQL: Query Language for RDF. PREFIX ex: <http://example.org/> SELECT ?title ?author WHERE { ?book ex:publishedBy <someone>. ?book ex:title ?title. ?book ex:author ?author }. Example result (Select result). Group patterns. { {?pattern thatIs <uri/simple> }
E N D
Semantic Web Chapter 7
SPARQL: Query Language for RDF PREFIX ex: <http://example.org/> SELECT ?title ?author WHERE { ?book ex:publishedBy <someone>. ?book ex:title ?title. ?book ex:author ?author }
Group patterns { {?pattern thatIs <uri/simple> } {} ?code usingPattern ?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"^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 isURI isBLANK isLITERAL STR LANG DATATYPE sameTERM(A,B) langMACHES(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: • "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: • Basic Graph Patterns (BGP) • Join (Conjunctions) • LeftJoin (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 LeftJoin • If Sub-Expressions are OPTIONAL Filter(F, A) • R := LeftJoin(R, A, F) • if SE is OPTIONAL A • R := LeftJoin(R, A, true) • Otherwise • R := Join(R, SE) • 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( LeftJoin( 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. • Read the book for understanding calculations with these ... • One result is that Z is neutral with respect to Join
Calculations in SPARQL algebra • Which simplifies a bit: Filter((?price < 15) Join( LeftJoin( 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 • RDFS and OWL not directly supported • OWL DL can be interpreted as graphs, BUT: • OWL DL Ontologies describe many interpretations • OWL DL interpretations may comprise an infinite number of elements
Conjunctive Queries for OWL DL • Conjunctive queries is a query formalism for OWL DL that MIGHT serve as a basis for a SPARQL for OWL extension
Conjunctive queries • Mainly follows first-order logic syntax: • Book(x) ^ publishedBy(x,CRCPress) ^ Author(x,y) • Allowed atoms: • 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: • Adding axiom to knowledge base: • ¬R(e,f) ≡ ¬(∃R{e})(f) • Book(x)^¬(∃publishedBy{CRCPress})(x)^Author(x,y)
Variables • Distinguished Variables: • 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: • 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> • conjunction of atoms describe a graph structure
Conjunctive Queries and SPARQL • ... but different • CQs lack features like OPTIONAL and UNION • Only SPARQL allows disjunction • SPARQL allows variables in place of property names, CQ have syntactic restrictions of first order logic
Conjunctive Queries and SPARQL • Different usage of variables • SPARQL represents placeholders in queries by variables or blank nodes • Not all variables need to be part of result in SPARQL • CQ use Distinguished and Non-Distinguished variables • Summarized by two features: • Anonymous values: Can the variable take values for which no concrete ID is known? • Output: Will the variable appear in the result?
Conjunctive Queries and SPARQL • Major difference is between SPARQL variables and Distinguished Variables • Latter is restricted to elements with concrete names • Thus, semantic approach from SPARQL leads to all entailed elements having to be returned • Strongest technical reason as to why no official extension from SPARQL to OWL DL exists