This module defines the types and helper functions to work with the basic buildings blocks of the RDF ecosystem. If you are new to RDF, you should take a look at the RDF 1.1 Primer for a introduction on its data model.
This type represents an RDF Term (which can be an IRI, a literal, or a blank node), a query variable, or a property path. They are used in different contexts, for example when encoding or decoding RDF graphs, or when building different parts of a SPARQL query, like WHERE, CONSTRUCT, or SELECT clauses, as well as expressions.
The compatible type variable is used to ensure that certain instances of
a Term can only be used in the right context. For example, subjects in an
RDF graph can only be IRI's or blank nodes, or, verbs in a WHERE clause can
only be IRI's, query variables or property paths.
For each term variant and all their combinations, we define two type aliases,
one with a type variable and one without, e.g. IsIri compatible and Iri.
Doing this makes the following code work, as the insert function uses the
general Is... compatible types.
import Rdf.Graph as Graph exposing (Graph)
alice : Iri
alice =
iri "http://example.org/alice"
knows : Iri
knows =
iri "http://example.org/#knows"
someone : BlankNode
someone =
blankNode "someone"
graph : Graph
graph =
Graph.insert alice knows someone Graph.emptyHere is an overview of all possible terms and in which context they are allowed:
| Terms | Graph | Where | Construct | Select | Expression | Path |
| | S P O | S V O | S V O | | | |
|---------------|-------|-------|-----------|--------|------------|------|
| BlankNode | x x | x x | x x | | | |
| Iri | x x x | x o x | x x x | | x | o |
| Literal | x | x | x | | x | |
| Var | | x x x | x x x | x | x | |
| Path | | x | | | | x |It follows that a term can be compatible or not within the following combinations:
IriVarBlankNode or IriBlankNode or Iri or LiteralBlankNode or Iri or Literal or VarBlankNode or Iri or VarIri or Literal or VarIri or VarVar or Path (which includes Iri)Path (which includes Iri)We consider an IRI to be the same as a predicate path and therefore define
VarOrPath instead of IriOrVarOrPath.
This section contains helper functions to create Term's from Elm
values.
Create an IRI (Internal Resource Identifier) by providing its string representation, e.g.
alice : Iri
alice =
iri "http://example.org/alice"Create a blank node by providing a blank node identifier, e.g.
unnamed : BlankNode
unnamed =
blankNode "unnamed"Warning: When you are using this function directly, you have to ensure yourself, that you don't run into naming conflicts.
Create a literal by providing its datatype IRI and its lexical form, e.g.
pi : Literal a
pi =
literal xsdDecimal "3.14"
xsdDecimal : Iri
xsdDecimal =
iri "http://www.w3.org/2001/XMLSchema#decimal"There are a few helper functions for creating literals of the most common datatypes which are part of built-in datatypes.
Create a query
variable of the
form ?name.
Create a query
variable of the
form $name.
Create an inverse property path.
Create a sequence property path.
Create an alternative property path.
Create a zero or more property path.
Create a one or more property path.
Create a zero or one property path.
Create a literal with datatype xsd:string.
string "Alice Wonderland"
--> literal
--> (iri "http://www.w3.org/2001/XMLSchema#string")
--> "Alice Wonderland"Create a literal with datatype xsd:boolean.
boolean True
--> literal
--> (iri "http://www.w3.org/2001/XMLSchema#boolean")
--> "true"Create a literal with datatype xsd:decimal. Here we are using the Decimal type from the torreyatcitty/the-best-decimal package.
import Decimal exposing (Decimal)
pi : Decimal
pi =
3.14
|> Decimal.fromFloat
|> Maybe.withDefault (Decimal.fromInt 0)
decimal pi
--> literal
--> (iri "http://www.w3.org/2001/XMLSchema#decimal")
--> "3.14"Create a literal with datatype xsd:integer.
integer 42
--> literal
--> (iri "http://www.w3.org/2001/XMLSchema#integer")
--> "42"Create a literal with datatype xsd:double.
double 3.14
--> literal
--> (iri "http://www.w3.org/2001/XMLSchema#double")
--> "3.14"Create a literal with datatype xsd:dateTime.
import Time
dateTime (Time.millisToPosix 0)
--> literal
--> (iri "http://www.w3.org/2001/XMLSchema#dateTime")
--> "1970-01-01T00:00:00.000Z"This section contains helper functions to convert any Term's into
a more specific variant.
Turn any Term into a BlankNodeOrIri if
possible.
Turn any Term into
a BlankNodeOrIriOrLiteral if possible.
This section contains helper functions to convert specific Term's
into Elm values.
Extract the URL from an Iri.
toUrl (iri "http://example.org")
--> "http://example.org"A PropertyPath models the inner structure of a Path.
Turn a Path into a PropertyPath to work with
its individual path segments.
toPropertyPath (iri "http://example.org")
--> Predicate (iri "http://example.org")
toPropertyPath (inverse (iri "http://example.org"))
--> Inverse (Predicate (iri "http://example.org"))Turn Literal into it's lexical
form.
lexicalForm (double 3.14)
--> "3.14"
lexicalForm (string "RDF is great!")
--> "RDF is great!"
lexicalForm (boolean True)
--> "true"Take any Term and extract its String value if it is a literal
with datatype xsd:string.
toString (string "Alice")
--> Just "Alice"
toString (int 42)
--> NothingTake any Term and extract its language tag and String value if
it is a literal with datatype xsd:langString.
toLangString (langString "de" "Apfel")
--> Just ( "de", "Apfel" )
toLangString (int 42)
--> NothingTake any Term and extract its Int value if it is a literal
with datatype xsd:integer or xsd:int.
toInt (integer 42)
--> Just 42
toInt (int 42)
--> Just 42
toInt (double 3.14)
--> NothingTake any Term and extract its Float value if it is a literal
with datatype xsd:double or xsd:float.
Rdf.toFloat (double 3.14)
--> Just 3.14
Rdf.toFloat (float 3.14)
--> Just 3.14
Rdf.toFloat (int 42)
--> NothingTake any Term and extract its Decimal value if it is a literal
with datatype xsd:decimal.
import Decimal
toDecimal (decimal (Decimal.fromIntWithExponent 314 -2))
--> Just (Decimal.fromIntWithExponent 314 -2)
toDecimal (float 3.14)
--> Nothing
toDecimal (int 42)
--> NothingTake any Term and extract its Posix value if it is a literal
with datatype xsd:date.
Take any Term and extract its Posix value if it is a literal
with datatype xsd:dateTime.
Take any Term and extract its Bool value if it is a literal
with datatype xsd:boolean.
toBool (boolean True)
--> Just TrueA few helper functions to transform Iri's.
Append a String to the path component of an Iri.
appendPath "/alice" (iri "http://example.org")
--> iri "http://example.org/alice"
appendPath "/datasets" (iri "http://example.org?sort=id")
--> iri "http://example.org/datasets?sort=id"
appendPath "/people" (iri "http://example.org#alice")
--> iri "http://example.org/people#alice"Drop the fragment from an Iri.
dropFragment (iri "http://example.org#alice")
--> iri "http://example.org"Set the fragment of an Iri
setFragment "alice" (iri "http://example.org")
--> iri "http://example.org#alice"
setFragment "bob" (iri "http://example.org#alice")
--> iri "http://example.org#bob"Set a query parameter in an Iri
setQueryParam "sort" "id" (iri "http://example.org")
--> iri "http://example.org?sort=id"
setQueryParam "sort" "name" (iri "http://example.org?sort=id")
--> iri "http://example.org?sort=name"Append a String to a query variable.
append "Label" (varQ "instance")
--> varQ "instanceLabel"
append "Label" (varD "instance")
--> varD "instanceLabel"Check if the second property path starts with the first one.
startsWith
(iri "http://example.org#knows")
(sequence (iri "http://example.org#knows")
[ rdfs "label" ]
)
--> True
startsWith
(iri "http://example.org#knows")
(iri "http://example.org#knows")
--> True
startsWith
(iri "http://example.org#knows")
(iri "http://example.org#owns")
--> FalseReturn the property
path which remains after
removing the first path from the beginning of the second path. This will return
Nothing if the second path does not start with the first, or if the resulting
path is empty.
rightOf
(iri "http://example.org#knows")
(sequence (iri "http://example.org#knows")
[ rdfs "label" ]
)
--> Just (asPath (rdfs "label"))
rightOf
(iri "http://example.org#knows")
(iri "http://example.org#knows")
--> Nothing
rightOf
(iri "http://example.org#knows")
(iri "http://example.org#owns")
--> NothingReturn the Iri from the last segment of the given property
path.
lastPredicatePath
(sequence (iri "http://example.org#knows")
[ rdfs "label" ]
)
--> Just (rdfs "label")A Prologue captures the base and prefixes which are used to
shorten the serialization of Term's.
The IRI http://www.w3.org/1999/02/22-rdf-syntax-ns#type which is used to
state that a node is an instance of an RDFS or OWL class.
A few helpers for commonly used IRI prefixes.
The prefix http://www.w3.org/1999/02/22-rdf-syntax-ns#.
The prefix http://www.w3.org/2000/01/rdf-schema#.
The prefix http://www.w3.org/2001/XMLSchema#.
The prefix http://www.w3.org/2002/07/owl#.
The prefix http://www.w3.org/ns/shacl#.
The prefix http://purl.org/dc/terms/.
The prefix http://www.w3.org/ns/prov#.
The prefix http://schema.org/.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be a blank node. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only blank node's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be a query variable. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only query variable's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be a property path. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only property path's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be a blank node or an IRI. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only blank node's or IRI's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be a blank node, an IRI, or a literal. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only blank node's, IRI's, or literal's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be a blank node, an IRI, a literal, or a query variable. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only blank node's, IRI's, literal's, or query variable's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be a blank node, an IRI, or a query variable. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only blank node's, IRI's, or query variable's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be an IRI, a literal, or a query variable. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only IRI's, literal's, or query variable's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
An RDF term which can only be a query variable or a property path. This type will only be used as the return type of functions and if the values need to be stored in the model. Take a look at Term for a general explanation of the idea behind this.
This type will only be used as the argument of functions and ensures that only query variable's or property path's are passed. Take a look at Term for a general explanation of the idea behind this.
When writing your own helper functions, it might be necessary that you have to convert the argument type into the return value type. Take a look at Term for a general explanation of the idea behind this.
You can ignore this type, it is only used for implementing the different
Term variants.
This type represents an RDF triple. It consists of
An RDF triple describes a directed labelled node within an RDF graph, pointing from the subject to the object and the label being the predicate.