sparql and the open linked data initiative

Download SPARQL and the Open Linked Data initiative

If you can't read please download the document

Upload: fulvio-corno

Post on 08-May-2015

6.117 views

Category:

Education


4 download

DESCRIPTION

An introduction to the SPARQL query language and its application to the Open Linked Data initiative. Slides for the PhD Course on Semantic Web (http://elite.polito.it/).

TRANSCRIPT

  • 1.SPARQL - QueryLanguage for RDFFulvio Corno, Laura Farinetti Politecnico di Torino Dipartimento di Automatica e Informatica e-Lite Research Group http://elite.polito.it

2. The new Semantic Web visionTo make data machine processable, we need:Unambiguous names for resources (that may alsobind data to real world objects): URIsA common data model to access, connect, describethe resources: RDFAccess to that data: SPARQLDefine common vocabularies: RDFS, OWL, SKOSReasoning logics: OWL, RulesSPARQL will make a huge difference (TimBerners-Lee, May 2006) F. Corno, L. Farinetti- Politecnico di Torino 2 3. SPARQLQueries are very important for distributed RDFdataComplex queries into the RDF data are oftennecessaryE.g.: give me the (a,b) pair of resources, for whichthere is an x such that (x parent a) and (b brother x)holds (i.e., return the uncles)This is the goal of SPARQL (Query Languagefor RDF)F. Corno, L. Farinetti- Politecnico di Torino 3 4. SPARQLW3C Recommendation: January 15th, 2008SPARQL queries RDF graphsAn RDF graph is a set of triplesSPARQL can be used to express queries acrossdiverse data sources, whether the data is storednatively as RDF or viewed as RDF viamiddlewareF. Corno, L. Farinetti- Politecnico di Torino 4 5. SPARQL and RDFIt is the triples that matter, not theserializationRDF/XML is the W3C recommendation but itnot a good choice because it allows multipleways to encode the same graphSPARQL uses the Turtle syntax, anN-Triples extension F. Corno, L. Farinetti- Politecnico di Torino 5 6. Turtle - Terse RDF Triple LanguageA serialization format for RDFA subset of Tim Berners-Lee and DanConnollys Notation 3 (N3) languageUnlike full N3, doesnt go beyond RDFs graph modelA superset of the minimal N-Triples formatTurtle has no official status with any standardsorganization, but has become popular amongstSemantic Web developers as a human-friendlyalternative to RDF/XML F. Corno, L. Farinetti- Politecnico di Torino6 7. Triple or Turtle notation F. Corno, L. Farinetti- Politecnico di Torino 7 8. Triple or Turtle notation quot;Eric Millerquot; . . quot;Dr.quot; . . F. Corno, L. Farinetti- Politecnico di Torino8 9. Triple or Turtle notation (abbreviated) w3people:EM#me contact:fullName quot;Eric Millerquot; .w3people:EM#me contact:mailbox .w3people:EM#me contact:personalTitle quot;Dr.quot; .w3people:EM#me rdf:type contact:Person . F. Corno, L. Farinetti- Politecnico di Torino 9 10. Turtle - Terse RDF Triple LanguagePlain text syntax for RDFBased on UnicodeMechanisms for namespace abbreviationAllows grouping of triples according tosubjectShortcuts for collectionsF. Corno, L. Farinetti- Politecnico di Torino 10 11. Turtle - Terse RDF Triple LanguageSimple triple:subject predicate object . :john rdf:label quot;Johnquot; . :john rdf:label quot;Johnquot; . Grouping triples:subject predicate object ; predicate object ...:john:johnrdf:label quot;Johnquot; ;rdf:label quot;Johnquot; ;rdf:type ex:Person ;rdf:type ex:Person ;ex:homePage http://example.org/johnspage/ .ex:homePage http://example.org/johnspage/ . F. Corno, L. Farinetti- Politecnico di Torino11 12. PrefixesMechanism for namespace abbreviation@prefix abbr: @prefix abbr: Example: @prefix rdf: @prefix rdf: Default:@prefix : @prefix : Example: @prefix : @prefix : F. Corno, L. Farinetti- Politecnico di Torino12 13. IdentifiersURIs: http://www.w3.org/1999/02/22-rdf-syntax-ns# http://www.w3.org/1999/02/22-rdf-syntax-ns# Qnames (Qualified names)namespace-abbr?:localnamerdf:typerdf:typedc:titledc:title:hasName:hasNameLiteralsquot;stringquot;(@lang)?(type)? quot;Johnquot; quot;Johnquot; quot;Helloquot;@en-GB quot;Helloquot;@en-GB quot;1.4quot;^^xs:decimal quot;1.4quot;^^xs:decimal F. Corno, L. Farinetti- Politecnico di Torino 13 14. Blank nodesSimple blank node:[] or _:x:john ex:hasFather [] . :john ex:hasFather [] . :john ex:hasFather _:x . :john ex:hasFather _:x .Blank node as subject:[ predicate object ; predicate object ... ] . [ ex:hasName quot;Johnquot;] . [ ex:hasName quot;Johnquot;] . [ ex:authorOf :lotr ; [ ex:authorOf :lotr ; ex:hasName quot;Tolkienquot;] . ex:hasName quot;Tolkienquot;] .F. Corno, L. Farinetti- Politecnico di Torino14 15. Collections( object1 ... objectn ):doc1 ex:hasAuthor (:john :mary) . :doc1 ex:hasAuthor (:john :mary) .Short for :doc1 ex:hasAuthor :doc1 ex:hasAuthor[ rdf:first :john;[ rdf:first :john;rdf:rest [ rdf:first :mary;rdf:rest [ rdf:first :mary;rdf:rest rdf:nil ]rdf:rest rdf:nil ]].]. F. Corno, L. Farinetti- Politecnico di Torino15 16. Example @prefixrdf: http://www.w3.org/1999/02/22-rdf-syntaxns# . @prefixrdf: http://www.w3.org/1999/02/22-rdf-syntaxns# . @prefixdc: . @prefixdc: . @prefix: . @prefix: . dc:title quot;RDF/XML Syntax Specification (Revised)quot; ; dc:title quot;RDF/XML Syntax Specification (Revised)quot; ; :editor [ :editor [ :fullName quot;Dave Beckettquot;; :fullName quot;Dave Beckettquot;; :homePage :homePage ]. ]. F. Corno, L. Farinetti- Politecnico di Torino16 17. SPARQLUses SQL-like syntaxPrefix mechanism to abbreviate URIsPREFIX dc: PREFIX dc: SELECT ?title SELECT ?title WHERE { dc:title WHERE { dc:title ?title } ?title } Variables to be returnedQuery pattern (list of triple patterns)Name of the graph FROMF. Corno, L. Farinetti- Politecnico di Torino17 18. SELECTVariables selection?xVariables: ?string?x?title?title?name?nameSyntax: SELECT var1,,varnSELECT ?name SELECT ?name SELECT ?x,?title SELECT ?x,?title F. Corno, L. Farinetti- Politecnico di Torino 18 19. WHEREGraph patterns to matchSet of triples{ (subject predicate object .)* }Subject: URI, QName, Blank node, Literal,VariablePredicate: URI, QName, Blank node, VariableObject: URI, QName, Blank node, Literal,Variable F. Corno, L. Farinetti- Politecnico di Torino19 20. Graph patterns The pattern contains unbound symbolsBy binding the symbols (if possible),subgraphs of the RDF graph are selectedIf there is such a selection, the queryreturns the bound resourcesF. Corno, L. Farinetti- Politecnico di Torino 20 21. Graph patterns E.g.: (subject,?p,?o)?p and ?o are unknowns F. Corno, L. Farinetti- Politecnico di Torino 21 22. Graph patternsSELECT ?p ?o SELECT ?p ?o WHERE {subject ?p ?o} WHERE {subject ?p ?o} The triplets in WHERE define the graph pattern,with ?p and ?o unbound symbolsThe query returns a list of matching p,o pairs F. Corno, L. Farinetti- Politecnico di Torino 22 23. Example 1SELECT ?cat ?val SELECT ?cat ?val WHERE { ?x rdf:value ?val. WHERE { ?x rdf:value ?val. ?x category ?cat } ?x category ?cat }Returns: [[quot;Total Membersquot;,100],[quot;Total Membersquot;,200],, [[quot;Total Membersquot;,100],[quot;Total Membersquot;,200],, [quot;Full Membersquot;,10],] [quot;Full Membersquot;,10],] F. Corno, L. Farinetti- Politecnico di Torino 23 24. Example 2SELECT ?cat ?val SELECT ?cat ?val WHERE { ?x rdf:value ?val. WHERE { ?x rdf:value ?val. ?x category ?cat. ?x category ?cat. FILTER(?val>=200). } FILTER(?val>=200). } Returns:[[quot;Total Membersquot;,200],][[quot;Total Membersquot;,200],]F. Corno, L. Farinetti- Politecnico di Torino 24 25. Example 3SELECT ?cat ?val ?uri SELECT ?cat ?val ?uri WHERE { ?x rdf:value ?val. WHERE { ?x rdf:value ?val. ?x category ?cat. ?x category ?cat. ?al contains ?x. ?al contains ?x. ?al linkTo ?uri } ?al linkTo ?uri }Returns:[[quot;Total Membersquot;,100,Resource(http://...)],,][[quot;Total Membersquot;,100,Resource(http://...)],,]F. Corno, L. Farinetti- Politecnico di Torino25 26. Example 4 SELECT ?cat ?val ?uriSELECT ?cat ?val ?uriWHERE{ ?x rdf:value ?val.WHERE{ ?x rdf:value ?val. ?x category ?cat. ?x category ?cat.OPTIONAL ?al contains ?x.OPTIONAL ?al contains ?x. ?al linkTo ?uri } ?al linkTo ?uri }Returns:[[quot;Total Membersquot;,100,Resource(http://...)],[[quot;Total Membersquot;,100,Resource(http://...)],, [quot;Full Membersquot;,20, ],,], [quot;Full Membersquot;,20, ],,] F. Corno, L. Farinetti- Politecnico di Torino 26 27. Other SPARQL FeaturesLimit the number of returned resultsRemove duplicates, sort them,Specify several data sources (via URI-s) withinthe query (essentially, a merge)Construct a graph combining a separate patternand the query resultsUse datatypes and/or language tags whenmatching a patternF. Corno, L. Farinetti- Politecnico di Torino27 28. SPARQL use in practiceLocally, i.e., bound to a programmingenvironments like JenaJena is a Java framework for building Semantic Webapplications; provides an environment for RDF, RDFSand OWL, SPARQL and includes a rule b - asedinference engineRemotely, e.g., over the network or into adatabase F. Corno, L. Farinetti- Politecnico di Torino28 29. F. Corno, L. Farinetti- Politecnico di Torino 29 30. Tools for conversionGRDDL (Gleaning Resource Descriptions from Dialects ofLanguages)W3C Recommendation (Sep 11th, 2007)Enables users to obtain RDF triples out of XML documentsRDFa (Resource Description Framework-in-attributes)W3C RecommendationSet of extensions to XHTML that allows to annotate XHTMLmarkup with semanticsUses attributes from XHTML's meta and link elements, andgeneralizes them so that they are usable on all elementsA simple mapping is defined so that RDF triples may beextractedF. Corno, L. Farinetti- Politecnico di Torino 30 31. SPARQL query structureA SPARQL query includes, in orderPrefix declarations, for abbreviating URIsA result clause, identifying what information toreturn from the queryThe query pattern, specifying what to queryfor in the underlying datasetQuery modifiers: slicing, ordering, andotherwise rearranging query results F. Corno, L. Farinetti- Politecnico di Torino 31 32. SPARQL query structureA SPARQL query includes, in order# prefix declarations# prefix declarationsPREFIX foo: PREFIX foo: ......# result clause# result clauseSELECT ...SELECT ...# query pattern# query patternWHERE {WHERE {......}}# query modifiers# query modifiersORDER BY ...ORDER BY ... F. Corno, L. Farinetti- Politecnico di Torino32 33. Dataset: Friend of a Friend (FOAF)FOAF is a standard RDF vocabulary fordescribing people and relationshipsTim Berners-Lee's FOAF information availableat http://www.w3.org/People/Berners-Lee/card @prefix card: . @prefix card: . @prefix foaf: . @prefix foaf: . card:i foaf:name quot;Timothy Berners-Leequot; . card:i foaf:name quot;Timothy Berners-Leequot; . foaf:name quot;Henry Storyquot; . foaf:name quot;Henry Storyquot; . foaf:name quot;Lee Feigenbaumquot; . foaf:name quot;Lee Feigenbaumquot; . card:amy foaf:name quot;Amy van der Hielquot; . card:amy foaf:name quot;Amy van der Hielquot; . ... ... F. Corno, L. Farinetti- Politecnico di Torino33 34. Example 1 simple triple patternIn the graph http://www.w3.org/People/Berners-Lee/card, find all subjects (?person) and objects(?name) linked with the foaf:name predicate.Then return all the values of ?name.In other words, find all names mentioned in TimBerners-Lees FOAF file PREFIX foaf: PREFIX foaf: SELECT ?name SELECT ?name WHERE { WHERE { ?person foaf:name ?name . ?person foaf:name ?name . } } F. Corno, L. Farinetti- Politecnico di Torino 34 35. SPARQL endpointsAccept queries and returns results via HTTPGeneric endpoints queries any Web-accessible RDF dataSpecific endpoints are hardwired to query againstparticular datasetsThe results of SPARQL queries can be returned in avariety of formats:XML, JSON, RDF, HTMLJSON (JavaScript Object Notation): lightweight computerdata interchange format; text-based, human-readableformat for representing simple data structures andassociative arraysF. Corno, L. Farinetti- Politecnico di Torino35 36. SPARQL endpointsThis query is for an arbitrary bit of RDF data(Tim Berners-Lee's FOAF file)=> generic endpoint to run itPossible choicesHP's ARQ at sparql.orghttp://sparql.org/sparql.htmlOpenLink's Virtuoso (Make sure to choose quot;Retrieveremote RDF data for all missing source graphsquot;)http://demo.openlinksw.com/sparqlRedlands Rasqalhttp://librdf.org/query/F. Corno, L. Farinetti- Politecnico di Torino 36 37. OpenLinks Virtuoso Dataset SPARQL query F. Corno, L. Farinetti- Politecnico di Torino37 38. Example 1 - simple triple pattern PREFIX foaf:PREFIX foaf:SELECT ?nameSELECT ?nameWHERE {WHERE {?person foaf:name ?name .?person foaf:name ?name .}} F. Corno, L. Farinetti- Politecnico di Torino 38 39. Example 2 multiple triple patternFind all people in Tim Berners L - ees FOAF file that havenames and email addressesReturn each persons URI, name, and email address Multiple triple patterns retrieve multiple properties abouta particular resourceSELECT * selects all variables mentioned in the query PREFIX foaf: PREFIX foaf: SELECT * SELECT * WHERE { WHERE { ?person foaf:name ?name . ?person foaf:name ?name . ?person foaf:mbox ?email . ?person foaf:mbox ?email . } } F. Corno, L. Farinetti- Politecnico di Torino39 40. Example 2 - multiple triple pattern F. Corno, L. Farinetti- Politecnico di Torino 40 41. Example 3 traversing a graphFind the homepage of anyone known byTim Berners-Lee F. Corno, L. Farinetti- Politecnico di Torino 41 42. Example 3 traversing a graphPREFIX foaf: PREFIX foaf: PREFIX card: PREFIX card: SELECT ?homepageSELECT ?homepageFROM FROM WHERE {WHERE {card:i foaf:knows ?known .card:i foaf:knows ?known .?known foaf:homepage ?homepage .?known foaf:homepage ?homepage .}} The FROM keyword specifies the target graph inthe queryBy using ?known as an object of one triple and thesubject of another, it is possible to traversemultiple links in the graph F. Corno, L. Farinetti- Politecnico di Torino 42 43. Dataset: DBPediaDBPedia is an RDF version of informationfrom WikipediaContains data derived from Wikipediasinfoboxes, category hierarchy, articleabstracts, and various external linksContains over 100 million triplesDataset: http://dbpedia.org/ F. Corno, L. Farinetti- Politecnico di Torino 43 44. Example 4 exploring DBPediaFind 15 example concepts in the DBPediadatasetSELECT DISTINCT ?conceptSELECT DISTINCT ?conceptWHERE {WHERE {?s a ?concept .?s a ?concept .} LIMIT 15} LIMIT 15 F. Corno, L. Farinetti- Politecnico di Torino 44 45. Example 4 exploring DBPediaLIMIT is a solution modifier that limits thenumber of rows returned from a querySPARQL has two other solution modifiersORDER BY for sorting query solutions on the value ofone or more variablesOFFSET, used in conjunction with LIMIT and ORDERBY to take a slice of a sorted solution set (e.g. forpaging)The SPARQL keyword a is a shortcut for thecommon predicate rdf:type (class of a resource)The DISTINCT modifier eliminates duplicaterows from the query results F. Corno, L. Farinetti- Politecnico di Torino45 46. Example 5 basic SPARQL filtersFind all landlocked countries with a population greaterthan 15 millionPREFIX rdfs: PREFIX rdfs: PREFIX type: PREFIX type: PREFIX prop: PREFIX prop: SELECT ?country_name ?populationSELECT ?country_name ?populationWHERE {WHERE {?country a type:LandlockedCountries ;?country a type:LandlockedCountries ; rdfs:label ?country_name ; rdfs:label ?country_name ; prop:populationEstimate ?population . prop:populationEstimate ?population .FILTER (?population > 15000000) .FILTER (?population > 15000000) .}} FILTER constraints use boolean conditions to filter outunwanted query resultsA semicolon (;) can be used to separate two triplepatterns that share the same subject F. Corno, L. Farinetti- Politecnico di Torino46 47. SPARQL filtersConditions on literal valuesSyntaxFILTER expressionFILTER expressionExamples FILTER (?age > 30) FILTER (?age > 30) FILTER isIRI(?x) FILTER isIRI(?x) FILTER !BOUND(?y) FILTER !BOUND(?y)F. Corno, L. Farinetti- Politecnico di Torino47 48. SPARQL filtersBOUND(var)true if var is bound in query answerfalse, otherwise!BOUND(var) enables negation a f ilure - sa-Testing typesisIRI(A): A is an Internationalized ResourceIdentifierisBLANK(A): A is a blank nodeisLITERAL(A): A is a literal F. Corno, L. Farinetti- Politecnico di Torino48 49. SPARQL filters Comparison between A=BA=BA != BA != B RDF terms A =B A =B A != B A != B Comparison betweenA = B Numeric and Date typesA B A >BBoolean AND/ORA && B A && B A || B A || BA + B A + B Basic arithmeticA - B A - B A * B A * B A / B A / B F. Corno, L. Farinetti- Politecnico di Torino 49 50. Example 5 basic SPARQL filters Note all the translatedduplicates in the resultsHow can we deal withthat? F. Corno, L. Farinetti- Politecnico di Torino 50 51. Example 6 SPARQL filtersFind me all landlocked countries with apopulation greater than 15 million (revisited),with the highest population country first PREFIX type: PREFIX type: PREFIX prop: PREFIX prop: SELECT ?country_name ?population SELECT ?country_name ?population WHERE { WHERE { ?country a type:LandlockedCountries ; ?country a type:LandlockedCountries ;rdfs:label ?country_name ; rdfs:label ?country_name ;prop:populationEstimate ?population . prop:populationEstimate ?population . FILTER (?population > 15000000 && FILTER (?population > 15000000 && langMatches(lang(?country_name), quot;ENquot;)) .langMatches(lang(?country_name), quot;ENquot;)) . } ORDER BY DESC(?population) } ORDER BY DESC(?population)F. Corno, L. Farinetti- Politecnico di Torino51 52. Example 6 SPARQL filterslang extracts a literals language tag, if anylangMatches matches a language tag against alanguage range F. Corno, L. Farinetti- Politecnico di Torino 52 53. Dataset: JamendoJamendo is a community collection of music allfreely licensed under Creative Commonslicenseshttp://www.jamendo.com/it/DBTune.org hosts a queryable RDF version ofinformation about Jamendo's music collectionData on thousands of artists, tens of thousands ofalbums, and nearly 100,000 trackshttp://dbtune.org/ F. Corno, L. Farinetti- Politecnico di Torino 53 54. Example 7 the wrong wayFind all Jamendo artists along with their image,home page, and the location theyre near PREFIX mo: PREFIX mo: PREFIX foaf: PREFIX foaf: SELECT ?name ?img ?hp ?locSELECT ?name ?img ?hp ?locWHERE {WHERE {?a a mo:MusicArtist ;?a a mo:MusicArtist ; foaf:name ?name ; foaf:name ?name ; foaf:img ?img ; foaf:img ?img ; foaf:homepage ?hp ; foaf:homepage ?hp ; foaf:based_near ?loc . foaf:based_near ?loc .}} F. Corno, L. Farinetti- Politecnico di Torino54 55. Example 7 DBTune SPARQL endpointhttp://dbtune.org/jamendo/store/Jamendo has information on about 3,500 artistsTrying the query we only get 2,667 results. What'swrong? F. Corno, L. Farinetti- Politecnico di Torino55 56. Example 7 the right wayNot every artist has an image, homepage, or location!OPTIONAL tries to match a graph pattern, but doesn'tfail the whole query if the optional match failsIf an OPTIONAL pattern fails to match for a particularsolution, any variables in that pattern remain unbound(no value) for that solution PREFIX mo: PREFIX mo: PREFIX foaf: PREFIX foaf: SELECT ?name ?img ?hp ?loc SELECT ?name ?img ?hp ?loc WHERE { WHERE { ?a a mo:MusicArtist ; ?a a mo:MusicArtist ;foaf:name ?name .foaf:name ?name . OPTIONAL { ?a foaf:img ?img } OPTIONAL { ?a foaf:img ?img } OPTIONAL { ?a foaf:homepage ?hp } OPTIONAL { ?a foaf:homepage ?hp } OPTIONAL { ?a foaf:based_near ?loc } OPTIONAL { ?a foaf:based_near ?loc } } } F. Corno, L. Farinetti- Politecnico di Torino 56 57. Dataset: GovTrackGovTrack provides SPARQL access todata on the U.S. CongressContains over 13,000,000 triples aboutlegislators, bills, and voteshttp://www.govtrack.us/F. Corno, L. Farinetti- Politecnico di Torino 57 58. Example 8 querying alternativesFind Senate bills that either John McCain or BarackObama sponsored and the other cosponsoredPREFIX bill: PREFIX bill: PREFIX dc: PREFIX dc: PREFIX foaf: PREFIX foaf: SELECT ?title ?sponsor ?statusSELECT ?title ?sponsor ?statusWHERE {WHERE {{ ?bill bill:sponsor ?mccain ; bill:cosponsor ?obama . }{ ?bill bill:sponsor ?mccain ; bill:cosponsor ?obama . }UNIONUNION{ ?bill bill:sponsor ?obama ; bill:cosponsor ?mccain . }{ ?bill bill:sponsor ?obama ; bill:cosponsor ?mccain . }?bill a bill:SenateBill ;?bill a bill:SenateBill ; bill:status ?status ;bill:status ?status ; bill:sponsor ?sponsor ;bill:sponsor ?sponsor ;dc:title ?title . dc:title ?title .?obama foaf:name quot;Barack Obamaquot; .?obama foaf:name quot;Barack Obamaquot; .?mccain foaf:name quot;John McCainquot; .?mccain foaf:name quot;John McCainquot; .}} F. Corno, L. Farinetti- Politecnico di Torino 58 59. Example 8 GovTrack specific endpointhttp://www.govtrack.us/developers/rdf.xpdThe UNION keyword forms a disjunction of two graphpatterns: solutions to both sides of the UNION areincluded in the results F. Corno, L. Farinetti- Politecnico di Torino59 60. RDF datasetsAll queries so far have been against a single graphIn SPARQL this is known as the default graphRDF datasets are composed of a single default graphand zero or more named graphs, identified by a URINamed graphs can be specified with one or moreFROM NAMED clauses, or they can be hardwired into aparticular SPARQL endpointThe SPARQL GRAPH keyword allows portions of aquery to match against the named graphs in the RDFdatasetAnything outside a GRAPH clause matches against thedefault graph F. Corno, L. Farinetti- Politecnico di Torino60 61. Dataset: semanticweb.orgdata.semanticweb.org hosts RDF data regardingworkshops, schedules, and presenters for theInternational Semantic Web (ISWC) and EuropeanSemantic Web Conference (ESWC) series of eventsPresents data via FOAF, SWRC, and iCal ontologiesThe data for each individual ISWC or ESWC event isstored in its own named graphi.e., there is one named graph per conference event contained inthis datasethttp://data.semanticweb.org/F. Corno, L. Farinetti- Politecnico di Torino61 62. Example 9 querying named graphs Find people who have been involved with atleast three ISWC or ESWC conference eventsSELECT DISTINCT ?personSELECT DISTINCT ?personWHERE {WHERE {GRAPH ?g1 { ?person a foaf:Person }GRAPH ?g1 { ?person a foaf:Person }GRAPH ?g2 { ?person a foaf:Person }GRAPH ?g2 { ?person a foaf:Person }GRAPH ?g3 { ?person a foaf:Person }GRAPH ?g3 { ?person a foaf:Person }FILTER(?g1 != ?g2 && ?g1 != ?g3 && ?g2 != ?g3) .FILTER(?g1 != ?g2 && ?g1 != ?g3 && ?g2 != ?g3) .}} F. Corno, L. Farinetti- Politecnico di Torino 62 63. Example 9 querying named graphs The GRAPH ?g construct allows a pattern to matchagainst one of the named graphs in the RDF datasetThe URI of the matching graph is bound to ?g(or whatever variable was actually used)The FILTER assures that were finding a person whooccurs in three distinct graphsThe Web interface used for this SPARQL query definesthe foaf: prefix, which is why it is omitted here F. Corno, L. Farinetti- Politecnico di Torino 63 64. Data.semanticweb.org specific SPARQL endpoint http://data.semanticweb.org/snorql/ F. Corno, L. Farinetti- Politecnico di Torino 64 65. Exercises - RDF@prefix: .@prefix: .@prefixont: .@prefixont: .@prefixvcard: .@prefixvcard: . :john:johnvcard:FN quot;John Smithquot; ;vcard:FN quot;John Smithquot; ;vcard:N [vcard:N [vcard:Given quot;Johnquot; ;vcard:Given quot;Johnquot; ;vcard:Family quot;Smithquot; ] ;vcard:Family quot;Smithquot; ] ;ont:hasAge 32 ;ont:hasAge 32 ;ont:marriedTo :mary .ont:marriedTo :mary .:mary:maryvcard:FN quot;Mary Smithquot; ;vcard:FN quot;Mary Smithquot; ;vcard:N [vcard:N [vcard:Given quot;Maryquot; ;vcard:Given quot;Maryquot; ;vcard:Family quot;Smithquot; ] ;vcard:Family quot;Smithquot; ] ;ont:hasAge 29 .ont:hasAge 29 .F. Corno, L. Farinetti- Politecnico di Torino65 66. SPARQL query exercise 1Return the full names of all people in the graphPREFIX vCard:PREFIX vCard:SELECT ?fullNameSELECT ?fullNameWHERE {?x vCard:FN ?fullName}WHERE {?x vCard:FN ?fullName}Result fullName fullName =============== =============== John Smith John Smith Mary Smith Mary Smith F. Corno, L. Farinetti- Politecnico di Torino 66 67. SPARQL query exercise 2Return the relation between John and MaryPREFIX : PREFIX : SELECT ?pSELECT ?pWHERE { :john ?p :mary }WHERE { :john ?p :mary }Resultpp==================================F. Corno, L. Farinetti- Politecnico di Torino67 68. SPARQL query exercise 3Return the spouse of a person whose name isJohn Smith PREFIX vCard: PREFIX vCard: PREFIX ont: PREFIX ont: SELECT ?y SELECT ?y WHERE {?x vCard:FN quot;John Smithquot;. WHERE {?x vCard:FN quot;John Smithquot;.?x ont:marriedTo ?y}?x ont:marriedTo ?y}Result y y ================= ================= F. Corno, L. Farinetti- Politecnico di Torino68 69. SPARQL query exercise 4Return the name and the first name of all peoplein the knowledge base PREFIX vCard: PREFIX vCard: SELECT ?name, ?firstName SELECT ?name, ?firstName WHERE {?x vCard:N ?name . WHERE {?x vCard:N ?name .?name vCard:Given ?firstName}?name vCard:Given ?firstName} Result namefirstName namefirstName ======================== ======================== John Smith quot;Johnquot; John Smith quot;Johnquot; Mary Smith quot;Maryquot; Mary Smith quot;Maryquot;F. Corno, L. Farinetti- Politecnico di Torino 69 70. SPARQL query exercise 5Return all people over 30 in the knowledge basePREFIX ont: PREFIX ont: SELECT ?x SELECT ?x WHERE {?x ont:hasAge ?age . WHERE {?x ont:hasAge ?age .FILTER(?age > 30)}FILTER(?age > 30)} Result x x ================= ================= F. Corno, L. Farinetti- Politecnico di Torino70 71. FROM Select RDF graph (= dataset) to be queriedIn case of multiple FROM clauses, graphs aremergedExamplePREFIX foaf: PREFIX foaf: SELECT ?name SELECT ?name FROM FROM WHERE { ?x foaf:name ?name } WHERE { ?x foaf:name ?name }F. Corno, L. Farinetti- Politecnico di Torino 71 72. SPARQL query exercise 6Graph http://example.org/bob@prefix foaf: . @prefix foaf: . _:a foaf:namequot;Bobquot; . _:a foaf:namequot;Bobquot; . _:a foaf:mbox . _:a foaf:mbox .Graph http://example.org/alice@prefix foaf: .@prefix foaf: ._:a foaf:name quot;Alicequot; ._:a foaf:name quot;Alicequot; ._:a foaf:mbox ._:a foaf:mbox .F. Corno, L. Farinetti- Politecnico di Torino 72 73. SPARQL query exercise 6Return the names of people in both graphsPREFIX foaf: PREFIX foaf: SELECT ?src ?nameSELECT ?src ?nameFROM NAMED FROM NAMED FROM NAMED FROM NAMED WHEREWHERE{ GRAPH ?src { ?x foaf:name ?name } }{ GRAPH ?src { ?x foaf:name ?name } }Result src name src name============================================================================== quot;Bobquot; quot;Bobquot; quot;Alicequot; quot;Alicequot;F. Corno, L. Farinetti- Politecnico di Torino73 74. The Linking Open Data Project 75. The Linking Open Data ProjectA fundamental prerequisite of the Semantic Webis the existence of large amounts of meaningfullyinterlinked RDF data on the Web.Linked Data is about using the Web to connectrelated data that wasnt previously linked, orusing the Web to lower the barriers to linkingdata currently linked using other methodsIt is a recommended best practice for exposing,sharing, and connecting pieces of data,information, and knowledge on the SemanticWeb using URIs and RDF F. Corno, L. Farinetti- Politecnico di Torino 75 76. The Linking Open Data ProjectCommunity effort to make various open datasetsavailable on the Web as RDF and to set RDFlinks between data items from different datasetsThe datasets are published according to theLinked Data principles and can therefore becrawled by Semantic Web search engines andnavigated using Semantic Web browsersSupported by W3CBegan early 2007http://linkeddata.org/home F. Corno, L. Farinetti- Politecnico di Torino 76 77. The Web of DocumentsAnalogy: a global filesystemDesigned for human consumptionPrimary objects: documentsLinks between documents (or sub-parts)Degree of structure in objects: fairly lowSemantics of content and links: implicit F. Corno, L. Farinetti- Politecnico di Torino 77 78. The Web of Linked DataAnalogy: a global databaseDesigned for machines first, humans laterPrimary objects: things (or descriptions ofthings)Links between thingsDegree of structure in (descriptions of)things: highSemantics of content and links: explicit F. Corno, L. Farinetti- Politecnico di Torino78 79. Linked DataA way of publishing data on the Web thatEncourages reuseReduces redundancyMaximizes its (real and potential) inter-connectednessEnables network effects to add value to dataF. Corno, L. Farinetti- Politecnico di Torino 79 80. Linked Data example F. Corno, L. Farinetti- Politecnico di Torino 80 81. Why publish Linked Data?Ease of discoveryEase of consumptionStandards-based data sharingReduced redundancyAdded valueBuild ecosystems around your data/content F. Corno, L. Farinetti- Politecnico di Torino81 82. Linked Open Data cloudMay 2007 F. Corno, L. Farinetti- Politecnico di Torino82 83. Main contributorsDBLP Computer science bibliographyProject Gutenberg Literary works inthe public domainRichard Cyganiak, Chris Bizer (FUBerlin) Piet Hensel, Hans Butschalowsky (FUBerlin)DBpedia Structured information fromWikipedia Revyu Community reviews aboutanythingUniversitt Leipzig, FU Berlin,OpenLinkTom Heath, Enrico Motta (OpenUniversity)DBtune, Jamendo CreativeCommons music repositoriesRDF Book Mashup Books from theAmazon APIYves Raimond (University of London)Tobias Gau, Chris Bizer (FU Berlin)Geonames World-wide geographicaldatabaseUS Census Data Statisticalinformation about the U.S.Bernard Vatant (Mondeca), Marc Wick(Geonames)Josh Tauberer (University ofPennsylvania), OpenLinkMusicbrainz Music and artistdatabaseWorld Factbook Country statistics,compiled by CIAFrederick Giasson, Kingsley Idehen(Zitgist) Piet Hensel, Hans Butschalowsky (FUBerlin)F. Corno, L. Farinetti- Politecnico di Torino 83 84. July 2007 F. Corno, L. Farinetti- Politecnico di Torino 84 85. August 2007 F. Corno, L. Farinetti- Politecnico di Torino 85 86. November 2007 F. Corno, L. Farinetti- Politecnico di Torino 86 87. February 2008 F. Corno, L. Farinetti- Politecnico di Torino 87 88. September 2008 F. Corno, L. Farinetti- Politecnico di Torino88 89. March 2009 F. Corno, L. Farinetti- Politecnico di Torino89 90. Statistics on datasetshttp://esw.w3.org/topic/TaskForces/CommunityProjects/LinkingOpenData/DataSets/Statistics F. Corno, L. Farinetti- Politecnico di Torino90 91. Statistics on links between datasetshttp://esw.w3.org/topic/TaskForces/CommunityProjects/LinkingOpenData/DataSets/LinkStatistics F. Corno, L. Farinetti- Politecnico di Torino91 92. Linked Data shopping listList of sites/datasets that the communitywould like to see published as Linked DataThis list may form the basis for somecampaign/action to encourage these datapublishers to embrace Linked Data http://community.linkeddata.org/MediaWiki/index.php?ShoppingList F. Corno, L. Farinetti- Politecnico di Torino92 93. The Linked Data principlesUse URIs as names for thingsAnything, not just documentsYou are not your homepageInformation resources and non-informationresourcesUse HTTP URIsGlobally unique names, distributed ownershipAllows people to look up those namesF. Corno, L. Farinetti- Politecnico di Torino93 94. Linked Data principlesProvide useful information in RDFWhen someone looks up a URIInclude RDF links to other URIsTo enable discovery of related information F. Corno, L. Farinetti- Politecnico di Torino 94 95. Link to other datasetsPopular predicates for linking owl:sameAs foaf:homepage foaf:topic foaf:based_near foaf:maker/foaf:made foaf:page foaf:primaryTopic rdfs:seeAlso F. Corno, L. Farinetti- Politecnico di Torino 95 96. Link to other Data Sets F. Corno, L. Farinetti- Politecnico di Torino 96 97. LInked Data toolsTools for Publishing Linked DataD2R Server: a tool for publishing relational databases as LinkedDataTalis Platform: the Talis Platform provides Linked Data-complianthosting for content and RDF dataPubby: a Linked Data frontend for SPARQL endpointsPaget: a framework for building Linked Data applicationsVapour: Linked Data ValidatorTools for consuming Linked DataSemantic Web Browsers and Client LibrariesSemantic Web Search EnginesLinked Data applications for end usersSemantic Web Browsers and Client LibrariesOther Linked Data ApplicationsF. Corno, L. Farinetti- Politecnico di Torino 97 98. The Tabulator Project Generic data browser and editorProvides a way to browse RDF data on thewebOpen source under the W3C softwarelicensehttp://www.w3.org/2005/ajar/tabF. Corno, L. Farinetti- Politecnico di Torino 98 99. The Tabulator Project F. Corno, L. Farinetti- Politecnico di Torino 99 100. PubbyMany triple stores and other SPARQL endpointscan be accessed only by SPARQL clientapplications that use the SPARQL protocolIt cannot be accessed by the growing variety ofLinked Data clientsPubby is designed to provide a Linked Datainterface to those RDF data sourceshttp://www4.wiwiss.fu-berlin.de/pubby/ F. Corno, L. Farinetti- Politecnico di Torino100 101. Pubby F. Corno, L. Farinetti- Politecnico di Torino 101 102. ReferencesW3C, Introduction to the Semantic Webhttp://www.w3.org/2006/Talks/0524-Edinburgh-IH/Lee Feigenbaum, SPARQL By Examplehttp://www.cambridgesemantics.com/2008/09/sparql-by-exampleValentina Tamma, Chapter 4: SPARQLhttp://www.csc.liv.ac.uk/~valli/Comp318/PDF/SPARQL.pdfTom Heath, An Introduction to Linked Datahttp://tomheath.com/slides/2009-02-austin-linkeddata-tutorial.pdf F. Corno, L. Farinetti- Politecnico di Torino 102 103. LicenseThis work is licensed under the CreativeCommons Attribution-Noncommercial-Share Alike 3.0 Unported License.To view a copy of this license, visithttp://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to CreativeCommons, 171 Second Street, Suite 300,San Francisco, California, 94105, USA.F. Corno, L. Farinetti- Politecnico di Torino 103