1 / 67

Chapter 3 Querying the Semantic Web

Chapter 3 Querying the Semantic Web. Grigoris Antoniou Paul Groth Frank van Harmelen Rinke Hoekstra. Lecture Outline. SPARQL Infrastructure Basics: Matching Patterns Filters Constructs for Dealing with an Open World Organizing Result Sets Other forms of SPARQL Queries Querying Schemas

darrin
Download Presentation

Chapter 3 Querying the Semantic Web

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3Querying the Semantic Web Grigoris Antoniou Paul Groth Frank van Harmelen Rinke Hoekstra A Semantic Web Primer

  2. Lecture Outline • SPARQL Infrastructure • Basics: Matching Patterns • Filters • Constructs for Dealing with an Open World • Organizing Result Sets • Other forms of SPARQL Queries • Querying Schemas • Adding Information with SPARQL Update • The Follow Your Nose Principle • Summary 2 A Semantic Web Primer

  3. SPARQL Infrastructure • To perform a SPARQL query, one needs software to execute the query • The most common software that does this is called a triple store―a database for RDF • Within the specifications for SPARQL, a triple store is termed as a Graph Store Chapter 3 A Semantic Web Primer

  4. Populating Triple Stores with RDF • A triple store needs to be populated with RDF before one can query the triple store • There is a mechanism called SPARQL Update that provides options for inserting and loading RDF into a triple store as well as deleting RDF from a triple store Chapter 3 A Semantic Web Primer

  5. SPARQL Endpoints • One can query a triple store by sending SPARQL queries once data is loaded into the store • Each triple store provides an endpoint to which SPARQL queries can be submitted • Clients can send queries to an endpoint using the HTTP protocol Chapter 3 A Semantic Web Primer

  6. SPARQL Endpoints on the Web • SPARQL uses standard Web technologies, so you will find numerous SPARQL endpoints on the Web • These endpoints provide access to large amounts of data • e.g. dbpedia.org/sparql provides an endpoint to query over an RDF representation of Wikipedia • A complete list of SPARQL endpoints at CKAN.org Chapter 3 A Semantic Web Primer

  7. Lecture Outline SPARQL Infrastructure Basics: Matching Patterns Filters Constructs for Dealing with an Open World Organizing Result Sets Other forms of SPARQL Queries Querying Schemas Adding Information with SPARQL Update The Follow Your Nose Principle Summary 7 Chapter 3 A Semantic Web Primer

  8. A Previous Example of RDF @prefix swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. @prefix dbpedia: <http://dbpedia.org/resource/>. @prefix dbpedia-owl: <http://dbpedia.org/ontology/>. swp:BaronWayApartment swp:hasNumberOfBedrooms 3; swp:isPartOf swp:BaronWayBuilding. swp:BaronWayBuilding dbpedia-owl:location dbpedia:Amsterdam, dbpedia:Netherlands. Chapter 3 A Semantic Web Primer

  9. Expressing a Query in SPARQL • We might like to ask a query over this data • for instance, find the location of the Baron Way building • We would want to match the following triples: swp:BaronWayBuilding dbpedia-owl:location dbpedia:Amsterdam. swp:BaronWayBuilding dbpedia-owl:location dbpedia:Netherlands. Chapter 3 A Semantic Web Primer

  10. Expressing a Query in SPARQL • We can just replace any component of the triple with a variable • Variables are denoted by a preceding ? mark • We can introduce a variable for location and write the following triple pattern swp:BaronWayBuilding dbpedia-owl:location ?location. Chapter 3 A Semantic Web Primer

  11. Executing a SPARQL Query • The triple store will take this triple pattern, or graph pattern, and try to find set of triples that match the pattern • In this case, what the triple store would return? • dbpedia:Amsterdam and dbpedia:Netherlands • The triple store finds all triples with swp:BaronWayBuilding as the subject and dbpedia-owl:location as the predicate Chapter 3 A Semantic Web Primer

  12. Making a Complete SPARQL Query • First, all prefixes need to be defined PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. • The PREFIX keyword denotes various abbreviations for URLs • We also need to tell the triple store that we are interested in particular variables in the results • ?location Chapter 3 A Semantic Web Primer

  13. Making a Complete SPARQL Query • A complete SPARQL would look like PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT ?location WHERE { swp:BaronWayBuilding dbpedia-owl:location ?location. } • The SELECT keyword indicates which variables are of interest • The graph pattern that needs to be matched appears with brackets after the WHERE keyword Chapter 3 A Semantic Web Primer

  14. Results of a SPARQL Query • The results of the query are returned in a set of mappings called bindings • Bindings denote which elements correspond to a given variable • Each row in the table is a single result or binding ?location http://dbpedia.org/resource/Amsterdam. http://dbpedia.org/resource/Netherlands. Chapter 3 A Semantic Web Primer

  15. Basis of SPARQL • The whole basis of SPARQL is this simple notion of trying to find sets of triples that match a given graph pattern • SPARQL provides functionality for specifying more complex graph patterns and providing results in different formats • The same procedure applies Chapter 3 A Semantic Web Primer

  16. Another Example SPARQL Query • Find where the BaronWayApartment is located PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT ?location WHERE { swp:BaronWayApartment swp:isPartOf ?building. ?building dbpedia-owl:location ?location. } Chapter 3 A Semantic Web Primer

  17. Another Example SPARQL Query SELECT ?location WHERE { swp:BaronWayApartment swp:isPartOf ?building. ?building dbpedia-owl:location ?location. } • The graph pattern has been extended • The variable is also in the subject position • can occurs in any position in the SPARQL query • The query reuses the variable name ?building Chapter 3 A Semantic Web Primer

  18. Another Example SPARQL Query swp:BaronWayApartment swp:hasNumberOfBedrooms 3; swp:isPartOf swp:BaronWayBuilding. swp:BaronWayBuilding dbpedia-owl:location dbpedia:Amsterdam, dbpedia:Netherlands. SELECT ?location WHERE { swp:BaronWayApartment swp:isPartOf ?building. ?building dbpedia-owl:location ?location. } Chapter 3 A Semantic Web Primer

  19. ?p ?o swp:hasNumberOfBedrooms 3 swp:isPartOf swp:BaronWayBuilding Matching More Than One Single Variable in SPARQL Queries • We might want to find all the information about Baron Way Apartment in the triple store SELECT ?p ?o WHERE { swp:BaronWayApartment ?p ?o. } Chapter 3 A Semantic Web Primer

  20. Limiting the Number of Answers to a SPARQL Query • For small datasets, all possible answers can be easily returned • For larger datasets, we may not know how many results there are or if the query would return a whole dataset • It is good practice to limit the number of answers a query would return by using the LIMIT keyword Chapter 3 A Semantic Web Primer

  21. Using the LIMIT Keyword SELECT ?p ?o WHERE { swp:BaronWayApartment ?p ?o. } LIMIT 10 • We limit the number of results to be returned to ten Chapter 3 A Semantic Web Primer

  22. Property Paths • SPARQL provides a way of expressing concisely chains of properties • This facility is called property paths • There are a number of property paths that can be used to help express long or arbitrary paths in SPARQL queries Chapter 3 A Semantic Web Primer

  23. An Example of Property Paths • Find all the apartments which are part of a building in Amsterdam SELECT ?apartment WHERE { ?apartment swp:isPartOf ?building. ?building dbpedia-owl:location dbpedia:Amsterdam. } Chapter 3 A Semantic Web Primer

  24. An Example of Property Paths • We can express the same thing as SELECT ?apartment WHERE { ?apartment swp:isPartOf/dbpedia-owl:location dbpedia:Amsterdam. } Chapter 3 A Semantic Web Primer

  25. Lecture Outline SPARQL Infrastructure Basics: Matching Patterns Filters Constructs for Dealing with an Open World Organizing Result Sets Other forms of SPARQL Queries Querying Schemas Adding Information with SPARQL Update The Follow Your Nose Principle Summary 25 Chapter 3 A Semantic Web Primer

  26. Filters Sometimes we want to put more complex constraints on the results of queries Those constraints can be expressed using filters For example, we would want to find apartments with more or less than a certain number of bedrooms We can ask this question in SPARQL using the FILTER keyword 26 Chapter 3 A Semantic Web Primer

  27. Using Filters SELECT ?apartment WHERE { ?apartment swp:hasNumberOfBedrooms ?bedrooms. FILTER (?bedrooms>2). } ?apartment swp:BaronWayAparment 27 Chapter 3 A Semantic Web Primer

  28. Numeric Filters and String Filters Less than, greater than, and equality are supported for numeric data types as well as date/time SPARQL allows for filtering on strings e.g. assume that the dataset contains the triple: swp:BaronWayApartment swp:address “4 Baron Way Circle”. We might want to find all the resources that contain “4 Baron Way” in their address 28 Chapter 3 A Semantic Web Primer

  29. Regular Expressions We can use regular expressions which are supported within SPARQL Regular expressions are a powerful of expressing string searches The regular expression for find the string “4 Baron Way” at the beginning of another string is “^4 Baron Way” 29 Chapter 3 A Semantic Web Primer

  30. An Example of Using Regular Expressions in SPARQL Filters SELECT ?apartment WHERE { ?apartment swp:address ?address. FILTER regex (?address, “^4 Baron Way”). } A specific filter function name is introduced, regex, after the FILTER keyword Parameters are given in the parentheses afterwards 30 Chapter 3 A Semantic Web Primer

  31. The str Function The str function will convert resources and literals into string representations that can then be used in regex e.g. we can search for Baron in the URL of the resource SELECT ?apartment ?address WHERE { ?apartment swp:address ?address. FILTER regex(str(?apartment), “Baron”). } 31 Chapter 3 A Semantic Web Primer

  32. Lecture Outline SPARQL Infrastructure Basics: Matching Patterns Filters Constructs for Dealing with an Open World Organizing Result Sets Other forms of SPARQL Queries Querying Schemas Adding Information with SPARQL Update The Follow Your Nose Principle Summary 32 Chapter 3 A Semantic Web Primer

  33. Open World Assumption Every resource on the Semantic Web may not be described using the same schema or does not have to share the same set of properties unlike in the traditional database This is called the open world assumption For instance, some apartments may be more well described than others or they may be described using different ontologies 33 Chapter 3 A Semantic Web Primer

  34. An Example of Open World Assumption swp:BaronWayApartment swp:hasNumberOfBedrooms 3. swp:BaronWayApartment dbpedia-owl:location dbpedia:Amsterdam. swp:BaronWayApartment rdfs:label “Baron Way Apartment for Rent”. swp:FloridaAveStudio swp:hasNumberOfBedrooms 1. swp:FloridaAveStudio dbpedia-owl:locationCity dbpedia:Amsterdam. The Florida Ave studio does not have a human-friendly label Its location is described using dbpedia-owl:locationCity instead of dbpedia-owl:location 34 Chapter 3 A Semantic Web Primer

  35. Constructs for Dealing with an Open World Even with this inconsistency, we would still like to query over the data We want to find the apartments located in Amsterdam and return their human-friendly label if they have one SPARQL provides two constructs for expressing such a query: UNION and OPTIONAL 35 Chapter 3 A Semantic Web Primer

  36. Constructs for Dealing with an Open World SELECT ?apartment ?label WHERE { {?apartment dbpedia-owl:location dbpedia:Amsterdam.} UNION {?apartment dbpedia-owl:locationCity dbpedia:Amsterdam.} OPTIONAL {?apartment rdfs:label ?label.} } ?apartment ?label swp:BaronWayApartment Baron Way Apartment for Rent swp:FloridaAveStudio 36 Chapter 3 A Semantic Web Primer

  37. Constructs for Dealing with an Open World The UNION keyword tells the triple store to return results that match one or both graph patterns The OPTIONAL keyword tells the triple store to return results for a particular graph pattern if available The graph pattern does not have to be satisfied for the query to return Without the optional graph pattern, the Florida Ave studio would not have been returned 37 Chapter 3 A Semantic Web Primer

  38. Constructs for Dealing with an Open World Property paths can also be used to create a more concise SPARQL query by using | operator SELECT ?apartment ?label WHERE { {?apartment dbpedia-owl:location|dbpedia-owl:locationCity dbpedia:Amsterdam.} OPTIONAL {?apartment rdfs:label ?label.} } 38 Chapter 3 A Semantic Web Primer

  39. Lecture Outline SPARQL Infrastructure Basics: Matching Patterns Filters Constructs for Dealing with an Open World Organizing Result Sets Other forms of SPARQL Queries Querying Schemas Adding Information with SPARQL Update The Follow Your Nose Principle Summary 39 Chapter 3 A Semantic Web Primer

  40. Organizing Result Sets We often want the query result to be returned in a particular way grouped, counted, or ordered SPARQL provides a number of functions to help organize the query result, for instance, LIMIT We can eliminate duplicate results from the result set using the DISTINCT keyword e.g. SELECT DISTINCT ?name WHERE 40 Chapter 3 A Semantic Web Primer

  41. ORDER BY Keyword SPARQL enables the ordering of a returned result set using the ORDER BY keyword e.g. we can ask for the apartments ordered by the number of bedrooms SELECT ?apartment ?bedrooms WHERE { ?apartment swp:hasNumberOfBedrooms ?bedrooms. } ORDER BY DESC(?bedrooms) 41 Chapter 3 A Semantic Web Primer

  42. ORDER BY Keyword The keyword DESC denotes descending order and ASC denotes ascending order Ordering strings or urls is done alphabetically ?apartment ?bedrooms swp:BaronWayApartment 3 swp:FloridaAveStudio 1 42 Chapter 3 A Semantic Web Primer

  43. Collecting Results Using Aggregate Functions Aggregates are used where the querier wishes to see a result which is computed over a group of results We can count the number of results using COUNT We can get asum over the results using SUM, We can also compute the minimum, maximum, and average of the result set using MIN, MAX, and AVG 43 Chapter 3 A Semantic Web Primer

  44. Collecting Results Using Aggregate Functions An example to compute the average number of bedrooms in the dataset SELECT (AVG(?bedrooms) AS ?avgNumRooms) WHERE { ?apartment swp:hasNumberOfBedrooms ?bedrooms. } ?avgNumRooms 2 44 Chapter 3 A Semantic Web Primer

  45. Collecting Results Using Aggregate Functions The aggregate function AVG is combined with the keyword AS to denote the variable in the result set We are not limited to applying these aggregations over the entire result set We can also aggregate for particular groups using the GROUP BY keyword 45 Chapter 3 A Semantic Web Primer

  46. Lecture Outline SPARQL Infrastructure Basics: Matching Patterns Filters Constructs for Dealing with an Open World Organizing Result Sets Other forms of SPARQL Queries Querying Schemas Adding Information with SPARQL Update The Follow Your Nose Principle Summary 46 Chapter 3 A Semantic Web Primer

  47. ASK Form So far we have focused on selecting certain values from a set of RDF data SPARQL also supports other forms of queries: ASK and CONSTRUCT The ASK form simply checks whether a graph pattern exists in a dataset instead of returning a result set (the following one would return true) ASK { ?apartment swp:hasNumberOfBedrooms 3.} 47 Chapter 3 A Semantic Web Primer

  48. CONSTRUCT Form The CONSTRUCT form is used to retrieve an RDF graph from a larger set of RDF Thus, one can query a triple store and retrieve an RDF graph other than a list of variable bindings For instance, we can create a new graph that labels big apartments as those having more than 2 bedrooms 48 Chapter 3 A Semantic Web Primer

  49. An Example of CONSTRUCT Form PREFIX ex: <http://www.example.org/> PREFIX dbpedia: <http://dbpedia.org/resource/> PREFIX geo: <http://www.geonames.org/ontology#> CONSTRUCT { ?apartment swp:hasNumberOfBedrooms ?bedrooms. ?apartment swp:isBigApartment true.} WHERE { ?apartment swp:hasNumberOfBedrooms ?bedrooms. FILTER (?bedrooms > 2) } 49 Chapter 3 A Semantic Web Primer

  50. An Example of CONSTRUCT Form The query would return the following graph: @prefix swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. @prefix dbpedia: <http://dbpedia.org/resource/>. @prefix dbpedia-owl: <http://dbpedia.org/ontology/>. @prefix xsd: <http://www.w3.org/2001/XMLSchema#>. swp:BaronWayApartment swp:hasNumberOfBedrooms 3. swp:BaronwayApartment swp:isBigApartment true. 50 Chapter 3 A Semantic Web Primer

More Related