hereon-mb/elm-rdf/1.1.0

NOTE: the latest version is 2.0.0

Rdf.Sparql

This module contains functions and types to create SPARQL 1.1 queries.

type Query

The Query type represents a SPARQL query.

toString : Query -> String

Turn a Query into a SPARQL query.

toStringWith : Prologue -> Query -> String

Turn a Query into a SPARQL query including and using the BASE and PREFIX declarations.

CONSTRUCT queries

construct : List Triple -> Query

Build a CONSTRUCT query.

FROM clauses

from : Iri -> Query -> Query

Restrict a query to a specific graph by adding a FROM.

WHERE clauses

The basic building block of a WHERE clause. Take a look at the functions below to learn how to create and combine these.

Set the WHERE clause of a Query to the given group graph pattern. If you don't provide this, the empty group graph pattern will be used.

Create the empty group graph pattern { }.

construct []
    |> where_ empty
    |> toString
--> "CONSTRUCT {} WHERE {}"
IsVar compatible1
-> List (IsBlankNodeOrIriOrLiteral compatible2)

Provide inline data.

import Rdf


construct []
    |> where_
        (values (Rdf.varQ "person")
            [ Rdf.iri "http://example.org/alice" ]
        )
    |> toString
    |> String.lines
--> [ "CONSTRUCT {} WHERE {"
--> , "  VALUES ?person {"
--> , "    <http://example.org/alice>"
--> , "  }"
--> , "}"
--> ]
triples : List Triple -> GroupGraphPattern

Create a basic graph pattern which matches a set of triple patterns.

import Rdf exposing (iri, varQ, a, rdfs)


