Remove references to current module names by using __MODULE__
This commit is contained in:
parent
c459d8e7fa
commit
c880026224
5 changed files with 251 additions and 239 deletions
|
@ -32,7 +32,7 @@ defmodule RDF.BlankNode do
|
|||
def new(id)
|
||||
|
||||
def new(id) when is_binary(id),
|
||||
do: %RDF.BlankNode{id: id}
|
||||
do: %__MODULE__{id: id}
|
||||
|
||||
def new(id) when is_reference(id),
|
||||
do: id |> :erlang.ref_to_list |> to_string |> String.replace(~r/\<|\>/, "") |> new
|
||||
|
@ -49,7 +49,7 @@ defmodule RDF.BlankNode do
|
|||
@spec equal_value?(t, t) :: boolean | nil
|
||||
def equal_value?(left, right)
|
||||
|
||||
def equal_value?(%RDF.BlankNode{id: left}, %RDF.BlankNode{id: right}),
|
||||
def equal_value?(%__MODULE__{id: left}, %__MODULE__{id: right}),
|
||||
do: left == right
|
||||
|
||||
def equal_value?(_, _),
|
||||
|
|
|
@ -36,7 +36,7 @@ defmodule RDF.Dataset do
|
|||
Creates an empty unnamed `RDF.Dataset`.
|
||||
"""
|
||||
@spec new :: t
|
||||
def new, do: %RDF.Dataset{}
|
||||
def new, do: %__MODULE__{}
|
||||
|
||||
@doc """
|
||||
Creates an `RDF.Dataset`.
|
||||
|
@ -86,12 +86,12 @@ defmodule RDF.Dataset do
|
|||
@spec new(input | [input], keyword) :: t
|
||||
def new(data, options)
|
||||
|
||||
def new(%RDF.Dataset{} = graph, options) do
|
||||
%RDF.Dataset{graph | name: options |> Keyword.get(:name) |> coerce_graph_name()}
|
||||
def new(%__MODULE__{} = graph, options) do
|
||||
%__MODULE__{graph | name: options |> Keyword.get(:name) |> coerce_graph_name()}
|
||||
end
|
||||
|
||||
def new(data, options) do
|
||||
%RDF.Dataset{}
|
||||
%__MODULE__{}
|
||||
|> new(options)
|
||||
|> add(data)
|
||||
end
|
||||
|
@ -121,38 +121,38 @@ defmodule RDF.Dataset do
|
|||
def add(dataset, {subject, predicate, objects}, graph_context),
|
||||
do: add(dataset, {subject, predicate, objects, graph_context})
|
||||
|
||||
def add(%RDF.Dataset{name: name, graphs: graphs},
|
||||
def add(%__MODULE__{name: name, graphs: graphs},
|
||||
{subject, predicate, objects, graph_context}, false) do
|
||||
with graph_context = coerce_graph_name(graph_context) do
|
||||
updated_graphs =
|
||||
Map.update(graphs, graph_context,
|
||||
Graph.new({subject, predicate, objects}, name: graph_context),
|
||||
fn graph -> Graph.add(graph, {subject, predicate, objects}) end)
|
||||
%RDF.Dataset{name: name, graphs: updated_graphs}
|
||||
%__MODULE__{name: name, graphs: updated_graphs}
|
||||
end
|
||||
end
|
||||
|
||||
def add(%RDF.Dataset{} = dataset, {subject, predicate, objects, _}, graph_context),
|
||||
def add(%__MODULE__{} = dataset, {subject, predicate, objects, _}, graph_context),
|
||||
do: add(dataset, {subject, predicate, objects, graph_context}, false)
|
||||
|
||||
def add(%RDF.Dataset{} = dataset, %Description{} = description, false),
|
||||
def add(%__MODULE__{} = dataset, %Description{} = description, false),
|
||||
do: add(dataset, description, nil)
|
||||
|
||||
def add(%RDF.Dataset{name: name, graphs: graphs},
|
||||
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)
|
||||
%RDF.Dataset{
|
||||
%__MODULE__{
|
||||
name: name,
|
||||
graphs: Map.put(graphs, graph_context, updated_graph)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def add(%RDF.Dataset{name: name, graphs: graphs}, %Graph{} = graph, false) do
|
||||
%RDF.Dataset{name: name,
|
||||
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)
|
||||
|
@ -160,10 +160,10 @@ defmodule RDF.Dataset do
|
|||
}
|
||||
end
|
||||
|
||||
def add(%RDF.Dataset{} = dataset, %Graph{} = graph, graph_context),
|
||||
def add(%__MODULE__{} = dataset, %Graph{} = graph, graph_context),
|
||||
do: add(dataset, %Graph{graph | name: coerce_graph_name(graph_context)}, false)
|
||||
|
||||
def add(%RDF.Dataset{} = dataset, %RDF.Dataset{} = other_dataset, graph_context) do
|
||||
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)
|
||||
|
@ -189,13 +189,13 @@ defmodule RDF.Dataset do
|
|||
@spec put(t, input | [input], Statement.coercible_graph_name | boolean | nil) :: t
|
||||
def put(dataset, statements, graph_context \\ false)
|
||||
|
||||
def put(%RDF.Dataset{} = dataset, {subject, predicate, objects}, false),
|
||||
def put(%__MODULE__{} = dataset, {subject, predicate, objects}, false),
|
||||
do: put(dataset, {subject, predicate, objects, nil})
|
||||
|
||||
def put(%RDF.Dataset{} = dataset, {subject, predicate, objects}, graph_context),
|
||||
def put(%__MODULE__{} = dataset, {subject, predicate, objects}, graph_context),
|
||||
do: put(dataset, {subject, predicate, objects, graph_context})
|
||||
|
||||
def put(%RDF.Dataset{name: name, graphs: graphs},
|
||||
def put(%__MODULE__{name: name, graphs: graphs},
|
||||
{subject, predicate, objects, graph_context}, false) do
|
||||
with graph_context = coerce_graph_name(graph_context) do
|
||||
new_graph =
|
||||
|
@ -205,15 +205,15 @@ defmodule RDF.Dataset do
|
|||
nil ->
|
||||
Graph.new({subject, predicate, objects}, name: graph_context)
|
||||
end
|
||||
%RDF.Dataset{name: name,
|
||||
%__MODULE__{name: name,
|
||||
graphs: Map.put(graphs, graph_context, new_graph)}
|
||||
end
|
||||
end
|
||||
|
||||
def put(%RDF.Dataset{} = dataset, {subject, predicate, objects, _}, graph_context),
|
||||
def put(%__MODULE__{} = dataset, {subject, predicate, objects, _}, graph_context),
|
||||
do: put(dataset, {subject, predicate, objects, graph_context}, false)
|
||||
|
||||
def put(%RDF.Dataset{} = dataset, statements, false) when is_list(statements) do
|
||||
def put(%__MODULE__{} = dataset, statements, false) when is_list(statements) do
|
||||
do_put dataset, Enum.group_by(statements,
|
||||
fn
|
||||
{s, _, _} -> {s, nil}
|
||||
|
@ -226,7 +226,7 @@ defmodule RDF.Dataset do
|
|||
end)
|
||||
end
|
||||
|
||||
def put(%RDF.Dataset{} = dataset, statements, graph_context) when is_list(statements) do
|
||||
def put(%__MODULE__{} = dataset, statements, graph_context) when is_list(statements) do
|
||||
with graph_context = coerce_graph_name(graph_context) do
|
||||
do_put dataset, Enum.group_by(statements,
|
||||
fn
|
||||
|
@ -240,24 +240,24 @@ defmodule RDF.Dataset do
|
|||
end
|
||||
end
|
||||
|
||||
def put(%RDF.Dataset{} = dataset, %Description{} = description, false),
|
||||
def put(%__MODULE__{} = dataset, %Description{} = description, false),
|
||||
do: put(dataset, description, nil)
|
||||
|
||||
def put(%RDF.Dataset{name: name, graphs: graphs},
|
||||
def put(%__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.put(description)
|
||||
%RDF.Dataset{
|
||||
%__MODULE__{
|
||||
name: name,
|
||||
graphs: Map.put(graphs, graph_context, updated_graph)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def put(%RDF.Dataset{name: name, graphs: graphs}, %Graph{} = graph, false) do
|
||||
%RDF.Dataset{name: name,
|
||||
def put(%__MODULE__{name: name, graphs: graphs}, %Graph{} = graph, false) do
|
||||
%__MODULE__{name: name,
|
||||
graphs:
|
||||
Map.update(graphs, graph.name, graph, fn current ->
|
||||
Graph.put(current, graph)
|
||||
|
@ -265,10 +265,10 @@ defmodule RDF.Dataset do
|
|||
}
|
||||
end
|
||||
|
||||
def put(%RDF.Dataset{} = dataset, %Graph{} = graph, graph_context),
|
||||
def put(%__MODULE__{} = dataset, %Graph{} = graph, graph_context),
|
||||
do: put(dataset, %Graph{graph | name: coerce_graph_name(graph_context)}, false)
|
||||
|
||||
def put(%RDF.Dataset{} = dataset, %RDF.Dataset{} = other_dataset, graph_context) do
|
||||
def put(%__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) ->
|
||||
put(dataset, graph, graph_context)
|
||||
|
@ -276,21 +276,21 @@ defmodule RDF.Dataset do
|
|||
end
|
||||
end
|
||||
|
||||
defp do_put(%RDF.Dataset{} = dataset, statements) when is_map(statements) do
|
||||
defp do_put(%__MODULE__{} = dataset, statements) when is_map(statements) do
|
||||
Enum.reduce statements, dataset,
|
||||
fn ({subject_with_context, predications}, dataset) ->
|
||||
do_put(dataset, subject_with_context, predications)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_put(%RDF.Dataset{name: name, graphs: graphs},
|
||||
defp do_put(%__MODULE__{name: name, graphs: graphs},
|
||||
{subject, graph_context}, predications)
|
||||
when is_list(predications) do
|
||||
with graph_context = coerce_graph_name(graph_context) do
|
||||
graph = Map.get(graphs, graph_context, Graph.new(name: graph_context))
|
||||
new_graphs = graphs
|
||||
|> Map.put(graph_context, Graph.put(graph, subject, predications))
|
||||
%RDF.Dataset{name: name, graphs: new_graphs}
|
||||
%__MODULE__{name: name, graphs: new_graphs}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -310,7 +310,7 @@ defmodule RDF.Dataset do
|
|||
@spec delete(t, input | [input], Statement.coercible_graph_name | boolean | nil) :: t
|
||||
def delete(dataset, statements, graph_context \\ false)
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, statements, graph_context) when is_list(statements) do
|
||||
def delete(%__MODULE__{} = dataset, statements, graph_context) when is_list(statements) do
|
||||
with graph_context = graph_context && coerce_graph_name(graph_context) do
|
||||
Enum.reduce statements, dataset, fn (statement, dataset) ->
|
||||
delete(dataset, statement, graph_context)
|
||||
|
@ -318,43 +318,43 @@ defmodule RDF.Dataset do
|
|||
end
|
||||
end
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, {_, _, _} = statement, false),
|
||||
def delete(%__MODULE__{} = dataset, {_, _, _} = statement, false),
|
||||
do: do_delete(dataset, nil, statement)
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, {_, _, _} = statement, graph_context),
|
||||
def delete(%__MODULE__{} = dataset, {_, _, _} = statement, graph_context),
|
||||
do: do_delete(dataset, graph_context, statement)
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, {subject, predicate, objects, graph_context}, false),
|
||||
def delete(%__MODULE__{} = dataset, {subject, predicate, objects, graph_context}, false),
|
||||
do: do_delete(dataset, graph_context, {subject, predicate, objects})
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, {subject, predicate, objects, _}, graph_context),
|
||||
def delete(%__MODULE__{} = dataset, {subject, predicate, objects, _}, graph_context),
|
||||
do: do_delete(dataset, graph_context, {subject, predicate, objects})
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, %Description{} = description, false),
|
||||
def delete(%__MODULE__{} = dataset, %Description{} = description, false),
|
||||
do: do_delete(dataset, nil, description)
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, %Description{} = description, graph_context),
|
||||
def delete(%__MODULE__{} = dataset, %Description{} = description, graph_context),
|
||||
do: do_delete(dataset, graph_context, description)
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, %RDF.Graph{name: name} = graph, false),
|
||||
def delete(%__MODULE__{} = dataset, %RDF.Graph{name: name} = graph, false),
|
||||
do: do_delete(dataset, name, graph)
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, %RDF.Graph{} = graph, graph_context),
|
||||
def delete(%__MODULE__{} = dataset, %RDF.Graph{} = graph, graph_context),
|
||||
do: do_delete(dataset, graph_context, graph)
|
||||
|
||||
def delete(%RDF.Dataset{} = dataset, %RDF.Dataset{graphs: graphs}, graph_context) do
|
||||
def delete(%__MODULE__{} = dataset, %__MODULE__{graphs: graphs}, graph_context) do
|
||||
Enum.reduce graphs, dataset, fn ({_, graph}, dataset) ->
|
||||
delete(dataset, graph, graph_context)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_delete(%RDF.Dataset{name: name, graphs: graphs} = dataset,
|
||||
defp do_delete(%__MODULE__{name: name, graphs: graphs} = dataset,
|
||||
graph_context, statements) do
|
||||
with graph_context = coerce_graph_name(graph_context),
|
||||
graph when not is_nil(graph) <- graphs[graph_context],
|
||||
new_graph = Graph.delete(graph, statements)
|
||||
do
|
||||
%RDF.Dataset{name: name,
|
||||
%__MODULE__{name: name,
|
||||
graphs:
|
||||
if Enum.empty?(new_graph) do
|
||||
Map.delete(graphs, graph_context)
|
||||
|
@ -374,15 +374,15 @@ defmodule RDF.Dataset do
|
|||
@spec delete_graph(t, Statement.graph_name | [Statement.graph_name] | nil) :: t
|
||||
def delete_graph(graph, graph_names)
|
||||
|
||||
def delete_graph(%RDF.Dataset{} = dataset, graph_names) when is_list(graph_names) do
|
||||
def delete_graph(%__MODULE__{} = dataset, graph_names) when is_list(graph_names) do
|
||||
Enum.reduce graph_names, dataset, fn (graph_name, dataset) ->
|
||||
delete_graph(dataset, graph_name)
|
||||
end
|
||||
end
|
||||
|
||||
def delete_graph(%RDF.Dataset{name: name, graphs: graphs}, graph_name) do
|
||||
def delete_graph(%__MODULE__{name: name, graphs: graphs}, graph_name) do
|
||||
with graph_name = coerce_graph_name(graph_name) do
|
||||
%RDF.Dataset{name: name, graphs: Map.delete(graphs, graph_name)}
|
||||
%__MODULE__{name: name, graphs: Map.delete(graphs, graph_name)}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -390,7 +390,7 @@ defmodule RDF.Dataset do
|
|||
Deletes the default graph.
|
||||
"""
|
||||
@spec delete_default_graph(t) :: t
|
||||
def delete_default_graph(%RDF.Dataset{} = graph),
|
||||
def delete_default_graph(%__MODULE__{} = graph),
|
||||
do: delete_graph(graph, nil)
|
||||
|
||||
|
||||
|
@ -411,7 +411,7 @@ defmodule RDF.Dataset do
|
|||
"""
|
||||
@impl Access
|
||||
@spec fetch(t, Statement.graph_name | nil) :: {:ok, Graph.t} | :error
|
||||
def fetch(%RDF.Dataset{graphs: graphs}, graph_name) do
|
||||
def fetch(%__MODULE__{graphs: graphs}, graph_name) do
|
||||
Access.fetch(graphs, coerce_graph_name(graph_name))
|
||||
end
|
||||
|
||||
|
@ -434,7 +434,7 @@ defmodule RDF.Dataset do
|
|||
:bar
|
||||
"""
|
||||
@spec get(t, Statement.graph_name | nil, Graph.t | nil) :: Graph.t | nil
|
||||
def get(%RDF.Dataset{} = dataset, graph_name, default \\ nil) do
|
||||
def get(%__MODULE__{} = dataset, graph_name, default \\ nil) do
|
||||
case fetch(dataset, graph_name) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
|
@ -445,14 +445,14 @@ defmodule RDF.Dataset do
|
|||
The graph with given name.
|
||||
"""
|
||||
@spec graph(t, Statement.graph_name | nil) :: Graph.t
|
||||
def graph(%RDF.Dataset{graphs: graphs}, graph_name),
|
||||
def graph(%__MODULE__{graphs: graphs}, graph_name),
|
||||
do: Map.get(graphs, coerce_graph_name(graph_name))
|
||||
|
||||
@doc """
|
||||
The default graph of a `RDF.Dataset`.
|
||||
"""
|
||||
@spec default_graph(t) :: Graph.t
|
||||
def default_graph(%RDF.Dataset{graphs: graphs}),
|
||||
def default_graph(%__MODULE__{graphs: graphs}),
|
||||
do: Map.get(graphs, nil, Graph.new)
|
||||
|
||||
|
||||
|
@ -460,7 +460,7 @@ defmodule RDF.Dataset do
|
|||
The set of all graphs.
|
||||
"""
|
||||
@spec graphs(t) :: [Graph.t]
|
||||
def graphs(%RDF.Dataset{graphs: graphs}), do: Map.values(graphs)
|
||||
def graphs(%__MODULE__{graphs: graphs}), do: Map.values(graphs)
|
||||
|
||||
|
||||
@doc """
|
||||
|
@ -487,7 +487,7 @@ defmodule RDF.Dataset do
|
|||
"""
|
||||
@impl Access
|
||||
@spec get_and_update(t, Statement.graph_name | nil, update_graph_fun) :: {Graph.t, input}
|
||||
def get_and_update(%RDF.Dataset{} = dataset, graph_name, fun) do
|
||||
def get_and_update(%__MODULE__{} = dataset, graph_name, fun) do
|
||||
with graph_context = coerce_graph_name(graph_name) do
|
||||
case fun.(get(dataset, graph_context)) do
|
||||
{old_graph, new_graph} ->
|
||||
|
@ -507,10 +507,10 @@ defmodule RDF.Dataset do
|
|||
@spec pop(t) :: {Statement.t | nil, t}
|
||||
def pop(dataset)
|
||||
|
||||
def pop(%RDF.Dataset{graphs: graphs} = dataset)
|
||||
def pop(%__MODULE__{graphs: graphs} = dataset)
|
||||
when graphs == %{}, do: {nil, dataset}
|
||||
|
||||
def pop(%RDF.Dataset{name: name, graphs: graphs}) do
|
||||
def pop(%__MODULE__{name: name, graphs: graphs}) do
|
||||
# TODO: Find a faster way ...
|
||||
[{graph_name, graph}] = Enum.take(graphs, 1)
|
||||
{{s, p, o}, popped_graph} = Graph.pop(graph)
|
||||
|
@ -518,7 +518,7 @@ defmodule RDF.Dataset do
|
|||
do: graphs |> Map.delete(graph_name),
|
||||
else: graphs |> Map.put(graph_name, popped_graph)
|
||||
|
||||
{{s, p, o, graph_name}, %RDF.Dataset{name: name, graphs: popped}}
|
||||
{{s, p, o, graph_name}, %__MODULE__{name: name, graphs: popped}}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -539,12 +539,12 @@ defmodule RDF.Dataset do
|
|||
"""
|
||||
@impl Access
|
||||
@spec pop(t, Statement.coercible_graph_name) :: {Statement.t | nil, t}
|
||||
def pop(%RDF.Dataset{name: name, graphs: graphs} = dataset, graph_name) do
|
||||
def pop(%__MODULE__{name: name, graphs: graphs} = dataset, graph_name) do
|
||||
case Access.pop(graphs, coerce_graph_name(graph_name)) do
|
||||
{nil, _} ->
|
||||
{nil, dataset}
|
||||
{graph, new_graphs} ->
|
||||
{graph, %RDF.Dataset{name: name, graphs: new_graphs}}
|
||||
{graph, %__MODULE__{name: name, graphs: new_graphs}}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -563,7 +563,7 @@ defmodule RDF.Dataset do
|
|||
3
|
||||
"""
|
||||
@spec statement_count(t) :: non_neg_integer
|
||||
def statement_count(%RDF.Dataset{graphs: graphs}) do
|
||||
def statement_count(%__MODULE__{graphs: graphs}) do
|
||||
Enum.reduce graphs, 0, fn ({_, graph}, count) ->
|
||||
count + Graph.triple_count(graph)
|
||||
end
|
||||
|
@ -581,7 +581,7 @@ defmodule RDF.Dataset do
|
|||
...> RDF.Dataset.subjects
|
||||
MapSet.new([RDF.iri(EX.S1), RDF.iri(EX.S2)])
|
||||
"""
|
||||
def subjects(%RDF.Dataset{graphs: graphs}) do
|
||||
def subjects(%__MODULE__{graphs: graphs}) do
|
||||
Enum.reduce graphs, MapSet.new, fn ({_, graph}, subjects) ->
|
||||
MapSet.union(subjects, Graph.subjects(graph))
|
||||
end
|
||||
|
@ -599,7 +599,7 @@ defmodule RDF.Dataset do
|
|||
...> RDF.Dataset.predicates
|
||||
MapSet.new([EX.p1, EX.p2])
|
||||
"""
|
||||
def predicates(%RDF.Dataset{graphs: graphs}) do
|
||||
def predicates(%__MODULE__{graphs: graphs}) do
|
||||
Enum.reduce graphs, MapSet.new, fn ({_, graph}, predicates) ->
|
||||
MapSet.union(predicates, Graph.predicates(graph))
|
||||
end
|
||||
|
@ -621,7 +621,7 @@ defmodule RDF.Dataset do
|
|||
...> ]) |> RDF.Dataset.objects
|
||||
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode)])
|
||||
"""
|
||||
def objects(%RDF.Dataset{graphs: graphs}) do
|
||||
def objects(%__MODULE__{graphs: graphs}) do
|
||||
Enum.reduce graphs, MapSet.new, fn ({_, graph}, objects) ->
|
||||
MapSet.union(objects, Graph.objects(graph))
|
||||
end
|
||||
|
@ -641,7 +641,7 @@ defmodule RDF.Dataset do
|
|||
MapSet.new([RDF.iri(EX.S1), RDF.iri(EX.S2), RDF.iri(EX.S3),
|
||||
RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode), EX.p1, EX.p2])
|
||||
"""
|
||||
def resources(%RDF.Dataset{graphs: graphs}) do
|
||||
def resources(%__MODULE__{graphs: graphs}) do
|
||||
Enum.reduce graphs, MapSet.new, fn ({_, graph}, resources) ->
|
||||
MapSet.union(resources, Graph.resources(graph))
|
||||
end
|
||||
|
@ -662,7 +662,7 @@ defmodule RDF.Dataset do
|
|||
{RDF.iri(EX.S2), RDF.iri(EX.p2), RDF.iri(EX.O2)}]
|
||||
"""
|
||||
@spec statements(t) :: [Statement.t]
|
||||
def statements(%RDF.Dataset{graphs: graphs}) do
|
||||
def statements(%__MODULE__{graphs: graphs}) do
|
||||
Enum.reduce graphs, [], fn ({_, graph}, all_statements) ->
|
||||
statements = Graph.triples(graph)
|
||||
if graph.name do
|
||||
|
@ -689,7 +689,7 @@ defmodule RDF.Dataset do
|
|||
@spec include?(t, Statement.t, Statement.coercible_graph_name | nil) :: boolean
|
||||
def include?(dataset, statement, graph_context \\ nil)
|
||||
|
||||
def include?(%RDF.Dataset{graphs: graphs}, triple = {_, _, _}, graph_context) do
|
||||
def include?(%__MODULE__{graphs: graphs}, triple = {_, _, _}, graph_context) do
|
||||
with graph_context = coerce_graph_name(graph_context) do
|
||||
if graph = graphs[graph_context] do
|
||||
Graph.include?(graph, triple)
|
||||
|
@ -699,7 +699,7 @@ defmodule RDF.Dataset do
|
|||
end
|
||||
end
|
||||
|
||||
def include?(%RDF.Dataset{} = dataset, {subject, predicate, object, graph_context}, _),
|
||||
def include?(%__MODULE__{} = dataset, {subject, predicate, object, graph_context}, _),
|
||||
do: include?(dataset, {subject, predicate, object}, graph_context)
|
||||
|
||||
|
||||
|
@ -714,7 +714,7 @@ defmodule RDF.Dataset do
|
|||
false
|
||||
"""
|
||||
@spec describes?(t, Statement.t, Statement.coercible_graph_name | nil) :: boolean
|
||||
def describes?(%RDF.Dataset{graphs: graphs}, subject, graph_context \\ nil) do
|
||||
def describes?(%__MODULE__{graphs: graphs}, subject, graph_context \\ nil) do
|
||||
with graph_context = coerce_graph_name(graph_context) do
|
||||
if graph = graphs[graph_context] do
|
||||
Graph.describes?(graph, subject)
|
||||
|
@ -737,7 +737,7 @@ defmodule RDF.Dataset do
|
|||
[nil, RDF.iri(EX.Graph1)]
|
||||
"""
|
||||
@spec who_describes(t, Statement.coercible_subject) :: [Graph.t]
|
||||
def who_describes(%RDF.Dataset{graphs: graphs}, subject) do
|
||||
def who_describes(%__MODULE__{graphs: graphs}, subject) do
|
||||
with subject = coerce_subject(subject) do
|
||||
graphs
|
||||
|> Map.values
|
||||
|
@ -802,7 +802,7 @@ defmodule RDF.Dataset do
|
|||
@spec values(t, Statement.term_mapping) :: map
|
||||
def values(dataset, mapping \\ &RDF.Statement.default_term_mapping/1)
|
||||
|
||||
def values(%RDF.Dataset{graphs: graphs}, mapping) do
|
||||
def values(%__MODULE__{graphs: graphs}, mapping) do
|
||||
Map.new graphs, fn {graph_name, graph} ->
|
||||
{mapping.({:graph_name, graph_name}), Graph.values(graph, mapping)}
|
||||
end
|
||||
|
@ -818,14 +818,14 @@ defmodule RDF.Dataset do
|
|||
@spec equal?(t | any, t | any) :: boolean
|
||||
def equal?(dataset1, dataset2)
|
||||
|
||||
def equal?(%RDF.Dataset{} = dataset1, %RDF.Dataset{} = dataset2) do
|
||||
def equal?(%__MODULE__{} = dataset1, %__MODULE__{} = dataset2) do
|
||||
clear_metadata(dataset1) == clear_metadata(dataset2)
|
||||
end
|
||||
|
||||
def equal?(_, _), do: false
|
||||
|
||||
defp clear_metadata(%RDF.Dataset{graphs: graphs} = dataset) do
|
||||
%RDF.Dataset{dataset |
|
||||
defp clear_metadata(%__MODULE__{graphs: graphs} = dataset) do
|
||||
%__MODULE__{dataset |
|
||||
graphs:
|
||||
Map.new(graphs, fn {name, graph} ->
|
||||
{name, RDF.Graph.clear_metadata(graph)}
|
||||
|
@ -835,31 +835,35 @@ defmodule RDF.Dataset do
|
|||
|
||||
|
||||
defimpl Enumerable do
|
||||
def member?(dataset, statement), do: {:ok, RDF.Dataset.include?(dataset, statement)}
|
||||
def count(dataset), do: {:ok, RDF.Dataset.statement_count(dataset)}
|
||||
alias RDF.Dataset
|
||||
|
||||
def member?(dataset, statement), do: {:ok, Dataset.include?(dataset, statement)}
|
||||
def count(dataset), do: {:ok, Dataset.statement_count(dataset)}
|
||||
def slice(_dataset), do: {:error, __MODULE__}
|
||||
|
||||
def reduce(%RDF.Dataset{graphs: graphs}, {:cont, acc}, _fun)
|
||||
def reduce(%Dataset{graphs: graphs}, {:cont, acc}, _fun)
|
||||
when map_size(graphs) == 0, do: {:done, acc}
|
||||
|
||||
def reduce(%RDF.Dataset{} = dataset, {:cont, acc}, fun) do
|
||||
{statement, rest} = RDF.Dataset.pop(dataset)
|
||||
def reduce(%Dataset{} = dataset, {:cont, acc}, fun) do
|
||||
{statement, rest} = Dataset.pop(dataset)
|
||||
reduce(rest, fun.(statement, acc), fun)
|
||||
end
|
||||
|
||||
def reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
|
||||
def reduce(dataset = %RDF.Dataset{}, {:suspend, acc}, fun) do
|
||||
def reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
|
||||
def reduce(dataset = %Dataset{}, {:suspend, acc}, fun) do
|
||||
{:suspended, acc, &reduce(dataset, &1, fun)}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
defimpl Collectable do
|
||||
alias RDF.Dataset
|
||||
|
||||
def into(original) do
|
||||
collector_fun = fn
|
||||
dataset, {:cont, list} when is_list(list)
|
||||
-> RDF.Dataset.add(dataset, List.to_tuple(list))
|
||||
dataset, {:cont, elem} -> RDF.Dataset.add(dataset, elem)
|
||||
-> Dataset.add(dataset, List.to_tuple(list))
|
||||
dataset, {:cont, elem} -> Dataset.add(dataset, elem)
|
||||
dataset, :done -> dataset
|
||||
_dataset, :halt -> :ok
|
||||
end
|
||||
|
|
|
@ -58,8 +58,8 @@ defmodule RDF.Description do
|
|||
do: new(subject) |> add(predicate, objects)
|
||||
def new(subject, statements) when is_list(statements),
|
||||
do: new(subject) |> add(statements)
|
||||
def new(subject, %RDF.Description{predications: predications}),
|
||||
do: %RDF.Description{new(subject) | predications: predications}
|
||||
def new(subject, %__MODULE__{predications: predications}),
|
||||
do: %__MODULE__{new(subject) | predications: predications}
|
||||
def new(subject, predications = %{}),
|
||||
do: new(subject) |> add(predications)
|
||||
|
||||
|
@ -71,7 +71,7 @@ defmodule RDF.Description do
|
|||
Statement.coercible_predicate,
|
||||
Statement.coercible_object | [Statement.coercible_object]
|
||||
) :: t
|
||||
def new(%RDF.Description{} = description, predicate, objects),
|
||||
def new(%__MODULE__{} = description, predicate, objects),
|
||||
do: add(description, predicate, objects)
|
||||
def new(subject, predicate, objects),
|
||||
do: new(subject) |> add(predicate, objects)
|
||||
|
@ -100,14 +100,14 @@ defmodule RDF.Description do
|
|||
end
|
||||
end
|
||||
|
||||
def add(%RDF.Description{subject: subject, predications: predications}, predicate, object) do
|
||||
def add(%__MODULE__{subject: subject, predications: predications}, predicate, object) do
|
||||
with triple_predicate = coerce_predicate(predicate),
|
||||
triple_object = coerce_object(object),
|
||||
new_predications = Map.update(predications,
|
||||
triple_predicate, %{triple_object => nil}, fn objects ->
|
||||
Map.put_new(objects, triple_object, nil)
|
||||
end) do
|
||||
%RDF.Description{subject: subject, predications: new_predications}
|
||||
%__MODULE__{subject: subject, predications: new_predications}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -126,7 +126,7 @@ defmodule RDF.Description do
|
|||
def add(description, {predicate, object}),
|
||||
do: add(description, predicate, object)
|
||||
|
||||
def add(description = %RDF.Description{}, {subject, predicate, object}) do
|
||||
def add(description = %__MODULE__{}, {subject, predicate, object}) do
|
||||
if coerce_subject(subject) == description.subject,
|
||||
do: add(description, predicate, object),
|
||||
else: description
|
||||
|
@ -141,14 +141,14 @@ defmodule RDF.Description do
|
|||
end
|
||||
end
|
||||
|
||||
def add(%RDF.Description{subject: subject, predications: predications},
|
||||
%RDF.Description{predications: other_predications}) do
|
||||
def add(%__MODULE__{subject: subject, predications: predications},
|
||||
%__MODULE__{predications: other_predications}) do
|
||||
merged_predications = Map.merge predications, other_predications,
|
||||
fn (_, objects, other_objects) -> Map.merge(objects, other_objects) end
|
||||
%RDF.Description{subject: subject, predications: merged_predications}
|
||||
%__MODULE__{subject: subject, predications: merged_predications}
|
||||
end
|
||||
|
||||
def add(description = %RDF.Description{}, predications = %{}) do
|
||||
def add(description = %__MODULE__{}, predications = %{}) do
|
||||
Enum.reduce predications, description, fn ({predicate, objects}, description) ->
|
||||
add(description, predicate, objects)
|
||||
end
|
||||
|
@ -172,16 +172,16 @@ defmodule RDF.Description do
|
|||
) :: t
|
||||
def put(description, predicate, objects)
|
||||
|
||||
def put(%RDF.Description{subject: subject, predications: predications},
|
||||
def put(%__MODULE__{subject: subject, predications: predications},
|
||||
predicate, objects) when is_list(objects) do
|
||||
with triple_predicate = coerce_predicate(predicate),
|
||||
triple_objects = Enum.reduce(objects, %{}, fn (object, acc) ->
|
||||
Map.put_new(acc, coerce_object(object), nil) end),
|
||||
do: %RDF.Description{subject: subject,
|
||||
do: %__MODULE__{subject: subject,
|
||||
predications: Map.put(predications, triple_predicate, triple_objects)}
|
||||
end
|
||||
|
||||
def put(%RDF.Description{} = description, predicate, object),
|
||||
def put(%__MODULE__{} = description, predicate, object),
|
||||
do: put(description, predicate, [object])
|
||||
|
||||
@doc """
|
||||
|
@ -204,10 +204,10 @@ defmodule RDF.Description do
|
|||
@spec put(t, statements | [statements]) :: t
|
||||
def put(description, statements)
|
||||
|
||||
def put(%RDF.Description{} = description, {predicate, object}),
|
||||
def put(%__MODULE__{} = description, {predicate, object}),
|
||||
do: put(description, predicate, object)
|
||||
|
||||
def put(%RDF.Description{} = description, {subject, predicate, object}) do
|
||||
def put(%__MODULE__{} = description, {subject, predicate, object}) do
|
||||
if coerce_subject(subject) == description.subject,
|
||||
do: put(description, predicate, object),
|
||||
else: description
|
||||
|
@ -216,7 +216,7 @@ defmodule RDF.Description do
|
|||
def put(description, {subject, predicate, object, _}),
|
||||
do: put(description, {subject, predicate, object})
|
||||
|
||||
def put(%RDF.Description{subject: subject} = description, statements) when is_list(statements) do
|
||||
def put(%__MODULE__{subject: subject} = description, statements) when is_list(statements) do
|
||||
statements
|
||||
|> Stream.map(fn
|
||||
{p, o} -> {coerce_predicate(p), o}
|
||||
|
@ -233,14 +233,14 @@ defmodule RDF.Description do
|
|||
end)
|
||||
end
|
||||
|
||||
def put(%RDF.Description{subject: subject, predications: predications},
|
||||
%RDF.Description{predications: other_predications}) do
|
||||
def put(%__MODULE__{subject: subject, predications: predications},
|
||||
%__MODULE__{predications: other_predications}) do
|
||||
merged_predications = Map.merge predications, other_predications,
|
||||
fn (_, _, other_objects) -> other_objects end
|
||||
%RDF.Description{subject: subject, predications: merged_predications}
|
||||
%__MODULE__{subject: subject, predications: merged_predications}
|
||||
end
|
||||
|
||||
def put(description = %RDF.Description{}, predications = %{}) do
|
||||
def put(description = %__MODULE__{}, predications = %{}) do
|
||||
Enum.reduce predications, description, fn ({predicate, objects}, description) ->
|
||||
put(description, predicate, objects)
|
||||
end
|
||||
|
@ -263,11 +263,11 @@ defmodule RDF.Description do
|
|||
end
|
||||
end
|
||||
|
||||
def delete(%RDF.Description{subject: subject, predications: predications} = descr, predicate, object) do
|
||||
def delete(%__MODULE__{subject: subject, predications: predications} = descr, predicate, object) do
|
||||
with triple_predicate = coerce_predicate(predicate),
|
||||
triple_object = coerce_object(object) do
|
||||
if (objects = predications[triple_predicate]) && Map.has_key?(objects, triple_object) do
|
||||
%RDF.Description{
|
||||
%__MODULE__{
|
||||
subject: subject,
|
||||
predications:
|
||||
if map_size(objects) == 1 do
|
||||
|
@ -295,10 +295,10 @@ defmodule RDF.Description do
|
|||
@spec delete(t, statements | [statements]) :: t
|
||||
def delete(description, statements)
|
||||
|
||||
def delete(desc = %RDF.Description{}, {predicate, object}),
|
||||
def delete(desc = %__MODULE__{}, {predicate, object}),
|
||||
do: delete(desc, predicate, object)
|
||||
|
||||
def delete(description = %RDF.Description{}, {subject, predicate, object}) do
|
||||
def delete(description = %__MODULE__{}, {subject, predicate, object}) do
|
||||
if coerce_subject(subject) == description.subject,
|
||||
do: delete(description, predicate, object),
|
||||
else: description
|
||||
|
@ -313,13 +313,13 @@ defmodule RDF.Description do
|
|||
end
|
||||
end
|
||||
|
||||
def delete(description = %RDF.Description{}, other_description = %RDF.Description{}) do
|
||||
def delete(description = %__MODULE__{}, other_description = %__MODULE__{}) do
|
||||
Enum.reduce other_description, description, fn ({_, predicate, object}, description) ->
|
||||
delete(description, predicate, object)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(description = %RDF.Description{}, predications = %{}) do
|
||||
def delete(description = %__MODULE__{}, predications = %{}) do
|
||||
Enum.reduce predications, description, fn ({predicate, objects}, description) ->
|
||||
delete(description, predicate, objects)
|
||||
end
|
||||
|
@ -332,15 +332,15 @@ defmodule RDF.Description do
|
|||
@spec delete_predicates(t, Statement.coercible_predicate | [Statement.coercible_predicate]) :: t
|
||||
def delete_predicates(description, properties)
|
||||
|
||||
def delete_predicates(%RDF.Description{} = description, properties) when is_list(properties) do
|
||||
def delete_predicates(%__MODULE__{} = description, properties) when is_list(properties) do
|
||||
Enum.reduce properties, description, fn (property, description) ->
|
||||
delete_predicates(description, property)
|
||||
end
|
||||
end
|
||||
|
||||
def delete_predicates(%RDF.Description{subject: subject, predications: predications}, property) do
|
||||
def delete_predicates(%__MODULE__{subject: subject, predications: predications}, property) do
|
||||
with property = coerce_predicate(property) do
|
||||
%RDF.Description{subject: subject, predications: Map.delete(predications, property)}
|
||||
%__MODULE__{subject: subject, predications: Map.delete(predications, property)}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -362,7 +362,7 @@ defmodule RDF.Description do
|
|||
"""
|
||||
@impl Access
|
||||
@spec fetch(t, Statement.coercible_predicate) :: {:ok, [Statement.object]} | :error
|
||||
def fetch(%RDF.Description{predications: predications}, predicate) do
|
||||
def fetch(%__MODULE__{predications: predications}, predicate) do
|
||||
with {:ok, objects} <- Access.fetch(predications, coerce_predicate(predicate)) do
|
||||
{:ok, Map.keys(objects)}
|
||||
end
|
||||
|
@ -383,7 +383,7 @@ defmodule RDF.Description do
|
|||
:bar
|
||||
"""
|
||||
@spec get(t, Statement.coercible_predicate, any) :: [Statement.object] | any
|
||||
def get(description = %RDF.Description{}, predicate, default \\ nil) do
|
||||
def get(description = %__MODULE__{}, predicate, default \\ nil) do
|
||||
case fetch(description, predicate) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
|
@ -403,7 +403,7 @@ defmodule RDF.Description do
|
|||
nil
|
||||
"""
|
||||
@spec first(t, Statement.coercible_predicate) :: Statement.object | nil
|
||||
def first(description = %RDF.Description{}, predicate) do
|
||||
def first(description = %__MODULE__{}, predicate) do
|
||||
description
|
||||
|> get(predicate, [])
|
||||
|> List.first
|
||||
|
@ -437,7 +437,7 @@ defmodule RDF.Description do
|
|||
Statement.coercible_object | nil,
|
||||
([Statement.Object] -> [Statement.Object])
|
||||
) :: t
|
||||
def update(description = %RDF.Description{}, predicate, initial \\ nil, fun) do
|
||||
def update(description = %__MODULE__{}, predicate, initial \\ nil, fun) do
|
||||
predicate = coerce_predicate(predicate)
|
||||
|
||||
case get(description, predicate) do
|
||||
|
@ -491,7 +491,7 @@ defmodule RDF.Description do
|
|||
Statement.coercible_predicate,
|
||||
([Statement.Object] -> {[Statement.Object], t} | :pop)
|
||||
) :: {[Statement.Object], t}
|
||||
def get_and_update(description = %RDF.Description{}, predicate, fun) do
|
||||
def get_and_update(description = %__MODULE__{}, predicate, fun) do
|
||||
with triple_predicate = coerce_predicate(predicate) do
|
||||
case fun.(get(description, triple_predicate)) do
|
||||
{objects_to_return, new_objects} ->
|
||||
|
@ -508,10 +508,10 @@ defmodule RDF.Description do
|
|||
@spec pop(t) :: {Triple.t | [Statement.Object] | nil, t}
|
||||
def pop(description)
|
||||
|
||||
def pop(description = %RDF.Description{predications: predications})
|
||||
def pop(description = %__MODULE__{predications: predications})
|
||||
when predications == %{}, do: {nil, description}
|
||||
|
||||
def pop(%RDF.Description{subject: subject, predications: predications}) do
|
||||
def pop(%__MODULE__{subject: subject, predications: predications}) do
|
||||
# TODO: Find a faster way ...
|
||||
predicate = List.first(Map.keys(predications))
|
||||
[{object, _}] = Enum.take(objects = predications[predicate], 1)
|
||||
|
@ -521,7 +521,7 @@ defmodule RDF.Description do
|
|||
else: elem(pop_in(predications, [predicate, object]), 1)
|
||||
|
||||
{{subject, predicate, object},
|
||||
%RDF.Description{subject: subject, predications: popped}}
|
||||
%__MODULE__{subject: subject, predications: popped}}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -537,12 +537,12 @@ defmodule RDF.Description do
|
|||
{nil, RDF.Description.new({EX.S, EX.P, EX.O})}
|
||||
"""
|
||||
@impl Access
|
||||
def pop(description = %RDF.Description{subject: subject, predications: predications}, predicate) do
|
||||
def pop(description = %__MODULE__{subject: subject, predications: predications}, predicate) do
|
||||
case Access.pop(predications, coerce_predicate(predicate)) do
|
||||
{nil, _} ->
|
||||
{nil, description}
|
||||
{objects, new_predications} ->
|
||||
{Map.keys(objects), %RDF.Description{subject: subject, predications: new_predications}}
|
||||
{Map.keys(objects), %__MODULE__{subject: subject, predications: new_predications}}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -560,7 +560,7 @@ defmodule RDF.Description do
|
|||
MapSet.new([EX.p1, EX.p2])
|
||||
"""
|
||||
@spec predicates(t) :: MapSet.t
|
||||
def predicates(%RDF.Description{predications: predications}),
|
||||
def predicates(%__MODULE__{predications: predications}),
|
||||
do: predications |> Map.keys |> MapSet.new
|
||||
|
||||
@doc """
|
||||
|
@ -580,14 +580,14 @@ defmodule RDF.Description do
|
|||
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode)])
|
||||
"""
|
||||
@spec objects(t) :: MapSet.t
|
||||
def objects(%RDF.Description{} = description),
|
||||
def objects(%__MODULE__{} = description),
|
||||
do: objects(description, &RDF.resource?/1)
|
||||
|
||||
@doc """
|
||||
The set of all resources used in the objects within a `RDF.Description` satisfying the given filter criterion.
|
||||
"""
|
||||
@spec objects(t, (Statement.object -> boolean)) :: MapSet.t
|
||||
def objects(%RDF.Description{predications: predications}, filter_fn) do
|
||||
def objects(%__MODULE__{predications: predications}, filter_fn) do
|
||||
Enum.reduce predications, MapSet.new, fn ({_, objects}, acc) ->
|
||||
objects
|
||||
|> Map.keys
|
||||
|
@ -622,16 +622,16 @@ defmodule RDF.Description do
|
|||
The list of all triples within a `RDF.Description`.
|
||||
"""
|
||||
@spec triples(t) :: keyword
|
||||
def triples(description = %RDF.Description{}), do: Enum.to_list(description)
|
||||
def triples(description = %__MODULE__{}), do: Enum.to_list(description)
|
||||
|
||||
defdelegate statements(description), to: RDF.Description, as: :triples
|
||||
defdelegate statements(description), to: __MODULE__, as: :triples
|
||||
|
||||
|
||||
@doc """
|
||||
Returns the number of statements of a `RDF.Description`.
|
||||
"""
|
||||
@spec count(t) :: non_neg_integer
|
||||
def count(%RDF.Description{predications: predications}) do
|
||||
def count(%__MODULE__{predications: predications}) do
|
||||
Enum.reduce predications, 0,
|
||||
fn ({_, objects}, count) -> count + Enum.count(objects) end
|
||||
end
|
||||
|
@ -643,7 +643,7 @@ defmodule RDF.Description do
|
|||
@spec include?(t, statements) :: boolean
|
||||
def include?(description, statement)
|
||||
|
||||
def include?(%RDF.Description{predications: predications},
|
||||
def include?(%__MODULE__{predications: predications},
|
||||
{predicate, object}) do
|
||||
with triple_predicate = coerce_predicate(predicate),
|
||||
triple_object = coerce_object(object) do
|
||||
|
@ -653,13 +653,13 @@ defmodule RDF.Description do
|
|||
end
|
||||
end
|
||||
|
||||
def include?(desc = %RDF.Description{subject: desc_subject},
|
||||
def include?(desc = %__MODULE__{subject: desc_subject},
|
||||
{subject, predicate, object}) do
|
||||
coerce_subject(subject) == desc_subject &&
|
||||
include?(desc, {predicate, object})
|
||||
end
|
||||
|
||||
def include?(%RDF.Description{}, _), do: false
|
||||
def include?(%__MODULE__{}, _), do: false
|
||||
|
||||
|
||||
@doc """
|
||||
|
@ -673,7 +673,7 @@ defmodule RDF.Description do
|
|||
false
|
||||
"""
|
||||
@spec describes?(t, Statement.subject) :: boolean
|
||||
def describes?(%RDF.Description{subject: subject}, other_subject) do
|
||||
def describes?(%__MODULE__{subject: subject}, other_subject) do
|
||||
with other_subject = coerce_subject(other_subject) do
|
||||
subject == other_subject
|
||||
end
|
||||
|
@ -715,7 +715,7 @@ defmodule RDF.Description do
|
|||
@spec values(t, Statement.term_mapping) :: map
|
||||
def values(description, mapping \\ &RDF.Statement.default_term_mapping/1)
|
||||
|
||||
def values(%RDF.Description{predications: predications}, mapping) do
|
||||
def values(%__MODULE__{predications: predications}, mapping) do
|
||||
Map.new predications, fn {predicate, objects} ->
|
||||
{
|
||||
mapping.({:predicate, predicate}),
|
||||
|
@ -734,11 +734,11 @@ defmodule RDF.Description do
|
|||
@spec take(t, [Statement.coercible_predicate] | Enum.t | nil) :: t
|
||||
def take(description, predicates)
|
||||
|
||||
def take(%RDF.Description{} = description, nil), do: description
|
||||
def take(%__MODULE__{} = description, nil), do: description
|
||||
|
||||
def take(%RDF.Description{predications: predications} = description, predicates) do
|
||||
def take(%__MODULE__{predications: predications} = description, predicates) do
|
||||
predicates = Enum.map(predicates, &(coerce_predicate/1))
|
||||
%RDF.Description{description | predications: Map.take(predications, predicates)}
|
||||
%__MODULE__{description | predications: Map.take(predications, predicates)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -749,7 +749,7 @@ defmodule RDF.Description do
|
|||
@spec equal?(t, t) :: boolean
|
||||
def equal?(description1, description2)
|
||||
|
||||
def equal?(%RDF.Description{} = description1, %RDF.Description{} = description2) do
|
||||
def equal?(%__MODULE__{} = description1, %__MODULE__{} = description2) do
|
||||
description1 == description2
|
||||
end
|
||||
|
||||
|
@ -757,31 +757,35 @@ defmodule RDF.Description do
|
|||
|
||||
|
||||
defimpl Enumerable do
|
||||
def member?(desc, triple), do: {:ok, RDF.Description.include?(desc, triple)}
|
||||
def count(desc), do: {:ok, RDF.Description.count(desc)}
|
||||
alias RDF.Description
|
||||
|
||||
def member?(desc, triple), do: {:ok, Description.include?(desc, triple)}
|
||||
def count(desc), do: {:ok, Description.count(desc)}
|
||||
def slice(_desc), do: {:error, __MODULE__}
|
||||
|
||||
def reduce(%RDF.Description{predications: predications}, {:cont, acc}, _fun)
|
||||
def reduce(%Description{predications: predications}, {:cont, acc}, _fun)
|
||||
when map_size(predications) == 0, do: {:done, acc}
|
||||
|
||||
def reduce(description = %RDF.Description{}, {:cont, acc}, fun) do
|
||||
{triple, rest} = RDF.Description.pop(description)
|
||||
def reduce(description = %Description{}, {:cont, acc}, fun) do
|
||||
{triple, rest} = Description.pop(description)
|
||||
reduce(rest, fun.(triple, acc), fun)
|
||||
end
|
||||
|
||||
def reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
|
||||
def reduce(description = %RDF.Description{}, {:suspend, acc}, fun) do
|
||||
def reduce(description = %Description{}, {:suspend, acc}, fun) do
|
||||
{:suspended, acc, &reduce(description, &1, fun)}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
defimpl Collectable do
|
||||
alias RDF.Description
|
||||
|
||||
def into(original) do
|
||||
collector_fun = fn
|
||||
description, {:cont, list} when is_list(list)
|
||||
-> RDF.Description.add(description, List.to_tuple(list))
|
||||
description, {:cont, elem} -> RDF.Description.add(description, elem)
|
||||
-> Description.add(description, List.to_tuple(list))
|
||||
description, {:cont, elem} -> Description.add(description, elem)
|
||||
description, :done -> description
|
||||
_description, :halt -> :ok
|
||||
end
|
||||
|
|
176
lib/rdf/graph.ex
176
lib/rdf/graph.ex
|
@ -38,7 +38,7 @@ defmodule RDF.Graph do
|
|||
Creates an empty unnamed `RDF.Graph`.
|
||||
"""
|
||||
@spec new :: t
|
||||
def new, do: %RDF.Graph{}
|
||||
def new, do: %__MODULE__{}
|
||||
|
||||
@doc """
|
||||
Creates an `RDF.Graph`.
|
||||
|
@ -101,14 +101,14 @@ defmodule RDF.Graph do
|
|||
@spec new(input | [input], keyword) :: t
|
||||
def new(data, options)
|
||||
|
||||
def new(%RDF.Graph{} = graph, options) do
|
||||
%RDF.Graph{graph | name: options |> Keyword.get(:name) |> coerce_graph_name()}
|
||||
def new(%__MODULE__{} = graph, options) do
|
||||
%__MODULE__{graph | name: options |> Keyword.get(:name) |> coerce_graph_name()}
|
||||
|> add_prefixes(Keyword.get(options, :prefixes))
|
||||
|> set_base_iri(Keyword.get(options, :base_iri))
|
||||
end
|
||||
|
||||
def new(data, options) do
|
||||
%RDF.Graph{}
|
||||
%__MODULE__{}
|
||||
|> new(options)
|
||||
|> add(data)
|
||||
end
|
||||
|
@ -136,8 +136,8 @@ defmodule RDF.Graph do
|
|||
prefixes as they are and just removes the triples.
|
||||
"""
|
||||
@spec clear(t) :: t
|
||||
def clear(%RDF.Graph{} = graph) do
|
||||
%RDF.Graph{graph | descriptions: %{}}
|
||||
def clear(%__MODULE__{} = graph) do
|
||||
%__MODULE__{graph | descriptions: %{}}
|
||||
end
|
||||
|
||||
|
||||
|
@ -150,7 +150,7 @@ defmodule RDF.Graph do
|
|||
Statement.coercible_predicate,
|
||||
Statement.coercible_object | [Statement.coercible_object]
|
||||
) :: t
|
||||
def add(%RDF.Graph{} = graph, subject, predicate, objects),
|
||||
def add(%__MODULE__{} = graph, subject, predicate, objects),
|
||||
do: add(graph, {subject, predicate, objects})
|
||||
|
||||
@doc """
|
||||
|
@ -168,7 +168,7 @@ defmodule RDF.Graph do
|
|||
@spec add(t, input | [input]) :: t
|
||||
def add(graph, triples)
|
||||
|
||||
def add(%RDF.Graph{} = graph, {subject, _, _} = statement),
|
||||
def add(%__MODULE__{} = graph, {subject, _, _} = statement),
|
||||
do: do_add(graph, coerce_subject(subject), statement)
|
||||
|
||||
def add(graph, {subject, predicate, object, _}),
|
||||
|
@ -180,10 +180,10 @@ defmodule RDF.Graph do
|
|||
end
|
||||
end
|
||||
|
||||
def add(%RDF.Graph{} = graph, %Description{subject: subject} = description),
|
||||
def add(%__MODULE__{} = graph, %Description{subject: subject} = description),
|
||||
do: do_add(graph, subject, description)
|
||||
|
||||
def add(graph, %RDF.Graph{descriptions: descriptions, prefixes: prefixes}) do
|
||||
def add(graph, %__MODULE__{descriptions: descriptions, prefixes: prefixes}) do
|
||||
graph =
|
||||
Enum.reduce descriptions, graph, fn ({_, description}, graph) ->
|
||||
add(graph, description)
|
||||
|
@ -196,8 +196,8 @@ defmodule RDF.Graph do
|
|||
end
|
||||
end
|
||||
|
||||
defp do_add(%RDF.Graph{descriptions: descriptions} = graph, subject, statements) do
|
||||
%RDF.Graph{graph |
|
||||
defp do_add(%__MODULE__{descriptions: descriptions} = graph, subject, statements) do
|
||||
%__MODULE__{graph |
|
||||
descriptions:
|
||||
Map.update(descriptions, subject, Description.new(statements),
|
||||
fn description ->
|
||||
|
@ -224,16 +224,16 @@ defmodule RDF.Graph do
|
|||
@spec put(t, input | [input]) :: t
|
||||
def put(graph, statements)
|
||||
|
||||
def put(%RDF.Graph{} = graph, {subject, _, _} = statement),
|
||||
def put(%__MODULE__{} = graph, {subject, _, _} = statement),
|
||||
do: do_put(graph, coerce_subject(subject), statement)
|
||||
|
||||
def put(graph, {subject, predicate, object, _}),
|
||||
do: put(graph, {subject, predicate, object})
|
||||
|
||||
def put(%RDF.Graph{} = graph, %Description{subject: subject} = description),
|
||||
def put(%__MODULE__{} = graph, %Description{subject: subject} = description),
|
||||
do: do_put(graph, subject, description)
|
||||
|
||||
def put(graph, %RDF.Graph{descriptions: descriptions, prefixes: prefixes}) do
|
||||
def put(graph, %__MODULE__{descriptions: descriptions, prefixes: prefixes}) do
|
||||
graph =
|
||||
Enum.reduce descriptions, graph, fn ({_, description}, graph) ->
|
||||
put(graph, description)
|
||||
|
@ -246,13 +246,13 @@ defmodule RDF.Graph do
|
|||
end
|
||||
end
|
||||
|
||||
def put(%RDF.Graph{} = graph, statements) when is_map(statements) do
|
||||
def put(%__MODULE__{} = graph, statements) when is_map(statements) do
|
||||
Enum.reduce statements, graph, fn ({subject, predications}, graph) ->
|
||||
put(graph, subject, predications)
|
||||
end
|
||||
end
|
||||
|
||||
def put(%RDF.Graph{} = graph, statements) when is_list(statements) do
|
||||
def put(%__MODULE__{} = graph, statements) when is_list(statements) do
|
||||
put(graph, Enum.group_by(statements, &(elem(&1, 0)), fn {_, p, o} -> {p, o} end))
|
||||
end
|
||||
|
||||
|
@ -262,11 +262,11 @@ defmodule RDF.Graph do
|
|||
@spec put(t, Statement.coercible_subject, Description.statements | [Description.statements]) :: t
|
||||
def put(graph, subject, predications)
|
||||
|
||||
def put(%RDF.Graph{descriptions: descriptions} = graph, subject, predications)
|
||||
def put(%__MODULE__{descriptions: descriptions} = graph, subject, predications)
|
||||
when is_list(predications) do
|
||||
with subject = coerce_subject(subject) do
|
||||
# TODO: Can we reduce this case also to do_put somehow? Only the initializer of Map.update differs ...
|
||||
%RDF.Graph{graph |
|
||||
%__MODULE__{graph |
|
||||
descriptions:
|
||||
Map.update(descriptions, subject, Description.new(subject, predications),
|
||||
fn current ->
|
||||
|
@ -279,8 +279,8 @@ defmodule RDF.Graph do
|
|||
def put(graph, subject, {_predicate, _objects} = predications),
|
||||
do: put(graph, subject, [predications])
|
||||
|
||||
defp do_put(%RDF.Graph{descriptions: descriptions} = graph, subject, statements) do
|
||||
%RDF.Graph{graph |
|
||||
defp do_put(%__MODULE__{descriptions: descriptions} = graph, subject, statements) do
|
||||
%__MODULE__{graph |
|
||||
descriptions:
|
||||
Map.update(descriptions, subject, Description.new(statements),
|
||||
fn current ->
|
||||
|
@ -306,7 +306,7 @@ defmodule RDF.Graph do
|
|||
Statement.coercible_predicate,
|
||||
Statement.coercible_object | [Statement.coercible_object]
|
||||
) :: t
|
||||
def put(%RDF.Graph{} = graph, subject, predicate, objects),
|
||||
def put(%__MODULE__{} = graph, subject, predicate, objects),
|
||||
do: put(graph, {subject, predicate, objects})
|
||||
|
||||
|
||||
|
@ -334,33 +334,33 @@ defmodule RDF.Graph do
|
|||
@spec delete(t, input | [input]) :: t
|
||||
def delete(graph, triples)
|
||||
|
||||
def delete(%RDF.Graph{} = graph, {subject, _, _} = triple),
|
||||
def delete(%__MODULE__{} = graph, {subject, _, _} = triple),
|
||||
do: do_delete(graph, coerce_subject(subject), triple)
|
||||
|
||||
def delete(graph, {subject, predicate, object, _}),
|
||||
do: delete(graph, {subject, predicate, object})
|
||||
|
||||
def delete(%RDF.Graph{} = graph, triples) when is_list(triples) do
|
||||
def delete(%__MODULE__{} = graph, triples) when is_list(triples) do
|
||||
Enum.reduce triples, graph, fn (triple, graph) ->
|
||||
delete(graph, triple)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(%RDF.Graph{} = graph, %Description{subject: subject} = description),
|
||||
def delete(%__MODULE__{} = graph, %Description{subject: subject} = description),
|
||||
do: do_delete(graph, subject, description)
|
||||
|
||||
def delete(%RDF.Graph{} = graph, %RDF.Graph{descriptions: descriptions}) do
|
||||
def delete(%__MODULE__{} = graph, %__MODULE__{descriptions: descriptions}) do
|
||||
Enum.reduce descriptions, graph, fn ({_, description}, graph) ->
|
||||
delete(graph, description)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_delete(%RDF.Graph{descriptions: descriptions} = graph,
|
||||
defp do_delete(%__MODULE__{descriptions: descriptions} = graph,
|
||||
subject, statements) do
|
||||
with description when not is_nil(description) <- descriptions[subject],
|
||||
new_description = Description.delete(description, statements)
|
||||
do
|
||||
%RDF.Graph{graph |
|
||||
%__MODULE__{graph |
|
||||
descriptions:
|
||||
if Enum.empty?(new_description) do
|
||||
Map.delete(descriptions, subject)
|
||||
|
@ -383,15 +383,15 @@ defmodule RDF.Graph do
|
|||
) :: t
|
||||
def delete_subjects(graph, subjects)
|
||||
|
||||
def delete_subjects(%RDF.Graph{} = 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) ->
|
||||
delete_subjects(graph, subject)
|
||||
end
|
||||
end
|
||||
|
||||
def delete_subjects(%RDF.Graph{descriptions: descriptions} = graph, subject) do
|
||||
def delete_subjects(%__MODULE__{descriptions: descriptions} = graph, subject) do
|
||||
with subject = coerce_subject(subject) do
|
||||
%RDF.Graph{graph | descriptions: Map.delete(descriptions, subject)}
|
||||
%__MODULE__{graph | descriptions: Map.delete(descriptions, subject)}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -432,7 +432,7 @@ defmodule RDF.Graph do
|
|||
Description.statements | [Description.statements] | nil,
|
||||
update_description_fun
|
||||
) :: t
|
||||
def update(graph = %RDF.Graph{}, subject, initial \\ nil, fun) do
|
||||
def update(graph = %__MODULE__{}, subject, initial \\ nil, fun) do
|
||||
subject = coerce_subject(subject)
|
||||
|
||||
case get(graph, subject) do
|
||||
|
@ -475,7 +475,7 @@ defmodule RDF.Graph do
|
|||
"""
|
||||
@impl Access
|
||||
@spec fetch(t, Statement.coercible_subject) :: {:ok, Description.t} | :error
|
||||
def fetch(%RDF.Graph{descriptions: descriptions}, subject) do
|
||||
def fetch(%__MODULE__{descriptions: descriptions}, subject) do
|
||||
Access.fetch(descriptions, coerce_subject(subject))
|
||||
end
|
||||
|
||||
|
@ -520,7 +520,7 @@ defmodule RDF.Graph do
|
|||
|
||||
"""
|
||||
@spec get(t, Statement.coercible_subject, Description.t | nil) :: Description.t | nil
|
||||
def get(%RDF.Graph{} = graph, subject, default \\ nil) do
|
||||
def get(%__MODULE__{} = graph, subject, default \\ nil) do
|
||||
case fetch(graph, subject) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
|
@ -531,14 +531,14 @@ defmodule RDF.Graph do
|
|||
The `RDF.Description` of the given subject.
|
||||
"""
|
||||
@spec description(t, Statement.coercible_subject) :: Description.t | nil
|
||||
def description(%RDF.Graph{descriptions: descriptions}, subject),
|
||||
def description(%__MODULE__{descriptions: descriptions}, subject),
|
||||
do: Map.get(descriptions, coerce_subject(subject))
|
||||
|
||||
@doc """
|
||||
All `RDF.Description`s within a `RDF.Graph`.
|
||||
"""
|
||||
@spec descriptions(t) :: [Description.t]
|
||||
def descriptions(%RDF.Graph{descriptions: descriptions}),
|
||||
def descriptions(%__MODULE__{descriptions: descriptions}),
|
||||
do: Map.values(descriptions)
|
||||
|
||||
|
||||
|
@ -568,7 +568,7 @@ defmodule RDF.Graph do
|
|||
@impl Access
|
||||
@spec get_and_update(t, Statement.coercible_subject, get_and_update_description_fun) ::
|
||||
{Description.t, input}
|
||||
def get_and_update(%RDF.Graph{} = graph, subject, fun) do
|
||||
def get_and_update(%__MODULE__{} = graph, subject, fun) do
|
||||
with subject = coerce_subject(subject) do
|
||||
case fun.(get(graph, subject)) do
|
||||
{old_description, new_description} ->
|
||||
|
@ -588,10 +588,10 @@ defmodule RDF.Graph do
|
|||
@spec pop(t) :: {Statement.t | nil, t}
|
||||
def pop(graph)
|
||||
|
||||
def pop(%RDF.Graph{descriptions: descriptions} = graph)
|
||||
def pop(%__MODULE__{descriptions: descriptions} = graph)
|
||||
when descriptions == %{}, do: {nil, graph}
|
||||
|
||||
def pop(%RDF.Graph{descriptions: descriptions} = graph) do
|
||||
def pop(%__MODULE__{descriptions: descriptions} = graph) do
|
||||
# TODO: Find a faster way ...
|
||||
[{subject, description}] = Enum.take(descriptions, 1)
|
||||
{triple, popped_description} = Description.pop(description)
|
||||
|
@ -599,7 +599,7 @@ defmodule RDF.Graph do
|
|||
do: descriptions |> Map.delete(subject),
|
||||
else: descriptions |> Map.put(subject, popped_description)
|
||||
|
||||
{triple, %RDF.Graph{graph | descriptions: popped}}
|
||||
{triple, %__MODULE__{graph | descriptions: popped}}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -618,12 +618,12 @@ defmodule RDF.Graph do
|
|||
"""
|
||||
@impl Access
|
||||
@spec pop(t, Statement.coercible_subject) :: {Description.t | nil, t}
|
||||
def pop(%RDF.Graph{descriptions: descriptions} = graph, subject) do
|
||||
def pop(%__MODULE__{descriptions: descriptions} = graph, subject) do
|
||||
case Access.pop(descriptions, coerce_subject(subject)) do
|
||||
{nil, _} ->
|
||||
{nil, graph}
|
||||
{description, new_descriptions} ->
|
||||
{description, %RDF.Graph{graph | descriptions: new_descriptions}}
|
||||
{description, %__MODULE__{graph | descriptions: new_descriptions}}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -642,7 +642,7 @@ defmodule RDF.Graph do
|
|||
|
||||
"""
|
||||
@spec subject_count(t) :: non_neg_integer
|
||||
def subject_count(%RDF.Graph{descriptions: descriptions}),
|
||||
def subject_count(%__MODULE__{descriptions: descriptions}),
|
||||
do: Enum.count(descriptions)
|
||||
|
||||
@doc """
|
||||
|
@ -659,7 +659,7 @@ defmodule RDF.Graph do
|
|||
|
||||
"""
|
||||
@spec triple_count(t) :: non_neg_integer
|
||||
def triple_count(%RDF.Graph{descriptions: descriptions}) do
|
||||
def triple_count(%__MODULE__{descriptions: descriptions}) do
|
||||
Enum.reduce descriptions, 0, fn ({_subject, description}, count) ->
|
||||
count + Description.count(description)
|
||||
end
|
||||
|
@ -677,7 +677,7 @@ defmodule RDF.Graph do
|
|||
...> RDF.Graph.subjects
|
||||
MapSet.new([RDF.iri(EX.S1), RDF.iri(EX.S2)])
|
||||
"""
|
||||
def subjects(%RDF.Graph{descriptions: descriptions}),
|
||||
def subjects(%__MODULE__{descriptions: descriptions}),
|
||||
do: descriptions |> Map.keys |> MapSet.new
|
||||
|
||||
@doc """
|
||||
|
@ -692,7 +692,7 @@ defmodule RDF.Graph do
|
|||
...> RDF.Graph.predicates
|
||||
MapSet.new([EX.p1, EX.p2])
|
||||
"""
|
||||
def predicates(%RDF.Graph{descriptions: descriptions}) do
|
||||
def predicates(%__MODULE__{descriptions: descriptions}) do
|
||||
Enum.reduce descriptions, MapSet.new, fn ({_, description}, acc) ->
|
||||
description
|
||||
|> Description.predicates
|
||||
|
@ -716,7 +716,7 @@ defmodule RDF.Graph do
|
|||
...> ]) |> RDF.Graph.objects
|
||||
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode)])
|
||||
"""
|
||||
def objects(%RDF.Graph{descriptions: descriptions}) do
|
||||
def objects(%__MODULE__{descriptions: descriptions}) do
|
||||
Enum.reduce descriptions, MapSet.new, fn ({_, description}, acc) ->
|
||||
description
|
||||
|> Description.objects
|
||||
|
@ -738,7 +738,7 @@ defmodule RDF.Graph do
|
|||
MapSet.new([RDF.iri(EX.S1), RDF.iri(EX.S2), RDF.iri(EX.S3),
|
||||
RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode), EX.p1, EX.p2])
|
||||
"""
|
||||
def resources(graph = %RDF.Graph{descriptions: descriptions}) do
|
||||
def resources(graph = %__MODULE__{descriptions: descriptions}) do
|
||||
Enum.reduce(descriptions, MapSet.new, fn ({_, description}, acc) ->
|
||||
description
|
||||
|> Description.resources
|
||||
|
@ -761,7 +761,7 @@ defmodule RDF.Graph do
|
|||
{RDF.iri(EX.S2), RDF.iri(EX.p2), RDF.iri(EX.O2)}]
|
||||
"""
|
||||
@spec triples(t) :: [Statement.t]
|
||||
def triples(%RDF.Graph{} = graph), do: Enum.to_list(graph)
|
||||
def triples(%__MODULE__{} = graph), do: Enum.to_list(graph)
|
||||
|
||||
defdelegate statements(graph), to: RDF.Graph, as: :triples
|
||||
|
||||
|
@ -770,7 +770,7 @@ defmodule RDF.Graph do
|
|||
Checks if the given statement exists within a `RDF.Graph`.
|
||||
"""
|
||||
@spec include?(t, Statement.t) :: boolean
|
||||
def include?(%RDF.Graph{descriptions: descriptions},
|
||||
def include?(%__MODULE__{descriptions: descriptions},
|
||||
triple = {subject, _, _}) do
|
||||
with subject = coerce_subject(subject),
|
||||
%Description{} <- description = descriptions[subject] do
|
||||
|
@ -791,7 +791,7 @@ defmodule RDF.Graph do
|
|||
false
|
||||
"""
|
||||
@spec describes?(t, Statement.coercible_subject) :: boolean
|
||||
def describes?(%RDF.Graph{descriptions: descriptions}, subject) do
|
||||
def describes?(%__MODULE__{descriptions: descriptions}, subject) do
|
||||
with subject = coerce_subject(subject) do
|
||||
Map.has_key?(descriptions, subject)
|
||||
end
|
||||
|
@ -843,7 +843,7 @@ defmodule RDF.Graph do
|
|||
@spec values(t, Statement.term_mapping) :: map
|
||||
def values(graph, mapping \\ &RDF.Statement.default_term_mapping/1)
|
||||
|
||||
def values(%RDF.Graph{descriptions: descriptions}, mapping) do
|
||||
def values(%__MODULE__{descriptions: descriptions}, mapping) do
|
||||
Map.new descriptions, fn {subject, description} ->
|
||||
{mapping.({:subject, subject}), Description.values(description, mapping)}
|
||||
end
|
||||
|
@ -861,16 +861,16 @@ defmodule RDF.Graph do
|
|||
@spec take(t, [Statement.coercible_subject] | Enum.t | nil, [Statement.coercible_predicate] | Enum.t | nil) :: t
|
||||
def take(graph, subjects, properties \\ nil)
|
||||
|
||||
def take(%RDF.Graph{} = graph, nil, nil), do: graph
|
||||
def take(%__MODULE__{} = graph, nil, nil), do: graph
|
||||
|
||||
def take(%RDF.Graph{descriptions: descriptions} = graph, subjects, nil) do
|
||||
def take(%__MODULE__{descriptions: descriptions} = graph, subjects, nil) do
|
||||
subjects = Enum.map(subjects, &(coerce_subject/1))
|
||||
%RDF.Graph{graph | descriptions: Map.take(descriptions, subjects)}
|
||||
%__MODULE__{graph | descriptions: Map.take(descriptions, subjects)}
|
||||
end
|
||||
|
||||
def take(%RDF.Graph{} = graph, subjects, properties) do
|
||||
def take(%__MODULE__{} = graph, subjects, properties) do
|
||||
graph = take(graph, subjects, nil)
|
||||
%RDF.Graph{graph |
|
||||
%__MODULE__{graph |
|
||||
descriptions: Map.new(graph.descriptions, fn {subject, description} ->
|
||||
{subject, Description.take(description, properties)}
|
||||
end)
|
||||
|
@ -887,7 +887,7 @@ defmodule RDF.Graph do
|
|||
@spec equal?(t | any, t | any) :: boolean
|
||||
def equal?(graph1, graph2)
|
||||
|
||||
def equal?(%RDF.Graph{} = graph1, %RDF.Graph{} = graph2) do
|
||||
def equal?(%__MODULE__{} = graph1, %__MODULE__{} = graph2) do
|
||||
clear_metadata(graph1) == clear_metadata(graph2)
|
||||
end
|
||||
|
||||
|
@ -911,18 +911,18 @@ defmodule RDF.Graph do
|
|||
) :: t
|
||||
def add_prefixes(graph, prefixes, conflict_resolver \\ nil)
|
||||
|
||||
def add_prefixes(%RDF.Graph{} = graph, nil, _), do: graph
|
||||
def add_prefixes(%__MODULE__{} = graph, nil, _), do: graph
|
||||
|
||||
def add_prefixes(%RDF.Graph{prefixes: nil} = graph, prefixes, _) do
|
||||
%RDF.Graph{graph | prefixes: RDF.PrefixMap.new(prefixes)}
|
||||
def add_prefixes(%__MODULE__{prefixes: nil} = graph, prefixes, _) do
|
||||
%__MODULE__{graph | prefixes: RDF.PrefixMap.new(prefixes)}
|
||||
end
|
||||
|
||||
def add_prefixes(%RDF.Graph{} = graph, additions, nil) do
|
||||
add_prefixes(%RDF.Graph{} = graph, additions, fn _, _, ns -> ns end)
|
||||
def add_prefixes(%__MODULE__{} = graph, additions, nil) do
|
||||
add_prefixes(%__MODULE__{} = graph, additions, fn _, _, ns -> ns end)
|
||||
end
|
||||
|
||||
def add_prefixes(%RDF.Graph{prefixes: prefixes} = graph, additions, conflict_resolver) do
|
||||
%RDF.Graph{graph |
|
||||
def add_prefixes(%__MODULE__{prefixes: prefixes} = graph, additions, conflict_resolver) do
|
||||
%__MODULE__{graph |
|
||||
prefixes: RDF.PrefixMap.merge!(prefixes, additions, conflict_resolver)
|
||||
}
|
||||
end
|
||||
|
@ -936,18 +936,18 @@ defmodule RDF.Graph do
|
|||
@spec delete_prefixes(t, PrefixMap.t) :: t
|
||||
def delete_prefixes(graph, prefixes)
|
||||
|
||||
def delete_prefixes(%RDF.Graph{prefixes: nil} = graph, _), do: graph
|
||||
def delete_prefixes(%__MODULE__{prefixes: nil} = graph, _), do: graph
|
||||
|
||||
def delete_prefixes(%RDF.Graph{prefixes: prefixes} = graph, deletions) do
|
||||
%RDF.Graph{graph | prefixes: RDF.PrefixMap.drop(prefixes, List.wrap(deletions))}
|
||||
def delete_prefixes(%__MODULE__{prefixes: prefixes} = graph, deletions) do
|
||||
%__MODULE__{graph | prefixes: RDF.PrefixMap.drop(prefixes, List.wrap(deletions))}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Clears all prefixes of the given `graph`.
|
||||
"""
|
||||
@spec clear_prefixes(t) :: t
|
||||
def clear_prefixes(%RDF.Graph{} = graph) do
|
||||
%RDF.Graph{graph | prefixes: nil}
|
||||
def clear_prefixes(%__MODULE__{} = graph) do
|
||||
%__MODULE__{graph | prefixes: nil}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -958,27 +958,27 @@ defmodule RDF.Graph do
|
|||
@spec set_base_iri(t, IRI.t | nil) :: t
|
||||
def set_base_iri(graph, base_iri)
|
||||
|
||||
def set_base_iri(%RDF.Graph{} = graph, nil) do
|
||||
%RDF.Graph{graph | base_iri: nil}
|
||||
def set_base_iri(%__MODULE__{} = graph, nil) do
|
||||
%__MODULE__{graph | base_iri: nil}
|
||||
end
|
||||
|
||||
def set_base_iri(%RDF.Graph{} = graph, base_iri) do
|
||||
%RDF.Graph{graph | base_iri: RDF.IRI.coerce_base(base_iri)}
|
||||
def set_base_iri(%__MODULE__{} = graph, base_iri) do
|
||||
%__MODULE__{graph | base_iri: RDF.IRI.coerce_base(base_iri)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Clears the base IRI of the given `graph`.
|
||||
"""
|
||||
@spec clear_base_iri(t) :: t
|
||||
def clear_base_iri(%RDF.Graph{} = graph) do
|
||||
%RDF.Graph{graph | base_iri: nil}
|
||||
def clear_base_iri(%__MODULE__{} = graph) do
|
||||
%__MODULE__{graph | base_iri: nil}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Clears the base IRI and all prefixes of the given `graph`.
|
||||
"""
|
||||
@spec clear_metadata(t) :: t
|
||||
def clear_metadata(%RDF.Graph{} = graph) do
|
||||
def clear_metadata(%__MODULE__{} = graph) do
|
||||
graph
|
||||
|> clear_base_iri()
|
||||
|> clear_prefixes()
|
||||
|
@ -986,30 +986,34 @@ defmodule RDF.Graph do
|
|||
|
||||
|
||||
defimpl Enumerable do
|
||||
def member?(graph, triple), do: {:ok, RDF.Graph.include?(graph, triple)}
|
||||
def count(graph), do: {:ok, RDF.Graph.triple_count(graph)}
|
||||
alias RDF.Graph
|
||||
|
||||
def member?(graph, triple), do: {:ok, Graph.include?(graph, triple)}
|
||||
def count(graph), do: {:ok, Graph.triple_count(graph)}
|
||||
def slice(_graph), do: {:error, __MODULE__}
|
||||
|
||||
def reduce(%RDF.Graph{descriptions: descriptions}, {:cont, acc}, _fun)
|
||||
def reduce(%Graph{descriptions: descriptions}, {:cont, acc}, _fun)
|
||||
when map_size(descriptions) == 0, do: {:done, acc}
|
||||
|
||||
def reduce(%RDF.Graph{} = graph, {:cont, acc}, fun) do
|
||||
{triple, rest} = RDF.Graph.pop(graph)
|
||||
def reduce(%Graph{} = graph, {:cont, acc}, fun) do
|
||||
{triple, rest} = Graph.pop(graph)
|
||||
reduce(rest, fun.(triple, acc), fun)
|
||||
end
|
||||
|
||||
def reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
|
||||
def reduce(%RDF.Graph{} = graph, {:suspend, acc}, fun) do
|
||||
def reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
|
||||
def reduce(%Graph{} = graph, {:suspend, acc}, fun) do
|
||||
{:suspended, acc, &reduce(graph, &1, fun)}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
alias RDF.Graph
|
||||
|
||||
def into(original) do
|
||||
collector_fun = fn
|
||||
graph, {:cont, list} when is_list(list)
|
||||
-> RDF.Graph.add(graph, List.to_tuple(list))
|
||||
graph, {:cont, elem} -> RDF.Graph.add(graph, elem)
|
||||
-> Graph.add(graph, List.to_tuple(list))
|
||||
graph, {:cont, elem} -> Graph.add(graph, elem)
|
||||
graph, :done -> graph
|
||||
_graph, :halt -> :ok
|
||||
end
|
||||
|
|
|
@ -40,7 +40,7 @@ defmodule RDF.List do
|
|||
do: new(RDF.iri(head), graph)
|
||||
|
||||
def new(head, graph) do
|
||||
with list = %RDF.List{head: head, graph: graph} do
|
||||
with list = %__MODULE__{head: head, graph: graph} do
|
||||
if well_formed?(list) do
|
||||
list
|
||||
end
|
||||
|
@ -79,7 +79,7 @@ defmodule RDF.List do
|
|||
graph = Keyword.get(opts, :graph, RDF.graph),
|
||||
{head, graph} = do_from(list, head, graph, opts)
|
||||
do
|
||||
%RDF.List{head: head, graph: graph}
|
||||
%__MODULE__{head: head, graph: graph}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -123,7 +123,7 @@ defmodule RDF.List do
|
|||
Nested lists are converted recursively.
|
||||
"""
|
||||
@spec values(t) :: Enumerable.t
|
||||
def values(%RDF.List{graph: graph} = list) do
|
||||
def values(%__MODULE__{graph: graph} = list) do
|
||||
Enum.map list, fn node_description ->
|
||||
value = Description.first(node_description, RDF.first)
|
||||
if node?(value, graph) do
|
||||
|
@ -141,7 +141,7 @@ defmodule RDF.List do
|
|||
The RDF nodes constituting a `RDF.List` as an Elixir list.
|
||||
"""
|
||||
@spec nodes(t) :: [BlankNode.t]
|
||||
def nodes(%RDF.List{} = list) do
|
||||
def nodes(%__MODULE__{} = list) do
|
||||
Enum.map list, fn node_description -> node_description.subject end
|
||||
end
|
||||
|
||||
|
@ -150,17 +150,17 @@ defmodule RDF.List do
|
|||
Checks if a list is the empty list.
|
||||
"""
|
||||
@spec empty?(t) :: boolean
|
||||
def empty?(%RDF.List{head: @rdf_nil}), do: true
|
||||
def empty?(%RDF.List{}), do: false
|
||||
def empty?(%__MODULE__{head: @rdf_nil}), do: true
|
||||
def empty?(%__MODULE__{}), do: false
|
||||
|
||||
|
||||
@doc """
|
||||
Checks if the given list consists of list nodes which are all blank nodes.
|
||||
"""
|
||||
@spec valid?(t) :: boolean
|
||||
def valid?(%RDF.List{head: @rdf_nil}), do: true
|
||||
def valid?(%__MODULE__{head: @rdf_nil}), do: true
|
||||
|
||||
def valid?(%RDF.List{} = list) do
|
||||
def valid?(%__MODULE__{} = list) do
|
||||
Enum.all? list, fn node_description ->
|
||||
RDF.bnode?(node_description.subject)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue