GRAPH
GRAPH
Allows SPARQL engines to distinguish between different RDF graphs that have be added into the store.
GRAPH
keywordProvides access to the graph identifiers for the added triples.
Datasets and GRAPH
may be used to record
provenantial information relating to a queried RDF graph. Each graph is
identified by a URI, so additional information may be added against that URI
by the system. Examples of information that may be included are the data and
time of the parsing of the graph or the URI that was resolved to retrieve the
graph.
The dataset may also be used to maintain different versions of the graph retrieved from one URI at different times by assigning them different graph identifying URIs.
Aggregator services, such as caches of FOAF documents, can use the SPARQL dataset to perform data management tasks, such as know which triples have to be updated when a document is reread, and to better inform spidering services.
_:a foaf:name "Alice" . _:a foaf:mbox <mailto:alice@work.example.com> . _:a foaf:mbox <mailto:alice@home.example.org> . ...
_:c foaf:mbox <mailto:bob@work.example.com> . _:c foaf:name "Robert" . _:c foaf:nick "bob" . ...
The SPARQL dataset consists of any number of named graphs, each of which is identified by a URI, and is a single, unnamed graph.
Triples in the background (unnamed, default) graph will
match patterns that do not use the GRAPH
keyword, triples in named graphs will match query
patterns that do use the GRAPH
keyword.
RDF graphs can be added as both a named and background graph.
There are two forms of data in RDF datasets: a default or background graph and a set of named graphs. These are used as follows:
FROM <http://example.org/base.rdf> FROM NAMED <http://example.org/data1.rdf> FROM NAMED <http://example.org/data2.rdf> ...
This can be omitted in which case the application can provide the dataset.
GRAPH
keywordPREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?graph WHERE GRAPH ?graph { ?x foaf:name "Alice" . }returns the URIs of the graphs that contain the triple about Alice's name
The GRAPH
keyword is used as a triple pattern block prefix.
It is followed by one of: a SPARQL variable, a URI, a qname, or a blank node.
The triple pattern black has the usual contents of a turtle triple expression and/or a FILTER
expression.
GRAPH
syntax examplesGRAPH ex:aliceFoaf.ttl { ?x foaf:name "Alice" ; foaf:mbox <mailto:alice@work.example.com> . }only matches the pattern if it appears in the graph identified by
ex:aliceFoaf.ttl
GRAPH ?graph { ?x foaf:name "Alice" ; foaf:mbox <mailto:alice@work.example.com> . }binds
?graph
to the URIs of graphs that contain the
patternThe two examples here show common uses of the GRAPH
keyword.
The first fragment is an example of a query that is designed to only match against a particular graph, this could be done for security reasons, for example, if only one particular graph is trusted to answer that part of the query.
The second fragment show of example of where the user wishes to know which graph has been used to answer the query. This is often done when the application issuing the query wishes to attach some kind of provenance to the query results.
PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?creator WHERE { ?graph dc:creator ?creator . GRAPH ?graph { ?x foaf:name "Alice" . } }
----------------------------------- | creator | =================================== | <mailto:alice@work.example.com> | | <mailto:eve@work.example.com> | -----------------------------------
This example uses triples about graph URIs made with the <> URI shortcut. The graph URI has some DC triple about who authored it:
<> rdf:type foaf:PersonalProfileDocument ; dc:creator <mailto:alice@work.example.com> .which can be queried.
PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ex: <http://example.com/www2005/> SELECT ?mbox WHERE GRAPH ex:aliceFoaf.ttl { ?alice foaf:mbox <mailto:alice@work.example.com> ; foaf:knows ?person . ?person foaf:mbox ?mbox . }
--------------------------------- | mbox | ================================= | <mailto:bob@work.example.com> | ---------------------------------
In this example we restrict the result to only come from a single graph. There are triples in other graphs that say things about who Alice knows (eveFoaf.ttl) which we wish to ignore for this query.
Copyright 2005 Dave Beckett, Steve Harris, Eric Prud'hommeaux and Andy Seaborne. Terms of use are given on the main Introduction to RDF Query with SPARQL Tutorial page.