SPARQL – Graph Patterns

Andy Seaborne

Hewlett-Packard Laboratories, Bristol

Learning Goals

After this part of the tutorial you should

In this section:

SPARQL Query

Terminology

Graph Pattern
General pattern : other patterns combined. Conjunction.
Triple Pattern
An RDF triple with the addition of allowing variables (named) in the 3-tuple.
Basic Pattern
Set of triple patterns - defines a graph shape
Query pattern
The pattern in a query
Solution
A number of variable bindings such that replacing the variables by these values in a query pattern yields a pattern that "matches" the graph.
Result set
The set of all solutions that the query engine can find.

Syntax

Triple Patterns

Data

@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

_:a foaf:name  "Alice" .
_:a foaf:mbox  <mailto:alice@example.net> .

_:b foaf:name  "Bob" .

Query

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name }

Results

-----------
| name    |
===========
| "Bob"   |
| "Alice" |
-----------

Matching

Basic Patterns

A set of triple patterns, all of which must be matched by a solution.

Basic Pattern Matching Definition

Definition: Pattern Solution of Basic Patterns

A Pattern Solution of Graph Pattern GP on graph G is any substitution S such that S(GP) is a subgraph of G.

Basic Patterns - Example

@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

_:a foaf:name  "Alice" .
_:a foaf:mbox  <mailto:alice@example.net> .

_:b foaf:name  "Bob" .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
        ?person foaf:mbox <mailto:alice@example.net> .
        ?person foaf:name ?name .
      }
-----------
| name    |
===========
| "Alice" |
-----------

Syntax of Triple Patterns

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
         ?person foaf:mbox <mailto:alice@example.net> ;
                 foaf:name ?name .
      }

Constraints

  • Restrictions on values in solutions.
  • Testing on value with datatype promotions.
  • Does not depend on RDF D-entailment.
FILTER ?x < 3 .
FILTER regex(?name , "Smith") .

Value Tests

  • Based on XQuery 1.0 and XPath 2.0 Function and Operators
  • XSD boolean, string, integer, decimal, float, double, dateTime
  • Notation <, >, =, <=, >= and != for value comparison
    Apply to any type
  • BOUND, isURI, isBLANK, isLITERAL
  • REGEX, LANG, DATATYPE, STR (lexical form)
  • Function call for casting and extensions functions

Value Tests - Example

@prefix dc:     <http://purl.org/dc/elements/1.1/> .
@prefix stock:  <http://example.org/stock#> .
@prefix inv:    <http://example.org/inventory#> .

stock:book1  dc:title  "SPARQL Query Language Tutorial" .
stock:book1  inv:price   10 .
stock:book1  inv:quantity 3 .

stock:book2  dc:title     "SPARQL Query Language (2nd ed)" .
stock:book2  inv:price   20 ; inv:quantity 5 .

stock:book3  dc:title  "Moving from SQL to SPARQL" .
stock:book3  inv:price   5 ; inv:quantity 0 .

stock:book4  dc:title  "Applying XQuery" .
stock:book4  inv:price   20 ; inv:quantity 8 .
PREFIX dc:             <http://purl.org/dc/elements/1.1/>
PREFIX stock:    <http://example.org/stock#>
PREFIX inv:          <http://example.org/inventory#>

SELECT ?book ?title
WHERE {
  ?book dc:title ?title .
  ?book inv:price ?price .   FILTER ?price < 15 .
  ?book inv:quantity ?num .  FILTER ?num > 0 . }
--------------------------------------------------
| book        | title                            |
==================================================
| stock:book1 | "SPARQL Query Language Tutorial" |
--------------------------------------------------

OPTIONAL - Adding additional information

  • RDF is "semi structured".
  • RDF has no integrity constraints
  • Quality of source will vary
  • Query language needs to add information to a result but without the query failing just because some information is missing.

OPTIONALS - Example

@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

_:a foaf:name  "Alice" .
_:a foaf:nick  "A-online" .

_:b foaf:name  "Bob" .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?nick
WHERE  { ?x foaf:name ?name .
         OPTIONAL {?x foaf:nick ?nick }
       }
------------------------
| name    | nick       |
========================
| "Alice" | "A-online" |
| "Bob"   |            |
------------------------
  • nick has no value when name is "Bob" because there is no triple _:b foaf:nick ... .
  • OPTIONAL{pattern} is the same as
    {pattern} OR { NOT(pattern) }

Group Patterns

  • Group patterns match if all subpatterns match and all constraints are satisfied
  • Basic patterns are groups of triple patterns
  • In SPARQL Syntax, groups are {}
  • In the syntax, graph operators take groups

UNION - Matching Alternatives - 1

@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" .

_:b dc11:title "SPARQL Query Language (2nd ed)" .

_:c dc10:title "SPARQL" .
_:c dc11:title "SPARQL" .
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>

SELECT DISTINCT ?title
WHERE {
         { ?book dc10:title ?title }
        UNION
         { ?book dc11:title ?title }
      }

------------------------------------
| title                            |
====================================
| "SPARQL Query Language Tutorial" |
| "SPARQL"                         |
| "SPARQL Query Language (2nd ed)" |
------------------------------------
  • SELECT DISTINCT is used to ensure unique title values

UNION - Matching Alternatives - 2

@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" .

_:b dc11:title "SPARQL Query Language (2nd ed)" .

_:c dc10:title "SPARQL" .
_:c dc11:title "SPARQL" .
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>

SELECT ?title_10 ?title_11
WHERE {
         { ?book dc10:title ?title_10 }
        UNION
         { ?book dc11:title ?title_11 }
      }

-----------------------------------------------------------------------
| title_10                         | title_11                         |
=======================================================================
| "SPARQL Query Language Tutorial" |                                  |
| "SPARQL"                         |                                  |
|                                  | "SPARQL"                         |
|                                  | "SPARQL Query Language (2nd ed)" |
-----------------------------------------------------------------------

Summary

SPARQL uses graph pattern matching

  • Triple patterns
  • Basic patterns
  • Groups
  • Optionals
  • Union

Values can be constrained by filters

Later, we will see matching over a set of graphs

Copyright

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.