core: RDF.Data protocol implementation for RDF.Dataset

This commit is contained in:
Marcel Otto 2017-06-04 22:19:21 +02:00
parent 371020360f
commit 0271181bca
3 changed files with 93 additions and 11 deletions

View file

@ -58,8 +58,7 @@ end
defimpl RDF.Data, for: RDF.Description do
def delete(%RDF.Description{subject: subject} = description,
%RDF.Description{subject: other_subject})
when subject != other_subject,
do: description
when subject != other_subject, do: description
def delete(description, statements), do: RDF.Description.delete(description, statements)
def pop(description), do: RDF.Description.pop(description)
@ -79,10 +78,8 @@ defimpl RDF.Data, for: RDF.Description do
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(%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)
@ -97,3 +94,21 @@ defimpl RDF.Data, for: RDF.Graph do
def subject_count(graph), do: RDF.Graph.subject_count(graph)
def statement_count(graph), do: RDF.Graph.triple_count(graph)
end
defimpl RDF.Data, for: RDF.Dataset do
def delete(%RDF.Dataset{name: name} = dataset, %RDF.Dataset{name: other_name})
when name != other_name, do: dataset
def delete(dataset, statements), do: RDF.Dataset.delete(dataset, statements)
def pop(dataset), do: RDF.Dataset.pop(dataset)
def include?(dataset, statements), do: RDF.Dataset.include?(dataset, statements)
def statements(dataset), do: RDF.Dataset.statements(dataset)
def subjects(dataset), do: RDF.Dataset.subjects(dataset)
def predicates(dataset), do: RDF.Dataset.predicates(dataset)
def objects(dataset), do: RDF.Dataset.objects(dataset)
def resources(dataset), do: RDF.Dataset.resources(dataset)
def subject_count(dataset), do: dataset |> subjects |> Enum.count
def statement_count(dataset), do: RDF.Dataset.statement_count(dataset)
end

View file

@ -259,7 +259,7 @@ defmodule RDF.Dataset do
Deletes statements from a `RDF.Dataset`.
The optional third argument `graph_context` defaulting to `nil` for the default
graph, specifies the graph to which the statements are added.
graph, specifies the graph from which the statements are deleted.
Note that this also applies when deleting a named graph. Its name is ignored over
`graph_context` and its default value.
@ -612,9 +612,8 @@ defmodule RDF.Dataset do
end
end
def include?(%RDF.Dataset{} = description,
{subject, predicate, object, graph_context}, _),
do: include?(description, {subject, predicate, object}, graph_context)
def include?(%RDF.Dataset{} = dataset, {subject, predicate, object, graph_context}, _),
do: include?(dataset, {subject, predicate, object}, graph_context)
# TODO: Can/should we isolate and move the Enumerable specific part to the Enumerable implementation?

View file

@ -14,9 +14,18 @@ defmodule RDF.DataTest do
EX.S2
|> EX.p2(EX.O3, EX.O4)
)
dataset =
Dataset.new
|> Dataset.add(graph)
|> Dataset.add((
Graph.new(EX.NamedGraph)
|> Graph.add(description)
|> Graph.add({EX.S3, EX.p3, EX.O5})), EX.NamedGraph
)
{:ok,
description: description,
graph: graph
graph: graph,
dataset: dataset,
}
end
@ -129,4 +138,63 @@ defmodule RDF.DataTest do
end
end
describe "RDF.Data protocol implementation of RDF.Dataset" do
test "delete", %{dataset: dataset} do
assert RDF.Data.delete(dataset, {EX.S, EX.p1, EX.O2}) ==
Dataset.delete(dataset, {EX.S, EX.p1, EX.O2})
assert RDF.Data.delete(dataset, {EX.S3, EX.p3, EX.O5, EX.NamedGraph}) ==
Dataset.delete(dataset, {EX.S3, EX.p3, EX.O5, EX.NamedGraph})
assert RDF.Data.delete(dataset, {EX.Other, EX.p1, EX.O2}) == dataset
end
test "deleting a Dataset with a different name does nothing", %{dataset: dataset} do
assert RDF.Data.delete(dataset,
%Dataset{dataset | name: EX.OtherDataset}) == dataset
end
test "pop", %{dataset: dataset} do
assert RDF.Data.pop(dataset) == Dataset.pop(dataset)
end
test "include?", %{dataset: dataset} do
assert RDF.Data.include?(dataset, {EX.S, EX.p1, EX.O2})
assert RDF.Data.include?(dataset, {EX.S2, EX.p2, EX.O3})
assert RDF.Data.include?(dataset, {EX.S3, EX.p3, EX.O5, EX.NamedGraph})
refute RDF.Data.include?(dataset, {EX.Other, EX.p1, EX.O2})
end
test "statements", %{dataset: dataset} do
assert RDF.Data.statements(dataset) == Dataset.statements(dataset)
end
test "subjects", %{dataset: dataset} do
assert RDF.Data.subjects(dataset) == MapSet.new([uri(EX.S), uri(EX.S2), uri(EX.S3)])
end
test "predicates", %{dataset: dataset} do
assert RDF.Data.predicates(dataset) == MapSet.new([EX.p1, EX.p2, EX.p3])
end
test "objects", %{dataset: dataset} do
assert RDF.Data.objects(dataset) ==
MapSet.new([uri(EX.O1), uri(EX.O2), uri(EX.O3), uri(EX.O4), uri(EX.O5), ~B<foo>])
end
test "resources", %{dataset: dataset} do
assert RDF.Data.resources(dataset) == MapSet.new([
uri(EX.S), uri(EX.S2), uri(EX.S3), EX.p1, EX.p2, EX.p3,
uri(EX.O1), uri(EX.O2), uri(EX.O3), uri(EX.O4), uri(EX.O5), ~B<foo>
])
end
test "subject_count", %{dataset: dataset} do
assert RDF.Data.subject_count(dataset) == 3
end
test "statement_count", %{dataset: dataset} do
assert RDF.Data.statement_count(dataset) == 13
end
end
end