Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jan Pettersen Nytun, UiA

Similar presentations


Presentation on theme: "Jan Pettersen Nytun, UiA"— Presentation transcript:

1 Jan Pettersen Nytun, UiA
SPARQL Part II 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

9 Starting Fuseki Web Application

10

11

12 Using Your SPARQL Endpoint
Jan Pettersen Nytun, UiA

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

14 @prefix : <http://www. uia. no/film_ontology#>
@prefix : < owl: < rdf: < xml: < xsd: < rdfs: < < . < rdf:type owl:Ontology . :Film rdf:type owl:Class . :Human rdf:type owl:Class . :Man rdf:type owl:Class ; rdfs:subClassOf :Human . :Woman rdf:type owl:Class ; rdfs:subClassOf :Human . :playedIn rdf:type owl:ObjectProperty . :Giant rdf:type :Film , owl:NamedIndividual . :EastOfEden rdf:type :Film , owl:NamedIndividual . :CarrollBaker rdf:type :Woman , owl:NamedIndividual ; :playedIn :Giant . :ElizabethTaylor rdf:type :Woman , owl:NamedIndividual ; :playedIn :Giant . :JoVanFleet rdf:type :Woman , owl:NamedIndividual ; :playedIn :EastOfEden . :JulieHarris rdf:type :Woman , owl:NamedIndividual ; :playedIn :EastOfEden . :MercedesMcCambridge rdf:type :Woman , owl:NamedIndividual ; :playedIn :Giant . :RockHudson rdf:type :Man , owl:NamedIndividual ; :playedIn :Giant . :JamesDean rdf:type :Man , owl:NamedIndividual ; :playedIn :EastOfEden , :Giant .

15

16

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

18

19 Etc. …

20

21 Insert Data

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

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

24 Prefixes prefix rdf: < prefix rdfs: < prefix owl: < prefix xsd: < prefix : <

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

26 In Protégé:

27 In Fuseki

28 Defining Graphs in Fuseki
Jan Pettersen Nytun, UiA

29 [ 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-uri and named-graph-uri parameters in the SPARQL Protocol or in the SPARQL query string using the FROM and FROM NAMED keywords.

30 You may add GRAPH when uploading file

31 prefix rdf: <http://www. w3
prefix rdf: < prefix rdfs: < prefix owl: < prefix xsd: < prefix : < SELECT ?actress ?movie WHERE { GRAPH < { :JamesDean :playedIn ?movie . ?actress :playedIn ?movie . ?actress rdf:type :Woman }

32 Find all actors.

33 The result contains two occurrences of JamesDean!
prefix rdf: < prefix rdfs: < prefix owl: < prefix xsd: < prefix : < SELECT ?actor WHERE { ?actor :playedIn ?movie . } The result contains two occurrences of JamesDean! actor 1 :CarrollBaker 2 :ElizabethTaylor 3 :JamesDean 4 :MercedesMcCambridge 5 :RockHudson 6 7 :JoVanFleet 8 :JulieHarris

34 Avoid duplicates by using DISTINCT

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

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

37 @prefix : <http://www. uia
@prefix owl: < . @prefix rdf: < . @prefix xml: < . @prefix rdfs: < . @prefix xsd: < . @base < . < rdf:type owl:Ontology . :hasBrother rdf:type owl:ObjectProperty . :hasParent rdf:type owl:ObjectProperty . :hasSister rdf:type owl:ObjectProperty . :hasUncle rdf:type owl:ObjectProperty . :birthday rdf:type owl:DatatypeProperty ; rdfs:range xsd:dateTime . :diedOn rdf:type owl:DatatypeProperty ; rdfs:range xsd:dateTime . :hasName rdf:type owl:DatatypeProperty ; rdfs:range xsd:string . :CityMan rdf:type owl:Class ; rdfs:subClassOf :Man . :Man rdf:type owl:Class ; rdfs:subClassOf :Person . :Person rdf:type owl:Class . :Woman rdf:type owl:Class ; rdfs:subClassOf :Person . owl:NamedIndividual ; :Ester rdf:type :Woman , :hasName "Ester"^^xsd:string ; :Sigmund . :hasBrother :Olav , :Jan rdf:type :Man , :birthday " T04:30:00"^^xsd:dateTime ; :hasName "Jan"^^xsd:string ; :hasParent :Sigmund . :Kirsten rdf:type :Woman , :birthday " T14:00:00"^^xsd:dateTime ; :hasName "Kirsten"^^xsd:string ; :Olav rdf:type :Man , :hasName "Olav"^^xsd:string ; :hasSister :Ester ; :hasBrother :Sigmund . :Rolf rdf:type :Man , :hasName "Rolf"^^xsd:string . :Sigmund rdf:type :Man , :birthday " T15:00:00"^^xsd:dateTime ; :diedOn "1980/07/14T21:32:52"^^xsd:dateTime ; :hasName "Sigmund"^^xsd:string ; :hasBrother :Rolf . :Sigrund rdf:type :Woman , :birthday " T15:00:007"^^xsd:dateTime ; :hasName "Sigrund"^^xsd:string ;

38 Use SPARQL and get all parents.

39

40 PREFIX rdf: <http://www. w3
PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < PREFIX : < SELECT ?parent WHERE { ?child :hasParent ?parent }

41 Avoid duplicate listings of parents in result.

42 Avoid duplicates of parents in result.

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

44 Find all properties with values of Jan

45 Find all persons born after the beginning of 1940

46 But first find all persons.
PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT DISTINCT ?person WHERE { ?person rdf:type :Person } OBS! The result set is empty!!

47 Find all men are easy! The result is all men:
PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT DISTINCT ?person WHERE { ?person rdf:type :Man} The result is all men:

48 Find all persons, i.e., find all members of Person and the subclasses of Person
PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < 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.

49 Find all persons and their birthdays
PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT DISTINCT ?person ?birthday WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . ?person :birthday ?birthday } (Persons without a birthday will not be found.)

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

51 Find all persons born after beginning of 1940
PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT DISTINCT ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . ?person :birthday ?birthday . FILTER( ?birthday > " T00:00:01"^^xsd:dateTime)}

