core: put statements from one graph into another

This commit is contained in:
Marcel Otto 2017-03-31 16:22:16 +02:00
parent a28f27133e
commit 2e75b75c83
2 changed files with 31 additions and 4 deletions

View file

@ -187,11 +187,18 @@ defmodule RDF.Graph do
def put(%RDF.Graph{name: name, descriptions: descriptions},
%Description{subject: subject} = description) do
description = if existing_description = descriptions[subject],
do: Description.put(existing_description, description),
else: description
%RDF.Graph{name: name,
descriptions: Map.put(descriptions, subject, description)}
descriptions:
Map.update(descriptions, subject, description, fn current ->
current |> Description.put(description)
end)
}
end
def put(graph, %RDF.Graph{descriptions: descriptions}) do
Enum.reduce descriptions, graph, fn ({_, description}, graph) ->
put(graph, description)
end
end
def put(%RDF.Graph{} = graph, statements) when is_map(statements) do

View file

@ -234,6 +234,26 @@ defmodule RDF.GraphTest do
assert graph_includes_statement?(g, {EX.S2, EX.P2, EX.O3})
assert graph_includes_statement?(g, {EX.S2, EX.P2, EX.O4})
end
test "a Graph" do
g =
Graph.new([
{EX.S1, EX.P1, EX.O1},
{EX.S1, EX.P3, EX.O3},
{EX.S2, EX.P2, EX.O2},
])
|> RDF.Graph.put(Graph.new([
{EX.S1, EX.P3, EX.O4},
{EX.S2, EX.P2, bnode(:foo)},
{EX.S3, EX.P3, EX.O3}
]))
assert Graph.triple_count(g) == 4
assert graph_includes_statement?(g, {EX.S1, EX.P1, EX.O1})
assert graph_includes_statement?(g, {EX.S1, EX.P3, EX.O4})
assert graph_includes_statement?(g, {EX.S2, EX.P2, bnode(:foo)})
assert graph_includes_statement?(g, {EX.S3, EX.P3, EX.O3})
end
end
test "pop a triple" do