2017-02-18 20:35:27 +00:00
|
|
|
defmodule RDF.Quad do
|
|
|
|
@moduledoc """
|
2017-06-16 20:50:56 +00:00
|
|
|
Helper functions for RDF quads.
|
2017-02-18 20:35:27 +00:00
|
|
|
|
2017-06-16 20:50:56 +00:00
|
|
|
A RDF Quad is represented as a plain Elixir tuple consisting of four valid
|
2020-10-09 14:32:24 +00:00
|
|
|
RDF values for subject, predicate, object and a graph name.
|
2017-02-18 20:35:27 +00:00
|
|
|
"""
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
alias RDF.{Statement, PropertyMap}
|
2017-02-18 20:35:27 +00:00
|
|
|
|
2020-10-10 13:45:25 +00:00
|
|
|
@type t :: {
|
|
|
|
Statement.subject(),
|
|
|
|
Statement.predicate(),
|
|
|
|
Statement.object(),
|
|
|
|
Statement.graph_name()
|
|
|
|
}
|
2020-02-28 17:51:48 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
@type t_values :: {String.t(), String.t(), any, String.t()}
|
2020-02-28 17:51:48 +00:00
|
|
|
|
2017-02-18 20:35:27 +00:00
|
|
|
@doc """
|
|
|
|
Creates a `RDF.Quad` 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-02-18 20:35:27 +00:00
|
|
|
|
|
|
|
Note: The `RDF.quad` function is a shortcut to this function.
|
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2017-02-18 20:35:27 +00:00
|
|
|
|
|
|
|
iex> RDF.Quad.new("http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph")
|
2017-04-10 01:06:20 +00:00
|
|
|
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
|
2020-10-09 14:32:24 +00:00
|
|
|
|
2017-06-16 22:44:11 +00:00
|
|
|
iex> RDF.Quad.new(EX.S, EX.p, 42, EX.Graph)
|
2017-08-20 20:35:14 +00:00
|
|
|
{RDF.iri("http://example.com/S"), RDF.iri("http://example.com/p"), RDF.literal(42), RDF.iri("http://example.com/Graph")}
|
2020-10-09 14:32:24 +00:00
|
|
|
|
|
|
|
iex> RDF.Quad.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")}
|
2017-02-18 20:35:27 +00:00
|
|
|
"""
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec new(
|
2020-06-29 08:37:42 +00:00
|
|
|
Statement.coercible_subject(),
|
|
|
|
Statement.coercible_predicate(),
|
|
|
|
Statement.coercible_object(),
|
2020-10-09 14:32:24 +00:00
|
|
|
Statement.coercible_graph_name(),
|
|
|
|
PropertyMap.t() | nil
|
2020-03-02 01:07:31 +00:00
|
|
|
) :: t
|
2020-10-09 14:32:24 +00:00
|
|
|
def new(subject, predicate, object, graph_name, property_map \\ nil)
|
|
|
|
|
|
|
|
def new(subject, predicate, object, graph_name, nil) do
|
2017-02-18 20:35:27 +00:00
|
|
|
{
|
2017-08-11 20:22:27 +00:00
|
|
|
Statement.coerce_subject(subject),
|
|
|
|
Statement.coerce_predicate(predicate),
|
|
|
|
Statement.coerce_object(object),
|
2020-10-09 14:32:24 +00:00
|
|
|
Statement.coerce_graph_name(graph_name)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def new(subject, predicate, object, graph_name, %PropertyMap{} = property_map) do
|
|
|
|
{
|
|
|
|
Statement.coerce_subject(subject),
|
|
|
|
Statement.coerce_predicate(predicate, property_map),
|
|
|
|
Statement.coerce_object(object),
|
|
|
|
Statement.coerce_graph_name(graph_name)
|
2017-02-18 20:35:27 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a `RDF.Quad` 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-02-18 20:35:27 +00:00
|
|
|
|
|
|
|
Note: The `RDF.quad` function is a shortcut to this function.
|
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2017-02-18 20:35:27 +00:00
|
|
|
|
|
|
|
iex> RDF.Quad.new {"http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph"}
|
2017-04-10 01:06:20 +00:00
|
|
|
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
|
2020-10-09 14:32:24 +00:00
|
|
|
|
2017-06-16 22:44:11 +00:00
|
|
|
iex> RDF.Quad.new {EX.S, EX.p, 42, EX.Graph}
|
2017-08-20 20:35:14 +00:00
|
|
|
{RDF.iri("http://example.com/S"), RDF.iri("http://example.com/p"), RDF.literal(42), RDF.iri("http://example.com/Graph")}
|
2020-10-09 14:32:24 +00:00
|
|
|
|
|
|
|
iex> RDF.Quad.new {EX.S, EX.p, 42}
|
|
|
|
{RDF.iri("http://example.com/S"), RDF.iri("http://example.com/p"), RDF.literal(42), nil}
|
|
|
|
|
|
|
|
iex> RDF.Quad.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")}
|
2017-02-18 20:35:27 +00:00
|
|
|
"""
|
2020-10-09 14:32:24 +00:00
|
|
|
@spec new(Statement.coercible_t(), PropertyMap.t() | nil) :: t
|
|
|
|
def new(statement, property_map \\ nil)
|
|
|
|
|
|
|
|
def new({subject, predicate, object, graph_name}, property_map) do
|
|
|
|
new(subject, predicate, object, graph_name, property_map)
|
|
|
|
end
|
|
|
|
|
|
|
|
def new({subject, predicate, object}, property_map) do
|
|
|
|
new(subject, predicate, object, nil, property_map)
|
|
|
|
end
|
2017-02-18 20:35:27 +00:00
|
|
|
|
2018-10-21 22:52:22 +00:00
|
|
|
@doc """
|
|
|
|
Returns a tuple of native Elixir values from a `RDF.Quad` of RDF terms.
|
|
|
|
|
2020-10-13 09:38:22 +00:00
|
|
|
When a `:context` option is given with a `RDF.PropertyMap`, predicates will
|
|
|
|
be mapped to the terms defined in the `RDF.PropertyMap`, if present.
|
2018-10-21 22:52:22 +00:00
|
|
|
|
2020-10-10 13:45:25 +00:00
|
|
|
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
|
|
|
|
2018-10-21 22:52:22 +00:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> RDF.Quad.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>}
|
2020-10-13 09:38:22 +00:00
|
|
|
...> |> RDF.Quad.values(context: %{p: ~I<http://example.com/p>})
|
2020-10-10 13:45:25 +00:00
|
|
|
{"http://example.com/S", :p, 42, "http://example.com/Graph"}
|
|
|
|
|
|
|
|
"""
|
2020-10-13 09:38:22 +00:00
|
|
|
@spec values(t, keyword) :: t_values | nil
|
|
|
|
def values(quad, opts \\ []) do
|
|
|
|
if property_map = PropertyMap.from_opts(opts) do
|
|
|
|
map(quad, Statement.default_property_mapping(property_map))
|
|
|
|
else
|
|
|
|
map(quad, &Statement.default_term_mapping/1)
|
|
|
|
end
|
2020-10-10 13:45:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns a tuple where each element from a `RDF.Quad` is mapped with the given function.
|
|
|
|
|
|
|
|
Returns `nil` if one of the components of the given tuple is not convertible via `RDF.Term.value/1`.
|
|
|
|
|
|
|
|
The function `fun` 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 `map/2` call.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.literal(42), ~I<http://example.com/Graph>}
|
|
|
|
...> |> RDF.Quad.map(fn
|
2018-11-04 21:27:25 +00:00
|
|
|
...> {:object, object} ->
|
|
|
|
...> RDF.Term.value(object)
|
|
|
|
...> {:graph_name, graph_name} ->
|
|
|
|
...> graph_name
|
|
|
|
...> {_, resource} ->
|
|
|
|
...> resource |> to_string() |> String.last() |> String.to_atom()
|
|
|
|
...> end)
|
|
|
|
{:S, :p, 42, ~I<http://example.com/Graph>}
|
|
|
|
|
2018-10-21 22:52:22 +00:00
|
|
|
"""
|
2020-10-10 13:45:25 +00:00
|
|
|
@spec map(t, Statement.term_mapping()) :: t_values | nil
|
|
|
|
def map({subject, predicate, object, graph_name}, fun) do
|
|
|
|
with subject_value when not is_nil(subject_value) <- fun.({:subject, subject}),
|
|
|
|
predicate_value when not is_nil(predicate_value) <- fun.({:predicate, predicate}),
|
|
|
|
object_value when not is_nil(object_value) <- fun.({:object, object}),
|
|
|
|
graph_name_value <- fun.({:graph_name, graph_name}) do
|
2020-10-09 14:32:24 +00:00
|
|
|
{subject_value, predicate_value, object_value, graph_name_value}
|
2018-11-04 21:27:25 +00:00
|
|
|
else
|
|
|
|
_ -> nil
|
2018-10-21 22:52:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-11 01:30:25 +00:00
|
|
|
@doc """
|
|
|
|
Checks if the given tuple is a valid RDF quad.
|
|
|
|
|
|
|
|
The elements of a valid RDF quad must be RDF terms. On the subject
|
|
|
|
position only IRIs and blank nodes allowed, while on the predicate and graph
|
2020-10-09 14:32:24 +00:00
|
|
|
name position only IRIs allowed. The object position can be any RDF term.
|
2018-11-11 01:30:25 +00:00
|
|
|
"""
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec valid?(t | any) :: boolean
|
2018-11-11 01:30:25 +00:00
|
|
|
def valid?(tuple)
|
|
|
|
def valid?({_, _, _, _} = quad), do: Statement.valid?(quad)
|
|
|
|
def valid?(_), do: false
|
2017-02-18 20:35:27 +00:00
|
|
|
end
|