From d0a1106b5d6cca4067e81d5f903e373362951a29 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Thu, 28 Oct 2021 21:29:25 +0200 Subject: [PATCH] Add RDF.Graph.annotations/1 --- lib/rdf/graph.ex | 15 +++++++++++++++ test/unit/star/graph_test.exs | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/rdf/graph.ex b/lib/rdf/graph.ex index f9d1765..cda27fa 100644 --- a/lib/rdf/graph.ex +++ b/lib/rdf/graph.ex @@ -636,6 +636,21 @@ defmodule RDF.Graph do Map.values(graph.descriptions) end + @doc """ + Returns the `RDF.Graph` of all annotations. + + Note: The graph includes only triples where the subject is a quoted triple. + Triples where only the object is a quoted triple are NOT included. + """ + @spec annotations(t) :: t + def annotations(%__MODULE__{} = graph) do + %__MODULE__{ + graph + | descriptions: + for(annotation = {{_, _, _}, _} <- graph.descriptions, into: %{}, do: annotation) + } + end + @doc """ Gets and updates the description of the given subject, in a single pass. diff --git a/test/unit/star/graph_test.exs b/test/unit/star/graph_test.exs index 4e596de..eae9c5e 100644 --- a/test/unit/star/graph_test.exs +++ b/test/unit/star/graph_test.exs @@ -715,6 +715,20 @@ defmodule RDF.Star.Graph.Test do end end + describe "annotations/1" do + test "when no annotations exist" do + assert Graph.annotations(graph()) == graph() + end + + test "when annotations exist" do + assert Graph.annotations(graph_with_annotation()) == graph_with_annotation() + + assert graph_with_annotation() + |> Graph.add(statement()) + |> Graph.annotations() == graph_with_annotation() + end + end + test "include?/3" do assert Graph.include?(graph_with_annotations(), star_statement()) assert Graph.include?(graph_with_annotations(), {EX.As, EX.ap(), statement()})