construct []
    |> where_
        (triples
            [ triple (varQ "person") a (iri "http://example.org#Person")
            , triple (varQ "person") (rdfs "label") (varQ "label")
            ]
        )
    |> toStringWith
        { base = Just "http://example.org#"
        , prefixes = [ ( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" ) ]
        }
    |> String.lines
--> [ "BASE <http://example.org#>"
--> , "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
--> , "CONSTRUCT {} WHERE {"
--> , "  ?person a <Person> ."
--> , "  ?person rdfs:label ?label ."
--> , "}"
--> ]

Make a graph pattern optional.

import Rdf exposing (iri, varQ, a, rdfs)


construct []
    |> where_
        (and
            (triples
                [ triple (varQ "person") a (iri "http://example.org#Person") ])
            (optional
                (triples
                    [ triple (varQ "person") (rdfs "label") (varQ "label") ])
            )
        )
    |> toStringWith
        { base = Just "http://example.org#"
        , prefixes = [ ( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" ) ]
        }
    |> String.lines
--> [ "BASE <http://example.org#>"
--> , "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
--> , "CONSTRUCT {} WHERE {"
--> , "  ?person a <Person> ."
--> , "  OPTIONAL {"
--> , "    ?person rdfs:label ?label ."
--> , "  }"
--> , "}"
--> ]

Create a graph pattern which matches the first or the second pattern.

import Rdf exposing (iri, varQ, a, rdfs)


construct []
    |> where_
        (or
            (triples [ triple (varQ "person") a (iri "http://example.org#Author") ])
            (triples [ triple (varQ "person") a (iri "http://example.org#Reader") ])
        )
    |> toStringWith
        { base = Just "http://example.org#"
        , prefixes = [ ( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" ) ]
        }
    |> String.lines
--> [ "BASE <http://example.org#>"
--> , "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
--> , "CONSTRUCT {} WHERE {"
--> , "  {"
--> , "    ?person a <Author> ."
--> , "  }"
--> , "  UNION"
--> , "  {"
--> , "    ?person a <Reader> ."
--> , "  }"
--> , "}"
--> ]

Create a graph pattern which matches the first and the second pattern.

import Rdf exposing (iri, varQ, a, rdfs)


construct []
    |> where_
        (and
            (triples [ triple (varQ "person") a (iri "http://example.org#Person") ])
            (triples [ triple (varQ "person") a (iri "http://example.org#Maintainer") ])
        )
    |> toStringWith
        { base = Just "http://example.org#"
        , prefixes = [ ( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" ) ]
        }
    |> String.lines
--> [ "BASE <http://example.org#>"
--> , "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
--> , "CONSTRUCT {} WHERE {"
--> , "  ?person a <Person> ."
--> , "  ?person a <Maintainer> ."
--> , "}"
--> ]

Add a filter expression.

import Rdf exposing (iri, string, varQ, a, rdfs)


construct []
    |> where_
        (and
            (triples
                [ triple (varQ "person") a (iri "http://example.org#Person")
                , triple (varQ "person") (rdfs "label") (varQ "label")
                ]
            )
            (filter
                (equals
                    (simple (varQ "person"))
                    (simple (string "Alice"))
                )
            )
        )
    |> toStringWith
        { base = Just "http://example.org#"
        , prefixes = [ ( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" ) ]
        }
    |> String.lines
--> [ "BASE <http://example.org#>"
--> , "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
--> , "CONSTRUCT {} WHERE {"
--> , "  ?person a <Person> ."
--> , "  ?person rdfs:label ?label ."
--> , "  FILTER(?person = \"Alice\")"
--> , "}"
--> ]
bind : Expression -> IsVar compatible -> GroupGraphPattern

Bind the result of an expression to a query variable.

import Rdf exposing (iri, string, varQ, a, rdfs)


construct []
    |> where_
        (and
            (triples
                [ triple (varQ "person") a (iri "http://example.org#Person")
                , triple (varQ "person") (iri "http://example.org#firstName") (varQ "firstName")
                , triple (varQ "person") (iri "http://example.org#lastName") (varQ "lastName")
                ]
            )
            (bind
                (concat
                    [ simple (varQ "firstName")
                    , simple (string " ")
                    , simple (varQ "lastName")
                    ]
                )
                (varQ "name")
            )
        )
    |> toStringWith
        { base = Just "http://example.org#"
        , prefixes = [ ( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" ) ]
        }
    |> String.lines
--> [ "BASE <http://example.org#>"
--> , "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
--> , "CONSTRUCT {} WHERE {"
--> , "  ?person a <Person> ."
--> , "  ?person <firstName> ?firstName ."
--> , "  ?person <lastName> ?lastName ."
--> , "  BIND("
--> , "    CONCAT("
--> , "      ?firstName,"
--> , "      \" \","
--> , "      ?lastName"
--> , "    )"
--> , "    AS"
--> , "    ?name"
--> , "  )"
--> , "}"
--> ]

Expressions

The basic building block of an expression. Take a look at the functions below to learn how to create and combine these.

simple : IsIriOrLiteralOrVar compatible -> Expression

Turn an RDF term into an Expression.

Check if the Expression is an IRI. This translates to isIRI.

Compare two expressions. This translates to <first> = <second>.

Concatenate a list of Expressions which evaluate to string literals. This translates to CONCAT.

Triple patterns/templates

type alias Triple =
{ subject : BlankNodeOrIriOrVar
, predicate : VarOrPath
, object : BlankNodeOrIriOrLiteralOrVar
}

This type represents triple patterns (which appear in WHERE clauses) as well as triple templates (which appear in CONSTRUCT clauses). Note that since predicates in triple templates cannot contain property paths, triples containing these will be skipped when converting Query's into SPARQL queries.

IsBlankNodeOrIriOrVar compatible1
-> IsVarOrPath compatible2
-> IsBlankNodeOrIriOrLiteralOrVar compatible3
-> Triple

Create a triple pattern/template by providing a subject, verb, and object.

String
-> IsBlankNodeOrIriOrVar compatible1
-> IsPath compatible2
-> IsBlankNodeOrIriOrLiteralOrVar compatible3
-> List Triple

Expand a triple pattern/template with a property path predicate into a list of triple patterns with IRI or variable predicates. The first argument is the prefix used for intermediate query variables.

import Rdf exposing (iri, inverse, sequence, varQ, asPath, a, rdfs)


construct []
    |> where_
        (triplesAlong "person"
            (varQ "child")
            (sequence
                (inverse (iri "http://example.org#hasChild"))
                [ inverse (iri "http://example.org#hasChild")
                , asPath a
                ]
            )
            (iri "http://example.org#Person")
            |> triples
        )
    |> toStringWith
        { base = Just "http://example.org#"
        , prefixes = [ ( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" ) ]
        }
    |> String.lines
--> [ "BASE <http://example.org#>"
--> , "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
--> , "CONSTRUCT {} WHERE {"
--> , "  ?person_0 <hasChild> ?child ."
--> , "  ?person_1 <hasChild> ?person_0 ."
--> , "  ?person_1 a <Person> ."
--> , "}"
--> ]

Results

type alias Results =
List (Dict String BlankNodeOrIriOrLiteral)

An instance of Results represents the results of a SELECT query.

resultsDecoder : Decoder Results

A JSON decoder to decode the "results" section of a variable binding result.