Skip to content

Commit

Permalink
Add equal?/2 on RDF.Description, RDF.Graph and RDF.Dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotto committed Apr 2, 2019
1 parent db0b9ba commit 158decc
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
@@ -1,4 +1,4 @@
# Change Log
# Changelog

All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/) and
Expand All @@ -11,9 +11,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and

- `RDF.PrefixMap`
- 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`
- configurable `RDF.default_prefixes`
- `RDF.Description.equal?/2`, `RDF.Graph.equal?/2` and `RDF.Dataset.equal?/2`


### Changed
Expand Down
15 changes: 15 additions & 0 deletions lib/rdf/dataset.ex
Expand Up @@ -779,6 +779,21 @@ defmodule RDF.Dataset do
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
def member?(dataset, statement), do: {:ok, RDF.Dataset.include?(dataset, statement)}
def count(dataset), do: {:ok, RDF.Dataset.statement_count(dataset)}
Expand Down
14 changes: 14 additions & 0 deletions lib/rdf/description.ex
Expand Up @@ -617,6 +617,20 @@ defmodule RDF.Description do
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
def member?(desc, triple), do: {:ok, RDF.Description.include?(desc, triple)}
def count(desc), do: {:ok, RDF.Description.count(desc)}
Expand Down
15 changes: 15 additions & 0 deletions lib/rdf/graph.ex
Expand Up @@ -683,6 +683,21 @@ defmodule RDF.Graph do
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 """
Adds `prefixes` to the given `graph`.
Expand Down
12 changes: 6 additions & 6 deletions test/acceptance/turtle_w3c_test.exs
Expand Up @@ -53,12 +53,12 @@ defmodule RDF.Turtle.W3C.Test do

test TestSuite.test_title(test_case), %{test_case: test_case} do
with base = to_string(TestSuite.test_input_file(test_case)) do
assert (TestSuite.test_input_file_path(test_case, "Turtle")
|> Turtle.read_file!(base: base)
|> RDF.Graph.clear_prefixes()
) ==
(TestSuite.test_result_file_path(test_case, "Turtle")
|> NTriples.read_file!)
assert RDF.Graph.equal?(
(TestSuite.test_input_file_path(test_case, "Turtle")
|> Turtle.read_file!(base: base)),
(TestSuite.test_result_file_path(test_case, "Turtle")
|> NTriples.read_file!)
)
end
end
end)
Expand Down
11 changes: 11 additions & 0 deletions test/unit/dataset_test.exs
Expand Up @@ -739,6 +739,17 @@ defmodule RDF.DatasetTest do
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
test "Enum.count" do
assert Enum.count(Dataset.new(name: EX.foo)) == 0
Expand Down
5 changes: 5 additions & 0 deletions test/unit/description_test.exs
Expand Up @@ -358,6 +358,11 @@ defmodule RDF.DescriptionTest do
%{p: ["Foo"]}
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
test "Enum.count" do
assert Enum.count(Description.new EX.foo) == 0
Expand Down
14 changes: 14 additions & 0 deletions test/unit/graph_test.exs
Expand Up @@ -462,6 +462,20 @@ defmodule RDF.GraphTest do
}
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
test "when prefixes already exist" do
graph = Graph.new(prefixes: %{xsd: XSD}) |> Graph.add_prefixes(ex: EX)
Expand Down

0 comments on commit 158decc

Please sign in to comment.