Add RDF.Data.descriptions/1

This commit is contained in:
Marcel Otto 2017-07-07 19:03:01 +02:00
parent cbc2b0f122
commit 49842af90f
5 changed files with 45 additions and 1 deletions

View file

@ -9,7 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Added
- Turtle decoder
- Turtle decoder
- `RDF.Data.descriptions/1` returns all descriptions of a RDF data structure
### Changed

View file

@ -529,6 +529,7 @@ The `RDF.Data` protocol offers various functions to access the contents of RDF d
- `RDF.Data.objects/1` returns the set of all resources on the object position of statements. Note: Literals not included.
- `RDF.Data.resources/1` returns the set of all used resources at any position in the contained RDF statements.
- `RDF.Data.description/2` returns all statements from a data structure about the given resource as a `RDF.Description`. It will be empty if no such statements exist. On a `RDF.Dataset` it will aggregate the statements about the resource from all graphs.
- `RDF.Data.descriptions/1` returns all `RDF.Description`s within a data structure (possible aggregated in the case of a `RDF.Dataset`)
- `RDF.Data.statements/1` returns a list of all contained RDF statements.
The `get` functions return individual elements of a RDF data structure:

View file

@ -49,6 +49,14 @@ defprotocol RDF.Data do
"""
def description(data, subject)
@doc """
Returns all `RDF.Description`s within a RDF data structure.
Note: On a `RDF.Dataset` this will return aggregated `RDF.Description`s about
the same subject from all graphs.
"""
def descriptions(data)
@doc """
Returns the set of all resources which are subject of the statements of a RDF data structure.
"""
@ -131,6 +139,8 @@ defimpl RDF.Data, for: RDF.Description do
end
end
def descriptions(description), do: [description]
def subjects(%RDF.Description{subject: subject}), do: MapSet.new([subject])
def predicates(description), do: RDF.Description.predicates(description)
def objects(description), do: RDF.Description.objects(description)
@ -188,6 +198,8 @@ defimpl RDF.Data, for: RDF.Graph do
def description(graph, subject),
do: RDF.Graph.description(graph, subject) || RDF.Description.new(subject)
def descriptions(graph), do: RDF.Graph.descriptions(graph)
def subjects(graph), do: RDF.Graph.subjects(graph)
def predicates(graph), do: RDF.Graph.predicates(graph)
def objects(graph), do: RDF.Graph.objects(graph)
@ -232,6 +244,12 @@ defimpl RDF.Data, for: RDF.Dataset do
end
end
def descriptions(dataset) do
dataset
|> subjects
|> Enum.map(&(description(dataset, &1)))
end
def subjects(dataset), do: RDF.Dataset.subjects(dataset)
def predicates(dataset), do: RDF.Dataset.predicates(dataset)
def objects(dataset), do: RDF.Dataset.objects(dataset)

View file

@ -351,6 +351,12 @@ defmodule RDF.Graph do
def description(%RDF.Graph{descriptions: descriptions}, subject),
do: Map.get(descriptions, convert_subject(subject))
@doc """
All `RDF.Description`s within a `RDF.Graph`.
"""
def descriptions(%RDF.Graph{descriptions: descriptions}),
do: Map.values(descriptions)
@doc """
Gets and updates the description of the given subject, in a single pass.

View file

@ -107,6 +107,10 @@ defmodule RDF.DataTest do
assert RDF.Data.description(description, uri(EX.Other)) == Description.new(EX.Other)
end
test "descriptions", %{description: description} do
assert RDF.Data.descriptions(description) == [description]
end
test "subjects", %{description: description} do
assert RDF.Data.subjects(description) == MapSet.new([uri(EX.S)])
end
@ -225,6 +229,11 @@ defmodule RDF.DataTest do
assert RDF.Data.description(graph, uri(EX.Other)) == Description.new(EX.Other)
end
test "descriptions", %{graph: graph, description: description} do
assert RDF.Data.descriptions(graph) ==
[description, EX.S2 |> EX.p2(EX.O3, EX.O4)]
end
test "subjects", %{graph: graph} do
assert RDF.Data.subjects(graph) == MapSet.new([uri(EX.S), uri(EX.S2)])
end
@ -323,6 +332,15 @@ defmodule RDF.DataTest do
assert RDF.Data.description(dataset, uri(EX.Other)) == Description.new(EX.Other)
end
test "descriptions", %{dataset: dataset, description: description} do
description_aggregate = Description.add(description, {EX.S, EX.p3, EX.O5})
assert RDF.Data.descriptions(dataset) == [
description_aggregate,
(EX.S2 |> EX.p2(EX.O3, EX.O4)),
(EX.S3 |> EX.p3(EX.O5))
]
end
test "statements", %{dataset: dataset} do
assert RDF.Data.statements(dataset) == Dataset.statements(dataset)
end