52 Find all landlocked countries with a population greater than 15 million. [ PREFIX rdfs: < PREFIX type: < PREFIX prop: < SELECT ?country_name ?population WHERE { ?country a type:LandlockedCountries ; rdfs:label ?country_name ; prop:populationEstimate ?population FILTER (?population > ) . } FILTER constraints use boolean conditions to filter out unwanted query results. rdfs:label is a common predicate for giving a human-friendly label to a resource.

53 Find all persons born

54 Find all persons born PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT DISTINCT ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . ?person :birthday ?birthday . FILTER( ?birthday > " T00:00:01"^^xsd:dateTime) FILTER( ?birthday < " T23:59:59"^^xsd:dateTime) }

55 Gives result: :Person, :Woman, :Man and :CityMan
:Person rdf:type owl:Class . :Woman rdf:type owl:Class ; rdfs:subClassOf :Person . :Man rdf:type owl:Class ; rdfs:subClassOf :Person . :CityMan rdf:type owl:Class ; rdfs:subClassOf :Man . PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT ?subject WHERE { ?subject rdfs:subClassOf* :Person} Match: :Person – :Woman rdfs:subClassOf :Person – :Man rdfs:subClassOf :Person – :CityMan rdfs:subClassOf :Man rdfs:subClassOf :Person Gives result: :Person, :Woman, :Man and :CityMan Transitive operator – one more example

56 Gives result: :Woman, :Man and :CityMan
:Person rdf:type owl:Class . :Woman rdf:type owl:Class ; rdfs:subClassOf :Person . :Man rdf:type owl:Class ; rdfs:subClassOf :Person . :CityMan rdf:type owl:Class ; rdfs:subClassOf :Man . PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT ?subject WHERE { ?subject rdfs:subClassOf+ :Person} Match: :Woman rdfs:subClassOf :Person - :Man rdfs:subClassOf :Person – :CityMan rdfs:subClassOf :Man rdfs:subClassOf :Person Gives result: :Woman, :Man and :CityMan

57 How to get the friends of your friends?
Existing : < :jane :friend :frank . :frank : friend :sarah . :sarah : friend :steve .

58 My friends are your friends!
@prefix : < :jane :friend :frank . :frank : friend :sarah . :sarah : friend :steve . PREFIX : < SELECT ?person WHERE { :jane :friend+ ?person . } Result: | person | ======== | :frank | | :sarah | | :steve |

59 How to list the names of all men together with death date – leave death date blank if missing?

60 Problem: List the names of all men together with their death date...
[4]: Every triple in the graph pattern must match in the data set in order for the match to succeed; if any triple fails to match, then no row appears in the result set at all. The following dos not work, it only list the men with a death date!

61 Using Optional Matches To Solve The Problem

62 MINUS Example MINUS removes patterns from the result set that match the patterns specified by the MINUS construct. Get all people (assumed) alive: PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < SELECT DISTINCT ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . MINUS {?person :diedOn ?d} } I did not get UNSAID or NOT EXISTS to work in the Protégé SPARQL plugin, but MINUS worked.

63 Yes/No Queries It is possible to ask Yes/No questions of a graph by using the ASK construct. [3]:

64 Is anybody dead, i.e., do anybody have a death date?

65 PREFIX : <http://www. uia
PREFIX : < PREFIX rdf: < PREFIX owl: < PREFIX rdfs: < PREFIX xsd: < ASK { ?person :diedOn ?deathday }

66 References [1] Book: David Poole and Alan Mackworth, Artificial Intelligence: Foundations of Computational Agents, Cambridge University Press, 2010, [2] [3] SPARQL 1.1 Query Language, W3C Recommendation 21 March 2013, [4] Semantic Web for the Working Ontologist, Second Edition: Effective Modeling in RDFS and OWL, May 20, 2011, by Dean Allemang, James Hendler [5] Appreciating SPARQL CONSTRUCT more, Bob DuCharme's weblog,


Download ppt "Jan Pettersen Nytun, UiA"

Similar presentations


Ads by Google