core: add statements of a Graph to a Dataset

This commit is contained in:
Marcel Otto 2017-04-01 01:19:54 +02:00
parent 7b659d5ddf
commit 186756bf81
2 changed files with 63 additions and 6 deletions

View file

@ -65,6 +65,12 @@ defmodule RDF.Dataset do
@doc """ @doc """
Adds triples and quads to a `RDF.Dataset`. Adds triples and quads to 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.
Note: This also applies when adding a named graph. Its name is ignored over
`graph_context` and its default value.
""" """
def add(dataset, statements, graph_context \\ nil) def add(dataset, statements, graph_context \\ nil)
@ -103,6 +109,17 @@ defmodule RDF.Dataset do
end end
end end
def add(%RDF.Dataset{name: name, graphs: graphs}, %Graph{} = graph,
graph_context) do
with graph_context = Quad.convert_graph_context(graph_context) do
%RDF.Dataset{name: name,
graphs:
Map.update(graphs, graph_context, Graph.new(graph_context, graph), fn current ->
current |> Graph.add(graph)
end)
}
end
end
@doc """ @doc """
Adds statements to a `RDF.Dataset` and overwrites all existing statements with the same subjects and predicates in the specified graph context. Adds statements to a `RDF.Dataset` and overwrites all existing statements with the same subjects and predicates in the specified graph context.

View file

@ -172,23 +172,63 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2})
ds = Dataset.add(ds, Description.new({EX.Subject1, EX.predicate3, EX.Object3}), EX.Graph) ds = Dataset.add(ds, Description.new({EX.Subject1, EX.predicate3, EX.Object3}), EX.Graph)
assert Enum.count(ds) == 3
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate3, EX.Object3, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate3, EX.Object3, EX.Graph})
end end
@tag skip: "TODO"
test "an unnamed Graph" do test "an unnamed Graph" do
ds = Dataset.add(dataset(), Graph.new(%{EX.Subject1 => [ ds = Dataset.add(dataset(), Graph.new([
{EX.predicate1, EX.Object1}, {EX.Subject1, EX.predicate1, EX.Object1},
{EX.predicate2, EX.Object2}, {EX.Subject1, EX.predicate2, EX.Object2},
]})) ]))
assert unnamed_graph?(Dataset.default_graph(ds))
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2})
ds = Dataset.add(ds, Graph.new({EX.Subject1, EX.predicate2, EX.Object3}))
assert unnamed_graph?(Dataset.default_graph(ds))
assert Enum.count(ds) == 3
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object3})
ds = Dataset.add(ds, Graph.new({EX.Subject1, EX.predicate2, EX.Object3}), EX.Graph)
assert unnamed_graph?(Dataset.default_graph(ds))
assert named_graph?(Dataset.graph(ds, EX.Graph), uri(EX.Graph))
assert Enum.count(ds) == 4
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object3})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object3, EX.Graph})
end end
@tag skip: "TODO"
test "a named Graph" do test "a named Graph" do
ds = Dataset.add(dataset(), Graph.new(EX.Graph1, [
{EX.Subject1, EX.predicate1, EX.Object1},
{EX.Subject1, EX.predicate2, EX.Object2},
]))
refute Dataset.graph(ds, EX.Graph1)
assert unnamed_graph?(Dataset.default_graph(ds))
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2})
ds = Dataset.add(ds, Graph.new(EX.Graph2, {EX.Subject1, EX.predicate2, EX.Object3}))
refute Dataset.graph(ds, EX.Graph2)
assert unnamed_graph?(Dataset.default_graph(ds))
assert Enum.count(ds) == 3
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object3})
ds = Dataset.add(ds, Graph.new(EX.Graph3, {EX.Subject1, EX.predicate2, EX.Object3}), EX.Graph)
assert named_graph?(Dataset.graph(ds, EX.Graph), uri(EX.Graph))
assert Enum.count(ds) == 4
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1, EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object2})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object3})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2, EX.Object3, EX.Graph})
end end
test "a list of Descriptions" do test "a list of Descriptions" do