diff --git a/lib/rdf/description.ex b/lib/rdf/description.ex index 55e3dee..ee7f161 100644 --- a/lib/rdf/description.ex +++ b/lib/rdf/description.ex @@ -405,6 +405,8 @@ defmodule RDF.Description do def triples(description = %RDF.Description{}), do: Enum.to_list(description) + defdelegate statements(description), to: RDF.Description, as: :triples + @doc """ Returns the number of statements of a `RDF.Description`. @@ -477,3 +479,24 @@ defimpl Enumerable, for: RDF.Description do def member?(desc, triple), do: {:ok, RDF.Description.include?(desc, triple)} def count(desc), do: {:ok, RDF.Description.count(desc)} end + +defimpl RDF.Data, for: RDF.Description do + def add(description, statements), do: RDF.Description.add(description, statements) + def put(description, statements), do: RDF.Description.put(description, statements) + def delete(description, statements), do: RDF.Description.delete(description, statements) + def pop(description), do: RDF.Description.pop(description) + + def include?(description, statements), + do: RDF.Description.include?(description, statements) + + def statements(description), do: RDF.Description.statements(description) + def predicates(description), do: RDF.Description.predicates(description) + def objects(description), do: RDF.Description.objects(description) + def subjects(%RDF.Description{subject: subject}), do: MapSet.new([subject]) + + def resources(%RDF.Description{subject: subject} = description), + do: RDF.Description.resources(description) |> MapSet.put(subject) + + def subject_count(_), do: 1 + def statement_count(description), do: RDF.Description.count(description) +end diff --git a/test/unit/description_test.exs b/test/unit/description_test.exs index 792cb83..d890f3c 100644 --- a/test/unit/description_test.exs +++ b/test/unit/description_test.exs @@ -336,4 +336,77 @@ defmodule RDF.DescriptionTest do end + describe "RDF.Data protocol implementation" do + setup do + {:ok, + description: Description.new(EX.S, [ + {EX.p1, [EX.O1, EX.O2]}, + {EX.p2, EX.O3}, + {EX.p3, [~B, ~L"bar"]}, + ]) + } + end + + test "add", %{description: description} do + assert RDF.Data.add(description, {EX.S, EX.p1, EX.O4}) == + Description.add(description, {EX.S, EX.p1, EX.O4}) + + assert RDF.Data.add(description, {EX.Other, EX.p2, EX.O4}) == description + end + + test "put", %{description: description} do + assert RDF.Data.put(description, {EX.S, EX.p1, EX.O4}) == + Description.put(description, {EX.S, EX.p1, EX.O4}) + + assert RDF.Data.put(description, {EX.Other, EX.p2, EX.O4}) == description + end + + test "delete", %{description: description} do + assert RDF.Data.delete(description, {EX.S, EX.p1, EX.O2}) == + Description.delete(description, {EX.S, EX.p1, EX.O2}) + + assert RDF.Data.delete(description, {EX.Other, EX.p1, EX.O2}) == description + end + + test "pop", %{description: description} do + assert RDF.Data.pop(description) == Description.pop(description) + end + + test "include?", %{description: description} do + assert RDF.Data.include?(description, {EX.S, EX.p1, EX.O2}) + refute RDF.Data.include?(description, {EX.Other, EX.p1, EX.O2}) + end + + test "statements", %{description: description} do + assert RDF.Data.statements(description) == Description.statements(description) + end + + test "subjects", %{description: description} do + assert RDF.Data.subjects(description) == MapSet.new([uri(EX.S)]) + end + + test "predicates", %{description: description} do + assert RDF.Data.predicates(description) == MapSet.new([EX.p1, EX.p2, EX.p3]) + end + + test "objects", %{description: description} do + assert RDF.Data.objects(description) == + MapSet.new([uri(EX.O1), uri(EX.O2), uri(EX.O3), ~B]) + end + + test "resources", %{description: description} do + assert RDF.Data.resources(description) == + MapSet.new([uri(EX.S), EX.p1, EX.p2, EX.p3, uri(EX.O1), uri(EX.O2), uri(EX.O3), ~B]) + end + + test "subject_count", %{description: description} do + assert RDF.Data.subject_count(description) == 1 + end + + test "statement_count", %{description: description} do + assert RDF.Data.statement_count(description) == 5 + end + + end + end