SPARQL (Contd.)
Copyright By PowCoder代写 加微信 powcoder
http://www.w3.org/TR/rdf-sparql-query/
https://jena.apache.org/
Chapter 3 of Semantic Web Primer
Blank Node!
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
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
Testing For the Presence of a Pattern
@prefix :
@prefix rdf:
@prefix foaf:
:alice rdf:type foaf:Person .
:alice foaf:name “Alice” .
:bob rdf:type foaf:Person .
PREFIX rdf:
PREFIX foaf:
SELECT ?person WHERE { ?person rdf:type foaf:Person . FILTER EXISTS { ?person foaf:name ?name } }
Modify the query so that the names are also included in the result set.
Testing For the Absence of a Pattern
@prefix :
@prefix rdf:
@prefix foaf:
:alice rdf:type foaf:Person .
:alice foaf:name “Alice” .
:bob rdf:type foaf:Person .
PREFIX rdf:
PREFIX foaf:
SELECT ?person WHERE { ?person rdf:type foaf:Person . FILTER NOT EXISTS { ?person foaf:name ?name } }
Removing Possible Solutions
@prefix :
@prefix foaf:
:alice foaf:givenName “Alice” ;
foaf:familyName “Smith” .
:bob foaf:givenName “Bob” ;
foaf:familyName “Jones” .
:carol foaf:givenName “Carol” ;
foaf:familyName “Smith” .
PREFIX foaf:
SELECT DISTINCT ?s WHERE { ?s ?p ?o . MINUS { ?s foaf:givenName “Bob” . } }
Try and see 🙂
NOT EXISTS vs. MINUS: sharing variables
@prefix :
:a :b :c .
SELECT * { ?s ?p ?o FILTER NOT EXISTS { ?x ?y ?z } }
SELECT * { ?s ?p ?o MINUS { ?x ?y ?z } }
NOT EXISTS vs. MINUS: sharing variables
@prefix :
:a :b :c .
SELECT * { ?s ?p ?o FILTER NOT EXISTS { ?x ?y ?z } }
No solutions: { ?x ?y ?z } matches given any ?s ?p ?o =>
NOT EXISTS { ?x ?y ?z } eliminates any solutions.
SELECT * { ?s ?p ?o MINUS { ?x ?y ?z } }
There is no shared variable between (?s ?p ?o) and (?x ?y ?z) =>
no bindings are eliminated.
NOT EXISTS vs. MINUS: fixed patterns
@prefix :
:a :b :c .
PREFIX :
SELECT * { ?s ?p ?o FILTER NOT EXISTS { :a :b :c } }
SELECT * { ?s ?p ?o MINUS{ :a :b :c } }
NOT EXISTS vs. MINUS: fixed patterns
@prefix :
:a :b :c .
PREFIX :
SELECT * { ?s ?p ?o FILTER NOT EXISTS { :a :b :c } }
No solutions.
SELECT * { ?s ?p ?o MINUS{ :a :b :c } }
There is no match of bindings => no solutions are eliminated.
NOT EXISTS vs. MINUS: inner filters
@prefix :
:a :p 1 . :a :q 1 . :a :q 2 .
:b :p 3.0 . :b :q 4.0 . :b :q 5.0 .
PREFIX :
SELECT * WHERE { ?x :p ?n FILTER NOT EXISTS { ?x :q ?m . FILTER(?n = ?m) } }
SELECT * WHERE { ?x :p ?n MINUS { ?x :q ?m . FILTER(?n = ?m) } }
The FILTER inside the pattern does not have a value for ?n and it is always unbound:
Example (MINUS)
@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 .
PREFIX foaf:
SELECT * WHERE { ?who foaf:name ?name
?who foaf:knows ?whom .
?whom foaf:name “Simon”.
} LIMIT 50
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
@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 .
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 . }
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
@prefix dc:
@prefix :
@prefix ns:
:book1 dc:title “SPARQL Tutorial” .
:book1 ns:price 42 .
:book2 dc:title “The Semantic Web” .
:book2 ns:price 23 .
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price { VALUES ?book { :book1 :book3 }
?book dc:title ?title ; ns:price ?price . }
Assignment
VALUES: Providing inline 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 .
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price { ?book dc:title ?title ; ns:price ?price . VALUES (?book ?title) { (UNDEF “SPARQL Tutorial”) (:book2 UNDEF) } }
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
@prefix dc:
@prefix :
@prefix ns:
:book1 dc:title “SPARQL Tutorial” . :book1 ns:price 42 . :book2 dc:title “The Semantic Web” .
:book2 ns:price 23 .
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price { ?book dc:title ?title ; ns:price ?price . } VALUES (?book ?title) { (UNDEF “SPARQL Tutorial”) (:book2 UNDEF) }
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.
SELECT ?book ?title ?price {VALUES (?book ?title) { (UNDEF “SPARQL Tutorial”) (:book2 UNDEF) (:book3 “SPARQL”)}OPTIONAL{?book dc:title ?title} . OPTIONAL{ :book ns:price ?price} }
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
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
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 100 .
:book3 :price 6 ; :sold 40 .
:book4 :price 8 ; :sold 50 .
PREFIX :
SELECT (AVG(?price) AS ?avg) WHERE { ?book :price ?price ; :sold ?sold . }
GROUP BY ?sold ORDER BY ?avg
Aggregates
HAVING operates over grouped solution sets, in the same way that FILTER operates over un-grouped ones.
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 100 .
:book3 :price 6 ; :sold 40 .
:book4 :price 8 ; :sold 50 .
PREFIX :
SELECT (AVG(?price) AS ?avg) WHERE { ?book :price ?price ; :sold ?sold . } GROUP BY ?sold HAVING (AVG(?price) > 6) ORDER BY ?avg
Can the query, in the previous slide, be shortened?
Aggregates
@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 .
PREFIX :
SELECT (SUM(?lprice) AS ?totalPrice) WHERE { ?org :affiliates ?auth . ?auth :writesBook ?book . ?book :price ?lprice . } GROUP BY ?org HAVING (SUM(?lprice) > 10)
Take a break
Solution Modifiers
@prefix :
:book1 :price 9 ; :sold 100 .
:book2 :price 5 ; :sold 100 .
:book3 :price 6 ; :sold 40 .
:book4 :price 8 ; :sold 50 .
PREFIX :
SELECT ?book ?sold ?price WHERE { ?book :price ?price ; :sold ?sold} ORDER BY DESC(?sold) DESC(?price)
Solution Modifiers
Projection
@prefix foaf:
_:a foaf:name “Alice” .
_:a foaf:mbox .
_:b foaf:name “Bob” .
_:b foaf:mbox .
PREFIX foaf:
SELECT ?name WHERE { ?x foaf:name ?name }
Solution Modifiers
Duplicate Solutions
@prefix foaf:
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
PREFIX foaf:
SELECT ?name WHERE { ?x foaf:name ?name }
Solution Modifiers
Duplicate Solutions (DISTINCT)
@prefix foaf:
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
PREFIX foaf:
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }
Eliminates duplicate solutions.
Solution Modifiers
Duplicate Solutions (REDUCED)
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)
@prefix foaf:
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
PREFIX foaf:
SELECT REDUCED ?name WHERE { ?x foaf:name ?name }
Solution Modifiers
@prefix foaf:
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
PREFIX foaf:
SELECT ?name WHERE { ?x foaf:name ?name } LIMIT 1
Solution Modifiers
@prefix foaf:
_:x foaf:name “Alice” .
_:x foaf:mbox .
_:y foaf:name “Alice” .
_:y foaf:mbox .
_:z foaf:name “Alice” .
_:z foaf:mbox .
PREFIX foaf:
SELECT ?name WHERE { ?x foaf:name ?name } OFFSET 1
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
@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 .
PREFIX :
SELECT * WHERE { ?x :sold ?sold} LIMIT 3 OFFSET 1
Solution Modifiers
Select different subsets of the query solutions
@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 .
PREFIX :
SELECT * WHERE { ?x :sold ?sold} ORDER BY ?sold LIMIT 3 OFFSET 1
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
@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 .
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
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
@prefix org:
_:a org:employeeName “Alice” .
_:a org:employeeId 12345 .
_:b org:employeeName “Bob” .
_:b org:employeeId 67890 .
PREFIX foaf:
PREFIX org:
CONSTRUCT { ?x foaf:name ?name } WHERE { ?x org:employeeName ?name }
@prefix org:
_:x foaf:name “Alice” .
_:y foaf:name “Bob” .
Query Forms (Construct)
Building RDF Graphs
@prefix foaf:
_:a foaf:name “Alice” .
_:a foaf:mbox .
PREFIX foaf:
PREFIX vcard:
CONSTRUCT {
WHERE { ?x foaf:name ?name }
@prefix vcard:
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
@prefix foaf:
_:a foaf:givenname “Alice” .
_:a foaf:family_name “Hacker” .
_:b foaf:firstname “Bob” .
_:b foaf:surname “Hacker” .
PREFIX foaf:
PREFIX vcard:
CONSTRUCT { ?x vcard:N _:v . _:v vcard:givenName ?gname ._:v vcard:familyName ?fname }
{ ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } .
{ ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname } .
Query Forms (Construct)
Templates with Blank Nodes
@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” .
@prefix vcard:
@prefix foaf:
[ vcard:N [ vcard:familyName “Hacker” ; vcard:givenName “Bob”]] .
[ vcard:N [ vcard:familyName “Hacker” ; vcard:givenName “Alice”]] .
Query Forms (Construct)
CONSTRUCT WHERE
PREFIX foaf:
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com