Limit and unify the forms of input on RDF.Dataset functions

This commit is contained in:
Marcel Otto 2020-09-28 17:39:31 +02:00
parent 2a44765631
commit 8b723286c5
4 changed files with 325 additions and 237 deletions

View file

@ -13,10 +13,13 @@ defmodule RDF.Dataset do
""" """
defstruct name: nil, graphs: %{}
@behaviour Access @behaviour Access
alias RDF.{Description, Graph, IRI, Statement} alias RDF.{Description, Graph, IRI, Statement}
import RDF.Statement import RDF.Statement
import RDF.Utils
@type graph_name :: IRI.t() | nil @type graph_name :: IRI.t() | nil
@ -29,8 +32,6 @@ defmodule RDF.Dataset do
@type update_graph_fun :: (Graph.t() -> {Graph.t(), input} | :pop) @type update_graph_fun :: (Graph.t() -> {Graph.t(), input} | :pop)
defstruct name: nil, graphs: %{}
@doc """ @doc """
Creates an empty unnamed `RDF.Dataset`. Creates an empty unnamed `RDF.Dataset`.
""" """
@ -47,12 +48,12 @@ defmodule RDF.Dataset do
## Examples ## Examples
RDF.Graph.new({EX.S, EX.p, EX.O}) RDF.Dataset.new({EX.S, EX.p, EX.O})
RDF.Graph.new(name: EX.GraphName) RDF.Dataset.new(name: EX.GraphName)
""" """
@spec new(input | [input] | keyword) :: t @spec new(input | keyword) :: t
def new(data_or_options) def new(data_or_options)
def new(data_or_options) def new(data_or_options)
@ -69,20 +70,14 @@ defmodule RDF.Dataset do
@doc """ @doc """
Creates an `RDF.Dataset` initialized with data. Creates an `RDF.Dataset` initialized with data.
The initial RDF triples can be provided The initial RDF triples can be provided in any form accepted by `add/3`.
- as a single statement tuple
- an `RDF.Description`
- an `RDF.Graph`
- an `RDF.Dataset`
- or a list with any combination of the former
Available options: Available options:
- `name`: the name of the dataset to be created - `name`: the name of the dataset to be created
""" """
@spec new(input | [input], keyword) :: t @spec new(input, keyword) :: t
def new(data, options) def new(data, options)
def new(%__MODULE__{} = graph, options) do def new(%__MODULE__{} = graph, options) do
@ -95,87 +90,62 @@ defmodule RDF.Dataset do
|> add(data) |> add(data)
end end
defp destination_graph(opts, default \\ nil) do
opts
|> Keyword.get(:graph, default)
|> coerce_graph_name()
end
@doc """ @doc """
Adds triples and quads to a `RDF.Dataset`. Adds triples and quads to a `RDF.Dataset`.
The optional third `graph_context` argument allows to set a different The triples can be provided in any form accepted by `add/2`.
destination graph to which the statements are added, ignoring the graph context
of given quads or the name of given graphs. - as a single statement tuple
- an `RDF.Description`
- an `RDF.Graph`
- an `RDF.Dataset`
- or a list with any combination of the former
The `graph` option allows to set a different destination graph to which the
statements should be added, ignoring the graph context of given quads or the
name of given graphs.
""" """
@spec add(t, input | [input], boolean | nil) :: t @spec add(t, input, keyword) :: t
def add(dataset, statements, graph_context \\ false) def add(dataset, statements, opts \\ [])
def add(dataset, statements, graph_context) when is_list(statements) do def add(dataset, {_, _, _, graph} = quad, opts),
with graph_context = graph_context && coerce_graph_name(graph_context) do do: do_add(dataset, destination_graph(opts, graph), quad)
Enum.reduce(statements, dataset, fn statement, dataset ->
add(dataset, statement, graph_context) def add(dataset, %Graph{} = graph, opts),
end) do: do_add(dataset, destination_graph(opts, graph.name), graph)
end
def add(%__MODULE__{} = dataset, %__MODULE__{} = other_dataset, opts) do
other_dataset
|> graphs()
|> Enum.reduce(dataset, &add(&2, &1, opts))
end end
def add(dataset, {subject, predicate, objects}, false), def add(dataset, input, opts) when is_list(input) or is_map(input) do
do: add(dataset, {subject, predicate, objects, nil}) Enum.reduce(input, dataset, &add(&2, &1, opts))
end
def add(dataset, {subject, predicate, objects}, graph_context), def add(dataset, input, opts), do: do_add(dataset, destination_graph(opts), input)
do: add(dataset, {subject, predicate, objects, graph_context})
def add( defp do_add(dataset, graph_name, input) do
%__MODULE__{name: name, graphs: graphs}, %__MODULE__{
{subject, predicate, objects, graph_context}, dataset
false | graphs:
) do lazy_map_update(
with graph_context = coerce_graph_name(graph_context) do dataset.graphs,
updated_graphs = graph_name,
Map.update( # when new:
graphs, fn -> Graph.new(input, name: graph_name) end,
graph_context, # when update:
Graph.new({subject, predicate, objects}, name: graph_context), fn graph -> Graph.add(graph, input) end
fn graph -> Graph.add(graph, {subject, predicate, objects}) end
) )
%__MODULE__{name: name, graphs: updated_graphs}
end
end
def add(%__MODULE__{} = dataset, {subject, predicate, objects, _}, graph_context),
do: add(dataset, {subject, predicate, objects, graph_context}, false)
def add(%__MODULE__{} = dataset, %Description{} = description, false),
do: add(dataset, description, nil)
def add(%__MODULE__{name: name, graphs: graphs}, %Description{} = description, graph_context) do
with graph_context = coerce_graph_name(graph_context) do
updated_graph =
Map.get(graphs, graph_context, Graph.new(name: graph_context))
|> Graph.add(description)
%__MODULE__{
name: name,
graphs: Map.put(graphs, graph_context, updated_graph)
} }
end end
end
def add(%__MODULE__{name: name, graphs: graphs}, %Graph{} = graph, false) do
%__MODULE__{
name: name,
graphs:
Map.update(graphs, graph.name, graph, fn current ->
Graph.add(current, graph)
end)
}
end
def add(%__MODULE__{} = dataset, %Graph{} = graph, graph_context),
do: add(dataset, %Graph{graph | name: coerce_graph_name(graph_context)}, false)
def add(%__MODULE__{} = dataset, %__MODULE__{} = other_dataset, graph_context) do
with graph_context = graph_context && coerce_graph_name(graph_context) do
Enum.reduce(graphs(other_dataset), dataset, fn graph, dataset ->
add(dataset, graph, graph_context)
end)
end
end
@doc """ @doc """
Adds statements to a `RDF.Dataset` and overwrites all existing statements with the same subjects and predicates in the specified graph context. Adds statements to a `RDF.Dataset` and overwrites all existing statements with the same subjects and predicates in the specified graph context.
@ -191,7 +161,7 @@ defmodule RDF.Dataset do
...> RDF.Dataset.put([{EX.S1, EX.P2, EX.O3}, {EX.S2, EX.P2, EX.O3}]) ...> RDF.Dataset.put([{EX.S1, EX.P2, EX.O3}, {EX.S2, EX.P2, EX.O3}])
RDF.Dataset.new([{EX.S1, EX.P1, EX.O1}, {EX.S1, EX.P2, EX.O3}, {EX.S2, EX.P2, EX.O3}]) RDF.Dataset.new([{EX.S1, EX.P1, EX.O1}, {EX.S1, EX.P2, EX.O3}, {EX.S2, EX.P2, EX.O3}])
""" """
@spec put(t, input | [input], Statement.coercible_graph_name() | boolean | nil) :: t @spec put(t, input, Statement.coercible_graph_name() | boolean | nil) :: t
def put(dataset, statements, graph_context \\ false) def put(dataset, statements, graph_context \\ false)
def put(%__MODULE__{} = dataset, {subject, predicate, objects}, false), def put(%__MODULE__{} = dataset, {subject, predicate, objects}, false),
@ -318,71 +288,51 @@ defmodule RDF.Dataset do
@doc """ @doc """
Deletes statements from a `RDF.Dataset`. Deletes statements from a `RDF.Dataset`.
The optional third `graph_context` argument allows to set a different The `graph` option allows to set a different destination graph from which the
destination graph from which the statements are deleted, ignoring the graph statements should be deleted, ignoring the graph context of given quads or the
context of given quads or the name of given graphs. name of given graphs.
Note: When the statements to be deleted are given as another `RDF.Dataset`, Note: When the statements to be deleted are given as another `RDF.Dataset`,
the dataset name must not match dataset name of the dataset from which the statements the dataset name must not match dataset name of the dataset from which the statements
are deleted. If you want to delete only datasets with matching names, you can are deleted. If you want to delete only datasets with matching names, you can
use `RDF.Data.delete/2`. use `RDF.Data.delete/2`.
""" """
@spec delete(t, input | [input], Statement.coercible_graph_name() | boolean | nil) :: t @spec delete(t, input, keyword) :: t
def delete(dataset, statements, graph_context \\ false) def delete(dataset, statements, opts \\ [])
def delete(%__MODULE__{} = dataset, statements, graph_context) when is_list(statements) do def delete(dataset, {_, _, _, graph} = quad, opts),
with graph_context = graph_context && coerce_graph_name(graph_context) do do: do_delete(dataset, destination_graph(opts, graph), quad)
Enum.reduce(statements, dataset, fn statement, dataset ->
delete(dataset, statement, graph_context) def delete(dataset, %Graph{} = graph, opts),
end) do: do_delete(dataset, destination_graph(opts, graph.name), graph)
end
def delete(%__MODULE__{} = dataset, %__MODULE__{} = other_dataset, opts) do
other_dataset
|> graphs()
|> Enum.reduce(dataset, &delete(&2, &1, opts))
end end
def delete(%__MODULE__{} = dataset, {_, _, _} = statement, false), def delete(dataset, input, opts) when is_list(input) or is_map(input) do
do: do_delete(dataset, nil, statement) Enum.reduce(input, dataset, &delete(&2, &1, opts))
def delete(%__MODULE__{} = dataset, {_, _, _} = statement, graph_context),
do: do_delete(dataset, graph_context, statement)
def delete(%__MODULE__{} = dataset, {subject, predicate, objects, graph_context}, false),
do: do_delete(dataset, graph_context, {subject, predicate, objects})
def delete(%__MODULE__{} = dataset, {subject, predicate, objects, _}, graph_context),
do: do_delete(dataset, graph_context, {subject, predicate, objects})
def delete(%__MODULE__{} = dataset, %Description{} = description, false),
do: do_delete(dataset, nil, description)
def delete(%__MODULE__{} = dataset, %Description{} = description, graph_context),
do: do_delete(dataset, graph_context, description)
def delete(%__MODULE__{} = dataset, %RDF.Graph{name: name} = graph, false),
do: do_delete(dataset, name, graph)
def delete(%__MODULE__{} = dataset, %RDF.Graph{} = graph, graph_context),
do: do_delete(dataset, graph_context, graph)
def delete(%__MODULE__{} = dataset, %__MODULE__{graphs: graphs}, graph_context) do
Enum.reduce(graphs, dataset, fn {_, graph}, dataset ->
delete(dataset, graph, graph_context)
end)
end end
defp do_delete(%__MODULE__{name: name, graphs: graphs} = dataset, graph_context, statements) do def delete(dataset, input, opts), do: do_delete(dataset, destination_graph(opts), input)
with graph_context = coerce_graph_name(graph_context),
graph when not is_nil(graph) <- graphs[graph_context], defp do_delete(dataset, graph_name, input) do
new_graph = Graph.delete(graph, statements) do if existing_graph = dataset.graphs[graph_name] do
new_graph = Graph.delete(existing_graph, input)
%__MODULE__{ %__MODULE__{
name: name, dataset
graphs: | graphs:
if Enum.empty?(new_graph) do if Enum.empty?(new_graph) do
Map.delete(graphs, graph_context) Map.delete(dataset.graphs, graph_name)
else else
Map.put(graphs, graph_context, new_graph) Map.put(dataset.graphs, graph_name, new_graph)
end end
} }
else else
nil -> dataset dataset
end end
end end
@ -695,7 +645,11 @@ defmodule RDF.Dataset do
end end
@doc """ @doc """
Returns if a given statement is in a `RDF.Dataset`. Checks if the given `input` statements exist within `dataset`.
The `graph` option allows to set a different destination graph in which the
statements should be checked, ignoring the graph context of given quads or the
name of given graphs.
## Examples ## Examples
@ -706,21 +660,34 @@ defmodule RDF.Dataset do
...> RDF.Dataset.include?(dataset, {EX.S1, EX.p1, EX.O1, EX.Graph}) ...> RDF.Dataset.include?(dataset, {EX.S1, EX.p1, EX.O1, EX.Graph})
true true
""" """
@spec include?(t, Statement.t(), Statement.coercible_graph_name() | nil) :: boolean @spec include?(t, input, keyword) :: boolean
def include?(dataset, statement, graph_context \\ nil) def include?(dataset, input, opts \\ [])
def include?(%__MODULE__{graphs: graphs}, triple = {_, _, _}, graph_context) do def include?(dataset, {_, _, _, graph} = quad, opts),
with graph_context = coerce_graph_name(graph_context) do do: do_include?(dataset, destination_graph(opts, graph), quad)
if graph = graphs[graph_context] do
Graph.include?(graph, triple) def include?(dataset, %Graph{} = graph, opts),
do: do_include?(dataset, destination_graph(opts, graph.name), graph)
def include?(%__MODULE__{} = dataset, %__MODULE__{} = other_dataset, opts) do
other_dataset
|> graphs()
|> Enum.all?(&include?(dataset, &1, opts))
end
def include?(dataset, input, opts) when is_list(input) or is_map(input) do
Enum.all?(input, &include?(dataset, &1, opts))
end
def include?(dataset, input, opts), do: do_include?(dataset, destination_graph(opts), input)
defp do_include?(%__MODULE__{graphs: graphs}, graph_name, input) do
if graph = graphs[graph_name] do
Graph.include?(graph, input)
else else
false false
end end
end end
end
def include?(%__MODULE__{} = dataset, {subject, predicate, object, graph_context}, _),
do: include?(dataset, {subject, predicate, object}, graph_context)
@doc """ @doc """
Checks if a graph of a `RDF.Dataset` contains statements about the given resource. Checks if a graph of a `RDF.Dataset` contains statements about the given resource.

