2016-10-15 16:26:56 +00:00
|
|
|
defmodule RDF.Description do
|
|
|
|
@moduledoc """
|
2017-06-16 20:50:56 +00:00
|
|
|
A set of RDF triples about the same subject.
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-06-16 20:50:56 +00:00
|
|
|
`RDF.Description` implements:
|
|
|
|
|
2018-03-19 00:50:05 +00:00
|
|
|
- Elixir's `Access` behaviour
|
|
|
|
- Elixir's `Enumerable` protocol
|
|
|
|
- Elixir's `Inspect` protocol
|
2017-06-16 20:50:56 +00:00
|
|
|
- the `RDF.Data` protocol
|
2016-10-15 16:26:56 +00:00
|
|
|
"""
|
2017-06-16 20:50:56 +00:00
|
|
|
|
2020-09-28 15:39:31 +00:00
|
|
|
@enforce_keys [:subject]
|
|
|
|
defstruct subject: nil, predications: %{}
|
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
@behaviour Access
|
|
|
|
|
2021-09-27 18:57:17 +00:00
|
|
|
alias RDF.PropertyMap
|
|
|
|
alias RDF.Star.{Statement, Triple}
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-02-28 17:51:48 +00:00
|
|
|
@type t :: %__MODULE__{
|
2020-06-29 08:37:42 +00:00
|
|
|
subject: Statement.subject(),
|
2020-02-28 17:51:48 +00:00
|
|
|
predications: predications
|
2020-06-29 08:37:42 +00:00
|
|
|
}
|
2020-02-28 17:51:48 +00:00
|
|
|
|
2020-10-09 20:20:52 +00:00
|
|
|
@type predications :: %{Statement.predicate() => %{Statement.object() => nil}}
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
@type input ::
|
2022-03-13 20:39:48 +00:00
|
|
|
Statement.coercible()
|
2020-09-25 14:07:37 +00:00
|
|
|
| {
|
|
|
|
Statement.coercible_predicate(),
|
|
|
|
Statement.coercible_object() | [Statement.coercible_object()]
|
|
|
|
}
|
2020-07-27 21:09:23 +00:00
|
|
|
| %{
|
|
|
|
Statement.coercible_predicate() =>
|
|
|
|
Statement.coercible_object() | [Statement.coercible_object()]
|
|
|
|
}
|
|
|
|
| [
|
2022-03-13 20:39:48 +00:00
|
|
|
Statement.coercible()
|
2020-09-25 14:07:37 +00:00
|
|
|
| {
|
|
|
|
Statement.coercible_predicate(),
|
|
|
|
Statement.coercible_object() | [Statement.coercible_object()]
|
|
|
|
}
|
2020-07-27 21:09:23 +00:00
|
|
|
| t
|
|
|
|
]
|
|
|
|
| t
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2017-06-16 21:03:44 +00:00
|
|
|
@doc """
|
2020-09-25 14:07:37 +00:00
|
|
|
Creates an `RDF.Description` about the given subject.
|
|
|
|
|
|
|
|
The created `RDF.Description` can be initialized with any form of data which
|
|
|
|
`add/2` understands with the `:init` option. Additionally a function returning
|
|
|
|
the initialization data in any of these forms can be as the `:init` value.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
RDF.Description.new(EX.S)
|
|
|
|
RDF.Description.new(EX.S, init: {EX.S, EX.p, EX.O})
|
|
|
|
RDF.Description.new(EX.S, init: {EX.p, [EX.O1, EX.O2]})
|
|
|
|
RDF.Description.new(EX.S, init: [{EX.p1, EX.O1}, {EX.p2, EX.O2}])
|
|
|
|
RDF.Description.new(EX.S, init: RDF.Description.new(EX.S, init: {EX.P, EX.O}))
|
|
|
|
RDF.Description.new(EX.S, init: fn -> {EX.p, EX.O} end)
|
|
|
|
|
2017-06-16 21:03:44 +00:00
|
|
|
"""
|
2020-09-25 14:07:37 +00:00
|
|
|
@spec new(Statement.coercible_subject() | t, keyword) :: t
|
|
|
|
def new(subject, opts \\ [])
|
|
|
|
|
|
|
|
def new(%__MODULE__{} = description, opts), do: new(description.subject, opts)
|
|
|
|
|
|
|
|
def new(subject, opts) do
|
2020-10-09 14:32:24 +00:00
|
|
|
{data, opts} = Keyword.pop(opts, :init)
|
|
|
|
|
2021-09-27 18:57:17 +00:00
|
|
|
%__MODULE__{subject: RDF.coerce_subject(subject)}
|
2020-10-09 14:32:24 +00:00
|
|
|
|> init(data, opts)
|
2020-09-25 14:07:37 +00:00
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
defp init(description, nil, _), do: description
|
|
|
|
defp init(description, fun, opts) when is_function(fun), do: add(description, fun.(), opts)
|
|
|
|
defp init(description, data, opts), do: add(description, data, opts)
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-07-28 12:45:12 +00:00
|
|
|
@doc """
|
|
|
|
Returns the subject IRI or blank node of a description.
|
|
|
|
"""
|
2020-10-05 12:32:18 +00:00
|
|
|
@spec subject(t) :: Statement.subject()
|
2020-07-28 12:45:12 +00:00
|
|
|
def subject(%__MODULE__{} = description), do: description.subject
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Changes the subject of a description.
|
|
|
|
"""
|
2020-10-05 12:32:18 +00:00
|
|
|
@spec change_subject(t, Statement.coercible_subject()) :: t
|
2020-07-28 12:45:12 +00:00
|
|
|
def change_subject(%__MODULE__{} = description, new_subject) do
|
2021-09-27 18:57:17 +00:00
|
|
|
%__MODULE__{description | subject: RDF.coerce_subject(new_subject)}
|
2020-07-28 12:45:12 +00:00
|
|
|
end
|
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
@doc """
|
2020-07-27 21:09:23 +00:00
|
|
|
Add statements to a `RDF.Description`.
|
2017-06-02 13:12:53 +00:00
|
|
|
|
|
|
|
Note: When the statements to be added are given as another `RDF.Description`,
|
|
|
|
the subject must not match subject of the description to which the statements
|
|
|
|
are added. As opposed to that `RDF.Data.merge/2` will produce a `RDF.Graph`
|
|
|
|
containing both descriptions.
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
## Examples
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.P1, EX.O1})
|
|
|
|
...> |> RDF.Description.add({EX.P2, EX.O2})
|
|
|
|
RDF.Description.new(EX.S, init: [{EX.P1, EX.O1}, {EX.P2, EX.O2}])
|
2020-10-13 13:24:31 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.P, EX.O1})
|
|
|
|
...> |> RDF.Description.add({EX.P, [EX.O2, EX.O3]})
|
|
|
|
RDF.Description.new(EX.S, init: [{EX.P, EX.O1}, {EX.P, EX.O2}, {EX.P, EX.O3}])
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
"""
|
|
|
|
@spec add(t, input, keyword) :: t
|
|
|
|
def add(description, input, opts \\ [])
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
def add(%__MODULE__{} = description, {subject, predicate, objects, _}, opts) do
|
|
|
|
add(description, {subject, predicate, objects}, opts)
|
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
def add(%__MODULE__{} = description, {subject, predicate, objects}, opts) do
|
2021-09-27 18:57:17 +00:00
|
|
|
if RDF.coerce_subject(subject) == description.subject do
|
2020-07-27 21:09:23 +00:00
|
|
|
add(description, {predicate, objects}, opts)
|
|
|
|
else
|
|
|
|
description
|
|
|
|
end
|
2016-11-11 15:40:58 +00:00
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
def add(%__MODULE__{} = description, {predicate, objects}, opts) do
|
2020-07-27 21:09:23 +00:00
|
|
|
normalized_objects =
|
|
|
|
objects
|
|
|
|
|> List.wrap()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Map.new(&{RDF.coerce_object(&1), nil})
|
2020-07-27 21:09:23 +00:00
|
|
|
|
|
|
|
if Enum.empty?(normalized_objects) do
|
|
|
|
description
|
|
|
|
else
|
|
|
|
%__MODULE__{
|
|
|
|
description
|
|
|
|
| predications:
|
|
|
|
Map.update(
|
|
|
|
description.predications,
|
2021-09-27 18:57:17 +00:00
|
|
|
RDF.coerce_predicate(predicate, PropertyMap.from_opts(opts)),
|
2020-07-27 21:09:23 +00:00
|
|
|
normalized_objects,
|
2021-09-27 18:57:17 +00:00
|
|
|
&Map.merge(&1, normalized_objects)
|
2020-07-27 21:09:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-10-05 10:41:55 +00:00
|
|
|
# This implementation is actually unnecessary as the implementation with the is_map clause
|
|
|
|
# would work perfectly fine with RDF.Descriptions Enumerable implementation.
|
|
|
|
# It exists only for performance reasons, since this version is roughly twice as fast.
|
|
|
|
def add(%__MODULE__{} = description, %__MODULE__{} = input_description, _opts) do
|
|
|
|
%__MODULE__{
|
|
|
|
description
|
|
|
|
| predications:
|
|
|
|
Map.merge(
|
|
|
|
description.predications,
|
|
|
|
input_description.predications,
|
|
|
|
fn _predicate, objects, new_objects ->
|
|
|
|
Map.merge(objects, new_objects)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-11-19 20:10:47 +00:00
|
|
|
def add(description, input, opts)
|
|
|
|
when is_list(input) or (is_map(input) and not is_struct(input)) do
|
|
|
|
Enum.reduce(input, description, &add(&2, &1, opts))
|
2020-10-05 10:41:55 +00:00
|
|
|
end
|
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
@doc """
|
|
|
|
Adds statements to a `RDF.Description` and overwrites all existing statements with already used predicates.
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
Note: As it is a destructive function this function is more strict in its handling of
|
|
|
|
`RDF.Description`s than `add/3`. The subject of a `RDF.Description` to be put must
|
2020-07-28 12:45:12 +00:00
|
|
|
match. If you want to overwrite existing statements with those from the description of
|
|
|
|
another subject, you'll have to explicitly change the subject with `change_subject/2`
|
|
|
|
first before using `put/3`.
|
2020-07-27 21:09:23 +00:00
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.P, EX.O1})
|
|
|
|
...> |> RDF.Description.put({EX.P, EX.O2})
|
|
|
|
RDF.Description.new(EX.S, init: {EX.P, EX.O2})
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
"""
|
|
|
|
@spec put(t, input, keyword) :: t
|
|
|
|
def put(description, input, opts \\ [])
|
2020-06-29 08:37:42 +00:00
|
|
|
|
|
|
|
def put(
|
2020-07-27 21:09:23 +00:00
|
|
|
%__MODULE__{subject: subject} = description,
|
|
|
|
%__MODULE__{subject: subject} = input,
|
|
|
|
_opts
|
2020-06-29 08:37:42 +00:00
|
|
|
) do
|
2020-07-27 21:09:23 +00:00
|
|
|
%__MODULE__{
|
|
|
|
description
|
|
|
|
| predications:
|
|
|
|
Enum.reduce(
|
|
|
|
input.predications,
|
|
|
|
description.predications,
|
|
|
|
fn {predicate, objects}, predications ->
|
|
|
|
Map.put(predications, predicate, objects)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
}
|
2016-11-15 00:26:57 +00:00
|
|
|
end
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
def put(%__MODULE__{} = description, %__MODULE__{}, _opts), do: description
|
2016-11-24 07:44:07 +00:00
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
def put(%__MODULE__{} = description, input, opts) do
|
2020-10-09 14:32:24 +00:00
|
|
|
put(description, description.subject |> new() |> add(input, opts), opts)
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes statements from a `RDF.Description`.
|
2017-06-02 13:12:53 +00:00
|
|
|
|
|
|
|
Note: When the statements to be deleted are given as another `RDF.Description`,
|
|
|
|
the subject must not match subject of the description from which the statements
|
|
|
|
are deleted. If you want to delete only a matching description subject, you can
|
|
|
|
use `RDF.Data.delete/2`.
|
2017-05-23 00:03:41 +00:00
|
|
|
"""
|
2020-07-27 21:09:23 +00:00
|
|
|
@spec delete(t, input, keyword) :: t
|
|
|
|
def delete(description, input, opts \\ [])
|
|
|
|
|
|
|
|
def delete(%__MODULE__{} = description, {subject, predicate, objects}, opts) do
|
2021-09-27 18:57:17 +00:00
|
|
|
if RDF.coerce_subject(subject) == description.subject do
|
2020-07-27 21:09:23 +00:00
|
|
|
delete(description, {predicate, objects}, opts)
|
|
|
|
else
|
|
|
|
description
|
|
|
|
end
|
2017-06-02 13:12:53 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
def delete(%__MODULE__{} = description, {subject, predicate, objects, _}, opts) do
|
|
|
|
delete(description, {subject, predicate, objects}, opts)
|
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
def delete(%__MODULE__{} = description, {predicate, objects}, opts) do
|
2021-09-27 18:57:17 +00:00
|
|
|
predicate = RDF.coerce_predicate(predicate, PropertyMap.from_opts(opts))
|
2020-07-27 21:09:23 +00:00
|
|
|
|
|
|
|
if current_objects = Map.get(description.predications, predicate) do
|
|
|
|
normalized_objects =
|
|
|
|
objects
|
|
|
|
|> List.wrap()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Enum.map(&RDF.coerce_object/1)
|
2020-07-27 21:09:23 +00:00
|
|
|
|
|
|
|
rest = Map.drop(current_objects, normalized_objects)
|
|
|
|
|
|
|
|
%__MODULE__{
|
|
|
|
description
|
|
|
|
| predications:
|
|
|
|
if Enum.empty?(rest) do
|
|
|
|
Map.delete(description.predications, predicate)
|
|
|
|
else
|
|
|
|
Map.put(description.predications, predicate, rest)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
else
|
|
|
|
description
|
|
|
|
end
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-05 10:41:55 +00:00
|
|
|
# This implementation is actually unnecessary as the implementation with the is_map clause
|
|
|
|
# would work perfectly fine with RDF.Descriptions Enumerable implementation.
|
|
|
|
# It exists only for performance reasons.
|
|
|
|
def delete(%__MODULE__{} = description, %__MODULE__{} = input_description, _opts) do
|
|
|
|
predications = description.predications
|
|
|
|
|
|
|
|
%__MODULE__{
|
|
|
|
description
|
|
|
|
| predications:
|
|
|
|
Enum.reduce(
|
|
|
|
input_description.predications,
|
|
|
|
predications,
|
|
|
|
fn {predicate, objects}, predications ->
|
|
|
|
if current_objects = Map.get(description.predications, predicate) do
|
|
|
|
rest = Map.drop(current_objects, Map.keys(objects))
|
|
|
|
|
|
|
|
if Enum.empty?(rest) do
|
|
|
|
Map.delete(predications, predicate)
|
|
|
|
else
|
|
|
|
Map.put(predications, predicate, rest)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
predications
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-11-19 20:10:47 +00:00
|
|
|
def delete(description, input, opts)
|
|
|
|
when is_list(input) or (is_map(input) and not is_struct(input)) do
|
|
|
|
Enum.reduce(input, description, &delete(&2, &1, opts))
|
2020-10-05 10:41:55 +00:00
|
|
|
end
|
|
|
|
|
2017-06-02 16:06:22 +00:00
|
|
|
@doc """
|
|
|
|
Deletes all statements with the given properties.
|
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec delete_predicates(t, Statement.coercible_predicate() | [Statement.coercible_predicate()]) ::
|
|
|
|
t
|
2017-06-02 16:06:22 +00:00
|
|
|
def delete_predicates(description, properties)
|
|
|
|
|
2020-06-28 21:40:03 +00:00
|
|
|
def delete_predicates(%__MODULE__{} = description, properties) when is_list(properties) do
|
2020-10-05 12:32:18 +00:00
|
|
|
Enum.reduce(properties, description, &delete_predicates(&2, &1))
|
2017-06-02 16:06:22 +00:00
|
|
|
end
|
|
|
|
|
2020-10-05 12:32:18 +00:00
|
|
|
def delete_predicates(%__MODULE__{} = description, property) do
|
2020-07-27 21:09:23 +00:00
|
|
|
%__MODULE__{
|
2020-10-05 12:32:18 +00:00
|
|
|
description
|
2021-09-27 18:57:17 +00:00
|
|
|
| predications: Map.delete(description.predications, RDF.coerce_predicate(property))
|
2020-07-27 21:09:23 +00:00
|
|
|
}
|
2017-06-02 16:06:22 +00:00
|
|
|
end
|
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
@doc """
|
|
|
|
Fetches the objects for the given predicate of a Description.
|
|
|
|
|
|
|
|
When the predicate can not be found `:error` is returned.
|
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.p, EX.O}) |> RDF.Description.fetch(EX.p)
|
2017-08-20 20:35:14 +00:00
|
|
|
{:ok, [RDF.iri(EX.O)]}
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: [{EX.P, EX.O1}, {EX.P, EX.O2}])
|
|
|
|
...> |> RDF.Description.fetch(EX.P)
|
2017-08-20 20:35:14 +00:00
|
|
|
{:ok, [RDF.iri(EX.O1), RDF.iri(EX.O2)]}
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S) |> RDF.Description.fetch(EX.foo)
|
2016-11-11 15:40:58 +00:00
|
|
|
:error
|
|
|
|
"""
|
2018-09-17 00:08:16 +00:00
|
|
|
@impl Access
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec fetch(t, Statement.coercible_predicate()) :: {:ok, [Statement.object()]} | :error
|
2020-10-05 12:32:18 +00:00
|
|
|
def fetch(%__MODULE__{} = description, predicate) do
|
|
|
|
with {:ok, objects} <-
|
2021-09-27 18:57:17 +00:00
|
|
|
Access.fetch(description.predications, RDF.coerce_predicate(predicate)) do
|
2016-11-11 15:40:58 +00:00
|
|
|
{:ok, Map.keys(objects)}
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2016-11-11 15:40:58 +00:00
|
|
|
Gets the objects for the given predicate of a Description.
|
|
|
|
|
2017-07-08 18:55:34 +00:00
|
|
|
When the predicate can not be found, the optionally given default value or `nil` is returned.
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.P, EX.O}) |> RDF.Description.get(EX.P)
|
2017-08-20 20:35:14 +00:00
|
|
|
[RDF.iri(EX.O)]
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S) |> RDF.Description.get(EX.foo)
|
2016-11-11 15:40:58 +00:00
|
|
|
nil
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S) |> RDF.Description.get(EX.foo, :bar)
|
2016-11-11 15:40:58 +00:00
|
|
|
:bar
|
2016-10-15 16:26:56 +00:00
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec get(t, Statement.coercible_predicate(), any) :: [Statement.object()] | any
|
2020-10-05 12:32:18 +00:00
|
|
|
def get(%__MODULE__{} = description, predicate, default \\ nil) do
|
2016-11-11 15:40:58 +00:00
|
|
|
case fetch(description, predicate) do
|
|
|
|
{:ok, value} -> value
|
2020-06-29 08:37:42 +00:00
|
|
|
:error -> default
|
2016-11-11 15:40:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-08 18:55:34 +00:00
|
|
|
@doc """
|
|
|
|
Gets a single object for the given predicate of a Description.
|
|
|
|
|
|
|
|
When the predicate can not be found, the optionally given default value or `nil` is returned.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.P, EX.O}) |> RDF.Description.first(EX.P)
|
2017-08-20 20:35:14 +00:00
|
|
|
RDF.iri(EX.O)
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S) |> RDF.Description.first(EX.foo)
|
2017-07-08 18:55:34 +00:00
|
|
|
nil
|
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec first(t, Statement.coercible_predicate()) :: Statement.object() | nil
|
2020-10-05 12:32:18 +00:00
|
|
|
def first(%__MODULE__{} = description, predicate) do
|
2017-07-08 18:55:34 +00:00
|
|
|
description
|
|
|
|
|> get(predicate, [])
|
2020-06-29 08:37:42 +00:00
|
|
|
|> List.first()
|
2017-07-08 18:55:34 +00:00
|
|
|
end
|
|
|
|
|
2019-10-23 15:31:21 +00:00
|
|
|
@doc """
|
|
|
|
Updates the objects of the `predicate` in `description` with the given function.
|
|
|
|
|
|
|
|
If `predicate` is present in `description` with `objects` as value,
|
|
|
|
`fun` is invoked with argument `objects` and its result is used as the new
|
|
|
|
list of objects of `predicate`. If `predicate` is not present in `description`,
|
2019-10-24 20:03:05 +00:00
|
|
|
`initial` is inserted as the objects of `predicate`. The initial value will
|
2019-10-23 15:31:21 +00:00
|
|
|
not be passed through the update function.
|
|
|
|
|
|
|
|
The initial value and the returned objects by the update function will automatically
|
|
|
|
coerced to proper RDF object values before added.
|
|
|
|
|
|
|
|
## Examples
|
2019-10-15 15:29:46 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.p, EX.O})
|
|
|
|
...> |> RDF.Description.update(EX.p, fn objects -> [EX.O2 | objects] end)
|
|
|
|
RDF.Description.new(EX.S, init: [{EX.p, EX.O}, {EX.p, EX.O2}])
|
|
|
|
iex> RDF.Description.new(EX.S)
|
|
|
|
...> |> RDF.Description.update(EX.p, EX.O, fn _ -> EX.O2 end)
|
|
|
|
RDF.Description.new(EX.S, init: {EX.p, EX.O})
|
2019-10-23 15:31:21 +00:00
|
|
|
|
|
|
|
"""
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec update(
|
|
|
|
t,
|
2020-06-29 08:37:42 +00:00
|
|
|
Statement.coercible_predicate(),
|
|
|
|
Statement.coercible_object() | nil,
|
2020-03-02 01:07:31 +00:00
|
|
|
([Statement.Object] -> [Statement.Object])
|
|
|
|
) :: t
|
2020-10-05 12:32:18 +00:00
|
|
|
def update(%__MODULE__{} = description, predicate, initial \\ nil, fun) do
|
2021-09-27 18:57:17 +00:00
|
|
|
predicate = RDF.coerce_predicate(predicate)
|
2019-10-23 15:31:21 +00:00
|
|
|
|
|
|
|
case get(description, predicate) do
|
2021-09-27 18:57:17 +00:00
|
|
|
nil when is_nil(initial) ->
|
|
|
|
description
|
|
|
|
|
2019-10-23 15:31:21 +00:00
|
|
|
nil ->
|
2021-09-27 18:57:17 +00:00
|
|
|
put(description, {predicate, initial})
|
2019-10-23 15:31:21 +00:00
|
|
|
|
|
|
|
objects ->
|
|
|
|
objects
|
|
|
|
|> fun.()
|
|
|
|
|> List.wrap()
|
|
|
|
|> case do
|
2020-06-29 08:37:42 +00:00
|
|
|
[] -> delete_predicates(description, predicate)
|
2020-07-27 21:09:23 +00:00
|
|
|
objects -> put(description, {predicate, objects})
|
2020-06-29 08:37:42 +00:00
|
|
|
end
|
2019-10-23 15:31:21 +00:00
|
|
|
end
|
|
|
|
end
|
2019-10-15 15:29:46 +00:00
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
@doc """
|
|
|
|
Gets and updates the objects of the given predicate of a Description, in a single pass.
|
|
|
|
|
2016-11-24 20:36:13 +00:00
|
|
|
Invokes the passed function on the objects of the given predicate; this
|
|
|
|
function should return either `{objects_to_return, new_object}` or `:pop`.
|
|
|
|
|
|
|
|
If the passed function returns `{objects_to_return, new_objects}`, the return
|
|
|
|
value of `get_and_update` is `{objects_to_return, new_description}` where
|
|
|
|
`new_description` is the input `Description` updated with `new_objects` for
|
|
|
|
the given predicate.
|
|
|
|
|
|
|
|
If the passed function returns `:pop` the objects for the given predicate are
|
|
|
|
removed and a `{removed_objects, new_description}` tuple gets returned.
|
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.P, EX.O})
|
|
|
|
...> |> RDF.Description.get_and_update(EX.P, fn current_objects ->
|
|
|
|
...> {current_objects, EX.New}
|
|
|
|
...> end)
|
|
|
|
{[RDF.iri(EX.O)], RDF.Description.new(EX.S, init: {EX.P, EX.New})}
|
|
|
|
iex> RDF.Graph.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}])
|
|
|
|
...> |> RDF.Graph.description(EX.S)
|
|
|
|
...> |> RDF.Description.get_and_update(EX.P1, fn _ -> :pop end)
|
|
|
|
{[RDF.iri(EX.O1)], RDF.Description.new(EX.S, init: {EX.P2, EX.O2})}
|
2016-11-11 15:40:58 +00:00
|
|
|
"""
|
2018-09-17 00:08:16 +00:00
|
|
|
@impl Access
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec get_and_update(
|
|
|
|
t,
|
2020-06-29 08:37:42 +00:00
|
|
|
Statement.coercible_predicate(),
|
2020-03-02 01:07:31 +00:00
|
|
|
([Statement.Object] -> {[Statement.Object], t} | :pop)
|
|
|
|
) :: {[Statement.Object], t}
|
2020-10-05 12:32:18 +00:00
|
|
|
def get_and_update(%__MODULE__{} = description, predicate, fun) do
|
2021-09-27 18:57:17 +00:00
|
|
|
triple_predicate = RDF.coerce_predicate(predicate)
|
2020-07-27 21:09:23 +00:00
|
|
|
|
|
|
|
case fun.(get(description, triple_predicate)) do
|
|
|
|
{objects_to_return, new_objects} ->
|
|
|
|
{objects_to_return, put(description, {triple_predicate, new_objects})}
|
|
|
|
|
|
|
|
:pop ->
|
|
|
|
pop(description, triple_predicate)
|
2016-11-11 15:40:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-16 21:03:44 +00:00
|
|
|
@doc """
|
2017-06-16 21:42:03 +00:00
|
|
|
Pops an arbitrary triple from a `RDF.Description`.
|
2017-06-16 21:03:44 +00:00
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec pop(t) :: {Triple.t() | [Statement.Object] | nil, t}
|
2017-06-16 21:03:44 +00:00
|
|
|
def pop(description)
|
|
|
|
|
2020-10-05 12:32:18 +00:00
|
|
|
def pop(%__MODULE__{predications: predications} = description)
|
2020-07-26 21:01:43 +00:00
|
|
|
when map_size(predications) == 0,
|
2020-06-29 08:37:42 +00:00
|
|
|
do: {nil, description}
|
2017-06-16 21:03:44 +00:00
|
|
|
|
2020-10-05 12:32:18 +00:00
|
|
|
def pop(%__MODULE__{predications: predications} = description) do
|
2020-07-26 21:01:43 +00:00
|
|
|
[{predicate, objects}] = Enum.take(predications, 1)
|
|
|
|
[{object, _}] = Enum.take(objects, 1)
|
2017-06-16 21:03:44 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
popped =
|
|
|
|
if Enum.count(objects) == 1,
|
|
|
|
do: elem(Map.pop(predications, predicate), 1),
|
|
|
|
else: elem(pop_in(predications, [predicate, object]), 1)
|
2017-06-16 21:03:44 +00:00
|
|
|
|
2020-10-05 12:32:18 +00:00
|
|
|
{
|
|
|
|
{description.subject, predicate, object},
|
|
|
|
%__MODULE__{description | predications: popped}
|
|
|
|
}
|
2017-06-16 21:03:44 +00:00
|
|
|
end
|
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
@doc """
|
2016-11-24 20:36:13 +00:00
|
|
|
Pops the objects of the given predicate of a Description.
|
|
|
|
|
|
|
|
When the predicate can not be found the optionally given default value or `nil` is returned.
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-11-11 15:40:58 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.P, EX.O})
|
|
|
|
...> |> RDF.Description.pop(EX.P)
|
2017-08-20 20:35:14 +00:00
|
|
|
{[RDF.iri(EX.O)], RDF.Description.new(EX.S)}
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S, init: {EX.P, EX.O})
|
|
|
|
...> |> RDF.Description.pop(EX.Missing)
|
|
|
|
{nil, RDF.Description.new(EX.S, init: {EX.P, EX.O})}
|
2016-11-11 15:40:58 +00:00
|
|
|
"""
|
2018-09-17 00:08:16 +00:00
|
|
|
@impl Access
|
2020-10-05 12:32:18 +00:00
|
|
|
def pop(%__MODULE__{} = description, predicate) do
|
2021-09-27 18:57:17 +00:00
|
|
|
case Access.pop(description.predications, RDF.coerce_predicate(predicate)) do
|
2016-11-11 15:40:58 +00:00
|
|
|
{nil, _} ->
|
|
|
|
{nil, description}
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
{objects, new_predications} ->
|
2020-10-05 12:32:18 +00:00
|
|
|
{
|
|
|
|
Map.keys(objects),
|
|
|
|
%__MODULE__{description | predications: new_predications}
|
|
|
|
}
|
2016-11-11 15:40:58 +00:00
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2016-11-02 02:19:19 +00:00
|
|
|
@doc """
|
|
|
|
The set of all properties used in the predicates within a `RDF.Description`.
|
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-11-02 02:19:19 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S1, init: [
|
2020-07-27 21:09:23 +00:00
|
|
|
...> {EX.p1, EX.O1},
|
|
|
|
...> {EX.p2, EX.O2},
|
2020-09-25 14:07:37 +00:00
|
|
|
...> {EX.p2, EX.O3}])
|
|
|
|
...> |> RDF.Description.predicates()
|
2016-11-02 02:19:19 +00:00
|
|
|
MapSet.new([EX.p1, EX.p2])
|
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec predicates(t) :: MapSet.t()
|
2020-10-05 12:32:18 +00:00
|
|
|
def predicates(%__MODULE__{} = description) do
|
|
|
|
description.predications |> Map.keys() |> MapSet.new()
|
|
|
|
end
|
2016-11-02 02:19:19 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
The set of all resources used in the objects within a `RDF.Description`.
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
Note: This function does collect only IRIs and BlankNodes, not Literals.
|
2016-11-02 02:19:19 +00:00
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-11-02 02:19:19 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S1, init: [
|
2020-07-27 21:09:23 +00:00
|
|
|
...> {EX.p1, EX.O1},
|
|
|
|
...> {EX.p2, EX.O2},
|
|
|
|
...> {EX.p3, EX.O2},
|
|
|
|
...> {EX.p4, RDF.bnode(:bnode)},
|
2020-09-25 14:07:37 +00:00
|
|
|
...> {EX.p3, "foo"}])
|
|
|
|
...> |> RDF.Description.objects()
|
2017-08-20 20:35:14 +00:00
|
|
|
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode)])
|
2016-11-02 02:19:19 +00:00
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec objects(t) :: MapSet.t()
|
2020-10-05 12:32:18 +00:00
|
|
|
def objects(%__MODULE__{} = description) do
|
|
|
|
objects(description, &RDF.resource?/1)
|
|
|
|
end
|
2017-07-21 22:18:22 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
The set of all resources used in the objects within a `RDF.Description` satisfying the given filter criterion.
|
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec objects(t, (Statement.object() -> boolean)) :: MapSet.t()
|
2020-10-05 12:32:18 +00:00
|
|
|
def objects(%__MODULE__{} = description, filter_fn) do
|
|
|
|
Enum.reduce(description.predications, MapSet.new(), fn {_, objects}, acc ->
|
2016-11-02 02:19:19 +00:00
|
|
|
objects
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Map.keys()
|
2017-07-21 22:18:22 +00:00
|
|
|
|> Enum.filter(filter_fn)
|
2020-06-29 08:37:42 +00:00
|
|
|
|> MapSet.new()
|
2016-11-02 02:19:19 +00:00
|
|
|
|> MapSet.union(acc)
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2016-11-02 02:19:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
The set of all resources used within a `RDF.Description`.
|
|
|
|
|
2017-06-16 22:27:05 +00:00
|
|
|
## Examples
|
2016-11-02 02:19:19 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S1, init: [
|
2020-07-27 21:09:23 +00:00
|
|
|
...> {EX.p1, EX.O1},
|
|
|
|
...> {EX.p2, EX.O2},
|
|
|
|
...> {EX.p1, EX.O2},
|
|
|
|
...> {EX.p2, RDF.bnode(:bnode)},
|
2020-09-25 14:07:37 +00:00
|
|
|
...> {EX.p3, "foo"}])
|
|
|
|
...> |> RDF.Description.resources()
|
2017-08-20 20:35:14 +00:00
|
|
|
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode), EX.p1, EX.p2, EX.p3])
|
2016-11-02 02:19:19 +00:00
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec resources(t) :: MapSet.t()
|
2020-10-05 12:32:18 +00:00
|
|
|
def resources(%__MODULE__{} = description) do
|
2021-09-27 18:57:17 +00:00
|
|
|
objects(description)
|
2016-11-02 02:19:19 +00:00
|
|
|
|> MapSet.union(predicates(description))
|
|
|
|
end
|
|
|
|
|
2017-06-16 21:03:44 +00:00
|
|
|
@doc """
|
|
|
|
The list of all triples within a `RDF.Description`.
|
2021-09-27 18:57:17 +00:00
|
|
|
|
|
|
|
When the optional `:filter_star` flag is set to `true` RDF-star triples with a triple as subject or object
|
|
|
|
will be filtered. So, for a description with a triple as a subject you'll always get an empty list.
|
2021-11-15 21:01:22 +00:00
|
|
|
The default value of the `:filter_star` flag is `false`.
|
2017-06-16 21:03:44 +00:00
|
|
|
"""
|
2021-09-27 18:57:17 +00:00
|
|
|
@spec triples(t, keyword) :: list(Triple.t())
|
|
|
|
def triples(%__MODULE__{subject: s} = description, opts \\ []) do
|
|
|
|
filter_star = Keyword.get(opts, :filter_star, false)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
filter_star and is_tuple(s) ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
filter_star ->
|
|
|
|
for {p, os} <- description.predications, {o, _} when not is_tuple(o) <- os do
|
|
|
|
{s, p, o}
|
|
|
|
end
|
|
|
|
|
|
|
|
true ->
|
|
|
|
for {p, os} <- description.predications, {o, _} <- os do
|
|
|
|
{s, p, o}
|
|
|
|
end
|
|
|
|
end
|
2021-03-05 22:23:06 +00:00
|
|
|
end
|
2016-11-30 23:06:45 +00:00
|
|
|
|
2021-09-27 18:57:17 +00:00
|
|
|
defdelegate statements(description, opts \\ []), to: __MODULE__, as: :triples
|
2017-05-24 21:38:46 +00:00
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
@doc """
|
|
|
|
Returns the number of statements of a `RDF.Description`.
|
|
|
|
"""
|
2020-10-11 11:11:18 +00:00
|
|
|
@spec statement_count(t) :: non_neg_integer
|
|
|
|
def statement_count(%__MODULE__{} = description) do
|
2020-10-05 12:32:18 +00:00
|
|
|
Enum.reduce(description.predications, 0, fn {_, objects}, count ->
|
|
|
|
count + Enum.count(objects)
|
|
|
|
end)
|
2016-11-11 15:40:58 +00:00
|
|
|
end
|
|
|
|
|
2020-10-11 11:11:18 +00:00
|
|
|
defdelegate count(description), to: __MODULE__, as: :statement_count
|
|
|
|
|
2022-04-05 19:06:29 +00:00
|
|
|
@doc """
|
|
|
|
Returns if the given `description` is empty.
|
|
|
|
|
|
|
|
Note: You should always prefer this over the use of `Enum.empty?/1` as it is significantly faster.
|
|
|
|
"""
|
|
|
|
@spec empty?(t) :: boolean
|
|
|
|
def empty?(%__MODULE__{} = description) do
|
|
|
|
Enum.empty?(description.predications)
|
|
|
|
end
|
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
@doc """
|
2020-09-28 15:39:31 +00:00
|
|
|
Checks if the given `input` statements exist within `description`.
|
2016-10-15 16:26:56 +00:00
|
|
|
"""
|
2020-10-09 14:32:24 +00:00
|
|
|
@spec include?(t, input, keyword) :: boolean
|
|
|
|
def include?(description, input, opts \\ [])
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
def include?(%__MODULE__{} = description, {subject, predicate, objects}, opts) do
|
2021-09-27 18:57:17 +00:00
|
|
|
RDF.coerce_subject(subject) == description.subject &&
|
2020-10-09 14:32:24 +00:00
|
|
|
include?(description, {predicate, objects}, opts)
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
def include?(%__MODULE__{} = description, {subject, predicate, objects, _}, opts) do
|
|
|
|
include?(description, {subject, predicate, objects}, opts)
|
2020-10-04 23:28:24 +00:00
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
def include?(%__MODULE__{} = description, {predicate, objects}, opts) do
|
2020-10-13 09:38:22 +00:00
|
|
|
if existing_objects =
|
2021-09-27 18:57:17 +00:00
|
|
|
description.predications[RDF.coerce_predicate(predicate, PropertyMap.from_opts(opts))] do
|
2020-07-27 21:09:23 +00:00
|
|
|
objects
|
|
|
|
|> List.wrap()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Enum.map(&RDF.coerce_object/1)
|
2020-07-27 21:09:23 +00:00
|
|
|
|> Enum.all?(fn object -> Map.has_key?(existing_objects, object) end)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-10-05 09:13:28 +00:00
|
|
|
def include?(
|
|
|
|
%__MODULE__{subject: subject, predications: predications},
|
2020-10-09 14:32:24 +00:00
|
|
|
%__MODULE__{subject: subject} = input,
|
|
|
|
_opts
|
2020-10-05 09:13:28 +00:00
|
|
|
) do
|
|
|
|
Enum.all?(input.predications, fn {predicate, objects} ->
|
|
|
|
if existing_objects = predications[predicate] do
|
|
|
|
Enum.all?(objects, fn {object, _} ->
|
|
|
|
Map.has_key?(existing_objects, object)
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
def include?(%__MODULE__{}, %__MODULE__{}, _), do: false
|
2020-10-05 09:13:28 +00:00
|
|
|
|
2021-11-19 20:10:47 +00:00
|
|
|
def include?(description, input, opts)
|
|
|
|
when is_list(input) or (is_map(input) and not is_struct(input)) do
|
|
|
|
Enum.all?(input, &include?(description, &1, opts))
|
2020-10-05 09:13:28 +00:00
|
|
|
end
|
|
|
|
|
2017-07-23 21:59:50 +00:00
|
|
|
@doc """
|
|
|
|
Checks if a `RDF.Description` has the given resource as subject.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S1, init: {EX.p1, EX.O1})
|
|
|
|
...> |> RDF.Description.describes?(EX.S1)
|
2017-07-23 21:59:50 +00:00
|
|
|
true
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(EX.S1, init: {EX.p1, EX.O1})
|
|
|
|
...> |> RDF.Description.describes?(EX.S2)
|
2017-07-23 21:59:50 +00:00
|
|
|
false
|
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec describes?(t, Statement.subject()) :: boolean
|
2020-06-28 21:40:03 +00:00
|
|
|
def describes?(%__MODULE__{subject: subject}, other_subject) do
|
2021-09-27 18:57:17 +00:00
|
|
|
subject == RDF.coerce_subject(other_subject)
|
2017-07-23 21:59:50 +00:00
|
|
|
end
|
|
|
|
|
2018-10-27 14:30:19 +00:00
|
|
|
@doc """
|
|
|
|
Returns a map of the native Elixir values of a `RDF.Description`.
|
|
|
|
|
|
|
|
The subject is not part of the result. It can be converted separately with
|
2020-10-10 13:45:25 +00:00
|
|
|
`RDF.Term.value/1`, or, if you want the subject in an outer map, just put the
|
|
|
|
the description in a graph and use `RDF.Graph.values/2`.
|
2018-10-27 14:30:19 +00:00
|
|
|
|
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-11-04 21:27:25 +00:00
|
|
|
|
2021-09-27 18:57:17 +00:00
|
|
|
Note: RDF-star statements where the object is a triple will be ignored.
|
|
|
|
|
2018-10-27 14:30:19 +00:00
|
|
|
## Examples
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(~I<http://example.com/S>, init: {~I<http://example.com/p>, ~L"Foo"})
|
2018-10-27 14:30:19 +00:00
|
|
|
...> |> RDF.Description.values()
|
|
|
|
%{"http://example.com/p" => ["Foo"]}
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
iex> RDF.Description.new(~I<http://example.com/S>, init: {~I<http://example.com/p>, ~L"Foo"})
|
2020-10-13 09:38:22 +00:00
|
|
|
...> |> RDF.Description.values(context: %{p: ~I<http://example.com/p>})
|
2020-10-10 13:45:25 +00:00
|
|
|
%{p: ["Foo"]}
|
|
|
|
|
|
|
|
"""
|
2020-10-13 09:38:22 +00:00
|
|
|
@spec values(t, keyword) :: map
|
|
|
|
def values(%__MODULE__{} = description, opts \\ []) do
|
|
|
|
if property_map = PropertyMap.from_opts(opts) do
|
2021-09-27 18:57:17 +00:00
|
|
|
map(description, RDF.Statement.default_property_mapping(property_map))
|
2020-10-13 09:38:22 +00:00
|
|
|
else
|
2021-09-27 18:57:17 +00:00
|
|
|
map(description, &RDF.Statement.default_term_mapping/1)
|
2020-10-13 09:38:22 +00:00
|
|
|
end
|
2020-10-10 13:45:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns a map of a `RDF.Description` where each element from its triples is mapped with the given function.
|
|
|
|
|
|
|
|
The subject is not part of the result. If you want the subject in an outer map,
|
|
|
|
just put the the description in a graph and use `RDF.Graph.map/2`.
|
|
|
|
|
|
|
|
The function `fun` will receive a tuple `{statement_position, rdf_term}` where
|
|
|
|
`statement_position` is one of the atoms `:predicate` or `:object`, 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.
|
|
|
|
|
2021-09-27 18:57:17 +00:00
|
|
|
Note: RDF-star statements where the object is a triple will be ignored.
|
|
|
|
|
2020-10-10 13:45:25 +00:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> RDF.Description.new(~I<http://example.com/S>, init: {~I<http://example.com/p>, ~L"Foo"})
|
|
|
|
...> |> RDF.Description.map(fn
|
2018-11-04 21:27:25 +00:00
|
|
|
...> {:predicate, predicate} ->
|
|
|
|
...> predicate
|
|
|
|
...> |> to_string()
|
|
|
|
...> |> String.split("/")
|
|
|
|
...> |> List.last()
|
|
|
|
...> |> String.to_atom()
|
|
|
|
...> {_, term} ->
|
|
|
|
...> RDF.Term.value(term)
|
|
|
|
...> end)
|
|
|
|
%{p: ["Foo"]}
|
|
|
|
|
2018-10-27 14:30:19 +00:00
|
|
|
"""
|
2020-10-10 13:45:25 +00:00
|
|
|
@spec map(t, Statement.term_mapping()) :: map
|
|
|
|
def map(description, fun)
|
2018-11-04 21:27:25 +00:00
|
|
|
|
2020-10-10 13:45:25 +00:00
|
|
|
def map(%__MODULE__{} = description, fun) do
|
2021-09-27 18:57:17 +00:00
|
|
|
Enum.reduce(description.predications, %{}, fn {predicate, objects}, map ->
|
|
|
|
objects
|
|
|
|
|> Map.keys()
|
|
|
|
|> Enum.reject(&is_tuple/1)
|
|
|
|
|> case do
|
|
|
|
[] ->
|
|
|
|
map
|
|
|
|
|
|
|
|
objects ->
|
|
|
|
Map.put(
|
|
|
|
map,
|
|
|
|
fun.({:predicate, predicate}),
|
|
|
|
Enum.map(objects, &fun.({:object, &1}))
|
|
|
|
)
|
|
|
|
end
|
2020-06-29 08:37:42 +00:00
|
|
|
end)
|
2018-10-27 14:30:19 +00:00
|
|
|
end
|
2017-07-23 21:59:50 +00:00
|
|
|
|
2019-10-14 07:13:56 +00:00
|
|
|
@doc """
|
|
|
|
Creates a description from another one by limiting its statements to those using one of the given `predicates`.
|
|
|
|
|
|
|
|
If `predicates` contains properties that are not used in the `description`, they're simply ignored.
|
2019-10-15 15:29:46 +00:00
|
|
|
|
|
|
|
If `nil` is passed, the description is left untouched.
|
2019-10-14 07:13:56 +00:00
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec take(t, [Statement.coercible_predicate()] | Enum.t() | nil) :: t
|
2019-10-15 15:29:46 +00:00
|
|
|
def take(description, predicates)
|
|
|
|
|
2020-06-28 21:40:03 +00:00
|
|
|
def take(%__MODULE__{} = description, nil), do: description
|
2019-10-15 15:29:46 +00:00
|
|
|
|
2020-10-05 12:32:18 +00:00
|
|
|
def take(%__MODULE__{} = description, predicates) do
|
|
|
|
%__MODULE__{
|
|
|
|
description
|
|
|
|
| predications:
|
2021-09-27 18:57:17 +00:00
|
|
|
Map.take(description.predications, Enum.map(predicates, &RDF.coerce_predicate/1))
|
2020-10-05 12:32:18 +00:00
|
|
|
}
|
2019-10-14 07:13:56 +00:00
|
|
|
end
|
2018-11-04 21:27:25 +00:00
|
|
|
|
2021-11-14 13:30:30 +00:00
|
|
|
@doc """
|
|
|
|
Removes all objects from a description which are quoted triples.
|
|
|
|
"""
|
2021-11-20 20:40:35 +00:00
|
|
|
@spec without_quoted_triple_objects(t) :: t
|
|
|
|
def without_quoted_triple_objects(%__MODULE__{} = description) do
|
2021-11-14 13:30:30 +00:00
|
|
|
%__MODULE__{
|
|
|
|
description
|
|
|
|
| predications:
|
|
|
|
Enum.reduce(description.predications, description.predications, fn
|
|
|
|
{predicate, objects}, predications ->
|
|
|
|
original_object_count = Enum.count(predications)
|
|
|
|
|
|
|
|
filtered_objects =
|
|
|
|
Enum.reject(objects, &match?({quoted_triple, _} when is_tuple(quoted_triple), &1))
|
|
|
|
|
|
|
|
case Enum.count(filtered_objects) do
|
|
|
|
0 -> Map.delete(predications, predicate)
|
|
|
|
^original_object_count -> predications
|
|
|
|
_ -> Map.put(predications, predicate, Map.new(filtered_objects))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-04-02 22:12:42 +00:00
|
|
|
@doc """
|
|
|
|
Checks if two `RDF.Description`s are equal.
|
|
|
|
|
|
|
|
Two `RDF.Description`s are considered to be equal if they contain the same triples.
|
|
|
|
"""
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec equal?(t, t) :: boolean
|
2019-04-02 22:12:42 +00:00
|
|
|
def equal?(description1, description2)
|
|
|
|
|
2020-06-28 21:40:03 +00:00
|
|
|
def equal?(%__MODULE__{} = description1, %__MODULE__{} = description2) do
|
2019-04-02 22:12:42 +00:00
|
|
|
description1 == description2
|
|
|
|
end
|
|
|
|
|
|
|
|
def equal?(_, _), do: false
|
|
|
|
|
2017-06-16 21:03:44 +00:00
|
|
|
defimpl Enumerable do
|
2020-06-28 21:40:03 +00:00
|
|
|
alias RDF.Description
|
|
|
|
|
|
|
|
def member?(desc, triple), do: {:ok, Description.include?(desc, triple)}
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2021-03-05 22:23:06 +00:00
|
|
|
def count(desc), do: {:ok, Description.statement_count(desc)}
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2021-11-19 20:10:47 +00:00
|
|
|
def slice(desc) do
|
|
|
|
size = Description.statement_count(desc)
|
|
|
|
{:ok, size, &Enumerable.List.slice(Description.triples(desc), &1, &2, size)}
|
2017-06-16 21:03:44 +00:00
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2021-03-05 22:23:06 +00:00
|
|
|
def reduce(desc, acc, fun) do
|
|
|
|
desc
|
|
|
|
|> Description.triples()
|
|
|
|
|> Enumerable.List.reduce(acc, fun)
|
2017-06-16 21:03:44 +00:00
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2017-10-30 10:40:44 +00:00
|
|
|
defimpl Collectable do
|
2020-06-28 21:40:03 +00:00
|
|
|
alias RDF.Description
|
|
|
|
|
2017-10-30 10:40:44 +00:00
|
|
|
def into(original) do
|
|
|
|
collector_fun = fn
|
2020-06-29 08:37:42 +00:00
|
|
|
description, {:cont, list} when is_list(list) ->
|
|
|
|
Description.add(description, List.to_tuple(list))
|
|
|
|
|
|
|
|
description, {:cont, elem} ->
|
|
|
|
Description.add(description, elem)
|
|
|
|
|
|
|
|
description, :done ->
|
|
|
|
description
|
|
|
|
|
|
|
|
_description, :halt ->
|
|
|
|
:ok
|
2017-10-30 10:40:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
{original, collector_fun}
|
|
|
|
end
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|