Support triples via maps and nested lists in Graph build blocks

This commit is contained in:
Marcel Otto 2022-04-06 21:08:01 +02:00
parent 9e62b23a77
commit 3ff1186336
2 changed files with 44 additions and 13 deletions

View file

@ -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

View file

@ -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)