程序代写代做代考 SQL Week 3

Week 3

Week 3
RDF: The Turtle Data Format
SPARQL (Basic)

Main Sources:

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

Dr. Davoud Mougouei

Lecture Outline
Turtle Syntax of RDF
SPARQL: Querying RDF Documents

Terse RDF Triple Language (Turtle)

The contents are mainly from: https://www.w3.org/TR/turtle/

What is it?
An alternative to RDF/XML
A textual syntax for RDF 
Allows an RDF graph to be completely written in a compact and natural text form with abbreviations for common usage patterns and datatypes.
Provides levels of compatibility with the triple pattern syntax of the SPARQL W3C Recommendation.
Offers better readability.
A Turtle document is a textual representations of an RDF graph

Green Goblin and Spiderman!
Turtle representation

@base .
@prefix rdf: .
@prefix rdfs: .
@prefix foaf: .
@prefix rel: .

<#green-goblin>
rel:enemyOf <#spiderman> ;
a foaf:Person ; # in the context of the Marvel universe foaf:name “Green Goblin” .

<#spiderman>
rel:enemyOf <#green-goblin> ;
a foaf:Person ; foaf:name “Spiderman”, “Человек-паук”@ru .

Green Goblin and Spiderman!
RDF/XML representation


.org/#green-goblin”>


Spiderman
Человек-паук


Green Goblin

Green Goblin and Spiderman!
Graph representation

Green Goblin and Spiderman!
Summary
This example introduced many of features of the Turtle language: @base and Relative IRIs, @prefix and prefixed names, predicate lists separated by ‘;’, object lists separated by ‘,’, the token a, and literals.

The Turtle grammar for triples is a subset of the SPARQL 1.1 Query Language grammar for TriplesBlock.

Simple Triples
The simplest triple statement is a sequence of (subject, predicate, object) terms, separated by whitespace and terminated by ‘.’ after each triple.

.

Predicate Lists

The subject will be referenced by a number of predicates and objects, separated by ‘;’.



;

“Spiderman” .



.


“Spiderman” .

Object Lists
A series of objects separated by ‘,’ following a predicate.

“Spiderman” , “Человек-паук”@ru .

“Spiderman” . “Человек-паук”@ru .

IRIs
Define a prefix label for the vocabulary IRI 

@prefix somePrefix: .
somePrefix:enemyOf
.

Define a prefix label using the SPARQL syntax

PREFIX somePrefix:

somePrefix:enemyOf
.

Note!
The ‘@prefix’ and ‘@base’ directives require a trailing ‘.’ after the IRI, the equalivent ‘PREFIX’ and ‘BASE’ must not have a trailing ‘.’ after the IRI part of the directive.

Write a Turtle representation
of the following RDF/XML document.
Validate your answer here:

http://ttl.summerofcode.be/

Example
RDF/XML




Discrete Maths




David Billington
Associate Professor

@prefix uni: .
@prefix rdf: .
@base .

<#CIT1111>
a uni:course;
uni:courseName “Discrete Maths” ;
uni:isTaughtBy <#T949318> .
<#T949318>
a uni:lecturer;
uni:name “David Billington” ;
uni:title “Associate Professor” .
Example
Turtle

Is this statement correct?

For a given RDF/XML document, there exists only one Turtle representation.

Example
Graph representation

Literals

@prefix : .

:atomicNumber 2 ; # xsd:integer
:atomicMass 4.002602 ; # xsd:decimal
:specificGravity 1.663E-4 ; # xsd:double
:isGass true . # xsd:boolean

http://www.ldf.fi/service/rdf-grapher

Literals
@prefix rdfs: .
@prefix show: .
@prefix xsd: .
show:218 rdfs:label “That Seventies Show”^^xsd:string . # literal with XML Schema string datatype
show:218 rdfs:label “That Seventies Show”^^ . # same as above
show:218 rdfs:label “That Seventies Show” . # same again
show:218 show:localName “That Seventies Show”@en . # literal with a language tag
show:218 show:localName ‘Cette Série des Années Soixante-dix’@fr . # literal delimited by single quote
show:218 show:localName “Cette Série des Années Septante”@fr-be . # literal with a region subtag

http://www.ldf.fi/service/rdf-grapher

Blank Nodes
RDF blank nodes in Turtle are expressed as _: followed by a blank node label which is a series of name characters.

@prefix foaf: .
_:alice foaf:knows _:bob .
_:bob foaf:knows _:alice .

@prefix foaf:
# Someone knows someone else, who has the name “David”.
[] foaf:knows [ foaf:name “David” ] .

Blank Nodes
Graph Representation

@prefix foaf: .
_:alice foaf:knows _:bob .
_:bob foaf:knows _:alice .
@prefix foaf:
# Someone knows someone else, who has the name “David”.
[] foaf:knows [ foaf:name “David” ] .

Nesting Unlabeled Blank Nodes