View file

@ -10,14 +10,14 @@ defmodule RDF.Description do
- the `RDF.Data` protocol - the `RDF.Data` protocol
""" """
@enforce_keys [:subject]
defstruct subject: nil, predications: %{}
@behaviour Access @behaviour Access
import RDF.Statement import RDF.Statement
alias RDF.{Statement, Triple} alias RDF.{Statement, Triple}
@enforce_keys [:subject]
defstruct subject: nil, predications: %{}
@type t :: %__MODULE__{ @type t :: %__MODULE__{
subject: Statement.subject(), subject: Statement.subject(),
predications: predications predications: predications
@ -128,11 +128,8 @@ defmodule RDF.Description do
} }
end end
def add(description, predications, opts) def add(description, input, opts) when is_map(input) or is_list(input) do
when is_map(predications) or is_list(predications) do Enum.reduce(input, description, &add(&2, &1, opts))
Enum.reduce(predications, description, fn
predications, description -> add(description, predications, opts)
end)
end end
def add(%__MODULE__{} = description, {subject, predicate, objects}, opts) do def add(%__MODULE__{} = description, {subject, predicate, objects}, opts) do
@ -250,11 +247,8 @@ defmodule RDF.Description do
} }
end end
def delete(description, predications, opts) def delete(description, input, opts) when is_map(input) or is_list(input) do
when is_map(predications) or is_list(predications) do Enum.reduce(input, description, &delete(&2, &1, opts))
Enum.reduce(predications, description, fn
predications, description -> delete(description, predications, opts)
end)
end end
def delete(%__MODULE__{} = description, {subject, predicate, objects}, opts) do def delete(%__MODULE__{} = description, {subject, predicate, objects}, opts) do
@ -603,7 +597,7 @@ defmodule RDF.Description do
end end
@doc """ @doc """
Checks if the given statement exists within a `RDF.Description`. Checks if the given `input` statements exist within `description`.
""" """
@spec include?(t, input) :: boolean @spec include?(t, input) :: boolean
def include?(description, input) def include?(description, input)

