core: put statements of a Graph to a Dataset

This commit is contained in:
Marcel Otto 2017-04-01 02:17:59 +02:00
parent 87ba61eeb2
commit ccc8b94ade
2 changed files with 31 additions and 2 deletions

View file

@ -134,6 +134,7 @@ defmodule RDF.Dataset do
end
end
@doc """
Adds statements to a `RDF.Dataset` and overwrites all existing statements with the same subjects and predicates in the specified graph context.
@ -214,6 +215,18 @@ defmodule RDF.Dataset do
end
end
def put(%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.put(graph)
end)
}
end
end
def put(%RDF.Dataset{} = dataset, statements, graph_context)
when is_map(statements) do
with graph_context = Quad.convert_graph_context(graph_context) do

View file

@ -330,12 +330,28 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.S2, EX.P2, EX.O2})
end
@tag skip: "TODO"
test "an unnamed Graph" do
ds = Dataset.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}, {EX.S1, EX.P3, EX.O3}])
|> RDF.Dataset.put(Graph.new([{EX.S1, EX.P3, EX.O4}, {EX.S1, EX.P2, bnode(:foo)}]))
assert Dataset.statement_count(ds) == 4
assert dataset_includes_statement?(ds, {EX.S1, EX.P1, EX.O1})
assert dataset_includes_statement?(ds, {EX.S1, EX.P3, EX.O4})
assert dataset_includes_statement?(ds, {EX.S1, EX.P2, bnode(:foo)})
assert dataset_includes_statement?(ds, {EX.S2, EX.P2, EX.O2})
end
@tag skip: "TODO"
test "a named Graph" do
ds = Dataset.new(
Graph.new(EX.GraphName, [{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}, {EX.S1, EX.P3, EX.O3}]))
|> RDF.Dataset.put(
Graph.new([{EX.S1, EX.P3, EX.O4}, {EX.S1, EX.P2, bnode(:foo)}]), EX.GraphName)
assert Dataset.statement_count(ds) == 4
assert dataset_includes_statement?(ds, {EX.S1, EX.P1, EX.O1, EX.GraphName})
assert dataset_includes_statement?(ds, {EX.S1, EX.P3, EX.O4, EX.GraphName})
assert dataset_includes_statement?(ds, {EX.S1, EX.P2, bnode(:foo), EX.GraphName})
assert dataset_includes_statement?(ds, {EX.S2, EX.P2, EX.O2, EX.GraphName})
end
# @tag skip: "TODO: Requires Dataset.put with a list to differentiate a list of statements, a list of Descriptions and list of Graphs. Do we want to support mixed lists also?"