hereon-mb/elm-rdf/1.1.2

NOTE: the latest version is 2.0.0

Rdf.Graph.Encode

Turn elm values into Graph's. This is what Json.Encode is for JSON values. Since generating RDF graphs potentially requires minting (local) names for blank nodes, you first have to create an Encoder and then turn this into an Graph by feeding it into encode with a Seed.

Encoding

type Encoder compatible

A value which knows how to encode an Elm value into an RDF graph.

The compatible type variable is used to ensure that certain instances of an Encoder can only be used in the right context.

encode : Seed -> IsGraphEncoder graph -> ( Graph, Seed )

Run an Encoder. You have to provide a Seed which is used for minting unique blank node identifiers. The function encode returns the RDF graph as well as an updated Seed which you should use for further encoding or parsing of RDF graphs to ensure you don't get blank node identifier clashes.

Primitives

node : IsBlankNodeOrIri compatible -> List PropertyEncoder -> GraphEncoder

Encode a node, for which we already have a name, alongside some properties where the node is the subject.

blankNode : List PropertyEncoder -> GraphEncoder

Encode a new blank node alongside some properties where the node is the subject. This will mint a new blank node identifier under the hood.

IsBlankNodeOrIri compatible
-> List PropertyEncoder
-> PropertyEncoder

This works like node but returns a PropertyEncoder. You can use this if you are deep in a tree of encoders and have to encode a different/detached part of the graph.

fromBlankNode : List PropertyEncoder -> PropertyEncoder

This works like blankNode but returns a PropertyEncoder. You can use this if you are deep in a tree of encoders and have to encode a different/detached part of the graph.

property : IsPath compatible -> IsGraphOrTermEncoder object -> PropertyEncoder

Encode a property by providing an IRI of property path and an object. The current focus node will be the subject.

term : IsBlankNodeOrIriOrLiteral compatible -> TermEncoder

Encode a single RDF term.

import Rdf exposing (iri, a)
import Rdf.Graph exposing (initialSeed, serializeTurtle)


blankNode
    [ property a (term (iri "http://example.org#Person")) ]
    |> encode initialSeed
    |> Tuple.first
    |> serializeTurtle
    |> String.lines
--> [ "_:2273e9c9-fa2d-4c28-ae87-947ad36cbecd"
--> , "  a <http://example.org#Person> ."
--> ]

Types

type alias GraphEncoder =
{ graph : Compatible
, graphOrTerm : Compatible
}

An Encoder which can be fed into encode to produce an RDF graph.

type alias IsGraphEncoder compatible =
{ compatible | graph : Compatible, graphOrTerm : Compatible }

This type will only be used as the argument of functions.

type alias PropertyEncoder =
Encoder { property : Compatible }

An Encoder which encodes an RDF property.

type alias IsPropertyEncoder compatible =
Encoder { compatible | property : Compatible }

This type will only be used as the argument of functions.

type alias TermEncoder =
{ term : Compatible
, graphOrTerm : Compatible
}

An Encoder which encodes an RDF term.

type alias IsTermEncoder compatible =
{ compatible | term : Compatible, graphOrTerm : Compatible }

This type will only be used as the argument of functions.

type alias GraphOrTermEncoder =
Encoder { graphOrTerm : Compatible }

This type will only be used as the argument of functions.

type alias IsGraphOrTermEncoder compatible =
Encoder { compatible | graphOrTerm : Compatible }

An Encoder which encodes an RDF graph or an RDf term.

Utility

You can ignore this type, it is only used for implementing the different Encoder variants.