程序代写代做代考 Lecture 4

Lecture 4

Lecture 4

SPARQL (Contd.)

http://www.w3.org/TR/rdf-sparql-query/
https://jena.apache.org/
Chapter 3 of Semantic Web Primer

Dr. Davoud Mougouei

Blank Node!




2018-01-12




2

Blank Node!
@prefix : .
@prefix rdf: .
@prefix owl: .
@prefix xsd: .

:Time_Span a owl:Class .
:event a :Activity ;
:has_time_span [
a :Time_Span ;
:at_some_time_within_date “2018-01-12″^^xsd:date
] .
3

4

Identify the blank node in the following XML/RDF document. Then write the TTL representation of the document.






Lecture Outline
SPARQL: Querying RDF Documents
Programming the semantic Web

Negation
Testing For the Presence of a Pattern
Data:
@prefix : .
@prefix rdf: .
@prefix foaf: .
:alice rdf:type foaf:Person .
:alice foaf:name “Alice” .
:bob rdf:type foaf:Person .
Query :
PREFIX rdf:
PREFIX foaf:
SELECT ?person WHERE { ?person rdf:type foaf:Person . FILTER EXISTS { ?person foaf:name ?name } }
Result:

Modify the query so that the names are also included in the result set.

Negation
Testing For the Absence of a Pattern
Data:
@prefix : .
@prefix rdf: .
@prefix foaf: .
:alice rdf:type foaf:Person .
:alice foaf:name “Alice” .
:bob rdf:type foaf:Person .
Query :
PREFIX rdf:
PREFIX foaf:
SELECT ?person WHERE { ?person rdf:type foaf:Person . FILTER NOT EXISTS { ?person foaf:name ?name } }
Result:

Negation
Removing Possible Solutions
Data:
@prefix : .
@prefix foaf: .
:alice foaf:givenName “Alice” ;
foaf:familyName “Smith” .
:bob foaf:givenName “Bob” ;
foaf:familyName “Jones” .
:carol foaf:givenName “Carol” ;
foaf:familyName “Smith” .
Query :
PREFIX foaf:
SELECT DISTINCT ?s WHERE { ?s ?p ?o . MINUS { ?s foaf:givenName “Bob” . } }
Result:
Try and see 🙂

Negation
NOT EXISTS vs. MINUS: sharing variables

Data:
@prefix : .
:a :b :c .
Query :
SELECT * { ?s ?p ?o FILTER NOT EXISTS { ?x ?y ?z } }
Result:
???
Query :
SELECT * { ?s ?p ?o MINUS { ?x ?y ?z } }
Result:
???

Negation
NOT EXISTS vs. MINUS: sharing variables

Data:
@prefix : .
:a :b :c .
Query :
SELECT * { ?s ?p ?o FILTER NOT EXISTS { ?x ?y ?z } }
Result:
No solutions: { ?x ?y ?z } matches given any ?s ?p ?o =>
NOT EXISTS { ?x ?y ?z } eliminates any solutions.
Query :
SELECT * { ?s ?p ?o MINUS { ?x ?y ?z } }
Result:
There is no shared variable between (?s ?p ?o) and (?x ?y ?z) =>
no bindings are eliminated.

Negation
NOT EXISTS vs. MINUS: fixed patterns

Data:
@prefix : .
:a :b :c .
Query :
PREFIX :
SELECT * { ?s ?p ?o FILTER NOT EXISTS { :a :b :c } }
Result:
???
Query :
SELECT * { ?s ?p ?o MINUS{ :a :b :c } }
Result:
???

Negation
NOT EXISTS vs. MINUS: fixed patterns

Data:
@prefix : .
:a :b :c .
Query :
PREFIX :
SELECT * { ?s ?p ?o FILTER NOT EXISTS { :a :b :c } }
Result:
No solutions.
Query :
SELECT * { ?s ?p ?o MINUS{ :a :b :c } }
Result:
There is no match of bindings => no solutions are eliminated.

Negation
NOT EXISTS vs. MINUS: inner filters

Data:
@prefix : .
:a :p 1 . :a :q 1 . :a :q 2 .
:b :p 3.0 . :b :q 4.0 . :b :q 5.0 .
Query :
PREFIX :
SELECT * WHERE { ?x :p ?n FILTER NOT EXISTS { ?x :q ?m . FILTER(?n = ?m) } }
Result:

