2016-10-15 16:26:56 +00:00
|
|
|
defmodule RDF.BlankNode do
|
|
|
|
@moduledoc """
|
2017-08-20 20:35:14 +00:00
|
|
|
A RDF blank node (aka bnode) is a local node of a graph without an IRI.
|
2017-05-19 15:16:43 +00:00
|
|
|
|
|
|
|
see <https://www.w3.org/TR/rdf11-primer/#section-blank-node>
|
|
|
|
and <https://www.w3.org/TR/rdf11-concepts/#section-blank-nodes>
|
2016-10-15 16:26:56 +00:00
|
|
|
"""
|
|
|
|
|
2020-02-28 17:51:48 +00:00
|
|
|
@type t :: %__MODULE__{
|
2020-03-02 01:07:31 +00:00
|
|
|
id: String.t
|
2020-02-28 17:51:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 22:27:40 +00:00
|
|
|
@enforce_keys [:id]
|
2016-10-15 16:26:56 +00:00
|
|
|
defstruct [:id]
|
|
|
|
|
2017-06-16 11:13:56 +00:00
|
|
|
@doc """
|
2017-08-20 20:35:14 +00:00
|
|
|
Creates a `RDF.BlankNode` with an arbitrary internal id.
|
2017-06-16 11:13:56 +00:00
|
|
|
"""
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec new :: t
|
2017-05-19 15:16:43 +00:00
|
|
|
def new,
|
|
|
|
do: new(make_ref())
|
2017-06-16 11:13:56 +00:00
|
|
|
|
|
|
|
@doc """
|
2017-08-20 20:35:14 +00:00
|
|
|
Creates a `RDF.BlankNode` with a user-defined identity.
|
2017-06-16 11:13:56 +00:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> RDF.bnode(:foo)
|
|
|
|
%RDF.BlankNode{id: "foo"}
|
|
|
|
"""
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec new(reference | String.t | atom | integer) :: t
|
2017-06-16 11:13:56 +00:00
|
|
|
def new(id)
|
|
|
|
|
2017-05-19 15:16:43 +00:00
|
|
|
def new(id) when is_binary(id),
|
2016-10-30 18:36:46 +00:00
|
|
|
do: %RDF.BlankNode{id: id}
|
2017-06-16 11:13:56 +00:00
|
|
|
|
2017-05-19 15:16:43 +00:00
|
|
|
def new(id) when is_reference(id),
|
|
|
|
do: id |> :erlang.ref_to_list |> to_string |> String.replace(~r/\<|\>/, "") |> new
|
2017-06-16 11:13:56 +00:00
|
|
|
|
2017-05-19 15:16:43 +00:00
|
|
|
def new(id) when is_atom(id) or is_integer(id),
|
|
|
|
do: id |> to_string |> new
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
|
2018-06-08 10:26:52 +00:00
|
|
|
@doc """
|
|
|
|
Tests for value equality of blank nodes.
|
|
|
|
|
|
|
|
Returns `nil` when the given arguments are not comparable as blank nodes.
|
|
|
|
"""
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec equal_value?(t, t) :: boolean | nil
|
2018-06-08 10:26:52 +00:00
|
|
|
def equal_value?(left, right)
|
|
|
|
|
|
|
|
def equal_value?(%RDF.BlankNode{id: left}, %RDF.BlankNode{id: right}),
|
|
|
|
do: left == right
|
|
|
|
|
|
|
|
def equal_value?(_, _),
|
|
|
|
do: nil
|
|
|
|
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defimpl String.Chars do
|
|
|
|
def to_string(%RDF.BlankNode{id: id}), do: "_:#{id}"
|
2017-05-19 15:17:41 +00:00
|
|
|
end
|
2017-08-20 20:35:14 +00:00
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|