+ All Categories
Home > Documents > SPARQL - Forsidegrimstad.uia.no/janpn/IKT437/2016/slides/pdf/SPARQL_2.pdf · S P O Apache Jena...

SPARQL - Forsidegrimstad.uia.no/janpn/IKT437/2016/slides/pdf/SPARQL_2.pdf · S P O Apache Jena...

Date post: 09-May-2018
Category:
Upload: tranlien
View: 224 times
Download: 0 times
Share this document with a friend
66
1 SPARQL Part II Jan Pettersen Nytun, UiA
Transcript

1

SPARQL

Part

II

Jan Pettersen Nytun, UiA

2

First a small SPARQL example

Jan Pettersen Nytun, UiA

Jan Pettersen Nytun, UIA, page 3

Ask:SELECT ?who WHERE { :JamesDean :playedIn ?what .

?what :directedBy ?who . }

Answer: :GeorgeStevens, :EliaKazan, :NicholasRay, :FredGuiol

[4]:

S

OP More Meaningful Variable Names

Jan Pettersen Nytun, UIA, page 4

[4]:

5

Installing Your Own SPARQL Endpoint

Jan Pettersen Nytun, UiA

S

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

Jan Pettersen Nytun, UIA, page 6

S

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

Jan Pettersen Nytun, UIA, page 7

S

OP Downloadhttps://jena.apache.org/download/

Jan Pettersen Nytun, UIA, page 8

S

OP Starting FusekiWeb Application

Jan Pettersen Nytun, UIA, page 9

S

OP

Jan Pettersen Nytun, UIA, page 10

S

OP

Jan Pettersen Nytun, UIA, page 11

12

Using YourSPARQL 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.

@prefix : <http://www.uia.no/film_ontology#> .

@prefix owl: <http://www.w3.org/2002/07/owl#> .

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

@prefix xml: <http://www.w3.org/XML/1998/namespace> .

@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

@base <http://www.uia.no/film_ontology> .

<http://www.uia.no/film_ontology> 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 .

S

OP

Jan Pettersen Nytun, UIA, page 15

S

OP

Jan Pettersen Nytun, UIA, page 16

S

OP

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

Jan Pettersen Nytun, UIA, page 17

S

OP

Jan Pettersen Nytun, UIA, page 18

S

OP

Etc. …

Jan Pettersen Nytun, UIA, page 19

S

OP

Jan Pettersen Nytun, UIA, page 20

S

OP Insert Data

Jan Pettersen Nytun, UIA, page 21

S

OP

Jan Pettersen Nytun, UIA, page 22

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

S

OP

Jan Pettersen Nytun, UIA, page 23

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

S

OP 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#>

Jan Pettersen Nytun, UIA, page 24

S

OP

Jan Pettersen Nytun, UIA, page 25

SELECT ?actress ?movie WHERE { :JamesDean :playedIn ?movie .

?actress :playedIn ?movie . ?actress rdf:type :Woman }

[4]:

S

OP

Jan Pettersen Nytun, UIA, page 26

In Protégé:

page 27

In Fuseki

28

Defining Graphs in Fuseki

Jan Pettersen Nytun, UiA

S

OP

Jan Pettersen Nytun, UIA, page 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.

[https://www.w3.org/TR/sparql11-protocol/]:

S

OP You may add GRAPH when uploading file

Jan Pettersen Nytun, UIA, page 30

S

OP

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 }

}

Jan Pettersen Nytun, UIA, page 31

S

OP

Jan Pettersen Nytun, UIA, page 32

Find all actors.

S

OP

Jan Pettersen Nytun, UIA, page 33

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

actor1 :CarrollBaker2 :ElizabethTaylor3 :JamesDean4 :MercedesMcCambridge5 :RockHudson6 :JamesDean7 :JoVanFleet8 :JulieHarris

S

OP

Avoid duplicatesby using DISTINCT

Jan Pettersen Nytun, UIA, page 34

S

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

Jan Pettersen Nytun, UIA, page 35

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.no/janpettersennytun/ontologies/family#> .

@prefix owl: <http://www.w3.org/2002/07/owl#> .

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

@prefix xml: <http://www.w3.org/XML/1998/namespace> .

@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

@base <http://www.uia.no/janpettersennytun/ontologies/family> .

<http://www.uia.no/janpettersennytun/ontologies/family> 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 .

: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"^ x̂sd: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"^ x̂sd: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 .

S

OP

Use SPARQL and get all parents.

Jan Pettersen Nytun, UIA, page 38

S

OP

Jan Pettersen Nytun, UIA, page 39

S

OP

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 }

Jan Pettersen Nytun, UIA, page 40