Query :
SELECT * WHERE { ?x :p ?n MINUS { ?x :q ?m . FILTER(?n = ?m) } }
Result:
The FILTER inside the pattern does not have a value for ?n and it is always unbound:

Negation
Example (MINUS)
Data:
@prefix foaf: .
_:bob foaf:name “Bob” .
_:simon foaf:name “Simon” .
_:alice foaf:name “Alice” ; foaf:knows _:simon .
_:john foaf:name “John” ; foaf:knows _:alice .
_:eve foaf:name “Eve” ; foaf:knows _:alice .
_:dan foaf:name “Dan” ; foaf:knows _:simon .
Query :
PREFIX foaf:
SELECT * WHERE { ?who foaf:name ?name
MINUS {
?who foaf:knows ?whom .
?whom foaf:name “Simon”.
}
} LIMIT 50
Result:
Try and see 🙂

Using MINUS, Write a query that returns the names of the people except those who know someone that knows someone whose name is “Simon”.

Using EXISTS, write a query that returns the names of the people who directly or indirectly (via someone else) know someone whose name is “Simon”

Assignment
BIND: Assigning to Variables

Data:
@prefix dc: .
@prefix : .
@prefix ns: .
:book1 dc:title “SPARQL Tutorial” .
:book1 ns:price 42 . :book1 ns:discount 0.2 .
:book2 dc:title “The Semantic Web” .
:book2 ns:price 23 .
:book2 ns:discount 0.25 .
Query :
PREFIX dc:
PREFIX ns:
PREFIX :
SELECT ?title ?price { ?x ns:price ?p . ?x ns:discount ?discount BIND (?p*(1-?discount) AS ?price) FILTER(?price < 20) ?x dc:title ?title . } Result: Write a query that returns the title and the price of the cheapest discounted book. Assignment VALUES: Providing inline data Data can be directly written in a graph pattern or added to a query using VALUES.  VALUES allows multiple variables to be specified in the data block. Assignment VALUES: Providing inline data Data: @prefix dc: .
@prefix : .
@prefix ns: .
:book1 dc:title “SPARQL Tutorial” .
:book1 ns:price 42 .
:book2 dc:title “The Semantic Web” .
:book2 ns:price 23 .
Query :
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price { VALUES ?book { :book1 :book3 }
?book dc:title ?title ; ns:price ?price . }
Result:

Assignment
VALUES: Providing inline data
Data:
@prefix dc: .
@prefix : .
@prefix ns: .
:book1 dc:title “SPARQL Tutorial” . :book1 ns:price 42 . :book2 dc:title “The Semantic Web” .
:book2 ns:price 23 .
Query :
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price { ?book dc:title ?title ; ns:price ?price . VALUES (?book ?title) { (UNDEF “SPARQL Tutorial”) (:book2 UNDEF) } }
Result:

If a variable has no value for a particular solution in the VALUES clause, the keyword UNDEF is used instead of an RDF term.

Assignment
VALUES: Providing inline data
Data:
@prefix dc: .
@prefix : .
@prefix ns: .
:book1 dc:title “SPARQL Tutorial” . :book1 ns:price 42 . :book2 dc:title “The Semantic Web” .
:book2 ns:price 23 .
Query :
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price { ?book dc:title ?title ; ns:price ?price . } VALUES (?book ?title) { (UNDEF “SPARQL Tutorial”) (:book2 UNDEF) }
Result:

The VALUES might have been specified to execute over the results of the SELECT query.

What are the results of the following queries? Compare the results.
Query 1
SELECT ?book ?title ?price {VALUES (?book ?title) { (UNDEF “SPARQL Tutorial”) (:book2 UNDEF) (:book3 “SPARQL”)}OPTIONAL{?book dc:title ?title} . OPTIONAL{ :book ns:price ?price} }
Query 2
SELECT ?book ?title ?price {OPTIONAL{?book dc:title ?title} . OPTIONAL{ :book ns:price ?price} VALUES (?book ?title) { (UNDEF “SPARQL Tutorial”) (:book2 UNDEF) (:book3 “SPARQL”)} }

Aggregates

Aggregates apply expressions over groups of solutions, to see a result which is computed over a group of solutions, rather than a single solution.

The maximum value that a particular variable takes, rather than each value individually.

By default a solution set consists of a single group, containing all solutions. Grouping may be specified using the GROUP BY syntax.

COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT, and SAMPLE.

Aggregates
GROUP BY

Query :
SELECT (AVG(?y) AS ?avg) WHERE { ?a 😡 ?x ; :y ?y . } GROUP BY ?x

In order to calculate aggregate values for a solution, the solution is first divided into one or more groups, and the aggregate value is calculated for each group.

If aggregates are used in the query level in SELECT, HAVING or ORDER BY but the GROUP BY term is not used, then this is taken to be a single implicit group, to which all solutions belong.

Within GROUP BY clauses the binding keyword, AS, may be used, such as GROUP BY (?x + ?y AS ?z). This is equivalent to { … BIND (?x + ?y AS ?z) } GROUP BY ?z.

For example, given a solution sequence S, ( {?x→2, ?y→3}, {?x→2, ?y→5}, {?x→6, ?y→7} ), we might wish to group the solutions according to the value of ?x, and calculate the average of the values of ?y for each group:

Aggregates
Group By

Data:
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 100 .
:book3 :price 6 ; :sold 40 .
:book4 :price 8 ; :sold 50 .
Query :
PREFIX :
SELECT (AVG(?price) AS ?avg) WHERE { ?book :price ?price ; :sold ?sold . }
GROUP BY ?sold ORDER BY ?avg
Result:

Aggregates
Having

HAVING operates over grouped solution sets, in the same way that FILTER operates over un-grouped ones.
Data:
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 100 .
:book3 :price 6 ; :sold 40 .
:book4 :price 8 ; :sold 50 .
Query :
PREFIX :
SELECT (AVG(?price) AS ?avg) WHERE { ?book :price ?price ; :sold ?sold . } GROUP BY ?sold HAVING (AVG(?price) > 6) ORDER BY ?avg
Result:

Can the query, in the previous slide, be shortened?

Aggregates
Example

Data:
@prefix : .
:org1 :affiliates :auth1, :auth2 .
:auth1 :writesBook :book1, :book2 .
:book1 :price 9 .
:book2 :price 5 .
:auth2 :writesBook :book3 .
:book3 :price 7 .
:org2 :affiliates :auth3 .
:auth3 :writesBook :book4 .
:book4 :price 7 .
Query :
PREFIX :
SELECT (SUM(?lprice) AS ?totalPrice) WHERE { ?org :affiliates ?auth . ?auth :writesBook ?book . ?book :price ?lprice . } GROUP BY ?org HAVING (SUM(?lprice) > 10)
Result:
????

Take a break 
32

Solution Modifiers
ORDER BY

Data:
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 100 .
:book3 :price 6 ; :sold 40 .
:book4 :price 8 ; :sold 50 .
Query :
PREFIX :
SELECT ?book ?sold ?price WHERE { ?book :price ?price ; :sold ?sold} ORDER BY DESC(?sold) DESC(?price)
Result:

Solution Modifiers
Projection

Data:
@prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:mbox .
_:b foaf:name “Bob” .
_:b foaf:mbox .
Query :
PREFIX foaf:
SELECT ?name WHERE { ?x foaf:name ?name }
Result:

Solution Modifiers
Duplicate Solutions
Data:
@prefix foaf: .
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
Query:
PREFIX foaf:
SELECT ?name WHERE { ?x foaf:name ?name }
Result:

Solution Modifiers
Duplicate Solutions (DISTINCT)
Data:
@prefix foaf: .
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
Query :
PREFIX foaf:
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }
Result:

 Eliminates duplicate solutions.

Solution Modifiers
Duplicate Solutions (REDUCED)
37
While the DISTINCT modifier ensures that duplicate solutions are eliminated from the solution set, REDUCED simply permits them to be eliminated.
The cardinality of any set of variable bindings in a REDUCED solution set is at least one and not more than the cardinality of the solution set with no DISTINCT or REDUCED modifier.

Solution Modifiers
Duplicate Solutions (REDUCED)
Data:
@prefix foaf: .
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
Query :
PREFIX foaf:
SELECT REDUCED ?name WHERE { ?x foaf:name ?name }
Result:
????

Solution Modifiers
LIMIT
Data:
@prefix foaf: .
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
Query :
PREFIX foaf:
SELECT ?name WHERE { ?x foaf:name ?name } LIMIT 1
Result:
????

Solution Modifiers
OFFSET
Data:
@prefix foaf: .
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
Query :
PREFIX foaf:
SELECT ?name WHERE { ?x foaf:name ?name } OFFSET 1
Result:
????

