2017-04-12 14:48:17 +00:00
|
|
|
defmodule RDF.Statement do
|
|
|
|
@moduledoc """
|
2017-06-16 20:50:56 +00:00
|
|
|
Helper functions for RDF statements.
|
2017-04-12 14:48:17 +00:00
|
|
|
|
|
|
|
A RDF statement is either a `RDF.Triple` or a `RDF.Quad`.
|
|
|
|
"""
|
|
|
|
|
2020-03-02 01:07:31 +00:00
|
|
|
alias RDF.{BlankNode, IRI, Literal, Quad, Term, Triple}
|
2020-05-06 16:04:19 +00:00
|
|
|
import RDF.Guards
|
2017-04-12 14:48:17 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
@type subject :: IRI.t() | BlankNode.t()
|
|
|
|
@type predicate :: IRI.t() | BlankNode.t()
|
|
|
|
@type object :: IRI.t() | BlankNode.t() | Literal.t()
|
|
|
|
@type graph_name :: IRI.t() | BlankNode.t()
|
2017-04-12 14:48:17 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
@type coercible_subject :: subject | atom | String.t()
|
|
|
|
@type coercible_predicate :: predicate | atom | String.t()
|
|
|
|
@type coercible_object :: object | any
|
|
|
|
@type coercible_graph_name :: graph_name | atom | String.t()
|
2017-04-12 14:48:17 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
@type qualified_term :: {atom, Term.t() | nil}
|
|
|
|
@type term_mapping :: (qualified_term -> any | nil)
|
2020-03-02 01:07:31 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
@type t :: Triple.t() | Quad.t()
|
2017-04-12 14:48:17 +00:00
|
|
|
|
2017-06-05 01:01:23 +00:00
|
|
|
@doc """
|
|
|
|
Creates a `RDF.Statement` tuple with proper RDF values.
|
|
|
|
|
2017-08-11 20:22:27 +00:00
|
|
|
An error is raised when the given elements are not coercible to RDF values.
|
2017-06-05 01:01:23 +00:00
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2017-06-05 01:01:23 +00:00
|
|
|
|
2018-11-04 21:27:25 +00:00
|
|
|
iex> RDF.Statement.coerce {"http://example.com/S", "http://example.com/p", 42}
|
2017-06-05 01:01:23 +00:00
|
|
|
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42)}
|
2018-11-04 21:27:25 +00:00
|
|
|
iex> RDF.Statement.coerce {"http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph"}
|
2017-06-05 01:01:23 +00:00
|
|
|
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
|
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec coerce(Triple.coercible_t()) :: Triple.t()
|
|
|
|
@spec coerce(Quad.coercible_t()) :: Quad.t()
|
2017-08-11 20:22:27 +00:00
|
|
|
def coerce(statement)
|
2020-06-29 08:37:42 +00:00
|
|
|
def coerce({_, _, _} = triple), do: Triple.new(triple)
|
2017-08-11 20:22:27 +00:00
|
|
|
def coerce({_, _, _, _} = quad), do: Quad.new(quad)
|
2017-06-05 01:01:23 +00:00
|
|
|
|
2017-04-12 14:48:17 +00:00
|
|
|
@doc false
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec coerce_subject(coercible_subject) :: subject
|
2017-08-20 20:35:14 +00:00
|
|
|
def coerce_subject(iri)
|
|
|
|
def coerce_subject(iri = %IRI{}), do: iri
|
2017-08-11 20:22:27 +00:00
|
|
|
def coerce_subject(bnode = %BlankNode{}), do: bnode
|
|
|
|
def coerce_subject("_:" <> identifier), do: RDF.bnode(identifier)
|
2020-05-06 16:04:19 +00:00
|
|
|
def coerce_subject(iri) when maybe_ns_term(iri) or is_binary(iri), do: RDF.iri!(iri)
|
2020-06-29 08:37:42 +00:00
|
|
|
def coerce_subject(arg), do: raise(RDF.Triple.InvalidSubjectError, subject: arg)
|
2017-04-12 14:48:17 +00:00
|
|
|
|
|
|
|
@doc false
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec coerce_predicate(coercible_predicate) :: predicate
|
2017-08-20 20:35:14 +00:00
|
|
|
def coerce_predicate(iri)
|
|
|
|
def coerce_predicate(iri = %IRI{}), do: iri
|
2017-04-12 14:48:17 +00:00
|
|
|
# Note: Although, RDF does not allow blank nodes for properties, JSON-LD allows
|
|
|
|
# them, by introducing the notion of "generalized RDF".
|
|
|
|
# TODO: Support an option `:strict_rdf` to explicitly disallow them or produce warnings or ...
|
2017-08-11 20:22:27 +00:00
|
|
|
def coerce_predicate(bnode = %BlankNode{}), do: bnode
|
2020-05-06 16:04:19 +00:00
|
|
|
def coerce_predicate(iri) when maybe_ns_term(iri) or is_binary(iri), do: RDF.iri!(iri)
|
2020-06-29 08:37:42 +00:00
|
|
|
def coerce_predicate(arg), do: raise(RDF.Triple.InvalidPredicateError, predicate: arg)
|
2017-04-12 14:48:17 +00:00
|
|
|
|
|
|
|
@doc false
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec coerce_object(coercible_object) :: object
|
2017-08-20 20:35:14 +00:00
|
|
|
def coerce_object(iri)
|
|
|
|
def coerce_object(iri = %IRI{}), do: iri
|
2017-08-11 20:22:27 +00:00
|
|
|
def coerce_object(literal = %Literal{}), do: literal
|
|
|
|
def coerce_object(bnode = %BlankNode{}), do: bnode
|
|
|
|
def coerce_object(bool) when is_boolean(bool), do: Literal.new(bool)
|
2020-05-06 16:04:19 +00:00
|
|
|
def coerce_object(atom) when maybe_ns_term(atom), do: RDF.iri(atom)
|
2017-08-11 20:22:27 +00:00
|
|
|
def coerce_object(arg), do: Literal.new(arg)
|
2017-04-12 14:48:17 +00:00
|
|
|
|
|
|
|
@doc false
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec coerce_graph_name(coercible_graph_name) :: graph_name
|
2017-08-20 20:35:14 +00:00
|
|
|
def coerce_graph_name(iri)
|
2017-08-11 20:22:27 +00:00
|
|
|
def coerce_graph_name(nil), do: nil
|
2017-08-20 20:35:14 +00:00
|
|
|
def coerce_graph_name(iri = %IRI{}), do: iri
|
2017-08-11 20:22:27 +00:00
|
|
|
def coerce_graph_name(bnode = %BlankNode{}), do: bnode
|
|
|
|
def coerce_graph_name("_:" <> identifier), do: RDF.bnode(identifier)
|
2020-05-06 16:04:19 +00:00
|
|
|
def coerce_graph_name(iri) when maybe_ns_term(iri) or is_binary(iri), do: RDF.iri!(iri)
|
2017-04-12 14:48:17 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
def coerce_graph_name(arg),
|
|
|
|
do: raise(RDF.Quad.InvalidGraphContextError, graph_context: arg)
|
2018-10-21 22:52:22 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
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`.
|
|
|
|
|
2018-11-04 21:27:25 +00:00
|
|
|
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.
|
|
|
|
|
2018-10-21 22:52:22 +00:00
|
|
|
## 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"}
|
|
|
|
|
2018-11-04 21:27:25 +00:00
|
|
|
iex> {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
|
|
|
|
...> |> RDF.Statement.values(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>}
|
|
|
|
|
2018-10-21 22:52:22 +00:00
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec values(t | any, term_mapping) :: Triple.t_values() | Quad.t_values() | nil
|
2018-11-04 21:27:25 +00:00
|
|
|
def values(statement, mapping \\ &default_term_mapping/1)
|
2020-06-29 08:37:42 +00:00
|
|
|
def values({_, _, _} = triple, mapping), do: RDF.Triple.values(triple, mapping)
|
2018-11-04 21:27:25 +00:00
|
|
|
def values({_, _, _, _} = quad, mapping), do: RDF.Quad.values(quad, mapping)
|
|
|
|
def values(_, _), do: nil
|
|
|
|
|
|
|
|
@doc false
|
2020-04-10 21:40:33 +00:00
|
|
|
@spec default_term_mapping(qualified_term) :: any | nil
|
2018-11-04 21:27:25 +00:00
|
|
|
def default_term_mapping(qualified_term)
|
|
|
|
def default_term_mapping({:graph_name, nil}), do: nil
|
|
|
|
def default_term_mapping({_, term}), do: RDF.Term.value(term)
|
2018-10-21 22:52:22 +00:00
|
|
|
|
2018-11-11 01:30:25 +00:00
|
|
|
@doc """
|
|
|
|
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.
|
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec valid?(Triple.t() | Quad.t() | any) :: boolean
|
2018-11-11 01:30:25 +00:00
|
|
|
def valid?(tuple)
|
|
|
|
|
|
|
|
def valid?({subject, predicate, object}) do
|
|
|
|
valid_subject?(subject) && valid_predicate?(predicate) && valid_object?(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?({subject, predicate, object, graph_name}) do
|
|
|
|
valid_subject?(subject) && valid_predicate?(predicate) && valid_object?(object) &&
|
|
|
|
valid_graph_name?(graph_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?(_), do: false
|
|
|
|
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec valid_subject?(subject | any) :: boolean
|
2020-06-29 08:37:42 +00:00
|
|
|
def valid_subject?(%IRI{}), do: true
|
2018-11-11 01:30:25 +00:00
|
|
|
def valid_subject?(%BlankNode{}), do: true
|
2020-06-29 08:37:42 +00:00
|
|
|
def valid_subject?(_), do: false
|
2018-11-11 01:30:25 +00:00
|
|
|
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec valid_predicate?(predicate | any) :: boolean
|
2020-06-29 08:37:42 +00:00
|
|
|
def valid_predicate?(%IRI{}), do: true
|
|
|
|
def valid_predicate?(_), do: false
|
2018-11-11 01:30:25 +00:00
|
|
|
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec valid_object?(object | any) :: boolean
|
2020-06-29 08:37:42 +00:00
|
|
|
def valid_object?(%IRI{}), do: true
|
|
|
|
def valid_object?(%BlankNode{}), do: true
|
|
|
|
def valid_object?(%Literal{}), do: true
|
|
|
|
def valid_object?(_), do: false
|
2018-11-11 01:30:25 +00:00
|
|
|
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec valid_graph_name?(graph_name | any) :: boolean
|
2020-06-29 08:37:42 +00:00
|
|
|
def valid_graph_name?(%IRI{}), do: true
|
|
|
|
def valid_graph_name?(_), do: false
|
2017-04-12 14:48:17 +00:00
|
|
|
end
|