S

OP

Avoid duplicate listingsof parents in result.

Jan Pettersen Nytun, UIA, page 41

S

OP

Avoid duplicatesof parents in result.

Jan Pettersen Nytun, UIA, page 42

S

OP

Find all the properties of Jan together with their

values.

Jan Pettersen Nytun, UIA, page 43

S

OP Find all properties with values of Jan

Jan Pettersen Nytun, UIA, page 44

S

OP

Find all persons born afterthe beginning of 1940

Jan Pettersen Nytun, UIA, page 45

S

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

Jan Pettersen Nytun, UIA, page 46

OBS! The result set is empty!!

S

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

Jan Pettersen Nytun, UIA, page 47

The result is all men:

S

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

Jan Pettersen Nytun, UIA, page 48

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.

S

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

Jan Pettersen Nytun, UIA, page 49(Persons without a birthday will not be found.)

S

OP

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

Jan Pettersen Nytun, UIA, page 50

S

OP Find all persons born after beginning of 1940

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 .?person :birthday ?birthday .

FILTER( ?birthday > "1940-01-01T00:00:01"^^xsd:dateTime)}

Jan Pettersen Nytun, UIA, page 51

S

OP Find me all landlocked countries with a

population greater than 15 million.[http://www.w3.org/2009/Talks/0615-qbe/]

Jan Pettersen Nytun, UIA, page 52

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

PREFIX type: <http://dbpedia.org/class/yago/>

PREFIX prop: <http://dbpedia.org/property/>

SELECT ?country_name ?population

WHERE { ?country a type:LandlockedCountries ;

rdfs:label ?country_name ;

prop:populationEstimate ?population .

FILTER (?population > 15000000) . }

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

S

OP

Find all persons born 1940-1955

Jan Pettersen Nytun, UIA, page 53

S

OP Find all persons born 1940-1955PREFIX : <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 .

?person :birthday ?birthday .

FILTER( ?birthday > "1940-01-01T00:00:01"^^xsd:dateTime)

FILTER( ?birthday < "1955-12-31T23:59:59"^^xsd:dateTime) }

Jan Pettersen Nytun, UIA, page 54

: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 : <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 ?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 :CityManJan Pettersen Nytun, UIA, page 55

Transitive operator –one more example

: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 : <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 ?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 :CityManJan Pettersen Nytun, UIA, page 56

S

OP

How to get the friends of your friends?

Jan Pettersen Nytun, UIA, page 57

Existing triples:

@prefix : <http://whatever.org/whatever#>

:jane :friend :frank .

:frank : friend :sarah .

:sarah : friend :steve .

S

OP My friends are your friends!

Jan Pettersen Nytun, UIA, page 58

@prefix : <http://rdfdata.org/whatever#>

:jane :friend :frank .

:frank : friend :sarah .

:sarah : friend :steve .

PREFIX : <http://rdfdata.org/whatever#>

SELECT ?person

WHERE {

:jane :friend+ ?person .

}

Result:-------------| person |========| :frank || :sarah || :steve |-------------

S

OP

How to list the names of all men together with death date – leave

death date blank if missing?

Jan Pettersen Nytun, UIA, page 59

S

OPProblem:

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.

page 60

The following dos not work, it only list the men with a death date!

S

OPUsing Optional Matches To Solve

The Problem

Jan Pettersen Nytun, UIA, page 61

S

OP MINUS ExampleMINUS removes patterns from the result set that match the patterns specified by the MINUS construct.

Get all people (assumed) alive:

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 .MINUS {?person :diedOn ?d} }

Jan Pettersen Nytun, UIA, page 62

I did not get UNSAID or NOT EXISTS to work in the Protégé SPARQL plugin, but MINUS worked.

S

OP Yes/No QueriesIt is possible to ask Yes/No questions of a

graph by using the ASK construct.

Jan Pettersen Nytun, UIA, page 63

[3]:

S

OP

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

Jan Pettersen Nytun, UIA, page 64

S

OP

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#>

ASK { ?person :diedOn ?deathday }

Jan Pettersen Nytun, UIA, page 65

S

OP References

Jan Pettersen Nytun, UIA, page 66

[1] Book: David Poole and Alan Mackworth, Artificial Intelligence: Foundations of Computational Agents, Cambridge University Press, 2010, http://artint.info/

[2] http://www.w3.org/TR/swbp-n-aryRelations/

[3] SPARQL 1.1 Query Language, W3C Recommendation 21 March 2013,http://www.w3.org/TR/2013/REC-sparql11-query-20130321/

[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, http://www.snee.com/bobdc.blog/2009/09/appreciating-sparql-construct.html


Recommended