Presentation is loading. Please wait.

Presentation is loading. Please wait.

SPARQL Exercise Much of this exercise has been copied from: INF3580/INF4580 – MANDATORY EXERCISE 3 http://www.uio.no/studier/emner/matnat/ifi/INF3580/v15/undervisnin.

Similar presentations


Presentation on theme: "SPARQL Exercise Much of this exercise has been copied from: INF3580/INF4580 – MANDATORY EXERCISE 3 http://www.uio.no/studier/emner/matnat/ifi/INF3580/v15/undervisnin."— Presentation transcript:

1 SPARQL Exercise Much of this exercise has been copied from: INF3580/INF4580 – MANDATORY EXERCISE 3 gsmateriale/oblig3.pdf

2 Copy the Turtle defined ontology on next slide to an ”
Copy the Turtle defined ontology on next slide to an ”.owl” file and load it into Protégé.

3 @prefix : <http://www. simpsons. no/simpsonsfamily#>
@prefix : < foaf: < owl: < rdf: < xml: < xsd: < rdfs: < . < rdf:type owl:Ontology . ################################################################# # # Object Properties :hasBrother rdf:type owl:ObjectProperty . :hasParent rdf:type owl:ObjectProperty . :hasSister rdf:type owl:ObjectProperty . :hasUncle rdf:type owl:ObjectProperty . :hasFamilyMember rdf:type owl:ObjectProperty . :hasSpouse rdf:type owl:ObjectProperty . :hasFather rdf:type owl:ObjectProperty . :hasMother rdf:type owl:ObjectProperty . # Data properties :birthday rdf:type owl:DatatypeProperty ; rdfs:range xsd:dateTime . :diedOn rdf:type owl:DatatypeProperty ; :hasName rdf:type owl:DatatypeProperty ; rdfs:range xsd:string . # Classes :Family rdf:type owl:Class . :Man rdf:type owl:Class ; rdfs:subClassOf foaf:Person . :Woman rdf:type owl:Class ; #################################################################### :Homer a foaf:Person ; foaf:age "36"^^xsd:int ; foaf:name "Homer Simpson" . :Marge foaf:age "34"^^xsd:int ; foaf:name "Marge Simpson" . :Lisa foaf:age "8"^^xsd:int ; foaf:name "Lisa Simpson" . :Bart foaf:age "10"^^xsd:int ; foaf:name "Bart Simpson" . :Maggie foaf:age "1"^^xsd:int ; foaf:name "Maggie Simpson" . :Simpsons a :Family ; :hasFamilyMember :Maggie , :Homer , :Lisa , :Bart, :Marge . :hasSpouse :Marge . :hasSpouse :Homer . :hasFather :Homer ; :hasMother :Marge . :Abraham a foaf:Person . :hasParent [ a foaf:Person ; :hasFather :Abraham ] . :Mona a foaf:Person . :hasMother :Mona :hasSister :Patty , :Selma ] ; :hasBrother :Herb :Herb a foaf:Person . :Patty a foaf:Person ; :hasSister :Selma . :Selma :hasSister :Patty . :hasSpouse

4 Task 1 Query: Find all Persons and order them by identifier, list also optionally their name. Use the result variable names ?person, ?name . Tip: This is a simple query, where you will need to use SELECT, WHERE, OPTIONAL and ORDER BY . If you do not get any results, try the query SELECT ?s ?p ?o WHERE {?s ?p ?o} and see if your get the expected results and namespaces. This query lists all triples in the graph.

5 Task 1 Query: Find all Persons and order them by identifier, list also optionally their name. Use the result variable names ?person, ?name . Proposed Solution: PREFIX : < PREFIX foaf: < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT ?person ?name WHERE { ?person a foaf:Person OPTIONAL {?person foaf:name ?name}} ORDER BY ?person

6 Task 2 Query: Find everyone who has a mother and list both the person and the mother. Order by mother. PREFIX : < PREFIX foaf: < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT ?person ?mother WHERE { ?person :hasMother ?mother } ORDER BY ?mother

7 Task 3 Query: Find everyone who has a mother or a father and list both the person and the mother or father. Order by mother/father. PREFIX : < PREFIX foaf: < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT DISTINCT ?person ?mother ?father WHERE { {?person :hasMother ?m} UNION {?person :hasFather ?f } OPTIONAL {?person :hasMother ?mother} OPTIONAL {?person :hasFather ?father} } ORDER BY ?mother ?father

8 Task 4 Query: Find all of Maggie’s grandmothers. Result variable name: ?grandmother . PREFIX : < PREFIX foaf: < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT ?grandmother WHERE { :Maggie :hasParent ?parent. ?parent :hasMother ?grandmother } ORDER BY ?mother May be improved with help of :more triples with :hasMother/:hasFather + OPTIONAL

9 Task 5 Query: Find everyone older than 10. Order by age, oldest first. Output name and age. Result variable names: ?person, ?age . PREFIX : < PREFIX foaf: < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT ?person ?age WHERE { ?person foaf:age ?age . FILTER (?age > "10"^^xsd:int ) } ORDER BY ?age

10 Task 6 Query: Is Herb the brother of Homer? (We know Homer has a brother named Herb, but here the question is: Do the ontology contain this information?) Use SPARQL construct ASK. PREFIX : < PREFIX foaf: < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < ASK { :Homer :hasBrother :Herb } # There are some information about brotherhood in the ontology (in blank nodes), but not enough …


Download ppt "SPARQL Exercise Much of this exercise has been copied from: INF3580/INF4580 – MANDATORY EXERCISE 3 http://www.uio.no/studier/emner/matnat/ifi/INF3580/v15/undervisnin."

Similar presentations


Ads by Google