core: add RDF.Data.description/2
This commit is contained in:
parent
9a128858b6
commit
96447ec258
2 changed files with 70 additions and 3 deletions
|
@ -23,6 +23,14 @@ defprotocol RDF.Data do
|
||||||
"""
|
"""
|
||||||
def statements(data)
|
def statements(data)
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns a `RDF.Description` of the given subject.
|
||||||
|
|
||||||
|
Note: On a `RDF.Dataset` this will return an aggregated `RDF.Description` with
|
||||||
|
the statements about this subject from all graphs.
|
||||||
|
"""
|
||||||
|
def description(data, subject)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns the set of all resources which are subject of the statements of a RDF data structure.
|
Returns the set of all resources which are subject of the statements of a RDF data structure.
|
||||||
"""
|
"""
|
||||||
|
@ -66,9 +74,18 @@ defimpl RDF.Data, for: RDF.Description do
|
||||||
do: RDF.Description.include?(description, statements)
|
do: RDF.Description.include?(description, statements)
|
||||||
|
|
||||||
def statements(description), do: RDF.Description.statements(description)
|
def statements(description), do: RDF.Description.statements(description)
|
||||||
|
|
||||||
|
def description(%RDF.Description{subject: subject} = description, requested_subject) do
|
||||||
|
with ^subject <- RDF.Statement.convert_subject(requested_subject) do
|
||||||
|
description
|
||||||
|
else
|
||||||
|
_ -> RDF.Description.new(requested_subject)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def subjects(%RDF.Description{subject: subject}), do: MapSet.new([subject])
|
||||||
def predicates(description), do: RDF.Description.predicates(description)
|
def predicates(description), do: RDF.Description.predicates(description)
|
||||||
def objects(description), do: RDF.Description.objects(description)
|
def objects(description), do: RDF.Description.objects(description)
|
||||||
def subjects(%RDF.Description{subject: subject}), do: MapSet.new([subject])
|
|
||||||
|
|
||||||
def resources(%RDF.Description{subject: subject} = description),
|
def resources(%RDF.Description{subject: subject} = description),
|
||||||
do: RDF.Description.resources(description) |> MapSet.put(subject)
|
do: RDF.Description.resources(description) |> MapSet.put(subject)
|
||||||
|
@ -86,6 +103,10 @@ defimpl RDF.Data, for: RDF.Graph do
|
||||||
def include?(graph, statements), do: RDF.Graph.include?(graph, statements)
|
def include?(graph, statements), do: RDF.Graph.include?(graph, statements)
|
||||||
|
|
||||||
def statements(graph), do: RDF.Graph.statements(graph)
|
def statements(graph), do: RDF.Graph.statements(graph)
|
||||||
|
|
||||||
|
def description(graph, subject),
|
||||||
|
do: RDF.Graph.description(graph, subject) || RDF.Description.new(subject)
|
||||||
|
|
||||||
def subjects(graph), do: RDF.Graph.subjects(graph)
|
def subjects(graph), do: RDF.Graph.subjects(graph)
|
||||||
def predicates(graph), do: RDF.Graph.predicates(graph)
|
def predicates(graph), do: RDF.Graph.predicates(graph)
|
||||||
def objects(graph), do: RDF.Graph.objects(graph)
|
def objects(graph), do: RDF.Graph.objects(graph)
|
||||||
|
@ -104,6 +125,18 @@ defimpl RDF.Data, for: RDF.Dataset do
|
||||||
def include?(dataset, statements), do: RDF.Dataset.include?(dataset, statements)
|
def include?(dataset, statements), do: RDF.Dataset.include?(dataset, statements)
|
||||||
|
|
||||||
def statements(dataset), do: RDF.Dataset.statements(dataset)
|
def statements(dataset), do: RDF.Dataset.statements(dataset)
|
||||||
|
|
||||||
|
def description(dataset, subject) do
|
||||||
|
with subject = RDF.Statement.convert_subject(subject) do
|
||||||
|
Enum.reduce RDF.Dataset.graphs(dataset), RDF.Description.new(subject), fn
|
||||||
|
%RDF.Graph{descriptions: %{^subject => graph_description}}, description ->
|
||||||
|
RDF.Description.add(description, graph_description)
|
||||||
|
_, description ->
|
||||||
|
description
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def subjects(dataset), do: RDF.Dataset.subjects(dataset)
|
def subjects(dataset), do: RDF.Dataset.subjects(dataset)
|
||||||
def predicates(dataset), do: RDF.Dataset.predicates(dataset)
|
def predicates(dataset), do: RDF.Dataset.predicates(dataset)
|
||||||
def objects(dataset), do: RDF.Dataset.objects(dataset)
|
def objects(dataset), do: RDF.Dataset.objects(dataset)
|
||||||
|
|
|
@ -20,7 +20,8 @@ defmodule RDF.DataTest do
|
||||||
|> Dataset.add((
|
|> Dataset.add((
|
||||||
Graph.new(EX.NamedGraph)
|
Graph.new(EX.NamedGraph)
|
||||||
|> Graph.add(description)
|
|> Graph.add(description)
|
||||||
|> Graph.add({EX.S3, EX.p3, EX.O5})), EX.NamedGraph
|
|> Graph.add({EX.S3, EX.p3, EX.O5})
|
||||||
|
|> Graph.add({EX.S, EX.p3, EX.O5})), EX.NamedGraph
|
||||||
)
|
)
|
||||||
{:ok,
|
{:ok,
|
||||||
description: description,
|
description: description,
|
||||||
|
@ -55,6 +56,18 @@ defmodule RDF.DataTest do
|
||||||
assert RDF.Data.statements(description) == Description.statements(description)
|
assert RDF.Data.statements(description) == Description.statements(description)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "description when the requested subject matches the Description.subject",
|
||||||
|
%{description: description} do
|
||||||
|
assert RDF.Data.description(description, description.subject) == description
|
||||||
|
assert RDF.Data.description(description, to_string(description.subject)) == description
|
||||||
|
assert RDF.Data.description(description, EX.S) == description
|
||||||
|
end
|
||||||
|
|
||||||
|
test "description when the requested subject does not match the Description.subject",
|
||||||
|
%{description: description} do
|
||||||
|
assert RDF.Data.description(description, uri(EX.Other)) == Description.new(EX.Other)
|
||||||
|
end
|
||||||
|
|
||||||
test "subjects", %{description: description} do
|
test "subjects", %{description: description} do
|
||||||
assert RDF.Data.subjects(description) == MapSet.new([uri(EX.S)])
|
assert RDF.Data.subjects(description) == MapSet.new([uri(EX.S)])
|
||||||
end
|
end
|
||||||
|
@ -109,6 +122,16 @@ defmodule RDF.DataTest do
|
||||||
assert RDF.Data.statements(graph) == Graph.statements(graph)
|
assert RDF.Data.statements(graph) == Graph.statements(graph)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "description when a description is present",
|
||||||
|
%{graph: graph, description: description} do
|
||||||
|
assert RDF.Data.description(graph, uri(EX.S)) == description
|
||||||
|
assert RDF.Data.description(graph, EX.S) == description
|
||||||
|
end
|
||||||
|
|
||||||
|
test "description when a description is not present", %{graph: graph} do
|
||||||
|
assert RDF.Data.description(graph, uri(EX.Other)) == Description.new(EX.Other)
|
||||||
|
end
|
||||||
|
|
||||||
test "subjects", %{graph: graph} do
|
test "subjects", %{graph: graph} do
|
||||||
assert RDF.Data.subjects(graph) == MapSet.new([uri(EX.S), uri(EX.S2)])
|
assert RDF.Data.subjects(graph) == MapSet.new([uri(EX.S), uri(EX.S2)])
|
||||||
end
|
end
|
||||||
|
@ -164,6 +187,17 @@ defmodule RDF.DataTest do
|
||||||
refute RDF.Data.include?(dataset, {EX.Other, EX.p1, EX.O2})
|
refute RDF.Data.include?(dataset, {EX.Other, EX.p1, EX.O2})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "description when a description is present",
|
||||||
|
%{dataset: dataset, description: description} do
|
||||||
|
description_aggregate = Description.add(description, {EX.S, EX.p3, EX.O5})
|
||||||
|
assert RDF.Data.description(dataset, uri(EX.S)) == description_aggregate
|
||||||
|
assert RDF.Data.description(dataset, EX.S) == description_aggregate
|
||||||
|
end
|
||||||
|
|
||||||
|
test "description when a description is not present", %{dataset: dataset} do
|
||||||
|
assert RDF.Data.description(dataset, uri(EX.Other)) == Description.new(EX.Other)
|
||||||
|
end
|
||||||
|
|
||||||
test "statements", %{dataset: dataset} do
|
test "statements", %{dataset: dataset} do
|
||||||
assert RDF.Data.statements(dataset) == Dataset.statements(dataset)
|
assert RDF.Data.statements(dataset) == Dataset.statements(dataset)
|
||||||
end
|
end
|
||||||
|
@ -193,7 +227,7 @@ defmodule RDF.DataTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "statement_count", %{dataset: dataset} do
|
test "statement_count", %{dataset: dataset} do
|
||||||
assert RDF.Data.statement_count(dataset) == 13
|
assert RDF.Data.statement_count(dataset) == 14
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue