hereon-mb/elm-rdf/1.1.1

NOTE: the latest version is 2.0.0

Rdf.Graph

This module defines the Graph type which models an RDF graph, and helper functions for building, updating, parsing, serializing these. Take a look at the Rdf.Graph.Decode module if you want to extract Elm values from a Graph. On the other hand, the Rdf.Graph.Encode module deals with conveniently building these Graph's.

type alias Graph =
Graph

This type represents an RDF Graph.

Build

empty : Graph

Create an empty RDF graph.

IsBlankNodeOrIri compatible1
-> IsIri compatible2
-> IsBlankNodeOrIriOrLiteral compatible3
-> Graph

Create an RDF graph which only contains one triple.

fromTriples : List Triple -> Graph

Convert a list of RDF triples into an RDF graph.

fromTriplesSafe : Seed -> List Triple -> ( Graph, Seed )

Like fromTriples, this converts a list of RDF triples into an RDF graph, while also minting new blank node identifiers using the provided Seed. This can be useful if you plan to merge multiple graphs with union and need to ensure that there are no identifiers used twice.

Seeds

type Seed

A Seed is used to mint unique blank node identifiers. Functions like parseSafe, fromTriplesSafe, or generateBlankNode take a Seed and then return an updated one. You should always use the returned Seed in subsequent calls to ensure that you don't get blank node identifier collisions.

A fixed Seed. This is useful for tests. For actual application code, take a look at seedGenerator.

seedGenerator : Generator Seed

A Random.Generator for generating a random Seed.

Turtle and N-Triples format

parse : String -> Result Error Graph

Parse a Turtle or N-Triples file and put all RDF triples into a Graph. This function will keep blank node identifiers and only mint new ones if they are not given in the original.

Important: For minting the initialSeed is being used. Therefore, you will probably get identificer clashes when parsing multiple graphs and combining them later. It is usually better to use parseSafe instead.

parseSafe : Seed -> String -> Result Error ( Graph, Seed )

Parse a Turtle or N-Triples file and put all RDF triples into a Graph. This function will keep blank node identifiers and only mint new ones if they are not given in the original. For minting it uses the provided Seed. Make sure to reuse the returned Seed in subsequent calls to avoid blank node identifier collisions.

type Error
= ErrorParser (List DeadEnd)
| MissingSubject
| MissingPredicate
| MissingBase
| CouldNotResolvePrefixedName String String

You get one of these if parsing fails.

errorToString : String -> Error -> String

Turn a Error into a more human friendly error message.

RDFJS

decoder : Decoder Graph

A JSON decoder for decoding a Graph from a JSON value according to the RDFJS Specification.

decoderSafe : Seed -> Decoder ( Graph, Seed )

Like decoder, but mint fresh blank node identifiers using the provided Seed.

encode : Graph -> Value

Encode a Graph into a JSON value according to the RDFJS Specification.

Query

isEmpty : Graph -> Bool

Determine if a Graph is empty.

isEmpty empty
--> True

Transform

IsBlankNodeOrIri compatible1
-> IsIri compatible2
-> IsBlankNodeOrIriOrLiteral compatible3
-> Graph
-> Graph

Insert an RDF triple into the Graph.

IsBlankNodeOrIri compatible1
-> IsPath compatible2
-> IsBlankNodeOrIriOrLiteral compatible3
-> Graph
-> Seed
-> ( Graph, Seed )

Insert a property into the graph. If you provide a property path which does not just consist of a single IRI (or its inverse), intermediate blank nodes will be minted using the provided Seed.

generateBlankNode : Seed -> ( BlankNode, Seed )

Create a blank node with a freshy minted identifier using the provided Seed.

mintBlankNodes : Seed -> Graph -> ( Graph, Seed )

Mint new identifiers for all blank nodes in the graph using the provided Seed. This can be usefull before merging multiple Graph's with union.

Base and prefixes

getBase : Graph -> Maybe String

Return the current base declaration.

setBase : String -> Graph -> Graph

Set the base declaration to the provided URL.

clearBase : Graph -> Graph

Remove the base declaration.

getPrefixes : Graph -> Dict String String

Return all defined prefix declarations.

addPrefix : String -> String -> Graph -> Graph

Add one prefix declaration to the Graph. An existing declaration with the same name will be overwritten.

addPrefixes : Dict String String -> Graph -> Graph

Add some prefix declarations to the Graph. Existing prefixes with the same name will be overwritten.

clearPrefixes : Graph -> Graph

Remove all prefix declarations.

Combine

union : Graph -> Graph -> Graph

Merge two Graph's into one.

Important: This will not mint new blank node identifiers, so you might get unwanted identifier collissions. Take a look at mintBlankNodes for a safer approach.

Export

serialize : Graph -> String

Serialize a Graph into the N-Triples format.

serializeTurtle : Graph -> String

Serialize a Graph into the Turtle format. Take a look the Base and prefixes section to adjust the serialization.