sparql in-class shared exercise. pop quiz if you have a large knowledge store, why should you not...

24
SPARQL In-Class Shared Exercise

Upload: arline-hensley

Post on 13-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

SPARQLIn-Class Shared Exercise

Pop Quiz

• If you have a large knowledge store, why should you not issue:

SELECT ?s ?p ?oWHERE { ?s ?p ?o }

Ans: It returns the entire knowledge store (i.e. all triples)

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>

Answer: 3

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

o

?August 16, 1999

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

o

?<empty>

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>

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. }

}

o

?http://example.org/students/Amyhttp://example.org/students/Mohamed

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.

}

o

?http://example.org/students/Johann

Returns the last of every list in the knowledge store - > Not generally useful.

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.

}

o

?http://example.org/students/Mohamed

Returns the next to last of every list in the knowledge store.

Exercise Preliminaries

• Have Gruff installed and working (item 3 of Lab 2)

• Get example rdf files (on eLearning Lecture 5 directory)

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

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. }

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 ?oWHERE { <http://www.w3.org/People/EM/contact#me> ?p ?o }

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 ?oWHERE { em:me ?p ?o }

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 ?oWHERE { ?s a ?o } or rdf:type for ‘a’ NOTE: rdf is predefined for you.

Answer: Just our store. Can use DISTINCTSELECT DISTINCT ?oWHERE { ?s a ?o }

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

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 ?oWHERE { ex:6.001 exv:students ?bn. ?bn rdf:_1 ?o}

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 ?oWHERE { ex:6.001 exv:students ?bn. ?bn rdf:_2 ?o}

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 ?oWHERE { ex:6.002 exv:students ?bn. ?bn rdf:first ?o }

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 ?oWHERE { ex:6.002 exv:students ?bn. ?bn rdf:rest ?bn2. ?bn2 rdf:first ?o. }

Last Gruff Example – remainder are verbal exercises

Explicit Inference

• My ontology says that zebra is a subclass of mammal. Modify the query below to implement the inference.SELECT ?oWHERE { ?s1 a mammal. ?s1 someProperty ?o}

SELECT ?oWHERE { {?s1 a mammal} UNION {?s1 a zebra} ?s1 someProperty ?o}

The Lesson: SPARQL can be automatically converted to accommodate subClassOf in an ontology.

Explicit Inference

• My ontology says that endsAtDateTime is a subproperty of endsAt. Modify the query below to implement the inference.SELECT ?oWHERE { ?s1 a event. ?s1 endsAt ?o}

SELECT ?oWHERE { ?s1 a event. {?s1 endsAt ?o} UNION {?s1 endsAtDateTime ?o} }

The Lesson: SPARQL can be automatically converted to accommodate subPropertyOf in an ontology.

Explicit Inference

• My ontology says the domain of property ‘teaches’ is class ‘person’. Modify the query below to implement the inference.SELECT ?oWHERE { ?s1 a person. ?s1 someProperty ?o}

SELECT ?oWHERE { {?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.

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)