Liking cljdoc? Tell your friends :D

Triples

Reference: 4.2 Syntax for Triple Patterns

The fundamental building block of RDF and SPARQL is the triple, a tuple consisting of a subject, predicate, and object. In Flint, there are two ways to write a series of triples.

The first is as a vector of three-element vectors. For example, the triples in the :where clause here are represented as vectors:

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

which after SPARQL translation becomes:

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

The other way to write a triple block in Flint is the normal form of the IGraph protocol. Each normal form map associates subjects with predicate-object maps, and each predicate-object map associates predicates with object sets. The example:

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

becomes:

WHERE {
    ?x foaf:name ?name ;
       foaf:age  ?age .
}

Note that only when triples written as normal form maps in Flint will they share subjects in the SPARQL representation. Since objects are represented as sets, a similar grouping occurs if more than one object exists in a set:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select   [?y]
 :where    [{?y {:foaf/givenName #{"Ymir", "_Ymir"}}}]}

which becomes

PREFIX foaf:<http://xmlns.com/foaf/0.1/>
SELECT ?y
WHERE {
    ?y foaf:givenName "Ymir" , "_Ymir"
}

which is the same as

PREFIX foaf:<http://xmlns.com/foaf/0.1/>
SELECT ?y
WHERE {
    ?y foaf:givenName "Ymir" .
    ?y foaf:givenName "_Ymir" .
}

Both forms can be combined to form a graph pattern:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"
            :dc   "<http://purl.org/dc/elements/1.1/>"}
 :select   [?name ?age ?title]
 :where    [{?x {:foaf/name #{?name}
                 :foaf/age  #{?age}}}
            [?y :dc/creator ?name]
            [?y :dc/title ?title]]}

which become:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc:   <http://purl.org/dc/elements/1.1/>
SELECT ?name ?age ?title
WHERE {
    ?x foaf:name ?name ;
       foaf:age ?age .
    ?y dc:creator ?name .
    ?y dc:title ?title .
}

Restrictions

Subjects can be one of the following:

Predicates can be one of the following:

Objects can be one of the following:

  • Variables
  • IRIs or prefixed IRIs
  • Blank nodes
  • Literals

NOTE: Property paths are not allowed in triples in CONSTRUCT clauses, nor in DELETE and INSERT DATA clauses.

NOTE: Blank nodes are not allowed in triples in DELETE clauses (including DELETE WHERE and DELETE DATA).

NOTE: Variables are not allowed in triples in DELETE DATA OR INSERT DATA clauses.

NOTE: Technically literals are allowed in subject position according to the SPARQL spec, but no RDF implementation accepts that, so Flint does not allow for subject literals either (unless they are in an RDF list as described below).

RDF Lists

Flint supports SPARQL's syntactic sugar for easy writing of RDF lists. For example:

{:select [?x]
 :where  [[(1 ?x 3 4) ?p ?o]]}

becomes

SELECT ?x
WHERE {
    (1 ?x 3 4) ?p ?o
}

which should then expanded out into an RDF list by your SPARQL query engine.

RDF lists be placed in both subject and object position. If they are placed in subject position, then predicates and objects are optional (which is not usually the case). The predicate-object map can be left empty in normal form representation:

{:select [?x]
 :where  [{(1 ?x 3 4) {}}]}

and can be left out entirely in triple representation:

{:select [?x]
 :where  [[(1 ?x 3 4)]]}

RDF lists can also be nested, both with themselves and with blank node vectors (see below):

{:select [?x]
 :where  [[(1 [:p :q] (2))]]}

Blank Node Vectors

Flint also has support for SPARQL's blank node syntactic sugar, which will be expanded out by the SPARQL engine. For example:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"
 :select   [?name]
 :where    [{[:foaf/name ?name
              :foaf/mbox "<mailto:foo@example.org>"] {}}]}}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
    [ foaf:name ?name
      foaf:mbox <mailto:foo@example.com> ] .
}

Note that like with RDF lists, the predicate and object may be omitted here.

Property Paths

Reference: 9. Property Paths

Property paths act as syntactic sugar for predicate sequences in RDF graphs. A property path in Flint is an IRI (including :a) or a list of the form (op path ...); this is similar to Clojure functions or Flint expressions. The following is a table of path operations:

FlintSPARQL formArglist
alt(path \| path \| ...)[& paths]
cat(path / path / ...)[& paths]
inv^(path)[path]
?(path)?[path]
*(path)*[path]
+(path)+[path]
not!(neg-path)[neg-path]

A neg-path is a subset of paths that can only include alt operations or IRIs. No other operations are allowed, even as args to alt ops.

An example of a query with a simple property path:

{:prefixes {:foaf "<http://xmlns.com/foaf/0.1/>"}
 :select   [?friendName]
 :where    [{?x {:foaf/name #{"Eren Jaeger"}
                (cat (* :foaf/knows) :foaf/name) #{?friendName}}}]}

becomes:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?friendName
WHERE {
    ?x foaf:name "Eren Jaeger" ;
       (foaf:knows* / foaf:name) ?friendName .
}

NOTE: Variables are not allowed in property paths.

Can you improve this documentation?Edit on GitHub

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

× close