causes the solutions generated to start after the specified number of solutions. An OFFSET of zero has no effect.

Solution Modifiers
Select different subsets of the query solutions
Data:
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 90 .
:book3 :price 6 ; :sold 60 .
:book4 :price 8 ; :sold 40 .
:book5 :price 4 ; :sold 45 .
:book6 :price 5 ; :sold 30 .
:book7 :price 3 ; :sold 20 .
:book8 :price 1 ; :sold 15 .
:book9 :price 8 ; :sold 10 .
:book10 :price 7 ; :sold 50 .
Query :
PREFIX :
SELECT * WHERE { ?x :sold ?sold} LIMIT 3 OFFSET 1
Result:
????

Solution Modifiers
Select different subsets of the query solutions
Data:
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 90 .
:book3 :price 6 ; :sold 60 .
:book4 :price 8 ; :sold 40 .
:book5 :price 4 ; :sold 45 .
:book6 :price 5 ; :sold 30 .
:book7 :price 3 ; :sold 20 .
:book8 :price 1 ; :sold 15 .
:book9 :price 8 ; :sold 10 .
:book10 :price 7 ; :sold 50 .
Query :
PREFIX :
SELECT * WHERE { ?x :sold ?sold} ORDER BY ?sold LIMIT 3 OFFSET 1
Result:
????

Using NOT EXISTS, write a query that returns, in descending order, the top 3 best-selling books that sold at least 30 times.

https://funnyjunk.com/

Solution Modifiers
Select different subsets of the query solutions
Data:
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 90 .
:book3 :price 6 ; :sold 60 .
:book4 :price 8 ; :sold 40 .
:book5 :price 4 ; :sold 45 .
:book6 :price 5 ; :sold 30 .
:book7 :price 3 ; :sold 20 .
:book8 :price 1 ; :sold 15 .
:book9 :price 8 ; :sold 10 .
:book10 :price 7 ; :sold 50 .
Query :
PREFIX :
SELECT ?price (AVG (?sold) AS ?S_AVG) WHERE { ?x :price ?price; :sold ?sold . FILTER (?sold>=30)} GROUP BY ?price ORDER BY DESC (?S_AVG) LIMIT 6 OFFSET 0
Result:
????

Query Forms
SPARQL has four query forms. These query forms use the solutions from pattern matching to form result sets or RDF graphs.
SELECT Returns all, or a subset of, the variables bound in a query pattern match.
CONSTRUCT Returns an RDF graph constructed by substituting variables in a set of triple templates.
ASK Returns a boolean indicating whether a query pattern matches or not.
DESCRIBE Returns an RDF graph that describes the resources found.

Query Forms (Construct)
Building RDF Graphs
The CONSTRUCT query returns a single RDF graph specified by a graph template.
The result is an RDF graph formed by taking each query solution in the solution sequence, substituting for the variables in the graph template, and combining the triples into a single RDF graph by set union

Query Forms (Construct)
Building RDF Graphs

Data:
@prefix org: .
_:a org:employeeName “Alice” .
_:a org:employeeId 12345 .
_:b org:employeeName “Bob” .
_:b org:employeeId 67890 .
Query:
PREFIX foaf:
PREFIX org:
CONSTRUCT { ?x foaf:name ?name } WHERE { ?x org:employeeName ?name }
Result:
@prefix org: .
_:x foaf:name “Alice” .
_:y foaf:name “Bob” .

Query Forms (Construct)
Building RDF Graphs

Data:
@prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:mbox .
Query:
PREFIX foaf:
PREFIX vcard:
CONSTRUCT { vcard:FN ?name}
WHERE { ?x foaf:name ?name }
Result:
@prefix vcard: . vcard:FN “Alice” .

Query Forms (Construct)
Templates with Blank Nodes

A template can create an RDF graph containing blank nodes.

The blank node labels are scoped to the template for each solution.

If the same label occurs twice in a template, then there will be one blank node created for each query solution, but there will be different blank nodes for triples generated by different query solutions.

Query Forms (Construct)
Templates with Blank Nodes

