Add empty?/1 on Description, Graph and Dataset

This commit is contained in:
Marcel Otto 2022-04-05 21:06:29 +02:00
parent d8f436fea1
commit bf158d90ae
7 changed files with 48 additions and 0 deletions

View file

@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- a `RDF.Graph` builder DSL available under the `RDF.Graph.build/2` function
- `RDF.Graph.new/2` and `RDF.Graph.add/2` support the addition of `RDF.Dataset`s
- `RDF.Description.empty?/1`, `RDF.Graph.empty?/1` and `RDF.Dataset.empty?/1` which
are significantly faster than `Enum.empty?/1`
- new guards in `RDF.Guards`: `is_statement/1` and `is_quad/1`
- `RDF.PropertyMap.terms/1` and `RDF.PropertyMap.iris/1`

View file

@ -656,6 +656,16 @@ defmodule RDF.Dataset do
end)
end
@doc """
Returns if the given `dataset` is empty.
Note: You should always prefer this over the use of `Enum.empty?/1` as it is significantly faster.
"""
@spec empty?(t) :: boolean
def empty?(%__MODULE__{} = dataset) do
Enum.empty?(dataset.graphs) or dataset |> graphs() |> Enum.all?(&Graph.empty?/1)
end
@doc """
Checks if the given `input` statements exist within `dataset`.

View file

@ -637,6 +637,16 @@ defmodule RDF.Description do
defdelegate count(description), to: __MODULE__, as: :statement_count
@doc """
Returns if the given `description` is empty.
Note: You should always prefer this over the use of `Enum.empty?/1` as it is significantly faster.
"""
@spec empty?(t) :: boolean
def empty?(%__MODULE__{} = description) do
Enum.empty?(description.predications)
end
@doc """
Checks if the given `input` statements exist within `description`.
"""

View file

@ -928,6 +928,16 @@ defmodule RDF.Graph do
defdelegate statements(graph, opts \\ []), to: __MODULE__, as: :triples
@doc """
Returns if the given `graph` is empty.
Note: You should always prefer this over the use of `Enum.empty?/1` as it is significantly faster.
"""
@spec empty?(t) :: boolean
def empty?(%__MODULE__{} = graph) do
Enum.empty?(graph.descriptions)
end
@doc """
Checks if the given `input` statements exist within `graph`.
"""

View file

@ -1621,6 +1621,12 @@ defmodule RDF.DatasetTest do
assert Dataset.statement_count(Dataset.new(statement())) == 1
end
test "empty?/1" do
assert Dataset.empty?(dataset()) == true
assert Dataset.empty?(Dataset.new(statement())) == false
assert Dataset.empty?(Dataset.new(graph())) == true
end
describe "include?/3" do
test "valid cases" do
dataset =

View file

@ -792,6 +792,11 @@ defmodule RDF.DescriptionTest do
assert Description.statement_count(description({EX.p(), EX.O})) == 1
end
test "empty?/1" do
assert Description.empty?(Description.new(EX.S)) == true
assert Description.empty?(description({EX.p(), EX.O})) == false
end
describe "include?/3" do
test "valid cases" do
desc =

View file

@ -1288,6 +1288,11 @@ defmodule RDF.GraphTest do
assert Graph.statement_count(Graph.new(statement())) == 1
end
test "empty?/1" do
assert Graph.empty?(graph()) == true
assert Graph.empty?(Graph.new(statement())) == false
end
describe "include?/3" do
test "valid cases" do
graph =