diff --git a/lib/rdf/dataset.ex b/lib/rdf/dataset.ex index b5ca19c..eb8431f 100644 --- a/lib/rdf/dataset.ex +++ b/lib/rdf/dataset.ex @@ -650,6 +650,9 @@ defmodule RDF.Dataset do @doc """ All statements within all graphs of a `RDF.Dataset`. + When the optional `:filter_star` flag is set to `true` RDF-star statements with + a triple as subject or object will be filtered. The default value is `false`. + ## Examples iex> RDF.Dataset.new([ @@ -661,11 +664,14 @@ defmodule RDF.Dataset do {RDF.iri(EX.S2), RDF.iri(EX.p2), RDF.iri(EX.O2)}, {RDF.iri(EX.S1), RDF.iri(EX.p1), RDF.iri(EX.O1), RDF.iri(EX.Graph)}] """ - @spec statements(t) :: [Statement.t()] - def statements(%__MODULE__{} = dataset) do + @spec statements(t, keyword) :: [Statement.t()] + def statements(%__MODULE__{} = dataset, opts \\ []) do Enum.flat_map(dataset.graphs, fn - {nil, graph} -> Graph.triples(graph) - {name, graph} -> Enum.map(graph, fn {s, p, o} -> {s, p, o, name} end) + {nil, graph} -> + Graph.triples(graph, opts) + + {name, graph} -> + graph |> Graph.triples(opts) |> Enum.map(fn {s, p, o} -> {s, p, o, name} end) end) end diff --git a/lib/rdf/description.ex b/lib/rdf/description.ex index 4ce1f22..0d5efcf 100644 --- a/lib/rdf/description.ex +++ b/lib/rdf/description.ex @@ -617,6 +617,7 @@ defmodule RDF.Description do When the optional `:filter_star` flag is set to `true` RDF-star triples with a triple as subject or object will be filtered. So, for a description with a triple as a subject you'll always get an empty list. + The default value of the `:filter_star` flag is `false`. """ @spec triples(t, keyword) :: list(Triple.t()) def triples(%__MODULE__{subject: s} = description, opts \\ []) do diff --git a/lib/rdf/graph.ex b/lib/rdf/graph.ex index aaa935a..89cd597 100644 --- a/lib/rdf/graph.ex +++ b/lib/rdf/graph.ex @@ -899,6 +899,9 @@ defmodule RDF.Graph do @doc """ The list of all statements within a `RDF.Graph`. + When the optional `:filter_star` flag is set to `true` RDF-star triples with + a triple as subject or object will be filtered. The default value is `false`. + ## Examples iex> RDF.Graph.new([ @@ -910,7 +913,7 @@ defmodule RDF.Graph do {RDF.iri(EX.S1), RDF.iri(EX.p2), RDF.iri(EX.O3)}, {RDF.iri(EX.S2), RDF.iri(EX.p2), RDF.iri(EX.O2)}] """ - @spec triples(t) :: [Statement.t()] + @spec triples(t, keyword) :: [Statement.t()] def triples(%__MODULE__{} = graph, opts \\ []) do if Keyword.get(opts, :filter_star, false) do Enum.flat_map(graph.descriptions, fn diff --git a/test/unit/star/dataset_test.exs b/test/unit/star/dataset_test.exs index 402c2e1..61cc685 100644 --- a/test/unit/star/dataset_test.exs +++ b/test/unit/star/dataset_test.exs @@ -40,4 +40,18 @@ defmodule RDF.Star.Dataset.Test do test "delete/3" do assert Dataset.delete(dataset_with_annotation(), annotation()) == dataset() end + + test ":filter_star opt on statements/1" do + assert dataset() + |> Dataset.put(statement()) + |> Dataset.put(statement(), graph: EX.Graph) + |> Dataset.put({statement(), EX.ap1(), EX.AO1}) + |> Dataset.put({statement(), EX.ap2(), "foo", EX.Graph}) + |> Dataset.put({statement(), EX.ap3(), EX.AO3}) + |> Dataset.statements(filter_star: true) == + dataset() + |> Dataset.put(statement()) + |> Dataset.put(statement(), graph: EX.Graph) + |> Dataset.statements() + end end