From ccc8b94adebeb7b7f2fee7b8f8a55fa786a8ddcb Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sat, 1 Apr 2017 02:17:59 +0200 Subject: [PATCH] core: put statements of a Graph to a Dataset --- lib/rdf/dataset.ex | 13 +++++++++++++ test/unit/dataset_test.exs | 20 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/rdf/dataset.ex b/lib/rdf/dataset.ex index 39ff032..d6fbf5e 100644 --- a/lib/rdf/dataset.ex +++ b/lib/rdf/dataset.ex @@ -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 diff --git a/test/unit/dataset_test.exs b/test/unit/dataset_test.exs index 8109739..3562ad3 100644 --- a/test/unit/dataset_test.exs +++ b/test/unit/dataset_test.exs @@ -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?"