From 3ff1186336ccbc99272c803ab18f8205e3313c69 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Wed, 6 Apr 2022 21:08:01 +0200 Subject: [PATCH] Support triples via maps and nested lists in Graph build blocks --- lib/rdf/graph_builder.ex | 5 ++- test/unit/graph_builder_test.exs | 52 ++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/lib/rdf/graph_builder.ex b/lib/rdf/graph_builder.ex index 487c619..a21f1a6 100644 --- a/lib/rdf/graph_builder.ex +++ b/lib/rdf/graph_builder.ex @@ -1,8 +1,6 @@ defmodule RDF.Graph.Builder do alias RDF.{Description, Graph, Dataset, PrefixMap} - import RDF.Guards - defmodule Error do defexception [:message] end @@ -121,7 +119,8 @@ defmodule RDF.Graph.Builder do defp rdf?(%Description{}), do: true defp rdf?(%Graph{}), do: true defp rdf?(%Dataset{}), do: true - defp rdf?(statement) when is_statement(statement), do: true + defp rdf?(statements) when is_map(statements), do: true + defp rdf?(statements) when is_tuple(statements), do: true defp rdf?(list) when is_list(list), do: true defp rdf?(invalid) do diff --git a/test/unit/graph_builder_test.exs b/test/unit/graph_builder_test.exs index 9140657..aa75fc8 100644 --- a/test/unit/graph_builder_test.exs +++ b/test/unit/graph_builder_test.exs @@ -90,6 +90,45 @@ defmodule RDF.Graph.BuilderTest do ]) end + test "triples given as maps" do + graph = + RDF.Graph.build do + %{ + EX.S => %{ + EX.p1() => EX.O1, + EX.p2() => [EX.O2, EX.O3] + } + } + end + + assert graph == + RDF.graph([ + EX.S + |> EX.p1(EX.O1) + |> EX.p2(EX.O2, EX.O3) + ]) + end + + test "triples given as nested list" do + graph = + RDF.Graph.build do + [ + {EX.S, + [ + {EX.p1(), EX.O1}, + {EX.p2(), [EX.O2, EX.O3]} + ]} + ] + end + + assert graph == + RDF.graph([ + EX.S + |> EX.p1(EX.O1) + |> EX.p2(EX.O2, EX.O3) + ]) + end + test "nested statements" do graph = RDF.Graph.build do @@ -150,13 +189,6 @@ defmodule RDF.Graph.BuilderTest do "foo" end end - - assert_raise Builder.Error, "invalid RDF data: {:ok, \"foo\"}", fn -> - RDF.Graph.build do - EX.S |> EX.p(EX.O) - {:ok, "foo"} - end - end end test "variable assignments" do @@ -324,7 +356,7 @@ defmodule RDF.Graph.BuilderTest do (fn -> RDF.Graph.build do # TODO: the following leads to a (RDF.Namespace.UndefinedTermError) Elixir.TestNS is not a RDF.Namespace - # @prefix custom: TestNS.Custom + # @prefix cust: TestNS.Custom @prefix cust: RDF.Graph.BuilderTest.TestNS.Custom Custom.S |> Custom.p(Custom.O) @@ -343,7 +375,7 @@ defmodule RDF.Graph.BuilderTest do (fn -> RDF.Graph.build do # TODO: the following leads to a (RDF.Namespace.UndefinedTermError) Elixir.TestNS is not a RDF.Namespace - # @prefix custom: TestNS.Custom + # @prefix TestNS.Custom @prefix RDF.Graph.BuilderTest.TestNS.Custom Custom.S |> Custom.p(Custom.O) @@ -383,7 +415,7 @@ defmodule RDF.Graph.BuilderTest do (fn -> RDF.Graph.build do # TODO: the following leads to a (RDF.Namespace.UndefinedTermError) Elixir.TestNS is not a RDF.Namespace - # @prefix custom: TestNS.Custom + # @base TestNS.Custom @base RDF.Graph.BuilderTest.TestNS.Custom Custom.S |> Custom.p(Custom.O)