RDF: The Turtle Data Format
SPARQL (Basic)
Copyright By PowCoder代写 加微信 powcoder
Main Sources:
https://www.w3.org/TR/turtle/
http://www.w3.org/TR/rdf-sparql-query/
Chapter 3 of Semantic Web Primer
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
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 ” ” .
<#spiderman>
rel:enemyOf <#green-goblin> ;
a foaf:Person ; foaf:name “Spiderman”, .
and Spiderman!
RDF/XML representation
.org/#green-goblin”>
and Spiderman!
Graph representation
and Spiderman!
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.
Define a prefix label for the vocabulary IRI
@prefix somePrefix:
somePrefix:enemyOf
Define a prefix label using the SPARQL syntax
PREFIX somePrefix:
somePrefix:enemyOf
The and 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/
@prefix uni:
@prefix rdf:
@base
<#CIT1111>
a uni:course;
uni:courseName “Discrete Maths” ;
uni:isTaughtBy <#T949318> .
<#T949318>
a uni:lecturer;
uni:name ” ” ;
uni:title “Associate Professor” .
Is this statement correct?
For a given RDF/XML document, there exists only one Turtle representation.
Graph representation
@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
@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”^^
show:218 rdfs:label “That Seventies Show” . # same again
show:218 show:localName “That Seventies . # literal with a language tag
show:218 show:localName ‘Cette Série des Années . # literal delimited by single quote
show:218 show:localName “Cette Série des Années . # 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
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:
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.
SELECT ?title WHERE {
“SPARQL Tutorial”
Multiple Matches
@prefix foaf:
_:a foaf:name ” Outlaw” .
_:a foaf:mbox .
_:b foaf:name ” ” .
_:b foaf:mbox .
_:c foaf:mbox .
PREFIX foaf:
SELECT ?name ?mbox WHERE {?x foaf:name ?name . ?x foaf:mbox ?mbox}
Matching RDF Literals
Matching Literals with Language Tags
@prefix dt:
@prefix ns:
@prefix :
@prefix xsd:
😡 ns:p .
:y ns:p “42”^^xsd:integer .
:z ns:p “abc”^^dt:specialDatatype .
SELECT ?v WHERE { ?v ?p “cat” }
SELECT ?v WHERE { ?v ?p
Take a break
Matching RDF Literals
Matching Literals with Numeric Types
@prefix dt:
@prefix ns:
@prefix :
@prefix xsd:
😡 ns:p .
:y ns:p “42”^^xsd:integer .
:z ns:p “abc”^^dt:specialDatatype .
SELECT ?v WHERE {?v ?p 42}
Matching RDF Literals
Matching Literals with Arbitrary Datatypes
@prefix dt:
@prefix ns:
@prefix :
@prefix xsd:
😡 ns:p .
:y ns:p “42”^^xsd:integer .
:z ns:p “abc”^^dt:specialDatatype .
SELECT ?v WHERE {?v ?p “abc”^^
Blank Node Labels in Query Results
@prefix foaf:
_:a foaf:name “Alice” .
_:b foaf:name “Bob” .
PREFIX foaf:
SELECT ?x ?name WHERE { ?x foaf:name ?name}
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
@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:
SELECT ?title WHERE {?x dc:title ?title FILTER regex(?title, “^SPA.*L$”, “i”) }
RDF Term Constraints
Restricting Numeric Values
@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 ns:
SELECT ?title ?price WHERE { ?x ns:price ?price . FILTER (?price < 30.5) . ?x dc:title ?title . }
Different Queries Same Result
PREFIX dc:
PREFIX ns:
SELECT $title WHERE {ns:book2 dc:title $title}
PREFIX dc:
PREFIX :
SELECT $title WHERE {:book2 dc:title $title}
PREFIX dc:
SELECT $title WHERE {
PREFIX dc:
BASE
SELECT $title WHERE {<#book2> dc:title $title}
Predicate-Object Lists
Simplified Pattern
?x foaf:name ?name ;
foaf:mbox ?mbox .
?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_” .
?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.
?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_” .
?x foaf:name ?name .
?x foaf:nick “Alice” .
?x foaf:nick “Alice_” .
Simplified Pattern
?x foaf:name ?name ;
foaf:nick “Alice”, “Alice_” ;
Optional Pattern Matching
@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” .
PREFIX foaf:
SELECT ?name ?mbox WHERE {?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } }
Modify the query in the previous slide to remove null values from the result.
Optional Pattern Matching
Constraints
@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 ns:
SELECT ?title ?price WHERE {?x dc:title ?title . OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) } }
Multiple Optional Patterns
@prefix foaf:
_:a foaf:name “Alice” .
_:a foaf:homepage
_:b foaf:name “Bob” .
_:b foaf:mbox .
PREFIX foaf:
SELECT ?name ?mbox ?hpage WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } . OPTIONAL { ?x foaf:homepage ?hpage } }
Matching Alternatives
@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)” .
PREFIX dc10:
PREFIX dc11:
SELECT ?title WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } }
Matching Alternatives
@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)” .
PREFIX dc10:
PREFIX dc11:
SELECT ?x ?y WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } }
Matching Alternatives
@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)” .
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 } }
Modify the query in the previous slide to include all titles with or without authors.
Write a query for the following.
@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” .
@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” .
PREFIX foaf:
SELECT ?nameX ?nameY ?nickY WHERE {
?x foaf:knows ?y ; foaf:name ?nameX . ?y foaf:name ?nameY . OPTIONAL { ?y foaf:nick ?nickY } }
/docProps/thumbnail.jpeg
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com