Liking cljdoc? Tell your friends :D

Graph Patterns

SPARQL/Flint :where clauses are written as graph patterns, which describe a selection of RDF triples. The simplest graph pattern, known as a basic graph pattern (BGP) consists of a series of RDF triples.* However, there are a number of clauses that can be used to create more advanced graph patterns:

* Technically a BGP also includes :filter clauses (though not the graph patterns described in the :filter).

Graph pattern clauses

:where

References: 5. Graph Patterns and 12. Subqueries

A :where clause in Flint is one of two things:

  • A sub-query, which is written as a :select query map.
  • A vector of graph patterns.

Each :where graph pattern is written as either a triple (either as a vector or IGraph normal form map), or as a vector of the form [:keyword & args]. :where patterns can be nested within each other.

Example of a :where clause containing a subquery in a nested :where:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"
            :dc   "<http://purl.org/dc/elements/1.1/>"}
 :select   [?person]
 :where    [[?person :foaf/interest ?doc]
            [:where {:select   [?doc [(max ?date) ?pubDate]]
                     :where    [[?h :foaf/name "Hange Zoe"]
                                [?h :foaf/publications ?doc]
                                [?doc :dc/date ?date]]
                     :group-by [?doc]}]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc:   <http://purl.org/dc/elements/1.1/>
SELECT ?person
WHERE {
    ?person foaf:interest ?doc .
    {
        SELECT ?doc (MAX(?date) AS ?pubDate)
        WHERE {
            ?h foaf:name "Hange Zoe" .
            ?h foaf:publications ?doc .
            ?doc dc:date ?date .
        }
        GROUP BY ?doc
    }
}

NOTE: Unlike their top-level counterparts, sub-:select queries cannot accept prologue, :from or :from-named clauses.

:optional

Reference: 6. Including Optional Values

The :optional keyword specifies graph patterns that do not need to be matched in order to form a query solution. In Flint, an :optional graph pattern has the form [:optional sub-where].

The example:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select   [?firstName ?lastName]
 :where    [[?x :foaf/givenName ?firstName]
            [:optional [[?x :foaf/familyName ?lastName]]]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?firstName ?lastName
WHERE {
    ?x foaf:firstName ?firstName .
    OPTIONAL {
        ?x foaf:familyName ?lastName .
    }
}

:union

Reference: 7. Matching Alternatives

The :union keyword specifies taking the union of the results of multiple graph patterns. In Flint, a :union graph pattern has the form [:union sub-where & sub-wheres].

The example:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select   [?x]
 :where    [[:union [[?x :foaf/name "Historia Reiss"]]
                    [[?x :foaf/name "Christa Lenz"]]]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?nickName
WHERE {
    {
        ?x foaf:name "Historia Reiss" .
    }
    UNION
    {
        ?x foaf:name "Christa Lenz" .
    }
}

:filter

Reference: 8.1 Filtering Using Graph Patterns

The :filter keyword is used to exclude results using expressions. In Flint, the :filter keyword is used in the form [:filter expr].

The example:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select   [?name ?age]
 :where    [[?x :foaf/name ?name]
            [?x :foaf/age ?age]
            [:filter (<= 18 ?age)]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?age
WHERE {
    ?x foaf:name ?name .
    ?x foaf:age ?age .
    FILTER (18 <= ?age)
}

:minus

Reference: 8.2 Removing Possible Solutions

The :minus keyword is used to remove the specified graph pattern from the result set. In Flint, the :minus graph pattern has the form [:minus sub-where].

The example:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select   [?name ?age]
 :where    [[?x :foaf/name ?name]
            [?x :foaf/name ?age]
            [:minus [[?x :foaf/name "Jean Kirstein"]]]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?age
WHERE {
    ?x foaf:name ?name .
    ?x foaf:age ?age
    MINUS {
        ?x foaf:name "Jean Kirstein" .
    }
}

:bind

Reference: 10.1 BIND: Assigning to Variables

The :bind keyword is used to bind the result of an expression to a variable. In Flint, the :bind keyword is used with an [expr var] form in the form [:bind [expr var]].

The example:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select   [?name ?isAdult]
 :where    [[?x :foaf/name ?name]
            [?x :foaf/age ?age]
            [:bind [(<= 18 ?age) ?isAdult]]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?isAdult
WHERE {
    ?x foaf:name ?name .
    ?x foaf:age ?age .
    BIND ((18 <= ?age) AS ?isAdult)
}

:values

Reference: 10.2 VALUES: Providing inline data

The :values keyword is used to inline values. In Flint, the :values keyword is used in the form [:values values-map].

The example:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"
            :food "<http://example.org/food/>"}
 :select   [?name ?age ?favoriteFood]
 :where    [{?x {:foaf/name #{?name}
                 :foaf/age  #{?age}}}
            [:values {?name         ["Sasha Blause" "Connie Springer"]
                      ?favoriteFood [:food/potato nil]}]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX food: <http://example.org/food/>
SELECT ?name ?age ?org ?favoriteFood
WHERE {
    ?x foaf:name ?name ;
       foaf:age ?age .
    VALUES (?name ?favoriteFood) {
        ("Sasha Blause" food:potato)
        ("Connie Springer" UNDEF)
    }
}

See the Modifiers document for more information on :values clauses, including more examples.

:graph

Reference: 13.3 Querying the Dataset

The :graph keyword is used to specify the named graph that the graph pattern exists in. In Flint, the :graph graph pattern has the form [:graph iri-or-var sub-where].

The example:

{:prefixes   {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select     [?name]
 :from-named ["<http://survey-corps.com/graph-data/>"]
 :where      [[:graph "<http://survey-corps.com/graph-data/>"
                      [[?x :foaf/name ?name]]]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
    GRAPH <http://survey-corps.com/graph-data/> {
        ?x foaf:name ?name .
    }
}

:service/:service-silent

Reference: SPARQL 1.1 Federated Query

The :service keyword is used for federated queries, i.e. queries across networks. The :service-silent variant is used in order to fail silently. In Flint, the :service graph pattern has the form [:service iri-or-var sub-where].

The example:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select   [?name]
 :where    [[:service "<http://survey-corps.com/graph-data/remote/>"
                      [[?x :foaf/name ?name]]]]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
    SERVICE <http://survey-corps.com/graph-data/remote> {
        ?x foaf:name ?name .
    }
}

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close