程序代写代做代考 Lecture 5

Lecture 5

Lecture 5

RDF & SPARQL (Contd.)

The slides are prepared by Dr. Davoud Mougouei

Lecture Outline
Programming the semantic Web
Working with RDF documents in Apache Jena
SPARQL queries in Jena
Remote data and local query processor
SPARQL endpoint
Understanding the semantic of SPARQL queries
Movie example

Programming the Semantic Web
3
    
 W

GraphDB
4

RDF4J

5
https://rdf4j.org/documentation/programming/

Apache Jena
Architecture

6
https://jena.apache.org/getting_started/index.html

Getting Started

7

Maven Dependencies

8

Copyright (Jena Examples)

9

Working with RDF Documents in Jena

Chapter 1
A Semantic Web Primer
10

A Simple RDF Model
11

Resources as Property Values
12

Resources as Property Values

13

Attribute Accessor Methods
14

Write RDF/XML into File
15

Read RDF XML from a File
16

Navigating a Model
17

Selecting the VCARD Resources
18

Selector Methods
19

Graph Operations
20

21

Graph Operations
Union

Containers
22

Literals
23

Literals

24
[ “11”^^ , “11” ] .

[ “chat”@fr , “chat”@en ] .

SPARQL Queries in Jena

25

26
SPARQL Queries
Libraries

SPARQL Queries
Select (local)
27

SPARQL Queries
Select (remote)
28

SPARQL Queries
Construct (local)
29

SPARQL Queries
Ask (local)
30

SPARQL Queries
Describe (local)
31

Take a break 
32

Remote Data &
Local Query Processor

Data is accessed via URI
Query is processed using Jena
Chapter 1
33

SPARQL Queries
Remote Data & Local Query Processor
34
Data:
http://dig.csail.mit.edu/2008/webdav/timbl/foaf.rdf
Query:
PREFIX foaf:
SELECT * WHERE {
?s foaf:name ?name . FILTER regex (?name,”^A” , “i”)
} limit 50
Results:
???

Write a query that finds the names that start with either A or B and end with C.

SPARQL Queries
Remote Data & Local Query Processor
36
Data:
http://dig.csail.mit.edu/2008/webdav/timbl/foaf.rdf
Query:
PREFIX foaf:
PREFIX card:
SELECT ?homepage
WHERE {
card:i foaf:knows ?known .
?known foaf:homepage ?homepage .
}
Results:
???

Query is processed at the Endpoint
SPARQL Endpoint
37

SPARQL Queries
SPARQL Endpoint
38
Endpoint:
http://dbpedia.org/sparql
Prefixes:
PREFIX owl:
PREFIX xsd:
PREFIX rdfs:
PREFIX rdf:
PREFIX foaf:
PREFIX dc:
PREFIX :
PREFIX dbpedia2:
PREFIX dbpedia:
PREFIX skos:
PREFIX dbo:

SPARQL Queries
SPARQL Endpoint
39
Semantic:
People who were born in Sydney before 1985
Query:
SELECT ?name ?birth ?death ?person
WHERE {
?person dbo:birthPlace :Sydney .
?person dbo:birthDate ?birth .
?person foaf:name ?name .
?person dbo:deathDate ?death .
FILTER (?birth < ”1985-01-01"^^xsd:date) . } ORDER BY ?name LIMIT 50 Results: ??? SPARQL Queries SPARQL Endpoint 40 Semantic: Musicians with German and English descriptions. Query: SELECT ?name ?description_en ?description_de ?musician WHERE { ?musician foaf:name ?name . ?musician a dbo:MusicalArtist . OPTIONAL { ?musician rdfs:comment ?description_en . FILTER (LANG(?description_en) = 'en') . } OPTIONAL { ?musician rdfs:comment ?description_de . FILTER (LANG(?description_de) = 'de') . } } LIMIT 50 Results: ??? SPARQL Queries SPARQL Endpoint 41 Semantic: Musicians who were born in Berlin. Query: SELECT ?name ?birth ?description ?person WHERE { ?person a dbo:MusicalArtist . ?person dbo:birthPlace :Berlin . ?person dbo:birthDate ?birth . ?person foaf:name ?name . ?person rdfs:comment ?description . FILTER (LANG(?description) = 'en') . } ORDER BY ?name LIMIT 50 Results: ??? SPARQL Queries SPARQL Endpoint 42 Semantic: Soccer players who are born in a country with more than 10 million inhabitants, who played as goalkeeper for a club that has a stadium with more than 30,000 seats and the club country is different from the birth country. Query: SELECT distinct ?soccerplayer ?countryOfBirth ?team ?countryOfTeam ?stadiumcapacity { ?soccerplayer a dbo:SoccerPlayer ; dbo:position ;
dbo:birthPlace/dbo:country* ?countryOfBirth ;
#dbo:number 13 ;
dbo:team ?team .
?team dbo:capacity ?stadiumcapacity ; dbo:ground ?countryOfTeam .
?countryOfBirth a dbo:Country ; dbo:populationTotal ?population .
?countryOfTeam a dbo:Country .
FILTER (?countryOfTeam != ?countryOfBirth)
FILTER (?stadiumcapacity > 30000)
FILTER (?population > 10000000)
} order by ?soccerplayer
Results:
???

Understanding the Semantic of SPARQL Queries

43

SPARQL Queries
Query Semantic
44
Query :
PREFIX foaf: PREFIX res:
SELECT DISTINCT ?string WHERE { res:Tom_Cruise foaf:homepage ?string . }
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
45
Query :
PREFIX dbo:
PREFIX res:
PREFIX rdf:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE {
?uri rdf:type dbo:Film . ?uri dbo:starring res:Julia_Roberts . ?uri dbo:starring res:Richard_Gere. OPTIONAL {?uri rdfs:label ?string . FILTER (lang(?string) = ‘en’) }
} LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
46
Query :
PREFIX dbo:
PREFIX res:
PREFIX rdf:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE {
?uri rdf:type dbo:Book . ?uri dbo:author res:Danielle_Steel .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = ‘en’) }
} LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
47
Query :
PREFIX res:
PREFIX dbp:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE {
res:WikiLeaks dbp:awards ?uri .
OPTIONAL { ?uri rdfs:label ?string. FILTER (lang(?string) = ‘en’) }
} LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
48
Query :
PREFIX dbo:
PREFIX res:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE {
res:Nile dbo:sourceCountry ?uri . OPTIONAL {
?uri rdfs:label ?string. FILTER (lang(?string) = ‘en’) }
} LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
49
Query :
PREFIX dbp:
PREFIX dbo:
PREFIX res:
PREFIX rdf:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE {
?uri rdf:type dbo:Company . ?uri dbp:industry ?industry . FILTER regex(?industry,’advertising’,’i’) . OPTIONAL {?uri rdfs:label ?string . FILTER (lang(?string) = ‘en’) }
} LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
50
Query :
PREFIX res:
PREFIX dbo:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE { res:Bruce_Carver dbo:deathCause ?uri . OPTIONAL {?uri rdfs:label ?string. FILTER (lang(?string) = ‘en’) }
} LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
51
Query :
PREFIX dbo:
PREFIX res:
PREFIX rdf:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE {
?uri rdf:type dbo:Airport . ?uri dbo:location res:California .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = ‘en’) }
} LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
52
Query :
PREFIX dbo:
PREFIX res:
PREFIX rdf:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE {
?uri rdf:type dbo:Film. ?uri dbo:starring res:Tom_Cruise .
OPTIONAL {?uri rdfs:label ?string . FILTER (lang(?string) = ‘en’) }
} LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
53
Query :
PREFIX dbo:
PREFIX res:
PREFIX rdf:
PREFIX rdfs:
SELECT DISTINCT ?uri ?string WHERE {
?company rdf:type dbo:Organisation .
?company dbo:foundationPlace res:California .
?uri dbo:developer ?company .
?uri rdf:type dbo:Software . OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = ‘en’) } } LIMIT 50
Semantic:
???
Results:
???

SPARQL Queries
Query Semantic
54
Query :
PREFIX dbo:
PREFIX res:
PREFIX rdf:
PREFIX rdfs:
SELECT COUNT(DISTINCT ?uri) WHERE {
?uri rdf:type dbo:Film .
?uri dbo:starring res:Leonardo_DiCaprio .
} LIMIT 50
Semantic:
???
Results:
???

Movies Example
55

Movies Example
Prefixes
56

Movies Example
RDF (TTL)
57

Movies Example
SPARQL Query
58
Query :
BASE
PREFIX xs:
SELECT DISTINCT ?p{
?s ?p ?o .
}
Semantic:
???
Results:
???

Movies Example
SPARQL Query
59
Query :
BASE
PREFIX xs:
SELECT ?m ?c WHERE {
?m ?c FILTER (?c = “Australia”).
}
Semantic:
???
Results:
???

Movies Example
SPARQL Query
60
Query :
BASE
PREFIX xs:
SELECT (COUNT(?c) as ?ct) WHERE {
?movie ?c .
?movie ?g FILTER regex(?g,”Horror”).
} GROUP BY ?c
Semantic:
???
Results:
???

Movies Example
SPARQL Query
61
Query :
BASE
PREFIX xs:
SELECT * {
?m ?score .
} ORDER BY DESC(?score) OFFSET 0 LIMIT 5
Semantic:
???
Results:
???

Movies Example
SPARQL Query
62
Query :
BASE
PREFIX xs:
SELECT ?c (AVG(xs:integer(?s)) as ?av) WHERE {
?m ?s .
?m ?c .
} GROUP BY (?c)
Semantic:
???
Results:
???

Movies Example
SPARQL Query
63
Query :
BASE
PREFIX xs:
SELECT ?c (SUM(xs:float(?d)) as ?total) WHERE {
?m ?d .
?m ?c .
} GROUP BY (?c)
Semantic:
???
Results:
???

Resources
http://www.w3.org/TR/rdf-sparql-query/
https://jena.apache.org/
https://aifb-ls3-kos.aifb.kit.edu/projects/spartiqulator/examples.htm
Chapter 3 of Semantic Web Primer

64

/docProps/thumbnail.jpeg