@prefix foaf: .
[ foaf:name “Alice” ] foaf:knows
[ foaf:name “Bob” ; foaf:knows [ foaf:name “Eve” ] ;
foaf:mbox ] .

The Graph representation?

Nesting Unlabeled Blank Nodes
Graph Representation

@prefix foaf: .
[ foaf:name “Alice” ] foaf:knows [ foaf:name “Bob” ; foaf:knows [ foaf:name “Eve” ] ; foaf:mbox ] .
http://www.ldf.fi/service/rdf-grapher

Collections

@prefix : .
# the object of this triple is the RDF collection blank node
:subject :predicate ( :a :b :c ) .

# an empty collection value – rdf:nil
:subject :predicate2 () .

More examples: https://www.w3.org/TR/turtle/

The Graph representation?

Lecture Outline
Turtle Syntax of RDF
SPARQL: Querying RDF Documents

SPARQL
Querying RDF Documents

The contents are mainly from:
http://www.w3.org/TR/rdf-sparql-query/

A Semantic Web Layer Stack

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.

Conventions
Namespaces

Prefix IRI
rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdfs: http://www.w3.org/2000/01/rdf-schema#
xsd: http://www.w3.org/2001/XMLSchema#
fn: http://www.w3.org/2005/xpath-functions#

The following slides assume the following namespace prefix bindings:

Conventions
Data Descriptions

We use the Turtle [TURTLE] data format to show each triple explicitly. Turtle allows IRIs to be abbreviated with prefixes:

@prefix dc: .
@prefix: . :book1 dc:title “SPARQL Tutorial” .

SPARQL Basics
? or $ for variables
SELECT for selection, WHERE for conditions
WHERE typically combines multiple conditions, over multiple subjects
In SQL this would be a JOIN
without the headache you get by using JOIN

Query Forms (Select)
The query consists of two parts:
The SELECT clause identifies the variables to appear in the query results, and
The WHERE clause provides the basic graph pattern to match against the data graph.

Query Forms (Select)
SPARQL query’s WHERE clause says “pull this data out of the dataset,” and the SELECT part names which parts of that pulled data you actually want to see.

Learning SPARQL, 2nd Edition

Writing a Simple Query
Find the title of a book from the given data graph.

Data:
“SPARQL Tutorial” .
Query:
SELECT ?title WHERE { ?title.}
Result:
“SPARQL Tutorial”

GraphDB ™ Free

Multiple Matches

Data:
@prefix foaf: .
_:a foaf:name “Johnny Lee Outlaw” .
_:a foaf:mbox .
_:b foaf:name “Peter Goodguy” .
_:b foaf:mbox .
_:c foaf:mbox .
Query:
PREFIX foaf:
SELECT ?name ?mbox WHERE {?x foaf:name ?name . ?x foaf:mbox ?mbox}
Result:

Matching RDF Literals
Matching Literals with Language Tags

Data:
@prefix dt: .
@prefix ns: .
@prefix : .
@prefix xsd: .
😡 ns:p “cat”@en .
:y ns:p “42”^^xsd:integer .
:z ns:p “abc”^^dt:specialDatatype .
Query:
SELECT ?v WHERE { ?v ?p “cat” }
Result:
???
Query:
SELECT ?v WHERE { ?v ?p “cat”@en}
Result:

Take a break 
42

Matching RDF Literals
Matching Literals with Numeric Types

Data:
@prefix dt: .
@prefix ns: .
@prefix : .
@prefix xsd: .
😡 ns:p “cat”@en .
:y ns:p “42”^^xsd:integer .
:z ns:p “abc”^^dt:specialDatatype .
Query:
SELECT ?v WHERE {?v ?p 42}
Result:

Matching RDF Literals
Matching Literals with Arbitrary Datatypes

Data:
@prefix dt: .
@prefix ns: .
@prefix : .
@prefix xsd: .
😡 ns:p “cat”@en .
:y ns:p “42”^^xsd:integer .
:z ns:p “abc”^^dt:specialDatatype .
Query:
SELECT ?v WHERE {?v ?p “abc”^^}
Result:

Blank Node Labels in Query Results

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

RDF Term Constraints 

Graph pattern matching produces a solution sequence, where each solution has a set of bindings of variables to RDF terms. SPARQL FILTERs restrict solutions to those for which the filter expression evaluates to TRUE.

SPARQL FILTER functions like regex can test RDF literals. regex matches only plain literals with no language tag. 

https://www.w3.org/TR/xpath-functions/#regex-syntax

RDF Term Constraints 
Restricting the Values of Strings

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:
SELECT ?title WHERE {?x dc:title ?title FILTER regex(?title, “^SPA.*L$”, “i”) }
Result:

