Add values/1 on RDF.Description, RDF.Graph and RDF.Dataset

This commit is contained in:
Marcel Otto 2018-10-27 16:30:19 +02:00
parent 9e06cea7ca
commit defd1857e5
7 changed files with 110 additions and 0 deletions

View file

@ -13,6 +13,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- `RDF.Statement.values/1`, `RDF.Triple.values/1` and `RDF.Quad.values/1`
returning a tuple of `RDF.Term.value/1` converted native Elixir values from a
tuple of RDF terms
- `RDF.Description.values/1`, `RDF.Graph.values/1` and `RDF.Dataset.values/1`
returning a map of `RDF.Term.value/1` converted native Elixir values from the
respective structure of RDF terms
### Fixed

View file

@ -728,6 +728,34 @@ defmodule RDF.Dataset do
end
@doc """
Returns a nested map of the native Elixir values of a `RDF.Dataset`.
## Examples
iex> [
...> {~I<http://example.com/S>, ~I<http://example.com/p>, ~L"Foo", ~I<http://example.com/Graph>},
...> {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.integer(42), }
...> ]
...> |> RDF.Dataset.new()
...> |> RDF.Dataset.values()
%{
"http://example.com/Graph" => %{
"http://example.com/S" => %{"http://example.com/p" => ["Foo"]}
},
nil => %{
"http://example.com/S" => %{"http://example.com/p" => [42]}
}
}
"""
def values(%RDF.Dataset{graphs: graphs}) do
Map.new graphs, fn {graph_name, graph} ->
{RDF.Term.value(graph_name), Graph.values(graph)}
end
end
defimpl Enumerable do
def member?(dataset, statement), do: {:ok, RDF.Dataset.include?(dataset, statement)}
def count(dataset), do: {:ok, RDF.Dataset.statement_count(dataset)}

View file

@ -572,6 +572,28 @@ defmodule RDF.Description do
end
end
@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
`RDF.Term.value/1`.
## Examples
iex> {~I<http://example.com/S>, ~I<http://example.com/p>, ~L"Foo"}
...> |> RDF.Description.new()
...> |> RDF.Description.values()
%{"http://example.com/p" => ["Foo"]}
"""
def values(%RDF.Description{subject: subject, predications: predications}) do
Map.new predications, fn {predicate, objects} ->
{
RDF.Term.value(predicate),
objects |> Map.keys() |> Enum.map(&RDF.Term.value/1)
}
end
end
defimpl Enumerable do
def member?(desc, triple), do: {:ok, RDF.Description.include?(desc, triple)}

View file

@ -602,6 +602,30 @@ defmodule RDF.Graph do
end
@doc """
Returns a nested map of the native Elixir values of a `RDF.Graph`.
## Examples
iex> [
...> {~I<http://example.com/S1>, ~I<http://example.com/p>, ~L"Foo"},
...> {~I<http://example.com/S2>, ~I<http://example.com/p>, RDF.integer(42)}
...> ]
...> |> RDF.Graph.new()
...> |> RDF.Graph.values()
%{
"http://example.com/S1" => %{"http://example.com/p" => ["Foo"]},
"http://example.com/S2" => %{"http://example.com/p" => [42]}
}
"""
def values(%RDF.Graph{descriptions: descriptions}) do
Map.new descriptions, fn {subject, description} ->
{RDF.Term.value(subject), Description.values(description)}
end
end
defimpl Enumerable do
def member?(graph, triple), do: {:ok, RDF.Graph.include?(graph, triple)}
def count(graph), do: {:ok, RDF.Graph.triple_count(graph)}

View file

@ -701,6 +701,21 @@ defmodule RDF.DatasetTest do
end
test "values/1" do
assert Dataset.new() |> Dataset.values() == %{}
assert Dataset.new([{EX.s1, EX.p, EX.o1}, {EX.s2, EX.p, EX.o2, EX.graph}])
|> Dataset.values() ==
%{
nil => %{
RDF.Term.value(EX.s1) => %{RDF.Term.value(EX.p) => [RDF.Term.value(EX.o1)]}
},
RDF.Term.value(EX.graph) => %{
RDF.Term.value(EX.s2) => %{RDF.Term.value(EX.p) => [RDF.Term.value(EX.o2)]},
}
}
end
describe "Enumerable protocol" do
test "Enum.count" do
assert Enum.count(Dataset.new EX.foo) == 0

View file

@ -339,6 +339,12 @@ defmodule RDF.DescriptionTest do
assert Enum.count(desc.predications) == 1
end
test "values/1" do
assert Description.new(EX.s) |> Description.values() == %{}
assert Description.new({EX.s, EX.p, ~L"Foo"}) |> Description.values() ==
%{RDF.Term.value(EX.p) => ["Foo"]}
end
describe "Enumerable protocol" do
test "Enum.count" do
assert Enum.count(Description.new EX.foo) == 0

View file

@ -363,6 +363,18 @@ defmodule RDF.GraphTest do
assert Enum.count(graph.descriptions) == 1
end
test "values/1" do
assert Graph.new() |> Graph.values() == %{}
assert Graph.new([{EX.s1, EX.p, EX.o1}, {EX.s2, EX.p, EX.o2}])
|> Graph.values() ==
%{
RDF.Term.value(EX.s1) => %{RDF.Term.value(EX.p) => [RDF.Term.value(EX.o1)]},
RDF.Term.value(EX.s2) => %{RDF.Term.value(EX.p) => [RDF.Term.value(EX.o2)]},
}
end
describe "Enumerable protocol" do
test "Enum.count" do
assert Enum.count(Graph.new EX.foo) == 0