Data:
@prefix foaf: .
_:a foaf:givenname “Alice” .
_:a foaf:family_name “Hacker” .
_:b foaf:firstname “Bob” .
_:b foaf:surname “Hacker” .
Query:
PREFIX foaf:
PREFIX vcard:
CONSTRUCT { ?x vcard:N _:v . _:v vcard:givenName ?gname ._:v vcard:familyName ?fname }
WHERE
{
{ ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } .
{ ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname } .
}
Result:
???

51
Query Forms (Construct)
Templates with Blank Nodes
Result:
@prefix vcard: .
_:v1 vcard:N _:x . _:x vcard:givenName “Alice” . _:x vcard:familyName “Hacker” .
_:v2 vcard:N _:z . _:z vcard:givenName “Bob” . _:z vcard:familyName “Hacker” .
Result:
@prefix vcard: .
@prefix foaf:  .
[ vcard:N  [ vcard:familyName  “Hacker” ; vcard:givenName   “Bob”]] .
[ vcard:N  [ vcard:familyName  “Hacker” ; vcard:givenName   “Alice”]] .
Result:

52
Query Forms (Construct)
CONSTRUCT WHERE
Query
PREFIX foaf: CONSTRUCT { ?x foaf:name ?name } WHERE { ?x foaf:name ?name }
Query (short form); WHERE is essential
PREFIX foaf: CONSTRUCT
WHERE { ?x foaf:name ?name }
When to use the short form?
When the template and the pattern are the same and the pattern is just a basic graph pattern (no FILTERs and no complex graph patterns are used). 

Write a query that constructs a graph of data model containing all people older than 40.
Data:
@prefix foaf: .
_:a foaf:givenname “Alice” ; foaf:family_name “Hacker” ; foaf:old 30 .
_:b foaf:firstname “Bob” ; foaf:surname “Hacker”; foaf:old 25 .
_:c foaf:givenname “Jeff” ; foaf:surname “Cool”; foaf:age 32 .
_:d foaf:firstname “David” ; foaf:surname “Goodman”; foaf:old 41 .
_:e foaf:givenname “Jenny” ; foaf:family_name “Cool”; foaf:age 50 .
_:f foaf:firstname “Sarah” ; foaf:surname “Hacker”; foaf:age 62 .

54
Query Forms (ASK)
Data:
@prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:homepage .
_:b foaf:name “Bob” .
_:b foaf:mbox .
Query:
PREFIX foaf:
ASK { ?x foaf:name “Alice” }
Result:
true

To test whether or not a query pattern has a solution.
No information is returned about the possible query solutions, just whether or not a solution exists.

55
Query Forms (ASK)
Data:
@prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:homepage .
_:b foaf:name “Bob” .
_:b foaf:mbox .
Query:
PREFIX foaf:
ASK { ?x foaf:name “Alice” ; foaf:mbox }
Result:
false

56
Query Forms (DESCRIBE)
Returns a single result RDF graph containing RDF data about resources.

This data is not prescribed by a SPARQL query, where the query client would need to know the structure of the RDF in the data source, but, instead, is determined by the SPARQL query processor.

The query pattern is used to create a result set.

The DESCRIBE form takes each of the resources identified in a solution, together with any resources directly named by IRI, and assembles a single RDF graph by taking a “description” which can come from any information available including the target RDF Dataset. The description is determined by the query service.

57
Query Forms (DESCRIBE)
Data
@prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:homepage .
_:b foaf:name “Bob” .
_:b foaf:mbox .
Query
PREFIX foaf:
DESCRIBE ?x WHERE {?x foaf:mbox
Result:

58
Query Forms (DESCRIBE)
Data
@prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:homepage .
_:b foaf:name “Bob” .
_:b foaf:mbox .
Query
PREFIX foaf:
DESCRIBE ?x WHERE { ?x foaf:name “Alice” } 
Result:

Is DESCRIBE an essential query form? i.e., can we achieve the same results using other query forms?

Lecture Outline
SPARQL: Querying RDF Documents
Programming the semantic Web

Programming the Semantic Web
61
    
 W

GraphDB
62

RDF4J

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

Apache Jena
Architecture

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

Getting Started

65

Lab Exercise 2

Task 1: Write and execute the SPARQL queries in Lecture 3 and Lecture 4 slides in both GraphDB and Apache Jena (download the eclipse project from Moodle). There is no need to submit your answers for this task.

Task 2: Answer the questions in Lecture 3 and Lecture 4 slides (question slides) and submit your answers via Moodle as a single PDF file.
66

/docProps/thumbnail.jpeg