core: RDF.Data protocol implementation for RDF.Graph

This commit is contained in:
Marcel Otto 2017-06-04 04:51:06 +02:00
parent 70e78ee326
commit 371020360f
3 changed files with 100 additions and 12 deletions

View file

@ -77,3 +77,23 @@ defimpl RDF.Data, for: RDF.Description do
def subject_count(_), do: 1
def statement_count(description), do: RDF.Description.count(description)
end
defimpl RDF.Data, for: RDF.Graph do
def delete(%RDF.Graph{name: name} = graph,
%RDF.Graph{name: other_name})
when name != other_name,
do: graph
def delete(graph, statements), do: RDF.Graph.delete(graph, statements)
def pop(graph), do: RDF.Graph.pop(graph)
def include?(graph, statements), do: RDF.Graph.include?(graph, statements)
def statements(graph), do: RDF.Graph.statements(graph)
def subjects(graph), do: RDF.Graph.subjects(graph)
def predicates(graph), do: RDF.Graph.predicates(graph)
def objects(graph), do: RDF.Graph.objects(graph)
def resources(graph), do: RDF.Graph.resources(graph)
def subject_count(graph), do: RDF.Graph.subject_count(graph)
def statement_count(graph), do: RDF.Graph.triple_count(graph)
end

View file

@ -523,6 +523,9 @@ defmodule RDF.Graph do
"""
def triples(graph = %RDF.Graph{}), do: Enum.to_list(graph)
defdelegate statements(graph), to: RDF.Graph, as: :triples
def include?(%RDF.Graph{descriptions: descriptions},
triple = {subject, _, _}) do
with subject = convert_subject(subject),

View file

@ -1,21 +1,30 @@
defmodule RDF.DataTest do
use RDF.Test.Case
describe "RDF.Data protocol implementation of RDF.Description" do
setup do
{:ok,
description: Description.new(EX.S, [
{EX.p1, [EX.O1, EX.O2]},
{EX.p2, EX.O3},
{EX.p3, [~B<foo>, ~L"bar"]},
])
}
end
setup do
description =
EX.S
|> EX.p1(EX.O1, EX.O2)
|> EX.p2(EX.O3)
|> EX.p3(~B<foo>, ~L"bar")
graph =
Graph.new
|> Graph.add(description)
|> Graph.add(
EX.S2
|> EX.p2(EX.O3, EX.O4)
)
{:ok,
description: description,
graph: graph
}
end
describe "RDF.Data protocol implementation of RDF.Description" do
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})
Description.delete(description, {EX.S, EX.p1, EX.O2})
assert RDF.Data.delete(description, {EX.Other, EX.p1, EX.O2}) == description
end
@ -64,4 +73,60 @@ defmodule RDF.DataTest do
end
end
describe "RDF.Data protocol implementation of RDF.Graph" do
test "delete", %{graph: graph} do
assert RDF.Data.delete(graph, {EX.S, EX.p1, EX.O2}) ==
Graph.delete(graph, {EX.S, EX.p1, EX.O2})
assert RDF.Data.delete(graph, {EX.Other, EX.p1, EX.O2}) == graph
end
test "deleting a Graph with a different name does nothing", %{graph: graph} do
assert RDF.Data.delete(graph,
%Graph{graph | name: EX.OtherGraph}) == graph
end
test "pop", %{graph: graph} do
assert RDF.Data.pop(graph) == Graph.pop(graph)
end
test "include?", %{graph: graph} do
assert RDF.Data.include?(graph, {EX.S, EX.p1, EX.O2})
assert RDF.Data.include?(graph, {EX.S2, EX.p2, EX.O3})
refute RDF.Data.include?(graph, {EX.Other, EX.p1, EX.O2})
end
test "statements", %{graph: graph} do
assert RDF.Data.statements(graph) == Graph.statements(graph)
end
test "subjects", %{graph: graph} do
assert RDF.Data.subjects(graph) == MapSet.new([uri(EX.S), uri(EX.S2)])
end
test "predicates", %{graph: graph} do
assert RDF.Data.predicates(graph) == MapSet.new([EX.p1, EX.p2, EX.p3])
end
test "objects", %{graph: graph} do
assert RDF.Data.objects(graph) ==
MapSet.new([uri(EX.O1), uri(EX.O2), uri(EX.O3), uri(EX.O4), ~B<foo>])
end
test "resources", %{graph: graph} do
assert RDF.Data.resources(graph) == MapSet.new([
uri(EX.S), uri(EX.S2), EX.p1, EX.p2, EX.p3,
uri(EX.O1), uri(EX.O2), uri(EX.O3), uri(EX.O4), ~B<foo>
])
end
test "subject_count", %{graph: graph} do
assert RDF.Data.subject_count(graph) == 2
end
test "statement_count", %{graph: graph} do
assert RDF.Data.statement_count(graph) == 7
end
end
end