core: add Description.new(%Description{}) which allows some simplifications
This commit is contained in:
parent
377707f0ef
commit
81ffadbc53
2 changed files with 44 additions and 51 deletions
|
@ -25,6 +25,8 @@ defmodule RDF.Description do
|
||||||
do: new(subject) |> add(predicate, object)
|
do: new(subject) |> add(predicate, object)
|
||||||
def new([statement | more_statements]),
|
def new([statement | more_statements]),
|
||||||
do: new(statement) |> add(more_statements)
|
do: new(statement) |> add(more_statements)
|
||||||
|
def new(%RDF.Description{} = description),
|
||||||
|
do: description
|
||||||
def new(subject),
|
def new(subject),
|
||||||
do: %RDF.Description{subject: convert_subject(subject)}
|
do: %RDF.Description{subject: convert_subject(subject)}
|
||||||
|
|
||||||
|
|
|
@ -97,18 +97,9 @@ defmodule RDF.Graph do
|
||||||
@doc """
|
@doc """
|
||||||
Adds triples to a `RDF.Graph`.
|
Adds triples to a `RDF.Graph`.
|
||||||
"""
|
"""
|
||||||
def add(%RDF.Graph{name: name, descriptions: descriptions},
|
def add(%RDF.Graph{} = graph, subject, predicate, objects),
|
||||||
subject, predicate, object) do
|
do: add(graph, {subject, predicate, objects})
|
||||||
with subject = convert_subject(subject) do
|
|
||||||
%RDF.Graph{name: name,
|
|
||||||
descriptions:
|
|
||||||
Map.update(descriptions, subject,
|
|
||||||
Description.new({subject, predicate, object}), fn description ->
|
|
||||||
description |> Description.add({predicate, object})
|
|
||||||
end)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Adds triples to a `RDF.Graph`.
|
Adds triples to a `RDF.Graph`.
|
||||||
|
@ -120,8 +111,8 @@ defmodule RDF.Graph do
|
||||||
"""
|
"""
|
||||||
def add(graph, triples)
|
def add(graph, triples)
|
||||||
|
|
||||||
def add(graph, {subject, predicate, object}),
|
def add(%RDF.Graph{} = graph, {subject, _, _} = statement),
|
||||||
do: add(graph, subject, predicate, object)
|
do: do_add(graph, convert_subject(subject), statement)
|
||||||
|
|
||||||
def add(graph, triples) when is_list(triples) do
|
def add(graph, triples) when is_list(triples) do
|
||||||
Enum.reduce triples, graph, fn (triple, graph) ->
|
Enum.reduce triples, graph, fn (triple, graph) ->
|
||||||
|
@ -129,15 +120,8 @@ defmodule RDF.Graph do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(%RDF.Graph{name: name, descriptions: descriptions},
|
def add(%RDF.Graph{} = graph, %Description{subject: subject} = description),
|
||||||
%Description{subject: subject} = description) do
|
do: do_add(graph, subject, description)
|
||||||
%RDF.Graph{name: name,
|
|
||||||
descriptions:
|
|
||||||
Map.update(descriptions, subject, description, fn current ->
|
|
||||||
current |> Description.add(description)
|
|
||||||
end)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def add(graph, %RDF.Graph{descriptions: descriptions}) do
|
def add(graph, %RDF.Graph{descriptions: descriptions}) do
|
||||||
Enum.reduce descriptions, graph, fn ({_, description}, graph) ->
|
Enum.reduce descriptions, graph, fn ({_, description}, graph) ->
|
||||||
|
@ -145,6 +129,17 @@ defmodule RDF.Graph do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp do_add(%RDF.Graph{name: name, descriptions: descriptions},
|
||||||
|
subject, statements) do
|
||||||
|
%RDF.Graph{name: name,
|
||||||
|
descriptions:
|
||||||
|
Map.update(descriptions, subject, Description.new(statements),
|
||||||
|
fn description ->
|
||||||
|
Description.add(description, statements)
|
||||||
|
end)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Puts statements to a `RDF.Graph`, overwriting all statements with the same subject and predicate.
|
Puts statements to a `RDF.Graph`, overwriting all statements with the same subject and predicate.
|
||||||
|
@ -156,17 +151,8 @@ defmodule RDF.Graph do
|
||||||
iex> RDF.Graph.new(EX.S, EX.P1, EX.O1) |> RDF.Graph.put(EX.S, EX.P2, EX.O2)
|
iex> RDF.Graph.new(EX.S, EX.P1, EX.O1) |> RDF.Graph.put(EX.S, EX.P2, EX.O2)
|
||||||
RDF.Graph.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}])
|
RDF.Graph.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}])
|
||||||
"""
|
"""
|
||||||
def put(%RDF.Graph{name: name, descriptions: descriptions},
|
def put(%RDF.Graph{} = graph, subject, predicate, objects),
|
||||||
subject, predicate, objects) do
|
do: put(graph, {subject, predicate, objects})
|
||||||
with subject = convert_subject(subject) do
|
|
||||||
%RDF.Graph{name: name,
|
|
||||||
descriptions:
|
|
||||||
Map.update(descriptions, subject,
|
|
||||||
Description.new(subject, predicate, objects),
|
|
||||||
fn current -> Description.put(current, predicate, objects) end)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Adds statements to a `RDF.Graph` and overwrites all existing statements with the same subjects and predicates.
|
Adds statements to a `RDF.Graph` and overwrites all existing statements with the same subjects and predicates.
|
||||||
|
@ -179,18 +165,11 @@ defmodule RDF.Graph do
|
||||||
"""
|
"""
|
||||||
def put(graph, statements)
|
def put(graph, statements)
|
||||||
|
|
||||||
def put(%RDF.Graph{} = graph, {subject, predicate, object}),
|
def put(%RDF.Graph{} = graph, {subject, _, _} = statement),
|
||||||
do: put(graph, subject, predicate, object)
|
do: do_put(graph, convert_subject(subject), statement)
|
||||||
|
|
||||||
def put(%RDF.Graph{name: name, descriptions: descriptions},
|
def put(%RDF.Graph{} = graph, %Description{subject: subject} = description),
|
||||||
%Description{subject: subject} = description) do
|
do: do_put(graph, subject, description)
|
||||||
%RDF.Graph{name: name,
|
|
||||||
descriptions:
|
|
||||||
Map.update(descriptions, subject, description, fn current ->
|
|
||||||
current |> Description.put(description)
|
|
||||||
end)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def put(graph, %RDF.Graph{descriptions: descriptions}) do
|
def put(graph, %RDF.Graph{descriptions: descriptions}) do
|
||||||
Enum.reduce descriptions, graph, fn ({_, description}, graph) ->
|
Enum.reduce descriptions, graph, fn ({_, description}, graph) ->
|
||||||
|
@ -208,14 +187,16 @@ defmodule RDF.Graph do
|
||||||
put(graph, Enum.group_by(statements, &(elem(&1, 0)), fn {_, p, o} -> {p, o} end))
|
put(graph, Enum.group_by(statements, &(elem(&1, 0)), fn {_, p, o} -> {p, o} end))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: Can we reduce this case also to do_put? Only the initializer differs ...
|
||||||
def put(%RDF.Graph{name: name, descriptions: descriptions}, subject, predications)
|
def put(%RDF.Graph{name: name, descriptions: descriptions}, subject, predications)
|
||||||
when is_list(predications) do
|
when is_list(predications) do
|
||||||
with subject = convert_subject(subject) do
|
with subject = convert_subject(subject) do
|
||||||
%RDF.Graph{name: name,
|
%RDF.Graph{name: name,
|
||||||
descriptions:
|
descriptions:
|
||||||
Map.update(descriptions, subject,
|
Map.update(descriptions, subject, Description.new(subject, predications),
|
||||||
Description.new(subject, predications),
|
fn current ->
|
||||||
fn current -> current |> Description.put(predications) end)
|
Description.put(current, predications)
|
||||||
|
end)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -223,6 +204,17 @@ defmodule RDF.Graph do
|
||||||
def put(graph, subject, {_predicate, _objects} = predications),
|
def put(graph, subject, {_predicate, _objects} = predications),
|
||||||
do: put(graph, subject, [predications])
|
do: put(graph, subject, [predications])
|
||||||
|
|
||||||
|
defp do_put(%RDF.Graph{name: name, descriptions: descriptions},
|
||||||
|
subject, statements) do
|
||||||
|
%RDF.Graph{name: name,
|
||||||
|
descriptions:
|
||||||
|
Map.update(descriptions, subject, Description.new(statements),
|
||||||
|
fn current ->
|
||||||
|
Description.put(current, statements)
|
||||||
|
end)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Deletes statements from a `RDF.Graph`.
|
Deletes statements from a `RDF.Graph`.
|
||||||
|
@ -241,7 +233,7 @@ defmodule RDF.Graph do
|
||||||
def delete(graph, triples)
|
def delete(graph, triples)
|
||||||
|
|
||||||
def delete(%RDF.Graph{} = graph, {subject, _, _} = triple) do
|
def delete(%RDF.Graph{} = graph, {subject, _, _} = triple) do
|
||||||
do_delete(graph, subject, triple)
|
do_delete(graph, convert_subject(subject), triple)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(%RDF.Graph{} = graph, triples) when is_list(triples) do
|
def delete(%RDF.Graph{} = graph, triples) when is_list(triples) do
|
||||||
|
@ -262,8 +254,7 @@ defmodule RDF.Graph do
|
||||||
|
|
||||||
defp do_delete(%RDF.Graph{name: name, descriptions: descriptions} = graph,
|
defp do_delete(%RDF.Graph{name: name, descriptions: descriptions} = graph,
|
||||||
subject, statements) do
|
subject, statements) do
|
||||||
with subject = convert_subject(subject),
|
with description when not is_nil(description) <- descriptions[subject],
|
||||||
description when not is_nil(description) <- descriptions[subject],
|
|
||||||
new_description = Description.delete(description, statements)
|
new_description = Description.delete(description, statements)
|
||||||
do
|
do
|
||||||
%RDF.Graph{name: name,
|
%RDF.Graph{name: name,
|
||||||
|
|
Loading…
Reference in a new issue