(04) Semantic Web Technologies - SPARQL

3,388 views
3,260 views

Published on

Presentation Slides for LEcture 'Semantic Web Technologies' winter semester 2012/13, Hasso Plattner Institute for IT Systems Engineering, 06. Nov. 2012, Dr. Harald Sack

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,388
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
86
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

(04) Semantic Web Technologies - SPARQL

  1. 1. Semantic Web Technologies Lecture Dr. Harald Sack Hasso-Plattner-Institut für IT Systems Engineering University of Potsdam Winter Semester 2012/13 Lecture Blog: http://semweb2013.blogspot.com/ This file is licensed under the Creative Commons Attribution-NonCommercial 3.0 (CC BY-NC 3.0)Dienstag, 6. November 12
  2. 2. Semantic Web Technologies Content2 1. Introduction 2. Semantic Web - Basic Architecture Languages of the Semantic Web - Part 1 3. Knowledge Representation and Logics Languages of the Semantic Web - Part 2 4. Applications in the ,Web of Data‘ Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  3. 3. SLast lecture...3 D F R SO ... WE NEED A WAY TO DEFINE RDF SCHEMATA FOR CLASSES AND RELATIONS ?! Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  4. 4. Semantic Web Technologies Content4 2. Semantic Web Basic Architecture The Languages of the Semantic Web - Part 1 2.1. Uniform Resource Identifier - URI 2.2. Recapitulation: XML and XMLSchema 2.3. Resource Description Framework - RDF 2.4. RDF Schema 2.5. How Do We Get Semantics into WWW? 2.6. Query Language SPARQL 2.7. Why RDF(S) is not enough Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  5. 5. 5 Q L A R SP Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, via http://www.snee.com/bobdc.blog/2008/10/learning-more-about-sparql.html Universität PotsdamDienstag, 6. November 12
  6. 6. 2. Semantic Web Basic Architecture The Languages of the Semantic Web - Part 16 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  7. 7. 2. Semantic Web Basic Architecture 2.6 Query Language SPARQL7 A Query Language for RDF • How do we get to the knowledge from RDF/RDFS Knowledge Bases? • Manual Parsing/Reading and Combination of RDF-Triple is complex • Can you imagine a relational database without SQL? • SPARQL Protocol and RDF Query Language ist • a Query Language for RDF Graph TRaversal (SPARQL Query Language Specification) • a Protocol Layer, to use SPARQL via http (SPARQL Protocol for RDF Specification) • an XML Output Format Specification for SPARQL Queries (SPARQL Query XML Results Format) • W3C Standard (SPARQL 1.0 since Januar 2008) • inspired by SQL Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  8. 8. 8 • SPARQL 1.0 allows • Extraction of data from structured or semi structures Data as • URIs, Blank Nodes, typed and untyped Literals. • RDF Subgraphs • Exploration of Data via Query for unknown relations • Execution of complex Join Operations on heterogeneous databases in a single query • Transformation of RDF Data from one vocabulary into another • Construction of new RDF Graphs based on RDF Query Graphs Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  9. 9. 9 • SPARQL 1.1 (in progress) allows • additional query features • aggregate functions, subqueries, negations, project expressions, property paths, basic federated queries • enables logical Entailment for • RDF, RDFS, OWL Direct and RDF-Based Semantics entailment, and RIF Core entailment. • enables Update of RDF Graphs as a full data manipulation language • enables the Discovery of information about the SPARQL service • enables Federated Queries distributed over different SPARQL Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  10. 10. 10 SPARQL 1.0 first Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  11. 11. 11 • SPARQL Variables are bound to RDF terms • e.g. ?journal, ?disease, ?price • In the same way as in SQL a Query for variables is performed via SELECT statement • e.g. SELECT ?title ?author ?published • A SELECT statement returns Query Results as a table ?title ?author ?published 1984 George Orwell 1948 Brave New World Aldous Huxley 1932 Fahrenheid 451 Ray Bradbury 1953 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  12. 12. SPARQL Query12 SELECT ?title ?author ?published SPARQL Endpoint ?title ?author ?published 1984 George Orwell 1948 Brave New World Aldous Huxley 1932 Fahrenheid 451 Ray Bradbury 1953 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  13. 13. 13 Graph Pattern • SPARQL is based on RDF Turtle serialization and basic graph pattern matching. • A Graph Pattern (Triple Pattern) is a RDF Triple that contains variables at any arbitrary place (Subject, Predicate, Object). • (Graph) Triple Pattern = Turtle + Variables • Examples: • Look for countries and their capitals: •?country geo:capital ?capital . • Given a FOAF URI, find the name of a person: •<http://hpi-web.de/id#haraldsack> foaf:name ?surname . • Which persons have the family name „Schmidt“? •?person pers:familyName “Schmidt“ . Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  14. 14. 14 Triple Pattern ?country geo:capital ?capital . RDF Graph dbpedia:Venezuela rdf:type dbpedia-owl:Country . dbpedia:Venezuela geo:capital “Caracas“ . dbpedia:Venezuela dbprop:language “Spanish“ . dbpedia:Germany rdf:type dbpedia-owl:Country . dbpedia:Germany geo:capital “Berlin“ . dbpedia:Germany dbprop:language “German“ . ... Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  15. 15. 15 Simple Query Pattern • Graph Pattern can be combined to form complex (conjunctive) queries for RDF graph traversal. • Find countries, their capitals and their population: •?country geo:capital ?capital . ?country geo:population ?population . • Given a FOAF URI, find the name of a person and her friends: •<http://hpi-web.de/id#haraldsack> foaf:name ?surname ; foaf:knows ?friend . ?friend foaf:name ?friend_surname . • Find all superiors of the 3. management level: •?emp hr:managedBy ?first_line . ?first_line hr:managedBy ?second_line . ?second_line hr:managedBy ?third_line . Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  16. 16. 16 SPARQL Query Format • inspired by SQL SELECT ?p, ?o WHERE { subject ?p ?o. } • Triple in „WHERE“ part defines graph query with variables ?p and ?o • Query returns table with matching ?p, ?o pairs ?p ?o ?p ?o subject ?p ?o ?p ?o Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  17. 17. 17 SPARQL Query Format • Search all lectures and their managers: PREFIX hpi: <http://hpi-web.de/swt1213#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?lecture ?manager FROM <...> WHERE { ?x rdf:type hpi:Lecture . ?x rdfs:label ?lecture . ?y rdf:type hpi:Staff . ?y rdfs:label ?manager ?x hpi:isManagedBy ?y . } • WHERE - specifies graph pattern to be matched • PREFIX - specifies one or more namespaces for using compact URIs (CURIEs) • FROM - specifies one or more RDF source graphs • BASE - defines a base URI Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  18. 18. 18 SPARQL Query Format • Search all lectures and their managers ordered by managers in descending order and limit the results to the first 10 starting the list at position 10: PREFIX hpi: <http://hpi-web.de/swt1213#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?lecture ?manager FROM <...> WHERE { ?x rdf:type hpi:Lecture ; rdfs:label ?lecture . ?y rdf:type hpi:Staff ; rdfs:label ?manager ?x hpi:isManagedBy ?y . } ORDER BY DESC (?manager) LIMIT 10 OFFSET 10 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  19. 19. 19 Blank Nodes in SPARQL Queries • as subject or object of a triple pattern • „non selectable“ variables PREFIX hpi: <http://hpi-web.de/swt1213#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?name WHERE { _:x rdf:type hpi:Lecture ; hpi:isManagedBy [ rdfs:label ?name] . } • blank node identifier might also occur in the results Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  20. 20. How should I know all those prefixes?20 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  21. 21. 21 SPARQL Query Format • The keyword FILTER specifies constraints for the results # Default Graph (stored at http://example.org/book) @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix : <http://example.org/book/> . @prefix ns: <http://example.org/ns#> . :book1 dc:title "SPARQL Tutorial" . :book1 ns:price 42 . :book2 dc:title "The Semantic Web" . :book2 ns:price 23 . PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ns: <http://example.org/ns#> • FILTER expressions SELECT ?title ?price contain operators FROM <http://example.org/book> and functions WHERE { ?x ns:price ?price . • FILTERs can NOT FILTER (?price < 30.5) assign/create new ?x dc:title ?title . values } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  22. 22. 22 SPARQL Unary Operators in Constraints Operator Type(A) Result Type !A xsd:boolean xsd:boolean +A numeric numeric -a numeric numeric BOUND(A) variable xsd:boolean isURI(A) RDF term xsd:boolean isBLANK(A) RDF term xsd:boolean isLITERAL(A) RDF term xsd:boolean STR(A) literal/URI simple literal LANG(A) literal simple literal DATATYPE(A) literal URI Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  23. 23. 23 More SPARQL Operators • Logical connectives && and || for xsd:boolean • Comparison operators =, !=, <, >, <=, and >= for numeric datatypes, xsd:dateTime, xsd:string, and xsd:boolean • Comparison operators = and != for other datatypes • Arithmetic operators +, -, *, and / for numeric datatypes • and in addition: • REGEX(String,Pattern) or REGEX(String,Pattern,Flags) • sameTERM(A,B) • langMATCHES(A,B) Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  24. 24. 24 Filter Constraints are Evaluated in 3-valued Logic • true, false, and error A B A || B A && B T T T T T F T F F T T F F F F F T E T E E T T E A !A F E E F T F E F E F F T E E E E E E Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  25. 25. 25 SPARQL Query Format • The keyword OPTIONAL selects optional elements from the RDF graph • complies to a Left Outer Join # Default Graph (stored at http://example.org/addresses) @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . _:a rdf:type foaf:Person . _:a foaf:name "Alice" . _:a foaf:mbox <mailto:alice@example.com> . _:a foaf:mbox <mailto:alice@work.example> . _:b rdf:type foaf:Person . _:b foaf:name "Bob" . PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox FROM <http://example.org/addresses> WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  26. 26. SPARQL Query Format26 • The keyword UNION allows for alternatives (logical disjunction) # Default Graph (stored at http://example.org/book) @prefix dc10: <http://purl.org/dc/elements/1.0/> . @prefix dc11: <http://purl.org/dc/elements/1.1/> . _:a dc10:title "SPARQL Query Language Tutorial" . _:a dc10:creator "Alice" . _:b dc11:title "SPARQL Protocol Tutorial" . _:b dc11:creator "Bob" . _:c dc10:title "SPARQL" . _:c dc11:title "SPARQL (updated)" . PREFIX dc10: <http://purl.org/dc/elements/1.0/> PREFIX dc11: <http://purl.org/dc/elements/1.1/> SELECT ?title FROM <http://example.org/books> WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  27. 27. 27 SPARQL Query Format • Negation in SPARQL • (complies to ,NOT EXISTS‘ in SQL) @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . _:a foaf:givenName "Alice". _:b foaf:givenName "Bob" . _:b dc:date "2005-04-04T04:04:04Z"^^xsd:dateTime . PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?name WHERE { ?x foaf:givenName ?name . OPTIONAL { ?x dc:date ?date } . FILTER (!bound(?date)) } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  28. 28. 28 SPARQL Query Format • SPARQL queries are executed over an RDF dataset • one (or more) default RDF graph • zero or more named RDF graphs • Named Graphs can explicitely addressed via keyword GRAPH and the URI of the named graph GRAPH <http://example.org/graph1.rdf> { ?x foaf:mbox ?mbox } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  29. 29. Default Graph29 hpi:g1 PREFIX hpi: ... Default Graph SELECT ... FROM hpi:g1 FROM hpi:g4 hpi:g4 FROM NAMED hpi:g1 FROM NAMED hpi:g2 FROM NAMED hpi:g3 WHERE { ... A ... GRAPH hpi:g3 { ... B ... hpi:g3 } hpi:g1 GRAPH ?g { ... C ... } } hpi:g2 Named Graphs Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  30. 30. 30 SPARQL Query Format • Example for Named Graphs Default Graph (stored at http://example.org/dft.ttl) @prefix dc: <http://purl.org/dc/elements/1.1/> . <http://example.org/bob> dc:publisher "Bob Hacker" . <http://example.org/alice> dc:publisher "Alice Hacker" . Named Graph: http://example.org/bob @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Bob" . _:a foaf:mbox <mailto:bob@oldcorp.example.org> . Named Graph: http://example.org/alice @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Alice" . _:a foaf:mbox <mailto:alice@work.example.org> . Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  31. 31. 31 SPARQL Query Format • Example for Named Graphs PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/> Default Graph SELECT ?who ?g ?mbox FROM <http://example.org/dft.ttl> FROM NAMED <http://example.org/alice> FROM NAMED <http://example.org/bob> Named Graph WHERE { ?g dc:publisher ?who . GRAPH ?g { ?x foaf:mbox ?mbox } } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  32. 32. 32 SPARQL Result Format • SPARQL results are given as well formed and valid XML documents <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> ... </sparql> • In a <head> element all variables of the SPARQL query are listed <head> <variable name="x"/> <variable name="hpage"/> <variable name="name"/> <variable name="mbox"/> <variable name="blurb"/> </head> Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  33. 33. 33 SPARQL Result Format • For each SPARQL Query result exists a <result> element <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="x"/> ... </head> <results> <result> <binding name="x"> ... </binding> <binding name="hpage"> ... </binding> </result> <result> ... </result> ... </results> </sparql> Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  34. 34. 34 SPARQL Result Format • Within a <binding> element a <head> variable is bound to a result <result> <binding name="x"> <bnode>r2</bnode> </binding> <binding name="hpage"> <uri>http://work.example.org/bob/</uri> </binding> <binding name="name"> <literal xml:lang="en">Bob</literal> </binding> <binding name="age"> <literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal> </binding> <binding name="mbox"> <uri>mailto:bob@work.example.org</uri> </binding> </result> Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  35. 35. 35 SPARQL Query Format (continued) • In addition to SELECT queries SPARQL allows: • ASK •Check whether there is at least one result •Result: true or false •Result is delivered as XML or JSON • CONSTRUCT •Result: an RDF graph constructed from a template •Template: graph pattern with variables from the query pattern •Result is RDF/XML or Turtle • DESCRIBE •Result: an RDF graph with data about resources •Result is RDF/XML or Turtle Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  36. 36. 36 SPARQL Construction of RDF Graphs • CONSTRUCT defines a template for the construction of new RDF Graphs @prefix org: <http://example.com/ns#> . _:a org:employeeName "Alice" . _:a org:employeeId 12345 . _:b org:employeeName "Bob" . _:b org:employeeId 67890 . data PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX org: <http://example.com/ns#> CONSTRUCT { ?x foaf:name ?name } WHERE { ?x org:employeeName ?name } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  37. 37. 37 SPARQL Construction of RDF Graphs • Result of a CONSTRUCT query as serialized RDF/XML <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:foaf="http://xmlns.com/foaf/0.1/"> <rdf:Description> <foaf:name>Alice</foaf:name> </rdf:Description> <rdf:Description> <foaf:name>Bob</foaf:name> </rdf:Description> </rdf:RDF> Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  38. 38. 38 SPARQL Protocol • Method to query/respond of SPARQL queries via http • A SPARQL URI consists out of 3 parts: 1.URL of a SPARQL endpoint (e.g. http://example.org/sparql) 2.RDF Graph(s) to be queried (optional, part of the query string, z.B. named-graph-uri=http://example.org/testrdf.rdf) 3.Query string (part of the query string, e.g. query=SELECT...) http://example.org/sparql?named-graph-uri=http%3A%2F%2Fexample.org%2Ftestrdf& query=SELECT+%3Freview_graph+WHERE+%7B%0D%0A++GRAPH+%3Frev iew_graph+%7B%0D%0A+++++%3Freview+rev%3Arating+10+.%0D%0A++ %7D%0D%0A%7D Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  39. 39. 39 SPARQL Protocol -- Example • Simple SPARQL Query PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?book ?who WHERE { ?book dc:creator ?who } • HTTP Trace of the SPARQL Query GET /sparql/?query=EncodedQuery&default-graph-uri=http:// www.other.example/books HTTP/1.1 Host: www.other.example User-agent: my-sparql-client/0.1 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  40. 40. 40 • HTTP Trace of the SPARQL Response HTTP/1.1 200 OK Date: Fri, 06 May 2008 20:55:12 GMT Server: Apache/1.3.29 (Unix) PHP/4.3.4 DAV/1.0.3 Connection: close Content-Type: application/sparql-results+xml <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results ordered="false" distinct="false"> <result> <binding name="who"> <literal>Bob Hacker</literal> </binding> <binding name=“book"> <literal>The Art of Hacking</literal> </binding> ... </sparql> Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  41. 41. 41 • some popular SPARQL (1.0) Endpoints • http://sparql.org/sparql.html -- General Purpose SPARQL Endpoint • http://dbpedia.org/sparql -- DBpedia SPARQL Endpoint • http://www4.wiwiss.fu-berlin.de/dblp/sparql -- DBLP (Computer Science Bibliographies) SPARQL Endpoint • http://linkedmdb.org/sparql -- Linked Movie Database SPARQL Endpoint • http://www4.wiwiss.fu-berlin.de/factbook/sparql -- CIA World Factbook SPARQL Endpoint • ... Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  42. 42. 42 nd a x a .. nt aft. sy r s in g d n ge rkin cha wo ur ill a occ st ght is e mi his h er se t : T ecau t ion s b t en tic At man se SPARQL 1.1 next Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  43. 43. SPARQL 1.1 -- New Features • SPARQL 1.1 Query:43 • Assignments (e.g. BIND, SELECT expressions) • Aggregate functions (e.g. COUNT, SUM, AVG) • Subqueries • Negation (EXISTS, NOT EXISTS, MINUS) • Property paths • Basic query federation (SERVICE, BINDINGS) • SPARQL 1.1 Update: • Graph update (INSERT DATA, DELETE DATA, INSERT, DELETE, DELETE WHERE, LOAD, CLEAR) • Graph management (CREATE, DROP, COPY, MOVE, ADD) • SPARQL 1.1 Entailment for RDF, RDFS, OWL, RIF • SPARQL 1.1 Service Descriptions Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  44. 44. 44 SPARQL 1.1 -- Assignments • SPARQL 1.1 allows the creation of new values in a query PREFIX ex: <http://example.org/> SELECT ?Item (?Pr * 1.1 AS ?NewP ) WHERE { ?Item ex:price ?Pr } Data @prefix ex: <http://example.org/> . Result ?Item ?NewP ex:lemonade1 ex:price 3 . ex:beer1 ex:price 3. lemonade1 3.3 ex:wine1 ex:price 3.50 . beer1 3.3 ex:liqueur1 ex:price "n/a". wine1 3.85 liqueur1 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  45. 45. 45 SPARQL 1.1 -- Aggregate Functions • SPARQL 1.1 allows the use of aggregate functions in queries PREFIX ex: <http://example.org/> SELECT (Count(?Item) AS ?C) WHERE { ?Item ex:price ?Pr } Data Result @prefix ex: <http://example.org/> . ?C ex:lemonade1 ex:price 3 ; rdf:type ex:Softdrink. 5 ex:beer1 ex:price 3; rdf:type ex:Beer. ex:wine1 ex:price 3.50 ; rdf:type ex:Wine. ex:wine2 ex:price 4 . rdf:type ex:Wine. ex:wine3 ex:price "n/a”; rdf:type ex:Wine. Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  46. 46. 46 SPARQL 1.1 -- Aggregate Functions • SPARQL 1.1 allows the use of aggregate functions in queries PREFIX ex: <http://example.org/> SELECT (Count(DISTINCT ?T) AS ?C) WHERE { ?Item rdf:type ?T } Data Result @prefix ex: <http://example.org/> . ?C ex:lemonade1 ex:price 3 ; rdf:type ex:Softdrink. 3 ex:beer1 ex:price 3; rdf:type ex:Beer. ex:wine1 ex:price 3.50 ; rdf:type ex:Wine. ex:wine2 ex:price 4 . rdf:type ex:Wine. ex:wine3 ex:price "n/a”; rdf:type ex:Wine. Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  47. 47. 47 SPARQL 1.1 -- Aggregate Functions • SPARQL 1.1 allows the use of aggregate functions in queries PREFIX ex: <http://example.org/> SELECT ?T (Count(?Item) AS ?C) WHERE { ?Item rdf:type ?T } GROUP BY ?T Data Result @prefix ex: <http://example.org/> . ?T ?C ex:lemonade1 ex:price 3 ; rdf:type ex:Softdrink. Softdrink 1 ex:beer1 ex:price 3; rdf:type ex:Beer. Beer 1 ex:wine1 ex:price 3.50 ; rdf:type ex:Wine. Wine 3 ex:wine2 ex:price 4 . rdf:type ex:Wine. ex:wine3 ex:price "n/a”; rdf:type ex:Wine. Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  48. 48. 48 SPARQL 1.1 -- Aggregate Functions • SPARQL 1.1 allows the use of aggregate functions in queries PREFIX ex: <http://example.org/> SELECT ?T (Count(?Item) AS ?C) WHERE { ?Item rdf:type ?T } GROUP BY ?T HAVING Count(?Item) > 1 Data Result @prefix ex: <http://example.org/> . ?T ?C ex:lemonade1 ex:price 3 ; rdf:type ex:Softdrink. Wine 3 ex:beer1 ex:price 3; rdf:type ex:Beer. ex:wine1 ex:price 3.50 ; rdf:type ex:Wine. ex:wine2 ex:price 4 . rdf:type ex:Wine. ex:wine3 ex:price "n/a”; rdf:type ex:Wine. Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  49. 49. 49 SPARQL 1.1 -- Aggregate Functions • more aggregate functions • SUM • AVG • MIN • MAX • SAMPLE -- „pick“ one non-deterministically • GROUP_CONCAT -- concatenate values with a designated string separator Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  50. 50. SPARQL 1.1 -- Aggregate Functions50 • Example for SAMPLE, GROUP_CONCAT PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ( SAMPLE(?N) as ?Name) ( GROUP_CONCAT(?M; SEPARATOR = ", ") AS ?Nicknames ) WHERE { ?P a foaf:Person ; foaf:name ?N ; foaf:nick ?M . } GROUP BY ?P @prefix ex: <http://example.org/> . Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . ex:alice a foaf:Person; foaf:name "Alice Wonderland"; foaf:nick "Alice", "The real Alice". Result ex:bob a foaf:Person; foaf:name "Robert Doe", "Robert Charles Doe", ?Name ?Nicknames "Robert C. Doe"; foaf:nick "Bob","Bobby","RobC","BobDoe". Alice Wonderland The real Alice, Alice ex:charles a foaf:Person; foaf:name "Charles Charles"; Charles Charles Charlie foaf:nick "Charlie" . Robert C. Doe Bob, BobDoe, RobC, Bobby Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  51. 51. SPARQL 1.1 -- Subqueries51 • Subqueries are a way to embed SPARQL queries within other queries SELECT ?T WHERE { ?D foaf:maker ?P ; rdfs:label ?T . { SELECT DISTINCT ?P WHERE { ?D foaf:maker <http://dblp.l3s.de/…/authors/Harald_Sack>, ?P . FILTER ( ?P != <http://dblp.l3s.de/…/authors/Harald_Sack> ) } LIMIT 10 } } • returns titles for 10 persons, instead of just 10 rows • result is achieved by first evaluating the inner query Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  52. 52. SPARQL 1.1 -- Negation52 • Filtering of query solutions is done within a FILTER expression using NOT EXISTS and EXISTS. PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?person WHERE { ?person rdf:type foaf:Person . FILTER NOT EXISTS { ?person foaf:name ?name } } Data @prefix : <http://example/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . :alice rdf:type foaf:Person . :alice foaf:name "Alice" . :bob rdf:type foaf:Person . Result ?person :bob Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  53. 53. SPARQL 1.1 -- Negation53 • Filtering of query solutions be removing possible solutions with MINUS. PREFIX : <http://example/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT ?s WHERE { ?s ?p ?o . MINUS { ?s foaf:givenName "Bob" . } } Data @prefix : <http://example/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . Result :alice foaf:givenName "Alice" ; ?s foaf:familyName "Smith" . :bob foaf:givenName "Bob" ; :alice foaf:familyName "Jones" . :carol :carol foaf:givenName "Carol" ; foaf:familyName "Smith" . Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  54. 54. SPARQL 1.1 -- Property Paths • A property path is a possible route through an RDF graph54 between two graph nodes. • trivial case: property path of length 1, i.e. a triple pattern • alternatives: match one or both possibilities { :book1 dc:title|rdfs:label ?displayString } • sequence: property path of length >1 { ?x foaf:mbox <mailto:alice@example> . ?x foaf:knows/foaf:knows/foaf:name ?name . } • inverse property paths: reversing the direction of the triple { ?x foaf:mbox <mailto:alice@example> } = { <mailto:alice@example> ^foaf:mbox ?x } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  55. 55. SPARQL 1.1 -- Property Paths55 • inverse path sequences paths { ?x foaf:knows/^foaf:knows ?y . FILTER(?x != ?y) } • arbitrary length match { ?x foaf:mbox <mailto:alice@example> . ?x foaf:knows+/foaf:name ?name . } • negated property paths { ?x !(rdf:type|^rdf:type) ?y } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  56. 56. SPARQL 1.1 -- Property Paths56 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  57. 57. SPARQL 1.1 -- Property Paths57 • can be used for some ontological inference • e.g. „find all beers in the beer ontology“ PREFIX beer: <http://www.purl.org/net/ontology/beer#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?beer FROM <http://www.purl.org/net/ontology/beer> WHERE { ?beer rdf:type/rdfs:subClassOf* beer:Beer . } Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  58. 58. SPARQL 1.1 -- New Features • SPARQL 1.1 Query:58 • Assignments (e.g. BIND, SELECT expressions) • Aggregate functions (e.g. COUNT, SUM, AVG) • Subqueries • Negation (EXISTS, NOT EXISTS, MINUS) come ... • Property paths re to • Basic query federation (SERVICE, BINDINGS) mo • SPARQL 1.1 Update: • Graph update (INSERT DATA, DELETE DATA, INSERT, DELETE, DELETE WHERE, LOAD, CLEAR) • Graph management (CREATE, DROP, COPY, MOVE, ADD) • SPARQL 1.1 Entailment for RDF, RDFS, OWL, RIF • SPARQL 1.1 Service Descriptions Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  59. 59. Semantic Web Technologies Content Sho 2. Semantic Web Basic Architecture59 rt The Languages of the Semantic Web - Part 1 • Exc Uniform Resource Identifier - URI • Tri Recapitulation: XML and XMLSchema urs • ple Resource Description Framework - RDF • RDF Schema Sto ion : • How Do We Get Semantics into WWW? res • µFormats, schema.org, RDFa and GRDDL • Query Language SPARQL • Why RDF(S) is not enough Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  60. 60. 60 How in RDBMS RDF to store RDF Data? • RDF Graph can be represented by a set of Triples (s,p,o) • Triples can simply be stored in a relational database system • Idea: Use a specific relational schema for RDF data and benefit from 40 years of research in the DB community • 3 steps for SPARQL query processing: (1) Convert SPARQL query to SQL query (w.r.t. the schema) (2) Use RDBMS to answer SQL query (3) Generate SPARQL query result from SQL query result Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  61. 61. RDF in RDBMS61 • RDF Graph can be represented by a set of Triples (s,p,o) • Triples can simply be stored in a relational database management system (RDBMS) • Motivation: Use a specific relational schema for RDF data and benefit from 40 years of research in the DB community • 3 steps for SPARQL query processing: (1) Convert SPARQL query to SQL query (w.r.t. the schema) (2) Use RDBMS to answer SQL query (3) Generate SPARQL query result from SQL query result Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  62. 62. Problem62 • How to store RDF triples to carry out efficient SPARQL queries? • 4 alternatives: 1.Giant Triple Storage 2.Property Tables 3.Vertically Partitioned Tables (Binary Tables) 4.Hexastore Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  63. 63. Giant Triple Storage • Basic idea:63 • Store all RDF triples in a single table • performance depends on efficient indexing Pros: • easy to implement • works for huge numbers of properties, if indexes are chosen with care Cons: • many self joins Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  64. 64. Giant Triple Storage • Basic idea:64 • Store all RDF triples in a single table • performance depends on efficient indexing SPARQL SELECT ?university WHERE { ?v rdf:type :GradStudent; :bachelorsFrom ?university . } SELECT t2.o AS university SQL FROM triples AS t1, triples AS t2 WHERE t1.p=‘type‘ AND t1.o=‘GradStudent‘ AND t2.p=‘bachelorsFrom‘ AND t1.s=t2.s Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  65. 65. ID Based Triple Storage65 • Use numerical identifier for each RDF term in the dataset • saves space and enhances efficiency RDF Term ID s p o :ID1 1 1 2 3 rdf:type 2 1 4 5 :FullProfessor 3 1 6 7 :teacherOf 4 1 8 9 “AI“ 5 1 10 11 :bachelorsFrom 6 “MIT“ 7 12 13 14 :mastersFrom 8 12 15 7 “Cambridge 9 12 4 16 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  66. 66. Quad Tables66 • Storing multiple RDF graphs • used for provenance, versioning, contexts, etc. RDF Term ID g s p o :ID1 1 100 1 2 3 rdf:type 2 100 1 4 5 :FullProfessor 3 101 12 16 7 :teacherOf 4 100 1 8 9 “AI“ 5 102 23 21 11 :bachelorsFrom 6 “MIT“ 7 100 1 6 7 :mastersFrom 8 102 23 22 8 “Cambridge 9 101 12 4 16 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  67. 67. Property Tables67 • Combining all (or some) properties of similar subjects in n-ary tables • Use ID based encoding for efficiency Pros: Cons: • Fewer joins • Potentially a lot of NULLs • If the data is structured, we • Clustering is not trivial have a relational DB • Multi-value properties are complicated Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  68. 68. Vertically Partitioned Tables (Binary Tables)68 • for each unique property create a two column table • Use ID based encoding for efficiency Pros: • Supports multi-value properties • No NULLs • Read only needed attributes (i.e. less I/O) • No clustering • Excellent performance (if number of properties is small, queries with bounded properties) Cons: • Expensive inserts • Bad performance (large number of properties, queries with unbounded properties) Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  69. 69. Vertically Partitioned Tables (Binary Tables)Row Store vs. physical storage Store relational DBs 69 • Different Column models for • Row Based Storage Different physical storage models are stored consecutively • Tuples (i.e. DB records) for relational DBs Row based storage: row needs to be read even if few • Entire attributes are projected ● Tuples (i.e. DB records) are stored consecutively ● Entire row needs to be read even if•few attributes Storage Column Based are projected • Read columns relevant to the query → projection is free Column based storage: • Inserts are expensive ● Read columns relevant to the 1 2 1 2 1 2 query → projection is free 3 3 3 ● Inserts are expensiveSack, Hasso-Plattner-Institut, Universität Potsdam Semantic Web Technologies , Dr. Harald Dienstag, 6. November 12
  70. 70. Hexastores70 • Create an index for every possible combination to enable efficient processing • spo, pos, osp, sop, pso, ops Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  71. 71. Hexastores71 • Create an index for every possible combination to enable efficient processing • spo, pos, osp, sop, pso, ops Pros: • fast joins (in the beginning) Cons: • 5 times more storage • weak performance, when disk access is necessary Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  72. 72. Semantic Web Technologies Content72 2. Semantic Web Basic Architecture The Languages of the Semantic Web - Part 1 2.1. Uniform Resource Identifier - URI 2.2. Recapitulation: XML and XMLSchema 2.3. Resource Description Framework - RDF 2.4. RDF Schema 2.5. How Do We Get Semantics into WWW? 2.6. Query Language SPARQL 2.7. Why RDF(S) is not enough Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  73. 73. 2. Semantic Web Basisarchitektur 2.7 Warum RDF noch nicht ausreicht73 Is there more in the World than RDF(S)? Vorlesung Semantic Web Technologien, Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  74. 74. 2.7 Why RDF(S) is not enough74 Locality of global properties subClassOf Vegetables Domain Range Animal eats Food subClassOf Meat Problem: Cows only eat vegetables Other animals also eat meat. Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  75. 75. 2.7 Why RDF(S) is not enough Disjunctive Classes75 subClassOf Woman Human ≠ Man subClassOf Problem: Subclass relation cannot express disjunctive class (subclass) membership Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  76. 76. 2.7 Why RDF(S) is not enough Class Combinations76 subClassOf Motorist subClassOf Motorcyclist Road User subClassOf Pedestrian subClassOf Cyclist Problem: Combination of classes define a new class. New class contains only members from given class combinations. Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  77. 77. 2.7 Why RDF(S) is not enough Cardinality Restrictions77 hasParent Human Parent Problem: Every human (usually) has two partents Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  78. 78. 2.7 Why RDF(S) is not enough78 Special Property Constraints • Transitivity (e.g. „is greater than“) • Uniqueness (e.g. „is mother of“) • Inversiveness (e.g. „is parent of“ and „is child of“) essiv ity! ntic expr more sema We need Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  79. 79. 2. Semantic Web Basic Architecture The Languages of the Semantic Web - Part 179 Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  80. 80. Semantic Web Technologies Content80 2. Semantic Web Basic Architecture The Languages of the Semantic Web - Part 1 2.1. Uniform Resource Identifier - URI 2.2. Recapitulation: XML and XMLSchema 2.3. Resource Description Framework - RDF 2.4. RDF Schema 2.5. How Do We Get Semantics into WWW? 2.6. Query Language SPARQL 2.7. Why RDF(S) is not enough Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  81. 81. next Lecture:81 e s g i l o to O n Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  82. 82. 2. Semantic Web Basic Architecture 2.6 Query Language SPARQL 2.7. Why RDF(S) is not enough82 Bibliography • P. Hitzler, S. Roschke, Y. Sure: Semantic Web Grundlagen, Springer, 2007. • P. Hitzler, M. Krötzsch, S. Rudolph: Foundations of Semantic Web Technologies, CRC Press, 2009. Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  83. 83. 2. Semantic Web Basic Architecture 2.6 Query Language SPARQL 2.7. Why RDF(S) is not enough83 • B. du Charme: Learning SPARQL, O‘Reilly Media, 2011. Acknowledgements: Thanks a lot to Olaf Hartig and Axel Polleres, who I stole some examples from... Semantic Web Technologies , Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12
  84. 84. 2. Semantic Web Basic Architecture 2.6 Query Language SPARQL 2.7. Why RDF(S) is not enough84 □Blog http://semweb2013.blogspot.com/ □Webseite http://www.hpi.uni-potsdam.de/studium/ lehrangebot/itse/veranstaltung/ semantic_web_technologien-3.html □bibsonomy - Bookmarks http://www.bibsonomy.org/user/lysander07/ swt1213_04 Lecture Semantic Web Technologies, Dr. Harald Sack, Hasso-Plattner-Institut, Universität PotsdamDienstag, 6. November 12

×