Add :filter_star opt to RDF.Dataset.statements/1

This commit is contained in:
Marcel Otto 2021-11-15 22:01:22 +01:00
parent 9695e137ce
commit 5bbd1409bb
4 changed files with 29 additions and 5 deletions

View file

@ -650,6 +650,9 @@ defmodule RDF.Dataset do
@doc """ @doc """
All statements within all graphs of a `RDF.Dataset`. 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 ## Examples
iex> RDF.Dataset.new([ 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.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)}] {RDF.iri(EX.S1), RDF.iri(EX.p1), RDF.iri(EX.O1), RDF.iri(EX.Graph)}]
""" """
@spec statements(t) :: [Statement.t()] @spec statements(t, keyword) :: [Statement.t()]
def statements(%__MODULE__{} = dataset) do def statements(%__MODULE__{} = dataset, opts \\ []) do
Enum.flat_map(dataset.graphs, fn Enum.flat_map(dataset.graphs, fn
{nil, graph} -> Graph.triples(graph) {nil, graph} ->
{name, graph} -> Enum.map(graph, fn {s, p, o} -> {s, p, o, name} end) Graph.triples(graph, opts)
{name, graph} ->
graph |> Graph.triples(opts) |> Enum.map(fn {s, p, o} -> {s, p, o, name} end)
end) end)
end end

View file

@ -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 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. 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()) @spec triples(t, keyword) :: list(Triple.t())
def triples(%__MODULE__{subject: s} = description, opts \\ []) do def triples(%__MODULE__{subject: s} = description, opts \\ []) do

View file

@ -899,6 +899,9 @@ defmodule RDF.Graph do
@doc """ @doc """
The list of all statements within a `RDF.Graph`. 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 ## Examples
iex> RDF.Graph.new([ 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.S1), RDF.iri(EX.p2), RDF.iri(EX.O3)},
{RDF.iri(EX.S2), RDF.iri(EX.p2), RDF.iri(EX.O2)}] {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 def triples(%__MODULE__{} = graph, opts \\ []) do
if Keyword.get(opts, :filter_star, false) do if Keyword.get(opts, :filter_star, false) do
Enum.flat_map(graph.descriptions, fn Enum.flat_map(graph.descriptions, fn

View file

@ -40,4 +40,18 @@ defmodule RDF.Star.Dataset.Test do
test "delete/3" do test "delete/3" do
assert Dataset.delete(dataset_with_annotation(), annotation()) == dataset() assert Dataset.delete(dataset_with_annotation(), annotation()) == dataset()
end 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 end