View Source RDF.Statement (RDF.ex v0.11.0)

Helper functions for RDF statements.

An RDF statement is either a RDF.Triple or a RDF.Quad.

Link to this section Summary

Functions

Creates a RDF.Statement tuple with proper RDF values.

Returns a tuple of native Elixir values from a RDF.Statement of RDF terms.

Creates a RDF.Triple or RDF.Quad with proper RDF values.

Checks if the given tuple is a valid RDF statement, i.e. RDF triple or quad.

Returns a tuple of native Elixir values from a RDF.Statement of RDF terms.

Link to this section Types

Specs

Link to this type

coercible_graph_name()

View Source

Specs

coercible_graph_name() :: graph_name() | atom() | String.t()

Specs

coercible_object() :: object() | any()

Specs

coercible_predicate() :: RDF.Resource.coercible()

Specs

coercible_subject() :: RDF.Resource.coercible()

Specs

coercible_t() :: coercible()

Specs

graph_name() :: RDF.Resource.t() | nil

Specs

object() :: RDF.Resource.t() | RDF.Literal.t()

Specs

position() :: :subject | :predicate | :object | :graph_name

Specs

predicate() :: RDF.Resource.t()

Specs

qualified_term() :: {position(), RDF.Term.t() | nil}

Specs

subject() :: RDF.Resource.t()

Specs

t() :: RDF.Triple.t() | RDF.Quad.t()

Specs

term_mapping() :: (qualified_term() -> any() | nil)

Link to this section Functions

Link to this function

coerce(statement, property_map \\ nil)

View Source

Specs

coerce(coercible(), RDF.PropertyMap.t() | nil) :: RDF.Triple.t() | RDF.Quad.t()

Creates a RDF.Statement tuple with proper RDF values.

An error is raised when the given elements are not coercible to RDF values.

examples

Examples

iex> RDF.Statement.coerce {"http://example.com/S", "http://example.com/p", 42}
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42)}
iex> RDF.Statement.coerce {"http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph"}
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
Link to this function

default_property_mapping(property_map)

View Source

Specs

default_property_mapping(RDF.PropertyMap.t()) :: term_mapping()

Specs

Returns a tuple of native Elixir values from a RDF.Statement of RDF terms.

Returns nil if one of the components of the given tuple is not convertible via RDF.Term.value/1.

The optional second argument allows to specify a custom mapping with a function which will receive a tuple {statement_position, rdf_term} where statement_position is one of the atoms :subject, :predicate, :object or :graph_name, while rdf_term is the RDF term to be mapped. When the given function returns nil this will be interpreted as an error and will become the overhaul result of the values/2 call.

examples

Examples

iex> {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
...> |> RDF.Statement.map(fn
...>      {:subject, subject} ->
...>        subject |> to_string() |> String.last()
...>      {:predicate, predicate} ->
...>        predicate |> to_string() |> String.last() |> String.to_atom()
...>      {:object, object} ->
...>        RDF.Term.value(object)
...>      {:graph_name, graph_name} ->
...>        graph_name
...>    end)
{"S", :p, 42, ~I<http://example.com/Graph>}
Link to this function

new(tuple, property_map \\ nil)

View Source

Creates a RDF.Triple or RDF.Quad with proper RDF values.

An error is raised when the given elements are not coercible to RDF values.

Note: The RDF.statement function is a shortcut to this function.

examples

Examples

iex> RDF.Statement.new({EX.S, EX.p, 42})
{RDF.iri("http://example.com/S"), RDF.iri("http://example.com/p"), RDF.literal(42)}

iex> RDF.Statement.new({EX.S, EX.p, 42, EX.Graph})
{RDF.iri("http://example.com/S"), RDF.iri("http://example.com/p"), RDF.literal(42), RDF.iri("http://example.com/Graph")}

iex> RDF.Statement.new({EX.S, :p, 42, EX.Graph}, RDF.PropertyMap.new(p: EX.p))
{RDF.iri("http://example.com/S"), RDF.iri("http://example.com/p"), RDF.literal(42), RDF.iri("http://example.com/Graph")}

See RDF.Triple.new/3.

See RDF.Quad.new/4.

Specs

valid?(RDF.Triple.t() | RDF.Quad.t() | any()) :: boolean()

Checks if the given tuple is a valid RDF statement, i.e. RDF triple or quad.

The elements of a valid RDF statement must be RDF terms. On the subject position only IRIs and blank nodes allowed, while on the predicate and graph context position only IRIs allowed. The object position can be any RDF term.

Specs

valid_graph_name?(graph_name() | any()) :: boolean()

Specs

valid_object?(object() | any()) :: boolean()

Specs

valid_predicate?(predicate() | any()) :: boolean()

Specs

valid_subject?(subject() | any()) :: boolean()
Link to this function

values(quad, opts \\ [])

View Source

Specs

Returns a tuple of native Elixir values from a RDF.Statement of RDF terms.

When a :context option is given with a RDF.PropertyMap, predicates will be mapped to the terms defined in the RDF.PropertyMap, if present.

Returns nil if one of the components of the given tuple is not convertible via RDF.Term.value/1.

examples

Examples

iex> RDF.Statement.values {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42)}
{"http://example.com/S", "http://example.com/p", 42}

iex> RDF.Statement.values {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
{"http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph"}

iex> {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42)}
...> |> RDF.Statement.values(context: %{p: ~I<http://example.com/p>})
{"http://example.com/S", :p, 42}