This module contains functions and types to create SPARQL 1.1 queries.
Turn a Query into a SPARQL query including and using the BASE and
PREFIX declarations.
CONSTRUCT queriesFROM clausesWHERE clausesThe 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 {}"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>"
--> , " }"
--> , "}"
--> ]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 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"
--> , " )"
--> , "}"
--> ]The basic building block of an expression. Take a look at the functions below to learn how to create and combine these.
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.
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.
Create a triple pattern/template by providing a subject, verb, and object.
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> ."
--> , "}"
--> ]An instance of Results represents the results of a SELECT query.
A JSON decoder to decode the
"results"
section of a variable binding result.
The
Querytype represents a SPARQL query.