From 2a447656311e91ff9bfa291becff930da7e57ae4 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Mon, 28 Sep 2020 12:01:23 +0200 Subject: [PATCH] Add support for list of descriptions to RDF.Graph.put/2 --- lib/rdf/graph.ex | 18 ++++++++++++++---- test/unit/graph_test.exs | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/rdf/graph.ex b/lib/rdf/graph.ex index 81efef9..d417244 100644 --- a/lib/rdf/graph.ex +++ b/lib/rdf/graph.ex @@ -277,10 +277,20 @@ defmodule RDF.Graph do def put(%__MODULE__{} = graph, statements) when is_list(statements) do put( graph, - Enum.group_by(statements, &elem(&1, 0), fn - {_, p, o} -> {p, o} - {_, predications} -> predications - end) + Enum.group_by( + statements, + fn + {subject, _} -> subject + {subject, _, _} -> subject + {subject, _, _, _} -> subject + %Description{subject: subject} -> subject + end, + fn + {_, p, o} -> {p, o} + {_, predications} -> predications + %Description{} = description -> description + end + ) ) end diff --git a/test/unit/graph_test.exs b/test/unit/graph_test.exs index 2e82c41..ea56575 100644 --- a/test/unit/graph_test.exs +++ b/test/unit/graph_test.exs @@ -402,6 +402,22 @@ defmodule RDF.GraphTest do assert graph_includes_statement?(g, {EX.S2, EX.P2, EX.O2}) end + test "a list of descriptions" do + g = + Graph.new([{EX.S1, EX.p1(), EX.O1}, {EX.S2, EX.p2(), EX.O2}, {EX.S1, EX.p3(), EX.O3}]) + |> RDF.Graph.put([ + EX.p1(EX.S1, EX.O41), + EX.p2(EX.S2, EX.O42), + EX.p2(EX.S2, EX.O43) + ]) + + assert Graph.triple_count(g) == 4 + assert graph_includes_statement?(g, {EX.S1, EX.p3(), EX.O3}) + assert graph_includes_statement?(g, {EX.S1, EX.p1(), EX.O41}) + assert graph_includes_statement?(g, {EX.S2, EX.p2(), EX.O42}) + assert graph_includes_statement?(g, {EX.S2, EX.p2(), EX.O43}) + end + test "a graph" do g = Graph.new([