1 / 24

SPARQL In-Class Shared Exercise

SPARQL In-Class Shared Exercise. Pop Quiz. If you have a large knowledge store, why should you not issue: SELECT ?s ?p ?o WHERE { ?s ?p ?o }. Ans: It returns the entire knowledge store (i.e. all triples). Answer: 3. Pop Quiz. 1. How many triples?

buck
Télécharger la présentation

SPARQL In-Class Shared Exercise

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. SPARQL In-Class Shared Exercise

  2. Pop Quiz • If you have a large knowledge store, why should you not issue: SELECT ?s ?p ?o WHERE { ?s ?p ?o } Ans: It returns the entire knowledge store (i.e. all triples)

  3. Answer: 3 Pop Quiz 1. How many triples? <rdf:Description rdf:about="http://www.example.org/index.html"> <exterms:creation-date>August 16, 1999</exterms:creation-date> <dc:language>en</dc:language> <dc:creator rdf:resource="http://www.example.org/staffid/85740"/> </rdf:Description>

  4. Pop Quiz 2. What is the query result? What is the query result: PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ex: <http://www.example.orgterms/> SELECT ?o WHERE { ?s dc:creator ?p. ?s ex:creation-date ?o } Answer: 3 August 16, 1999

  5. Pop Quiz 3. What is the query result? What is the query result: PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ex: <http://www.example.orgterms/> SELECT ?o WHERE { ?s dc:creator ?p. ?s ex:creation-date ?o. ?p dc:language “en” } Answer: 3 <empty>

  6. Ans: 7 Pop Quiz • 1. How many triples? • <rdf:Description rdf:about="http://example.org/courses/6.001"> • <s:students> • <rdf:Bag> • <rdf:li rdf:resource="http://example.org/students/Amy"/> • <rdf:li rdf:resource="http://example.org/students/Mohamed"/> • <rdf:li rdf:resource="http://example.org/students/Johann"/> • <rdf:li rdf:resource="http://example.org/students/Maria"/> • <rdf:li rdf:resource="http://example.org/students/Phuong"/> • </rdf:Bag> • </s:students> • </rdf:Description>

  7. Pop Quiz 2. What is the query result? What is the query result: PREFIX ex: <http://example.org/students/> PREFIX exv: <http://example.org/students/vocab#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?o WHERE { ?s a rdf:Bag. ?s2 exv:students ?s. { ?s rdf:_1 ?o. } UNION { ?s rdf:_2 ?o. } } http://example.org/students/Amy http://example.org/students/Mohamed

  8. http://example.org/students/Johann Returns the last of every list in the knowledge store - > Not generally useful. Pop Quiz 2. What is the query result? What is the query result: PREFIX ex: <http://example.org/students/> PREFIX exv: <http://example.org/students/vocab#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?o WHERE { (?o2) rdf:first ?o. }

  9. http://example.org/students/Mohamed Returns the next to last of every list in the knowledge store. Pop Quiz 3. What is the query result? What is the query result: PREFIX ex: <http://example.org/students/> PREFIX exv: <http://example.org/students/vocab#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?o WHERE { ?s2 rdf:rest (?o2); rdf:first ?o. }

  10. Exercise Preliminaries • Have Gruff installed and working (item 3 of Lab 2) • Get example rdf files (on eLearning Lecture 5 directory)

  11. Start-up (Together) • Start GRUFF • Create a new triple store • Put somewhere convenient • Name it funtoday • Use default estimate of size • Load rdf files • Load one at a time • ericMiller.rdf • smallSet.rdf • Display Both Graphs • Add->Display all triples

  12. First SPARQL • Switch to gruff Query view • View->Query view • Create a query that returns all triples (using the SELECT construct) • Run it - How many triples? • Shows at bottom just after query runs • Hit ‘Do Query’ again SELECT ?s ?p ?o WHERE { ?s ?p ?o. }

  13. Sub-selection • Return the objects and properties for ‘…Me’ • Windows help: Shift+LeftClick, then Ctrl-C can copy a field (Ctrl-V to paste) (See View menu for shortcuts) • Run it - How many triples? SELECT ?p ?o WHERE { <http://www.w3.org/People/EM/contact#me> ?p ?o }

  14. PREFIX • Add a prefix statement for …Me • Call the prefix em • Change the where clause to use the prefix • Run the query • How many triples • Same answer as before? PREFIX em: <http://www.w3.org/People/EM/contact#> SELECT ?p ?o WHERE { em:me ?p ?o }

  15. Classes Used • Change the query to find all the classes being used in the knowledge store • Did the query get to unique answers or was it just our store? SELECT ?o WHERE { ?s a ?o } or rdf:type for ‘a’ NOTE: rdf is predefined for you. Answer: Just our store. Can use DISTINCT SELECT DISTINCT ?o WHERE { ?s a ?o }

  16. More Data • Load more rdf files • BagExample.rdf • collectionExample.rdf • Display all nodes • Arrange 6.001 and 6.002, each as top of a path (with some shared resources) • Make note of the types of nodes, which have class types, and names of property types. • Especially that bag uses a type, list doesn’t

  17. First Bag Item • Write select query to return the ‘first’ bag item of class 6.001 down the students path. • Use the prefixes: • PREFIX ex: <http://example.org/courses/> • PREFIX exv: <http://example.org/students/vocab#> PREFIX ex: <http://example.org/courses/> PREFIX exv: <http://example.org/students/vocab#> SELECT ?o WHERE { ex:6.001 exv:students ?bn. ?bn rdf:_1 ?o}

  18. Bag Iteration • How do you change the query to get the next bag item? Answer: Change _1 to _2 -similar for all others until no return PREFIX ex: <http://example.org/courses/> PREFIX exv: <http://example.org/students/vocab#> SELECT ?o WHERE { ex:6.001 exv:students ?bn. ?bn rdf:_2 ?o}

  19. First List Item • Change the query to return the ‘first’ list item of class 6.002 down the students path. • Use the prefixes: • PREFIX ex: <http://example.org/courses/> • PREFIX exv: <http://example.org/students/vocab#> PREFIX ex: <http://example.org/courses/> PREFIX exv: <http://example.org/students/vocab#> SELECT ?o WHERE { ex:6.002 exv:students ?bn. ?bn rdf:first ?o }

  20. List Iteration • How do you change the query to get the next list item? Answer: Extend the chain further down -You cannot use the blank node identifier in a new sparql query (per the specification). -You have to extend the chain more for each -rdf:nil return indicates end of list -this can be a fine structure for non-SPARQL graph access PREFIX ex: <http://example.org/courses/> PREFIX exv: <http://example.org/students/vocab#> SELECT ?o WHERE { ex:6.002 exv:students ?bn. ?bn rdf:rest ?bn2. ?bn2 rdf:first ?o. } Last Gruff Example – remainder are verbal exercises

  21. Explicit Inference • My ontology says that zebra is a subclass of mammal. Modify the query below to implement the inference. SELECT ?o WHERE { ?s1 a mammal. ?s1 someProperty ?o } SELECT ?o WHERE { {?s1 a mammal} UNION {?s1 a zebra} ?s1 someProperty ?o } The Lesson: SPARQL can be automatically converted to accommodate subClassOf in an ontology.

  22. Explicit Inference • My ontology says that endsAtDateTime is a subproperty of endsAt. Modify the query below to implement the inference. SELECT ?o WHERE { ?s1 a event. ?s1 endsAt ?o } SELECT ?o WHERE { ?s1 a event. {?s1 endsAt ?o} UNION {?s1 endsAtDateTime ?o} } The Lesson: SPARQL can be automatically converted to accommodate subPropertyOf in an ontology.

  23. Explicit Inference • My ontology says the domain of property ‘teaches’ is class ‘person’. Modify the query below to implement the inference. SELECT ?o WHERE { ?s1 a person. ?s1 someProperty ?o } SELECT ?o WHERE { {?s1 a person} UNION {?s1 teaches ?junk} ?s1 someProperty ?o } The Lesson: SPARQL can be automatically converted to accommodate domain and range in an ontology.

  24. Explicit Inference • WARNING • Implementation of inference of complicated ontologies in SPARQL queries can easily make SPARQL queries unreasonably long to execute • There are limits to how much inference can be performed in a SPARQL SELECT • It cannot perform inference over classes, properties, etc. found during the query (i.e. found as a variable like ?p)

More Related