RDF Term Constraints 
Restricting Numeric Values

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 ns:
SELECT ?title ?price WHERE { ?x ns:price ?price . FILTER (?price < 30.5) . ?x dc:title ?title . } Result: Different Queries Same Result Query 1: PREFIX dc:
PREFIX ns:
SELECT $title WHERE {ns:book2 dc:title $title}
Query 2:
PREFIX dc:
PREFIX :
SELECT $title WHERE {:book2 dc:title $title}
Query 3:
PREFIX dc:
SELECT $title WHERE { dc:title $title}
Query 4:
PREFIX dc:
BASE
SELECT $title WHERE {<#book2> dc:title $title}

Predicate-Object Lists
Simplified Pattern
?x foaf:name ?name ;
foaf:mbox ?mbox .

Pattern
?x foaf:name ?name .
?x foaf:mbox ?mbox .

Triple patterns with a common subject can be written so that the subject is only written once and is used for more than one triple pattern using “;”

Object Lists
Simplified Pattern
?x foaf:nick “Alice” , “Alice_” .

Pattern
?x foaf:nick “Alice” .
?x foaf:nick “Alice_” .

If triple patterns share both subject and predicate, the objects may be separated by “,”

Simplify the following SPARQL query pattern.
Pattern
?x foaf:name ?name .
?x foaf:nick “Alice” .
?x foaf:nick “Alice_” .

Predicate-Object List Combined with Object Lists
Simplified Pattern
?x foaf:name ?name ;
foaf:nick “Alice” ;
foaf:nick “Alice_” .

Pattern
?x foaf:name ?name .
?x foaf:nick “Alice” .
?x foaf:nick “Alice_” .

1
Simplified Pattern
?x foaf:name ?name ;
foaf:nick “Alice”, “Alice_” ;

2

Optional Pattern Matching
Data:
@prefix foaf: .
@prefix rdf: .
_:a rdf:type foaf:Person .
_:a foaf:name “Alice” .
_:a foaf:mbox .
_:a foaf:mbox .
_:b rdf:type foaf:Person .
_:b foaf:name “Bob” .
Query :
PREFIX foaf:
SELECT ?name ?mbox WHERE {?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } }
Result:

Modify the query in the previous slide to remove null values from the result.

Optional Pattern Matching
Constraints
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 ns:
SELECT ?title ?price WHERE {?x dc:title ?title . OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) } } Result: Multiple Optional Patterns Data: @prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:homepage .
_:b foaf:name “Bob” .
_:b foaf:mbox .
Query :
PREFIX foaf:
SELECT ?name ?mbox ?hpage WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } . OPTIONAL { ?x foaf:homepage ?hpage } }
Result:

Matching Alternatives
Data:
@prefix dc10: .
@prefix dc11: .
_:a dc10:title “SPARQL Query Language Tutorial” .
_:a dc10:creator “Alice” .
_:b dc11:title “SPARQL Protocol Tutorial” .
_:b dc11:creator “Bob” .
_:c dc10:title “SPARQL” .
_:c dc11:title “SPARQL (updated)” .
Query :
PREFIX dc10:
PREFIX dc11:
SELECT ?title WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } }
Result:

Matching Alternatives
Data:
@prefix dc10: .
@prefix dc11: .
_:a dc10:title “SPARQL Query Language Tutorial” .
_:a dc10:creator “Alice” .
_:b dc11:title “SPARQL Protocol Tutorial” .
_:b dc11:creator “Bob” .
_:c dc10:title “SPARQL” .
_:c dc11:title “SPARQL (updated)” .
Query :
PREFIX dc10:
PREFIX dc11:
SELECT ?x ?y WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } }
Result:

Matching Alternatives
Data:
@prefix dc10: .
@prefix dc11: .
_:a dc10:title “SPARQL Query Language Tutorial” .
_:a dc10:creator “Alice” .
_:b dc11:title “SPARQL Protocol Tutorial” .
_:b dc11:creator “Bob” .
_:c dc10:title “SPARQL” .
_:c dc11:title “SPARQL (updated)” .
Query :
PREFIX dc10:
PREFIX dc11:
SELECT ?title ?author WHERE { { ?book dc10:title ?title . ?book dc10:creator ?author } UNION { ?book dc11:title ?title . ?book dc11:creator ?author } }
Result:

Modify the query in the previous slide to include all titles with or without authors.

Write a query for the following.
Data:
@prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:knows _:b .
_:a foaf:knows _:c .
_:b foaf:name “Bob” .
_:c foaf:name “Clare” .
_:c foaf:nick “CT” .
Query :
???
Result:

Data:
@prefix foaf: .
_:a foaf:name “Alice” .
_:a foaf:knows _:b .
_:a foaf:knows _:c .
_:b foaf:name “Bob” .
_:c foaf:name “Clare” .
_:c foaf:nick “CT” .
Query :
PREFIX foaf:
SELECT ?nameX ?nameY ?nickY WHERE {
?x foaf:knows ?y ; foaf:name ?nameX . ?y foaf:name ?nameY . OPTIONAL { ?y foaf:nick ?nickY } }
Result:

/docProps/thumbnail.jpeg