2016-10-15 16:26:56 +00:00
|
|
|
defmodule RDF.BlankNode do
|
|
|
|
@moduledoc """
|
2021-01-13 15:55:24 +00:00
|
|
|
An 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-07-08 20:02:58 +00:00
|
|
|
value: String.t()
|
2020-06-29 08:37:42 +00:00
|
|
|
}
|
2020-02-28 17:51:48 +00:00
|
|
|
|
2020-07-08 20:02:58 +00:00
|
|
|
@enforce_keys [:value]
|
|
|
|
defstruct [:value]
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-06-16 11:13:56 +00:00
|
|
|
@doc """
|
2020-07-08 20:02:58 +00:00
|
|
|
Creates a `RDF.BlankNode`.
|
2017-06-16 11:13:56 +00:00
|
|
|
"""
|
2020-03-02 01:07:31 +00:00
|
|
|
@spec new :: t
|
2020-11-01 12:48:17 +00:00
|
|
|
def new, do: new(:erlang.unique_integer([:positive]))
|
2017-06-16 11:13:56 +00:00
|
|
|
|
|
|
|
@doc """
|
2020-07-08 20:02:58 +00:00
|
|
|
Creates a `RDF.BlankNode` with a user-defined value for its identity.
|
2017-06-16 11:13:56 +00:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> RDF.bnode(:foo)
|
2020-07-08 20:02:58 +00:00
|
|
|
%RDF.BlankNode{value: "foo"}
|
2017-06-16 11:13:56 +00:00
|
|
|
"""
|
2020-06-29 08:37:42 +00:00
|
|
|
@spec new(reference | String.t() | atom | integer) :: t
|
2020-07-08 20:02:58 +00:00
|
|
|
def new(value)
|
2017-06-16 11:13:56 +00:00
|
|
|
|
2020-11-01 03:03:21 +00:00
|
|
|
def new(string) when is_binary(string), do: %__MODULE__{value: string}
|
|
|
|
def new(atom) when is_atom(atom), do: atom |> to_string() |> new()
|
|
|
|
def new(integer) when is_integer(integer), do: new("b#{integer}")
|
2017-06-16 11:13:56 +00:00
|
|
|
|
2020-11-01 03:03:21 +00:00
|
|
|
def new(ref) when is_reference(ref) do
|
|
|
|
"#Ref<" <> value = ref |> :erlang.ref_to_list() |> to_string()
|
|
|
|
value |> String.trim_trailing(">") |> new()
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-11-01 01:26:18 +00:00
|
|
|
@doc """
|
|
|
|
Returns the internal string representation of a blank node.
|
|
|
|
"""
|
|
|
|
def value(%__MODULE__{} = bnode), do: bnode.value
|
|
|
|
|
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)
|
|
|
|
|
2020-07-08 20:02:58 +00:00
|
|
|
def equal_value?(%__MODULE__{value: left}, %__MODULE__{value: right}),
|
2018-06-08 10:26:52 +00:00
|
|
|
do: left == right
|
|
|
|
|
|
|
|
def equal_value?(_, _),
|
|
|
|
do: nil
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defimpl String.Chars do
|
2020-07-08 20:02:58 +00:00
|
|
|
def to_string(bnode), do: "_:#{bnode.value}"
|
2017-05-19 15:17:41 +00:00
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|