1 / 66

SPARQL Part II

SPARQL Part II. Jan Pettersen Nytun, UiA. First a small SPARQL example. Jan Pettersen Nytun, UiA. [4]:. Ask: SELECT ?who WHERE { : JamesDean : playedIn ?what . ? what : directedBy ?who . }

uhlig
Download Presentation

SPARQL Part II

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. SPARQLPartII Jan Pettersen Nytun, UiA

  2. First a small SPARQL example Jan Pettersen Nytun, UiA

  3. [4]: Ask: SELECT ?who WHERE { :JamesDean :playedIn ?what . ?what :directedBy ?who . } Answer: :GeorgeStevens, :EliaKazan, :NicholasRay, :FredGuiol

  4. More Meaningful Variable Names [4]:

  5. Installing Your Own SPARQL Endpoint Jan Pettersen Nytun, UiA

  6. Apache Jena Fuseki • SPARQL server / SPARQL endpoint enables users to query a knowledge base via the SPARQL language • Has a user interface for server monitoring and administration. • Provides the SPARQL 1.1 protocols for query and update.

  7. TDB • Fuseki is tightly integrated with TDB to provide a robust, transactional persistent storage layer. • TDB is a component of Jena for RDF storage and query. It support the full range of Jena APIs. TDB can be used as a high performance RDF store on a single machine.

  8. Download https://jena.apache.org/download/

  9. Starting FusekiWeb Application

  10. Using YourSPARQL Endpoint Jan Pettersen Nytun, UiA

  11. You may copy the text on next slide into a ttl-file (OWL in Turtle format) and upload it.

  12. @prefix : <http://www.uia.no/film_ontology#> . @prefixowl: <http://www.w3.org/2002/07/owl#> . @prefixrdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefixxml: <http://www.w3.org/XML/1998/namespace> . @prefixxsd: <http://www.w3.org/2001/XMLSchema#> . @prefixrdfs: <http://www.w3.org/2000/01/rdf-schema#> . @base <http://www.uia.no/film_ontology> . <http://www.uia.no/film_ontology> rdf:typeowl:Ontology . :Film rdf:typeowl:Class . :Human rdf:typeowl:Class . :Man rdf:typeowl:Class; rdfs:subClassOf:Human . :Womanrdf:typeowl:Class; rdfs:subClassOf:Human . :playedInrdf:typeowl:ObjectProperty . :Giantrdf:type:Film , owl:NamedIndividual . :EastOfEdenrdf:type:Film , owl:NamedIndividual . :CarrollBakerrdf:type:Woman, owl:NamedIndividual; :playedIn :Giant . :ElizabethTaylorrdf:type:Woman, owl:NamedIndividual ; :playedIn :Giant . :JoVanFleetrdf:type:Woman, owl:NamedIndividual ; :playedIn :EastOfEden . :JulieHarrisrdf:type:Woman, owl:NamedIndividual ; :playedIn:EastOfEden . :MercedesMcCambridgerdf:type :Woman, owl:NamedIndividual ; :playedIn :Giant . :RockHudsonrdf:type:Man , owl:NamedIndividual ; :playedIn :Giant. :JamesDeanrdf:type:Man , owl:NamedIndividual ; :playedIn :EastOfEden , :Giant .

  13. The postfix of the file must be correct (must be ttl if Turtle)

  14. Etc. …

  15. Insert Data

  16. Find the women that played in the same movies as James Dean.

  17. Given the following graph, find the women that played in the same movies as James Dean.

  18. Prefixes prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#>

  19. [4]: SELECT ?actress ?movie WHERE { :JamesDean :playedIn ?movie . ?actress :playedIn ?movie . ?actress rdf:type :Woman }

  20. In Protégé:

  21. In Fuseki

  22. Defining Graphs in Fuseki Jan Pettersen Nytun, UiA

  23. 2.1.4 Specifying an RDF Dataset A SPARQL query is executed against an RDF Dataset. The RDF Dataset for a query may be specified either via the default-graph-uriand named-graph-uri parameters in the SPARQL Protocol or in the SPARQL query string using the FROM and FROM NAMED keywords. [https://www.w3.org/TR/sparql11-protocol/]:

  24. You may add GRAPH when uploading file

  25. prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#> SELECT ?actress ?movie WHERE { GRAPH <http://localhost:3030/Testing/data/JPNGRAPHH> { :JamesDean :playedIn ?movie . ?actress :playedIn ?movie . ?actress rdf:type :Woman } }

  26. Find all actors.

  27. The result contains two occurrences of JamesDean! prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#> SELECT ?actor WHERE { ?actor :playedIn ?movie . }

  28. Avoid duplicatesby using DISTINCT

  29. Ordering of the SPARQL statement triples • Semantically the order is insignificant. • In regard to efficiency: • Typically the queries are processed in top-to-bottom order… • Order triples in a query so that the first triples gives fewest matches, e.g., by have the triples with fewest new variables first.

  30. New ontology for the next slides. You may copy the text on next slide into a ttl-file and upload it.

  31. @prefix : <http://www.uia.no/janpettersennytun/ontologies/family#> . @prefixowl: <http://www.w3.org/2002/07/owl#> . @prefixrdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefixxml: <http://www.w3.org/XML/1998/namespace> . @prefixxsd: <http://www.w3.org/2001/XMLSchema#> . @prefixrdfs: <http://www.w3.org/2000/01/rdf-schema#> . @base <http://www.uia.no/janpettersennytun/ontologies/family> . <http://www.uia.no/janpettersennytun/ontologies/family> rdf:typeowl:Ontology . :hasBrotherrdf:typeowl:ObjectProperty . :hasParentrdf:typeowl:ObjectProperty . :hasSisterrdf:typeowl:ObjectProperty . :hasUnclerdf:typeowl:ObjectProperty . :birthdayrdf:typeowl:DatatypeProperty; rdfs:rangexsd:dateTime . :diedOnrdf:typeowl:DatatypeProperty; rdfs:rangexsd:dateTime . :hasNamerdf:typeowl:DatatypeProperty; rdfs:rangexsd:string . :CityManrdf:typeowl:Class; rdfs:subClassOf:Man . :Man rdf:typeowl:Class; rdfs:subClassOf :Person . :Person rdf:typeowl:Class . :Womanrdf:typeowl:Class; rdfs:subClassOf :Person . :Ester rdf:type :Woman , owl:NamedIndividual ; :hasName "Ester"^^xsd:string ; :hasBrother :Olav , :Sigmund . :Jan rdf:type :Man , owl:NamedIndividual ; :birthday "1957-12-16T04:30:00"^^xsd:dateTime ; :hasName "Jan"^^xsd:string ; :hasParent :Sigmund . :Kirsten rdf:type :Woman , owl:NamedIndividual ; :birthday "1956-05-12T14:00:00"^^xsd:dateTime ; :hasName "Kirsten"^^xsd:string ; :hasParent :Sigmund . :Olav rdf:type :Man , owl:NamedIndividual ; :hasName "Olav"^^xsd:string ; :hasSister :Ester ; :hasBrother :Sigmund . :Rolf rdf:type :Man , owl:NamedIndividual ; :hasName "Rolf"^^xsd:string . :Sigmund rdf:type :Man , owl:NamedIndividual ; :birthday "1933-08-21T15:00:00"^^xsd:dateTime ; :diedOn "1980/07/14T21:32:52"^^xsd:dateTime ; :hasName "Sigmund"^^xsd:string ; :hasBrother :Rolf . :Sigrund rdf:type :Woman , owl:NamedIndividual ; :birthday "1952-07-21T15:00:007"^^xsd:dateTime ; :hasName "Sigrund"^^xsd:string ; :hasParent :Sigmund .

  32. Use SPARQL and get all parents.

  33. PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> SELECT ?parent WHERE { ?child :hasParent ?parent }

  34. Avoid duplicate listingsof parents in result.

  35. Avoid duplicatesof parents in result.

  36. Find all the properties of Jan together with their values.

  37. Find all properties with values of Jan

  38. Find all persons born afterthe beginning of 1940

  39. But first find all persons. PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?person WHERE { ?person rdf:type:Person } OBS! The result set is empty!!

  40. Find all men are easy! PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?person WHERE { ?person rdf:type:Man} The result is all men:

  41. Find all persons, i.e., find all members of Person and the subclasses of Person PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person } SPARQL 1.1 includes a transitive operator: *[4]: If we include a * after a property name, then the triple matches any number of chained occurrences of the same property.

  42. Find all persons and their birthdays PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?person ?birthday WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . ?person :birthday ?birthday } (Persons without a birthday will not be found.)

  43. Find all persons born after beginning of 1940 by using FILTER.

More Related