Add RDF.Diff.apply/2

This commit is contained in:
Marcel Otto 2019-11-20 01:45:34 +01:00
parent b6438b1c41
commit a1cbbd24b6
2 changed files with 107 additions and 1 deletions

View file

@ -30,7 +30,7 @@ defmodule RDF.Diff do
defp coerce_graph(data), do: Graph.new(data)
@doc """
Computes the diff between two `RDF.Graph`s or `RDF.Description`s.
Computes a diff between two `RDF.Graph`s or `RDF.Description`s.
The first argument represents the original and the second argument the new version
of the RDF data to be compared. Any combination of `RDF.Graph`s or
@ -149,4 +149,25 @@ defmodule RDF.Diff do
deletions: Graph.add(diff1.deletions, diff2.deletions)
)
end
@doc """
Applies a diff to a `RDF.Graph` or `RDF.Description` by deleting the `deletions` and adding the `additions` of the `diff`.
Deletions of statements which are not present in the given graph or description
are simply ignored.
The result of an application is always a `RDF.Graph`, even if a `RDF.Description`
is given and the additions from the diff are all about the subject of this description.
"""
def apply(diff, rdf_data)
def apply(%__MODULE__{} = diff, %Graph{} = graph) do
graph
|> Graph.delete(diff.deletions)
|> Graph.add(diff.additions)
end
def apply(%__MODULE__{} = diff, %Description{} = description) do
__MODULE__.apply(diff, Graph.new(description))
end
end

View file

@ -197,4 +197,89 @@ defmodule RDF.DiffTest do
])
)
end
describe "apply/2" do
test "on a graph" do
assert Diff.new(
additions: Graph.new([
EX.S1
|> EX.p(EX.O3)
|> EX.p3(EX.O),
EX.S3
|> EX.p(EX.O)
]),
deletions: Graph.new([
EX.S1
|> EX.p(EX.O1)
|> EX.p2(EX.O),
EX.S2
|> EX.p(EX.O)
]))
|> Diff.apply(Graph.new([
EX.S1
|> EX.p(EX.O1, EX.O2)
|> EX.p2(EX.O),
EX.S2
|> EX.p(EX.O)
])) ==
Graph.new([
EX.S1
|> EX.p(EX.O2, EX.O3)
|> EX.p3(EX.O),
EX.S3
|> EX.p(EX.O)
])
end
test "on a description" do
assert Diff.new(
additions: Graph.new([
EX.S1
|> EX.p(EX.O3)
|> EX.p3(EX.O),
EX.S3
|> EX.p(EX.O)
]),
deletions: Graph.new([
EX.S1
|> EX.p(EX.O1)
|> EX.p2(EX.O),
]))
|> Diff.apply(
EX.S1
|> EX.p(EX.O1, EX.O2)
|> EX.p2(EX.O)
) ==
Graph.new([
EX.S1
|> EX.p(EX.O2, EX.O3)
|> EX.p3(EX.O),
EX.S3
|> EX.p(EX.O)
])
end
test "when the statements to be deleted are not present" do
assert Diff.new(
additions: Graph.new(
EX.S1
|> EX.p(EX.O4)
),
deletions: Graph.new([
EX.S1
|> EX.p(EX.O2, EX.O3)
|> EX.p2(EX.O),
EX.S2
|> EX.p(EX.O)
]))
|> Diff.apply(Graph.new(
EX.S1
|> EX.p(EX.O1, EX.O2)
)) ==
Graph.new(
EX.S1
|> EX.p(EX.O1, EX.O4)
)
end
end
end