Change RDF.Graph.description/2 fallback value

This commit is contained in:
Marcel Otto 2022-04-09 01:13:23 +02:00
parent 8ef6de926e
commit d056d33b21
5 changed files with 41 additions and 15 deletions

View file

@ -23,6 +23,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Changed
- `RDF.Graph.description/2` is no longer an alias for `RDF.Graph.get/2`, but
has a different behaviour now: it will return an empty description when no
description for the requested subject exists in the graph
- The inspect string of `RDF.Description` now includes the subject separately, so
it can be seen also when the description is empty.

View file

@ -1 +1 @@
0.11.1-pre
0.12.0-pre

View file

@ -78,7 +78,7 @@ defprotocol RDF.Data do
def predicates(data)
@doc """
Returns the set of all resources used in the objects within the statements of a RDF data structure.
Returns the set of all resources used in the objects within the statements of a RDF data structure.
"""
def objects(data)
@ -189,10 +189,10 @@ defimpl RDF.Data, for: RDF.Description do
do: Description.describes?(description, subject)
def description(%Description{subject: subject} = description, s) do
with ^subject <- Statement.coerce_subject(s) do
if match?(^subject, Statement.coerce_subject(s)) do
description
else
_ -> Description.new(s)
Description.new(s)
end
end
@ -286,11 +286,9 @@ defimpl RDF.Data, for: RDF.Graph do
def include?(graph, input, opts \\ []), do: Graph.include?(graph, input, opts)
def describes?(graph, subject),
do: Graph.describes?(graph, subject)
def describes?(graph, subject), do: Graph.describes?(graph, subject)
def description(graph, subject),
do: Graph.description(graph, subject) || Description.new(subject)
def description(graph, subject), do: Graph.description(graph, subject)
def descriptions(graph), do: Graph.descriptions(graph)

View file

@ -620,22 +620,25 @@ defmodule RDF.Graph do
end
@doc """
Gets the description of the given subject.
Gets the description of the given `subject` in the given `graph`.
When the subject can not be found the optionally given default value or `nil` is returned.
When the subject can not be found the optionally given default value or
`nil` is returned.
## Examples
iex> RDF.Graph.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}])
...> |> RDF.Graph.get(EX.S1)
RDF.Description.new(EX.S1, init: {EX.P1, EX.O1})
iex> RDF.Graph.new() |> RDF.Graph.get(EX.Foo)
iex> RDF.Graph.get(RDF.Graph.new(), EX.Foo)
nil
iex> RDF.Graph.new() |> RDF.Graph.get(EX.Foo, :bar)
iex> RDF.Graph.get(RDF.Graph.new(), EX.Foo, :bar)
:bar
"""
@spec get(t, Statement.coercible_subject(), Description.t() | nil) :: Description.t() | nil
@spec get(t, Statement.coercible_subject(), any) :: Description.t() | any
def get(%__MODULE__{} = graph, subject, default \\ nil) do
case fetch(graph, subject) do
{:ok, value} -> value
@ -643,7 +646,29 @@ defmodule RDF.Graph do
end
end
defdelegate description(graph, subject), to: __MODULE__, as: :get
@doc """
Returns the description of the given `subject` in the given `graph`.
As opposed to `get/3` this function returns an empty `RDF.Description` when
the subject does not exist in the given `graph`.
## Examples
iex> RDF.Graph.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}])
...> |> RDF.Graph.description(EX.S1)
RDF.Description.new(EX.S1, init: {EX.P1, EX.O1})
iex> RDF.Graph.description(RDF.Graph.new(), EX.Foo)
RDF.Description.new(EX.Foo)
"""
@spec description(t, Statement.coercible_subject()) :: Description.t()
def description(%__MODULE__{} = graph, subject) do
case fetch(graph, subject) do
{:ok, value} -> value
:error -> Description.new(subject)
end
end
@doc """
All `RDF.Description`s within a `RDF.Graph`.

View file

@ -209,7 +209,7 @@ defmodule RDF.List do
defp do_reduce(%RDF.List{head: head, graph: graph}, {:cont, acc}, fun) do
with description when not is_nil(description) <-
Graph.description(graph, head),
Graph.get(graph, head),
[_] <- Description.get(description, RDF.first()),
[rest] <- Description.get(description, RDF.rest()),
acc = fun.(description, acc) do