To jest stara wersja strony!
Opis
SemWeb_SPARQL
* research on SPARQL,
* provide a presentation,
* look for language support
* other tools
* look for/build Prolog API, e.g. SeRQL
Spotkania
20090326
What is SPARQL
Resource Description Framework (RDF) sets a standard for describing metadata in a way that computers can understand and process it, and integrate information from disparate information sources on the Web. RDF has needed a standard query language for some time and having one will make many development tasks much easier. Or how would you feel using relational databases without SQL?
SPARQL (‘sparkle’) is a standardized query language for RDF data. It offers developers of Semantic Web applications a way to write queries across this wide range of information. Used with a common protocol, applications can access and combine information from multiple sources on the Web. For example, SPARQL can be used to write Semantic Web applications in areas like financial markets, tourism, transportation, and pharmaceuticals.
SPARQL, which is both a query language and a data access protocol, has the ability to become a key component in Web 2.0 applications: as a standard backed by a flexible data model, it can provide a common query mechanism for all Web 2.0 applications – just what Web 2.0 needs. As SPARQL emerges, we can expect to see a fast growing number of application developers
Introduction
RDF – flexible and extensible way to represent information about WWW resources
SPARQL - query language for getting information from RDF graphs. It provides facilities to:
extract information in the form of URIs, blank nodes, plain and typed literals.
extract RDF subgraphs.
construct new RDF graphs based on information in the queried graphs
matching graph patterns
variables – global scope; indicated by ‘?‘ or ‘$‘
query terms – based on Turtle syntax
terms delimited by „<>” are relative
URI references
data description format - Turtle
Graph Patterns
Basic Graph Pattern – set of Triple Patterns
Group Pattern - a set of graph patterns must all match
Value Constraints - restrict RDF terms in a solution
Optional Graph Patterns .- additional patterns may extend the solution
Alternative Graph Pattern – two or more possible patterns are tried
Patterns on Named Graphs - patterns are matched against named graphs
Basic Graph Pattern
SELECT ?x ?v WHERE { ?x ?x ?v }
rdf:type rdf:type rdf:Property
Basic Graph Pattern - Multiple Matches
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Johnny Lee Outlaw" .
_:a foaf:mbox <mailto:jlow@example.com> .
_:b foaf:name "Peter Goodguy" .
_:b foaf:mbox <mailto:peter@example.org> .
_:c foaf:mbox <mailto:carol@example.org> .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
{ ?x foaf:name ?name .
?x foaf:mbox ?mbox }
Query Result:
name | mbox |
„Johnny Lee Outlaw” | <mailto:jlow@example.com> |
„Peter Goodguy” | <mailto:peter@example.org> |
Basic Graph Pattern - Blank Nodes
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .\\
_:a foaf:name "Alice" .\\
_:b foaf:name "Bob" .\\
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>\\
SELECT ?x ?name\\
WHERE { ?x foaf:name ?name }\\
x | name |
_:c | „Alice” |
_:d | „Bob” |
Group Pattern
In a SPARQL query string, a group graph pattern is delimited with braces: {}. For example, this query's query pattern is a group graph pattern of one basic graph pattern.
PREFIX foaf: <http://xmlns.com/foaf/0.1/> \\
SELECT ?name ?mbox\\
WHERE {
?x foaf:name ?name .
?x foaf:mbox ?mbox .
}
The same solutions would be obtained from a query that grouped the triple patterns into two basic graph patterns. For example, the query below has a different structure but would yield the same solutions as the previous query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/> \\
SELECT ?name ?mbox \\
WHERE {
{ ?x foaf:name ?name . }
{ ?x foaf:mbox ?mbox . }
}
Value Constraints
Constraints can be given in an optional graph pattern. For example:
{
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix : <http://example.org/book/>
@prefix ns: <http://example.org/ns#> .
}
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
Query:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price
WHERE { ?x dc:title ?title .
OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) }
}
Query results:
title | price |
„SPARQL Tutorial” |
„The Semantic Web” | 23 |
Optional graph patterns
{
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix : <http://example.org/book/>
@prefix ns: <http://example.org/ns#> .
}
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
Query:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price
WHERE { ?x dc:title ?title .
OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) }
}
Query results:
title | price |
„SPARQL Tutorial” |
„The Semantic Web” | 23 |
Multiple Optional Blocks
Graph patterns are defined recursively. A graph pattern may have zero or more optional graph patterns, and any part of a query pattern may have an optional part. In this example, there are two optional graph patterns.
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:homepage <http://work.example.org/alice/> .
_:b foaf:name "Bob" .
_:b foaf:mbox <mailto:bob@work.example> .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox ?hpage
WHERE { ?x foaf:name ?name .
OPTIONAL { ?x foaf:mbox ?mbox } .
OPTIONAL { ?x foaf:homepage ?hpage }
}
Query result:
Alternative Graph Patterns
SPARQL provides a means of combining graph patterns so that one of several alternative graph patterns may match. If more than one of the alternatives matches, all the possible pattern solutions are found.
Pattern alternatives are syntactically specified with the UNION keyword.
Data:
@prefix dc10: <http://purl.org/dc/elements/1.0/> .
@prefix dc11: <http://purl.org/dc/elements/1.1/> .
_: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: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?title
WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } }
Query result:
title |
„SPARQL Protocol Tutorial” |
„SPARQL” |
„SPARQL (updated)„ |
„SPARQL Query Language Tutorial” |
This will return results with the variable x bound for solutions from the left branch of the UNION, and y bound for the solutions from the right branch. If neither part of the UNION pattern matched, then the graph pattern would not match.
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?title ?author
WHERE { { ?book dc10:title ?title . ?book dc10:creator ?author }
UNION
{ ?book dc11:title ?title . ?book dc11:creator ?author }
}
author | title |
„Alice” | „SPARQL Protocol Tutorial” |
„Bob” | „SPARQL Query Language Tutorial” |
RDF Dataset
RDF Dataset- The Relationship between Named and Background Graphs (I)
# Background graph
@prefix dc: <http://purl.org/dc/elements/1.1/> .
<http://example.org/bob> dc:publisher "Bob" .
<http://example.org/alice> dc:publisher "Alice" .
# Graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
# Graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .
RDF Dataset- The Relationship between Named and Background Graphs (II)
# Background graph
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:x foaf:name "Bob" .
_:x foaf:mbox <mailto:bob@oldcorp.example.org> .
_:y foaf:name "Alice" .
_:y foaf:mbox <mailto:alice@work.example.org> .
# Graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
# Graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .
Querying the Dataset
The use of GRAPH changes the active graph for matching basic graph patterns within part of the query. Outside the use of GRAPH, the default graph is matched by basic graph patterns.
# Graph: http://example.org/foaf/aliceFoaf
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example> .
_:a foaf:knows _:b .
_:b rdfs:seeAlso <http://example.org/foaf/bobFoaf> .
<http://example.org/foaf/bobFoaf> rdf:type foaf:PersonalProfileDocument .
_:b foaf:name "Bob" .
_:b foaf:mbox <mailto:bob@work.example> .
_:b foaf:age 32 .
# Graph: http://example.org/foaf/bobFoaf
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
_:1 foaf:mbox <mailto:bob@work.example> .
_:1 rdfs:seeAlso <http://example.org/foaf/bobFoaf> .
_:1 foaf:age 35 .
<http://example.org/foaf/bobFoaf> rdf:type foaf:PersonalProfileDocument .
Querying the Dataset - Accessing Graph Labels
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?src ?bobAge
WHERE { GRAPH ?src
{
?x foaf:mbox <mailto:bob@work.example> .
?x foaf:age ?bobAge }
}
Query results:
Querying the Dataset - Restricting by Graph Label
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX data: <http://example.org/foaf/>
SELECT ?age
WHERE
{
GRAPH data:bobFoaf
{
?x foaf:mbox <mailto:bob@work.example> .
?x foaf:age ?age }
}
Query results:
Querying the Dataset - Restricting via Query Pattern
Query:
PREFIX data: <http://example.org/foaf/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?mbox ?age ?ppd
WHERE
{ GRAPH data:aliceFoaf
{ ?alice foaf:mbox <mailto:alice@work.example> ;
foaf:knows ?whom .
?whom foaf:mbox ?mbox ;
rdfs:seeAlso ?ppd .
?ppd a foaf:PersonalProfileDocument . } .
GRAPH ?ppd
{
?w foaf:mbox ?mbox ;
foaf:age ?age
}
}
Query results:
Sprawozdanie
Prezentacja
Materiały