View file

@ -11,14 +11,14 @@ defmodule RDF.Graph do
""" """
defstruct name: nil, descriptions: %{}, prefixes: nil, base_iri: nil
@behaviour Access @behaviour Access
import RDF.Statement import RDF.Statement
import RDF.Utils import RDF.Utils
alias RDF.{Description, IRI, PrefixMap, Statement} alias RDF.{Description, IRI, PrefixMap, Statement}
defstruct name: nil, descriptions: %{}, prefixes: nil, base_iri: nil
@type graph_description :: %{Statement.subject() => Description.t()} @type graph_description :: %{Statement.subject() => Description.t()}
@type t :: %__MODULE__{ @type t :: %__MODULE__{
@ -175,15 +175,15 @@ defmodule RDF.Graph do
@spec add(t, input) :: t @spec add(t, input) :: t
def add(graph, input) def add(graph, input)
def add(graph, {subject, predications}),
do: do_add(graph, coerce_subject(subject), predications)
def add(%__MODULE__{} = graph, {subject, _, _} = triple), def add(%__MODULE__{} = graph, {subject, _, _} = triple),
do: do_add(graph, coerce_subject(subject), triple) do: do_add(graph, coerce_subject(subject), triple)
def add(graph, {subject, predicate, object, _}), def add(graph, {subject, predicate, object, _}),
do: add(graph, {subject, predicate, object}) do: add(graph, {subject, predicate, object})
def add(graph, {subject, predications}),
do: do_add(graph, coerce_subject(subject), predications)
def add(%__MODULE__{} = graph, %Description{subject: subject} = description) do def add(%__MODULE__{} = graph, %Description{subject: subject} = description) do
if Description.count(description) > 0 do if Description.count(description) > 0 do
do_add(graph, subject, description) do_add(graph, subject, description)
@ -205,10 +205,8 @@ defmodule RDF.Graph do
end end
end end
def add(graph, statements) when is_list(statements) or is_map(statements) do def add(graph, input) when is_list(input) or is_map(input) do
Enum.reduce(statements, graph, fn triple, graph -> Enum.reduce(input, graph, &add(&2, &1))
add(graph, triple)
end)
end end
defp do_add(%__MODULE__{descriptions: descriptions} = graph, subject, statements) do defp do_add(%__MODULE__{descriptions: descriptions} = graph, subject, statements) do
@ -241,13 +239,13 @@ defmodule RDF.Graph do
""" """
@spec put(t, input) :: t @spec put(t, input) :: t
def put(graph, statements) def put(graph, input)
def put(graph, {subject, predications}), def put(graph, {subject, predications}),
do: do_put(graph, coerce_subject(subject), predications) do: do_put(graph, coerce_subject(subject), predications)
def put(%__MODULE__{} = graph, {subject, _, _} = statement), def put(%__MODULE__{} = graph, {subject, _, _} = triple),
do: do_put(graph, coerce_subject(subject), statement) do: do_put(graph, coerce_subject(subject), triple)
def put(graph, {subject, predicate, object, _}), def put(graph, {subject, predicate, object, _}),
do: put(graph, {subject, predicate, object}) do: put(graph, {subject, predicate, object})
@ -268,17 +266,17 @@ defmodule RDF.Graph do
end end
end end
def put(%__MODULE__{} = graph, statements) when is_map(statements) do def put(%__MODULE__{} = graph, input) when is_map(input) do
Enum.reduce(statements, graph, fn {subject, predications}, graph -> Enum.reduce(input, graph, fn {subject, predications}, graph ->
put(graph, {subject, predications}) put(graph, {subject, predications})
end) end)
end end
def put(%__MODULE__{} = graph, statements) when is_list(statements) do def put(%__MODULE__{} = graph, input) when is_list(input) do
put( put(
graph, graph,
Enum.group_by( Enum.group_by(
statements, input,
fn fn
{subject, _} -> subject {subject, _} -> subject
{subject, _, _} -> subject {subject, _, _} -> subject
@ -318,7 +316,7 @@ defmodule RDF.Graph do
use `RDF.Data.delete/2`. use `RDF.Data.delete/2`.
""" """
@spec delete(t, input | [input]) :: t @spec delete(t, input) :: t
def delete(graph, triples) def delete(graph, triples)
def delete(%__MODULE__{} = graph, {subject, _, _} = triple), def delete(%__MODULE__{} = graph, {subject, _, _} = triple),
@ -330,10 +328,8 @@ defmodule RDF.Graph do
def delete(graph, {subject, predicate, object, _}), def delete(graph, {subject, predicate, object, _}),
do: delete(graph, {subject, predicate, object}) do: delete(graph, {subject, predicate, object})
def delete(%__MODULE__{} = graph, statements) when is_list(statements) or is_map(statements) do def delete(%__MODULE__{} = graph, input) when is_list(input) or is_map(input) do
Enum.reduce(statements, graph, fn statement, graph -> Enum.reduce(input, graph, &delete(&2, &1))
delete(graph, statement)
end)
end end
def delete(%__MODULE__{} = graph, %Description{subject: subject} = description), def delete(%__MODULE__{} = graph, %Description{subject: subject} = description),
@ -345,9 +341,9 @@ defmodule RDF.Graph do
end) end)
end end
defp do_delete(%__MODULE__{descriptions: descriptions} = graph, subject, statements) do defp do_delete(%__MODULE__{descriptions: descriptions} = graph, subject, input) do
if description = descriptions[subject] do if description = descriptions[subject] do
new_description = Description.delete(description, statements) new_description = Description.delete(description, input)
%__MODULE__{ %__MODULE__{
graph graph
@ -373,9 +369,7 @@ defmodule RDF.Graph do
def delete_subjects(graph, subjects) def delete_subjects(graph, subjects)
def delete_subjects(%__MODULE__{} = graph, subjects) when is_list(subjects) do def delete_subjects(%__MODULE__{} = graph, subjects) when is_list(subjects) do
Enum.reduce(subjects, graph, fn subject, graph -> Enum.reduce(subjects, graph, &delete_subjects(&2, &1))
delete_subjects(graph, subject)
end)
end end
def delete_subjects(%__MODULE__{descriptions: descriptions} = graph, subject) do def delete_subjects(%__MODULE__{descriptions: descriptions} = graph, subject) do
@ -762,9 +756,11 @@ defmodule RDF.Graph do
defdelegate statements(graph), to: RDF.Graph, as: :triples defdelegate statements(graph), to: RDF.Graph, as: :triples
@doc """ @doc """
Checks if the given statement exists within a `RDF.Graph`. Checks if the given `input` statements exist within `graph`.
""" """
@spec include?(t, input) :: boolean @spec include?(t, input) :: boolean
def include?(graph, input)
def include?(%__MODULE__{} = graph, {subject, _, _} = triple), def include?(%__MODULE__{} = graph, {subject, _, _} = triple),
do: do_include?(graph, coerce_subject(subject), triple) do: do_include?(graph, coerce_subject(subject), triple)
@ -787,9 +783,9 @@ defmodule RDF.Graph do
Enum.all?(statements, &include?(graph, &1)) Enum.all?(statements, &include?(graph, &1))
end end
defp do_include?(%__MODULE__{descriptions: descriptions}, subject, statements) do defp do_include?(%__MODULE__{descriptions: descriptions}, subject, input) do
if description = descriptions[subject] do if description = descriptions[subject] do
Description.include?(description, statements) Description.include?(description, input)
else else
false false
end end

View file

@ -152,7 +152,7 @@ defmodule RDF.DatasetTest do
end end
end end
describe "add" do describe "add/3" do
test "a proper triple is added to the default graph" do test "a proper triple is added to the default graph" do
assert Dataset.add(dataset(), {iri(EX.Subject), EX.predicate(), iri(EX.Object)}) assert Dataset.add(dataset(), {iri(EX.Subject), EX.predicate(), iri(EX.Object)})
|> dataset_includes_statement?({EX.Subject, EX.predicate(), EX.Object}) |> dataset_includes_statement?({EX.Subject, EX.predicate(), EX.Object})
@ -191,10 +191,12 @@ defmodule RDF.DatasetTest do
end end
test "a quad and an overwriting graph context" do test "a quad and an overwriting graph context" do
assert Dataset.add(dataset(), {EX.Subject, EX.predicate(), EX.Object, EX.Graph}, EX.Other) assert Dataset.add(dataset(), {EX.Subject, EX.predicate(), EX.Object, EX.Graph},
graph: EX.Other
)
|> dataset_includes_statement?({EX.Subject, EX.predicate(), EX.Object, EX.Other}) |> dataset_includes_statement?({EX.Subject, EX.predicate(), EX.Object, EX.Other})
assert Dataset.add(dataset(), {EX.Subject, EX.predicate(), EX.Object, EX.Graph}, nil) assert Dataset.add(dataset(), {EX.Subject, EX.predicate(), EX.Object, EX.Graph}, graph: nil)
|> dataset_includes_statement?({EX.Subject, EX.predicate(), EX.Object}) |> dataset_includes_statement?({EX.Subject, EX.predicate(), EX.Object})
end end
@ -220,7 +222,7 @@ defmodule RDF.DatasetTest do
) )
end end
test "a list of triples without specification of the default context" do test "a list of triples without an overwriting graph context" do
ds = ds =
Dataset.add(dataset(), [ Dataset.add(dataset(), [
{EX.Subject1, EX.predicate1(), EX.Object1}, {EX.Subject1, EX.predicate1(), EX.Object1},
@ -233,7 +235,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject3, EX.predicate3(), EX.Object3}) assert dataset_includes_statement?(ds, {EX.Subject3, EX.predicate3(), EX.Object3})
end end
test "a list of triples with specification of the default context" do test "a list of triples with an overwriting graph context" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -242,7 +244,7 @@ defmodule RDF.DatasetTest do
{EX.Subject1, EX.predicate2(), EX.Object2}, {EX.Subject1, EX.predicate2(), EX.Object2},
{EX.Subject3, EX.predicate3(), EX.Object3} {EX.Subject3, EX.predicate3(), EX.Object3}
], ],
EX.Graph graph: EX.Graph
) )
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1, EX.Graph})
@ -257,7 +259,7 @@ defmodule RDF.DatasetTest do
{EX.Subject1, EX.predicate2(), EX.Object2}, {EX.Subject1, EX.predicate2(), EX.Object2},
{EX.Subject3, EX.predicate3(), EX.Object3} {EX.Subject3, EX.predicate3(), EX.Object3}
], ],
nil graph: nil
) )
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1, nil}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1, nil})
@ -265,7 +267,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject3, EX.predicate3(), EX.Object3, nil}) assert dataset_includes_statement?(ds, {EX.Subject3, EX.predicate3(), EX.Object3, nil})
end end
test "a list of quads without specification of the default context" do test "a list of quads without an overwriting graph context" do
ds = ds =
Dataset.add(dataset(), [ Dataset.add(dataset(), [
{EX.Subject, EX.predicate1(), EX.Object1, EX.Graph1}, {EX.Subject, EX.predicate1(), EX.Object1, EX.Graph1},
@ -278,7 +280,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate1(), EX.Object1, EX.Graph2}) assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate1(), EX.Object1, EX.Graph2})
end end
test "a list of quads with specification of the default context" do test "a list of quads with an overwriting graph context" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -287,7 +289,7 @@ defmodule RDF.DatasetTest do
{EX.Subject, EX.predicate2(), EX.Object2, nil}, {EX.Subject, EX.predicate2(), EX.Object2, nil},
{EX.Subject, EX.predicate1(), EX.Object1, EX.Graph2} {EX.Subject, EX.predicate1(), EX.Object1, EX.Graph2}
], ],
EX.Graph graph: EX.Graph
) )
assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate1(), EX.Object1, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate1(), EX.Object1, EX.Graph})
@ -302,7 +304,7 @@ defmodule RDF.DatasetTest do
{EX.Subject, EX.predicate2(), EX.Object2, nil}, {EX.Subject, EX.predicate2(), EX.Object2, nil},
{EX.Subject, EX.predicate1(), EX.Object1, EX.Graph2} {EX.Subject, EX.predicate1(), EX.Object1, EX.Graph2}
], ],
nil graph: nil
) )
assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate1(), EX.Object1, nil}) assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate1(), EX.Object1, nil})
@ -313,19 +315,77 @@ defmodule RDF.DatasetTest do
test "a list of mixed triples and quads" do test "a list of mixed triples and quads" do
ds = ds =
Dataset.add(dataset(), [ Dataset.add(dataset(), [
{EX.Subject1, EX.predicate1(), EX.Object1, EX.GraphName}, {EX.S1, EX.p1(), EX.O1, EX.Graph},
{EX.Subject3, EX.predicate3(), EX.Object3} {EX.S3, EX.p3(), EX.O3}
]) ])
assert dataset_includes_statement?( assert dataset_includes_statement?(ds, {EX.S1, EX.p1(), EX.O1, EX.Graph})
ds, assert dataset_includes_statement?(ds, {EX.S3, EX.p3(), EX.O3, nil})
{EX.Subject1, EX.predicate1(), EX.Object1, EX.GraphName}
ds =
Dataset.add(
dataset(),
[
{EX.S1, EX.p1(), EX.O1, EX.Graph},
{EX.S3, EX.p3(), EX.O3}
],
graph: EX.Graph2
) )
assert dataset_includes_statement?(ds, {EX.S1, EX.p1(), EX.O1, EX.Graph2})
assert dataset_includes_statement?(ds, {EX.S3, EX.p3(), EX.O3, EX.Graph2})
end
test "a map without an overwriting graph context" do
ds =
Dataset.add(dataset(), %{
EX.Subject1 => %{
EX.predicate1() => EX.Object1,
EX.predicate2() => EX.Object2
},
EX.Subject3 => {EX.predicate3(), EX.Object3}
})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2})
assert dataset_includes_statement?(ds, {EX.Subject3, EX.predicate3(), EX.Object3})
end
test "a map with an overwriting graph context" do
ds =
Dataset.add(
dataset(),
%{
EX.Subject1 => [
{EX.predicate2(), EX.Object2},
{EX.predicate1(), EX.Object1}
],
EX.Subject3 => %{EX.predicate3() => EX.Object3}
},
graph: EX.Graph
)
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1, EX.Graph})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2, EX.Graph})
assert dataset_includes_statement?(ds, {EX.Subject3, EX.predicate3(), EX.Object3, EX.Graph})
ds =
Dataset.add(
dataset(),
[
{EX.Subject1, EX.predicate1(), EX.Object1},
{EX.Subject1, EX.predicate2(), EX.Object2},
{EX.Subject3, EX.predicate3(), EX.Object3}
],
graph: nil
)
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1, nil})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2, nil})
assert dataset_includes_statement?(ds, {EX.Subject3, EX.predicate3(), EX.Object3, nil}) assert dataset_includes_statement?(ds, {EX.Subject3, EX.predicate3(), EX.Object3, nil})
end end
test "a Description without specification of the default context" do test "a description without an overwriting graph context" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -340,7 +400,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2})
end end
test "a Description with specification of the default context" do test "a description with an overwriting graph context" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -350,7 +410,7 @@ defmodule RDF.DatasetTest do
{EX.predicate2(), EX.Object2} {EX.predicate2(), EX.Object2}
] ]
), ),
nil graph: nil
) )
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1})
@ -360,7 +420,7 @@ defmodule RDF.DatasetTest do
Dataset.add( Dataset.add(
ds, ds,
Description.new(EX.Subject1, init: {EX.predicate3(), EX.Object3}), Description.new(EX.Subject1, init: {EX.predicate3(), EX.Object3}),
EX.Graph graph: EX.Graph
) )
assert Enum.count(ds) == 3 assert Enum.count(ds) == 3
@ -369,7 +429,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate3(), EX.Object3, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate3(), EX.Object3, EX.Graph})
end end
test "an unnamed Graph without specification of the default context" do test "an unnamed graph without an overwriting graph context" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -391,7 +451,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object3}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object3})
end end
test "an unnamed Graph with specification of the default context" do test "an unnamed graph with an overwriting graph context" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -399,21 +459,21 @@ defmodule RDF.DatasetTest do
{EX.Subject1, EX.predicate1(), EX.Object1}, {EX.Subject1, EX.predicate1(), EX.Object1},
{EX.Subject1, EX.predicate2(), EX.Object2} {EX.Subject1, EX.predicate2(), EX.Object2}
]), ]),
nil graph: nil
) )
assert unnamed_graph?(Dataset.default_graph(ds)) assert unnamed_graph?(Dataset.default_graph(ds))
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2})
ds = Dataset.add(ds, Graph.new({EX.Subject1, EX.predicate2(), EX.Object3}), nil) ds = Dataset.add(ds, Graph.new({EX.Subject1, EX.predicate2(), EX.Object3}), graph: nil)
assert unnamed_graph?(Dataset.default_graph(ds)) assert unnamed_graph?(Dataset.default_graph(ds))
assert Enum.count(ds) == 3 assert Enum.count(ds) == 3
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object2})
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object3}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object3})
ds = Dataset.add(ds, Graph.new({EX.Subject1, EX.predicate2(), EX.Object3}), EX.Graph) ds = Dataset.add(ds, Graph.new({EX.Subject1, EX.predicate2(), EX.Object3}), graph: EX.Graph)
assert unnamed_graph?(Dataset.default_graph(ds)) assert unnamed_graph?(Dataset.default_graph(ds))
assert named_graph?(Dataset.graph(ds, EX.Graph), iri(EX.Graph)) assert named_graph?(Dataset.graph(ds, EX.Graph), iri(EX.Graph))
assert Enum.count(ds) == 4 assert Enum.count(ds) == 4
@ -423,7 +483,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object3, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object3, EX.Graph})
end end
test "a named Graph without specification of the default context" do test "a named graph without an overwriting graph context" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -472,7 +532,7 @@ defmodule RDF.DatasetTest do
) )
end end
test "a named Graph with specification of the default context" do test "a named graph with an overwriting graph context" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -483,7 +543,7 @@ defmodule RDF.DatasetTest do
], ],
name: EX.Graph1 name: EX.Graph1
), ),
nil graph: nil
) )
refute Dataset.graph(ds, EX.Graph1) refute Dataset.graph(ds, EX.Graph1)
@ -495,7 +555,7 @@ defmodule RDF.DatasetTest do
Dataset.add( Dataset.add(
ds, ds,
Graph.new({EX.Subject1, EX.predicate2(), EX.Object3}, name: EX.Graph2), Graph.new({EX.Subject1, EX.predicate2(), EX.Object3}, name: EX.Graph2),
nil graph: nil
) )
refute Dataset.graph(ds, EX.Graph2) refute Dataset.graph(ds, EX.Graph2)
@ -509,7 +569,7 @@ defmodule RDF.DatasetTest do
Dataset.add( Dataset.add(
ds, ds,
Graph.new({EX.Subject1, EX.predicate2(), EX.Object3}, name: EX.Graph3), Graph.new({EX.Subject1, EX.predicate2(), EX.Object3}, name: EX.Graph3),
EX.Graph graph: EX.Graph
) )
assert named_graph?(Dataset.graph(ds, EX.Graph), iri(EX.Graph)) assert named_graph?(Dataset.graph(ds, EX.Graph), iri(EX.Graph))
@ -520,7 +580,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object3, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object3, EX.Graph})
end end
test "an unnamed Dataset" do test "an unnamed dataset" do
ds = ds =
Dataset.add( Dataset.add(
dataset(), dataset(),
@ -536,7 +596,10 @@ defmodule RDF.DatasetTest do
ds = Dataset.add(ds, Dataset.new({EX.Subject1, EX.predicate2(), EX.Object3})) ds = Dataset.add(ds, Dataset.new({EX.Subject1, EX.predicate2(), EX.Object3}))
ds = Dataset.add(ds, Dataset.new({EX.Subject1, EX.predicate2(), EX.Object3, EX.Graph})) ds = Dataset.add(ds, Dataset.new({EX.Subject1, EX.predicate2(), EX.Object3, EX.Graph}))
ds = Dataset.add(ds, Dataset.new({EX.Subject1, EX.predicate2(), EX.Object4}), EX.Graph)
ds =
Dataset.add(ds, Dataset.new({EX.Subject1, EX.predicate2(), EX.Object4}), graph: EX.Graph)
assert ds.name == nil assert ds.name == nil
assert Enum.count(ds) == 5 assert Enum.count(ds) == 5
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1})
@ -546,7 +609,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object4, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object4, EX.Graph})
end end
test "a named Dataset" do test "a named dataset" do
ds = ds =
Dataset.add( Dataset.add(
named_dataset(), named_dataset(),
@ -575,7 +638,7 @@ defmodule RDF.DatasetTest do
Dataset.add( Dataset.add(
ds, ds,
Dataset.new({EX.Subject1, EX.predicate2(), EX.Object4}, name: EX.DS2), Dataset.new({EX.Subject1, EX.predicate2(), EX.Object4}, name: EX.DS2),
EX.Graph graph: EX.Graph
) )
assert ds.name == iri(EX.DatasetName) assert ds.name == iri(EX.DatasetName)
@ -587,7 +650,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object4, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate2(), EX.Object4, EX.Graph})
end end
test "a list of Descriptions" do test "a list of descriptions" do
ds = ds =
Dataset.add(dataset(), [ Dataset.add(dataset(), [
Description.new(EX.Subject1, init: {EX.predicate1(), EX.Object1}), Description.new(EX.Subject1, init: {EX.predicate1(), EX.Object1}),
@ -607,7 +670,7 @@ defmodule RDF.DatasetTest do
Description.new(EX.Subject2, init: {EX.predicate2(), EX.Object2}), Description.new(EX.Subject2, init: {EX.predicate2(), EX.Object2}),
Description.new(EX.Subject1, init: {EX.predicate3(), EX.Object3}) Description.new(EX.Subject1, init: {EX.predicate3(), EX.Object3})
], ],
EX.Graph graph: EX.Graph
) )
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1, EX.Graph}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate1(), EX.Object1, EX.Graph})
@ -618,7 +681,7 @@ defmodule RDF.DatasetTest do
assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate3(), EX.Object3}) assert dataset_includes_statement?(ds, {EX.Subject1, EX.predicate3(), EX.Object3})
end end
test "a list of Graphs" do test "a list of graphs" do
ds = ds =
Dataset.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}]) Dataset.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}])
|> RDF.Dataset.add([ |> RDF.Dataset.add([
@ -653,6 +716,12 @@ defmodule RDF.DatasetTest do
assert_raise RDF.IRI.InvalidError, fn -> assert_raise RDF.IRI.InvalidError, fn ->
Dataset.add(dataset(), {iri(EX.Subject), EX.predicate(), iri(EX.Object), "not a IRI"}) Dataset.add(dataset(), {iri(EX.Subject), EX.predicate(), iri(EX.Object), "not a IRI"})
end end
assert_raise RDF.IRI.InvalidError, fn ->
Dataset.add(dataset(), {iri(EX.Subject), EX.predicate(), iri(EX.Object)},
graph: "not a IRI"
)
end
end end
end end
@ -815,7 +884,7 @@ defmodule RDF.DatasetTest do
{EX.S3, EX.p3(), [~B<foo>, ~L"bar"], EX.Graph2} {EX.S3, EX.p3(), [~B<foo>, ~L"bar"], EX.Graph2}
]) ])
assert Dataset.delete(dataset3, {EX.S2, EX.p2(), [EX.O1, EX.O2]}, EX.Graph1) == assert Dataset.delete(dataset3, {EX.S2, EX.p2(), [EX.O1, EX.O2]}, graph: EX.Graph1) ==
Dataset.new([ Dataset.new([
{EX.S1, EX.p1(), EX.O1}, {EX.S1, EX.p1(), EX.O1},
{EX.S3, EX.p3(), [~B<foo>, ~L"bar"], EX.Graph2} {EX.S3, EX.p3(), [~B<foo>, ~L"bar"], EX.Graph2}
@ -837,22 +906,49 @@ defmodule RDF.DatasetTest do
]) == Dataset.new({EX.S3, EX.p3(), ~L"bar", EX.Graph2}) ]) == Dataset.new({EX.S3, EX.p3(), ~L"bar", EX.Graph2})
end end
test "multiple statements with a Description", test "multiple statements with a map",
%{dataset1: dataset1, dataset2: dataset2} do
assert Dataset.delete(dataset1, %{EX.S1 => {EX.p1(), EX.O1}}) ==
Dataset.new()
assert Dataset.delete(
dataset1,
%{EX.S1 => {EX.p1(), EX.O1}},
graph: EX.Graph
) ==
dataset1
assert Dataset.delete(
dataset2,
%{EX.S2 => %{EX.p2() => EX.O2}},
graph: EX.Graph
) ==
dataset1
assert Dataset.delete(dataset2, %{EX.S1 => %{EX.p1() => EX.O1}}) ==
Dataset.new({EX.S2, EX.p2(), EX.O2, EX.Graph})
end
test "multiple statements with a description",
%{dataset1: dataset1, dataset2: dataset2} do %{dataset1: dataset1, dataset2: dataset2} do
assert Dataset.delete(dataset1, Description.new(EX.S1, init: {EX.p1(), EX.O1})) == assert Dataset.delete(dataset1, Description.new(EX.S1, init: {EX.p1(), EX.O1})) ==
Dataset.new() Dataset.new()
assert Dataset.delete(dataset1, Description.new(EX.S1, init: {EX.p1(), EX.O1}), EX.Graph) == assert Dataset.delete(dataset1, Description.new(EX.S1, init: {EX.p1(), EX.O1}),
graph: EX.Graph
) ==
dataset1 dataset1
assert Dataset.delete(dataset2, Description.new(EX.S2, init: {EX.p2(), EX.O2}), EX.Graph) == assert Dataset.delete(dataset2, Description.new(EX.S2, init: {EX.p2(), EX.O2}),
graph: EX.Graph
) ==
dataset1 dataset1
assert Dataset.delete(dataset2, Description.new(EX.S1, init: {EX.p1(), EX.O1})) == assert Dataset.delete(dataset2, Description.new(EX.S1, init: {EX.p1(), EX.O1})) ==
Dataset.new({EX.S2, EX.p2(), EX.O2, EX.Graph}) Dataset.new({EX.S2, EX.p2(), EX.O2, EX.Graph})
end end
test "multiple statements with a Graph", test "multiple statements with a graph",
%{dataset1: dataset1, dataset2: dataset2, dataset3: dataset3} do %{dataset1: dataset1, dataset2: dataset2, dataset3: dataset3} do
assert Dataset.delete(dataset1, Graph.new({EX.S1, EX.p1(), EX.O1})) == Dataset.new() assert Dataset.delete(dataset1, Graph.new({EX.S1, EX.p1(), EX.O1})) == Dataset.new()
@ -865,8 +961,11 @@ defmodule RDF.DatasetTest do
assert Dataset.delete(dataset2, Graph.new({EX.S2, EX.p2(), EX.O2}, name: EX.Graph)) == assert Dataset.delete(dataset2, Graph.new({EX.S2, EX.p2(), EX.O2}, name: EX.Graph)) ==
dataset1 dataset1
assert Dataset.delete(dataset2, Graph.new({EX.S2, EX.p2(), EX.O2}), EX.Graph) == dataset1 assert Dataset.delete(dataset2, Graph.new({EX.S2, EX.p2(), EX.O2}), graph: EX.Graph) ==
assert Dataset.delete(dataset2, Graph.new({EX.S2, EX.p2(), EX.O2}), EX.Graph) == dataset1 dataset1
assert Dataset.delete(dataset2, Graph.new({EX.S2, EX.p2(), EX.O2}), graph: EX.Graph) ==
dataset1
assert Dataset.delete( assert Dataset.delete(
dataset3, dataset3,
@ -898,7 +997,7 @@ defmodule RDF.DatasetTest do
{EX.S3, EX.p3(), [~L"bar"], EX.Graph2} {EX.S3, EX.p3(), [~L"bar"], EX.Graph2}
]) ])
assert Dataset.delete(dataset3, Graph.new({EX.S3, EX.p3(), ~B<foo>}), EX.Graph2) == assert Dataset.delete(dataset3, Graph.new({EX.S3, EX.p3(), ~B<foo>}), graph: EX.Graph2) ==
Dataset.new([ Dataset.new([
{EX.S1, EX.p1(), EX.O1}, {EX.S1, EX.p1(), EX.O1},
{EX.S2, EX.p2(), [EX.O1, EX.O2], EX.Graph1}, {EX.S2, EX.p2(), [EX.O1, EX.O2], EX.Graph1},
@ -906,7 +1005,7 @@ defmodule RDF.DatasetTest do
]) ])
end end
test "multiple statements with a Dataset", test "multiple statements with a dataset",
%{dataset1: dataset1, dataset2: dataset2} do %{dataset1: dataset1, dataset2: dataset2} do
assert Dataset.delete(dataset1, dataset1) == Dataset.new() assert Dataset.delete(dataset1, dataset1) == Dataset.new()
assert Dataset.delete(dataset1, dataset2) == Dataset.new() assert Dataset.delete(dataset1, dataset2) == Dataset.new()
@ -978,6 +1077,38 @@ defmodule RDF.DatasetTest do
assert Enum.count(dataset.graphs) == 1 assert Enum.count(dataset.graphs) == 1
end end
test "include?/3" do
dataset =
Dataset.new([
{EX.S1, EX.p(), EX.O1},
{EX.S2, EX.p(), EX.O2, EX.Graph}
])
assert Dataset.include?(dataset, {EX.S1, EX.p(), EX.O1})
refute Dataset.include?(dataset, {EX.S2, EX.p(), EX.O2})
assert Dataset.include?(dataset, {EX.S2, EX.p(), EX.O2, EX.Graph})
assert Dataset.include?(dataset, {EX.S2, EX.p(), EX.O2}, graph: EX.Graph)
assert Dataset.include?(dataset, [{EX.S1, EX.p(), EX.O1}])
assert Dataset.include?(dataset, [{EX.S2, EX.p(), EX.O2}], graph: EX.Graph)
assert Dataset.include?(dataset, [
{EX.S1, EX.p(), EX.O1},
{EX.S2, EX.p(), EX.O2, EX.Graph}
])
refute Dataset.include?(dataset, [
{EX.S1, EX.p(), EX.O1},
{EX.S2, EX.p(), EX.O2}
])
assert Dataset.include?(dataset, EX.S1 |> EX.p(EX.O1))
refute Dataset.include?(dataset, EX.S2 |> EX.p(EX.O2))
assert Dataset.include?(dataset, EX.p(EX.S2, EX.O2), graph: EX.Graph)
assert Dataset.include?(dataset, Graph.new(EX.S1 |> EX.p(EX.O1)))
assert Dataset.include?(dataset, dataset)
end
test "values/1" do test "values/1" do
assert Dataset.new() |> Dataset.values() == %{} assert Dataset.new() |> Dataset.values() == %{}