Add equal?/2 on RDF.Description, RDF.Graph and RDF.Dataset

This commit is contained in:
Marcel Otto 2019-04-03 00:12:42 +02:00
parent db0b9baade
commit 158decc16b
8 changed files with 83 additions and 8 deletions

View file

@ -1,4 +1,4 @@
# Change Log # Changelog
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/) and This project adheres to [Semantic Versioning](http://semver.org/) and
@ -11,9 +11,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- `RDF.PrefixMap` - `RDF.PrefixMap`
- prefix management of `RDF.Graph`s: - prefix management of `RDF.Graph`s:
- the structure now has `prefixes` field with an optional `RDF.PrefixMap` - the structure now has a `prefixes` field with an optional `RDF.PrefixMap`
- new functions `add_prefixes/2`, `delete_prefixes/2` and `clear_prefixes/1` - new functions `add_prefixes/2`, `delete_prefixes/2` and `clear_prefixes/1`
- configurable `RDF.default_prefixes` - configurable `RDF.default_prefixes`
- `RDF.Description.equal?/2`, `RDF.Graph.equal?/2` and `RDF.Dataset.equal?/2`
### Changed ### Changed

View file

@ -779,6 +779,21 @@ defmodule RDF.Dataset do
end end
@doc """
Checks if two `RDF.Dataset`s are equal.
Two `RDF.Dataset`s are considered to be equal if they contain the same triples
and have the same name.
"""
def equal?(dataset1, dataset2)
def equal?(%RDF.Dataset{} = dataset1, %RDF.Dataset{} = dataset2) do
dataset1 == dataset2
end
def equal?(_, _), do: false
defimpl Enumerable do defimpl Enumerable do
def member?(dataset, statement), do: {:ok, RDF.Dataset.include?(dataset, statement)} def member?(dataset, statement), do: {:ok, RDF.Dataset.include?(dataset, statement)}
def count(dataset), do: {:ok, RDF.Dataset.statement_count(dataset)} def count(dataset), do: {:ok, RDF.Dataset.statement_count(dataset)}

View file

@ -617,6 +617,20 @@ defmodule RDF.Description do
end end
@doc """
Checks if two `RDF.Description`s are equal.
Two `RDF.Description`s are considered to be equal if they contain the same triples.
"""
def equal?(description1, description2)
def equal?(%RDF.Description{} = description1, %RDF.Description{} = description2) do
description1 == description2
end
def equal?(_, _), do: false
defimpl Enumerable do defimpl Enumerable do
def member?(desc, triple), do: {:ok, RDF.Description.include?(desc, triple)} def member?(desc, triple), do: {:ok, RDF.Description.include?(desc, triple)}
def count(desc), do: {:ok, RDF.Description.count(desc)} def count(desc), do: {:ok, RDF.Description.count(desc)}

View file

@ -683,6 +683,21 @@ defmodule RDF.Graph do
end end
@doc """
Checks if two `RDF.Graph`s are equal.
Two `RDF.Graph`s are considered to be equal if they contain the same triples
and have the same name. The prefixes of the graph are irrelevant for equality.
"""
def equal?(graph1, graph2)
def equal?(%RDF.Graph{} = graph1, %RDF.Graph{} = graph2) do
clear_prefixes(graph1) == clear_prefixes(graph2)
end
def equal?(_, _), do: false
@doc """ @doc """
Adds `prefixes` to the given `graph`. Adds `prefixes` to the given `graph`.

View file

@ -53,12 +53,12 @@ defmodule RDF.Turtle.W3C.Test do
test TestSuite.test_title(test_case), %{test_case: test_case} do test TestSuite.test_title(test_case), %{test_case: test_case} do
with base = to_string(TestSuite.test_input_file(test_case)) do with base = to_string(TestSuite.test_input_file(test_case)) do
assert (TestSuite.test_input_file_path(test_case, "Turtle") assert RDF.Graph.equal?(
|> Turtle.read_file!(base: base) (TestSuite.test_input_file_path(test_case, "Turtle")
|> RDF.Graph.clear_prefixes() |> Turtle.read_file!(base: base)),
) == (TestSuite.test_result_file_path(test_case, "Turtle")
(TestSuite.test_result_file_path(test_case, "Turtle") |> NTriples.read_file!)
|> NTriples.read_file!) )
end end
end end
end) end)

View file

@ -739,6 +739,17 @@ defmodule RDF.DatasetTest do
end end
test "equal/2" do
assert Dataset.new({EX.S, EX.p, EX.O}) |> Dataset.equal?(Dataset.new({EX.S, EX.p, EX.O}))
assert Dataset.new({EX.S, EX.p, EX.O}, name: EX.Dataset1)
|> Dataset.equal?(Dataset.new({EX.S, EX.p, EX.O}, name: EX.Dataset1))
refute Dataset.new({EX.S, EX.p, EX.O}) |> Dataset.equal?(Dataset.new({EX.S, EX.p, EX.O2}))
refute Dataset.new({EX.S, EX.p, EX.O}, name: EX.Dataset1)
|> Dataset.equal?(Dataset.new({EX.S, EX.p, EX.O}, name: EX.Dataset2))
end
describe "Enumerable protocol" do describe "Enumerable protocol" do
test "Enum.count" do test "Enum.count" do
assert Enum.count(Dataset.new(name: EX.foo)) == 0 assert Enum.count(Dataset.new(name: EX.foo)) == 0

View file

@ -358,6 +358,11 @@ defmodule RDF.DescriptionTest do
%{p: ["Foo"]} %{p: ["Foo"]}
end end
test "equal/2" do
assert Description.new({EX.S, EX.p, EX.O}) |> Description.equal?(Description.new({EX.S, EX.p, EX.O}))
refute Description.new({EX.S, EX.p, EX.O}) |> Description.equal?(Description.new({EX.S, EX.p, EX.O2}))
end
describe "Enumerable protocol" do describe "Enumerable protocol" do
test "Enum.count" do test "Enum.count" do
assert Enum.count(Description.new EX.foo) == 0 assert Enum.count(Description.new EX.foo) == 0

View file

@ -462,6 +462,20 @@ defmodule RDF.GraphTest do
} }
end end
test "equal/2" do
assert Graph.new({EX.S, EX.p, EX.O}) |> Graph.equal?(Graph.new({EX.S, EX.p, EX.O}))
assert Graph.new({EX.S, EX.p, EX.O}, name: EX.Graph1)
|> Graph.equal?(Graph.new({EX.S, EX.p, EX.O}, name: EX.Graph1))
assert Graph.new({EX.S, EX.p, EX.O}, prefixes: %{ex: EX})
|> Graph.equal?(Graph.new({EX.S, EX.p, EX.O}, prefixes: %{xsd: XSD}))
refute Graph.new({EX.S, EX.p, EX.O}) |> Graph.equal?(Graph.new({EX.S, EX.p, EX.O2}))
refute Graph.new({EX.S, EX.p, EX.O}, name: EX.Graph1)
|> Graph.equal?(Graph.new({EX.S, EX.p, EX.O}, name: EX.Graph2))
end
describe "add_prefixes/2" do describe "add_prefixes/2" do
test "when prefixes already exist" do test "when prefixes already exist" do
graph = Graph.new(prefixes: %{xsd: XSD}) |> Graph.add_prefixes(ex: EX) graph = Graph.new(prefixes: %{xsd: XSD}) |> Graph.add_prefixes(ex: EX)