Add RDF.NQuads.Encoder.stream/2

This commit is contained in:
Marcel Otto 2020-11-04 21:09:29 +01:00
parent 426b6d832c
commit a361a018a0
3 changed files with 74 additions and 9 deletions

View file

@ -9,7 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Added
- serialization functions for reading from and writing to streams
- general serialization functions for reading from and writing to streams
and implementations for N-Triples and N-Quads (Turtle still to come)
- `RDF.Dataset.prefixes/1` for getting an aggregated `RDF.PrefixMap` over all graphs
- `RDF.PrefixMap.put/3` for adding a prefix mapping and overwrite an existing one
- `RDF.BlankNode.value/1` for getting the internal string representation of a blank node

View file

@ -8,13 +8,20 @@ defmodule RDF.NQuads.Encoder do
@impl RDF.Serialization.Encoder
@callback encode(RDF.Data.t(), keyword) :: {:ok, String.t()} | {:error, any}
def encode(data, _opts \\ []) do
result =
data
|> Enum.reduce([], &[statement(&1) | &2])
|> Enum.reverse()
|> Enum.join("\n")
{:ok,
data
|> Enum.reduce([], &[statement(&1) | &2])
|> Enum.reverse()
|> Enum.join()}
end
{:ok, if(result == "", do: result, else: "#{result}\n")}
@impl RDF.Serialization.Encoder
@spec stream(RDF.Data.t(), keyword) :: Enumerable.t()
def stream(data, opts \\ []) do
case Keyword.get(opts, :mode, :string) do
:string -> Stream.map(data, &statement(&1))
:iodata -> Stream.map(data, &iolist_statement(&1))
end
end
@spec statement(Statement.t()) :: String.t()
@ -25,12 +32,38 @@ defmodule RDF.NQuads.Encoder do
end
def statement({subject, predicate, object, graph}) do
"#{term(subject)} #{term(predicate)} #{term(object)} #{term(graph)} ."
"#{term(subject)} #{term(predicate)} #{term(object)} #{term(graph)} .\n"
end
def statement({subject, predicate, object}) do
"#{term(subject)} #{term(predicate)} #{term(object)} ."
"#{term(subject)} #{term(predicate)} #{term(object)} .\n"
end
defdelegate term(value), to: RDF.NTriples.Encoder
@spec iolist_statement(Statement.t()) :: iolist
def iolist_statement(statement)
def iolist_statement({subject, predicate, object, nil}) do
iolist_statement({subject, predicate, object})
end
def iolist_statement({subject, predicate, object, graph}) do
[
iolist_term(subject),
" ",
iolist_term(predicate),
" ",
iolist_term(object),
" ",
iolist_term(graph),
" .\n"
]
end
def iolist_statement({subject, predicate, object}) do
[iolist_term(subject), " ", iolist_term(predicate), " ", iolist_term(object), " .\n"]
end
defdelegate iolist_term(value), to: RDF.NTriples.Encoder
end

View file

@ -14,6 +14,10 @@ defmodule RDF.NQuads.EncoderTest do
defvocab EX, base_iri: "http://example.org/#", terms: [], strict: false
test "stream_support?/0" do
assert NQuads.Encoder.stream_support?()
end
describe "serializing a graph" do
test "an empty graph is serialized to an empty string" do
assert NQuads.Encoder.encode!(Graph.new()) == ""
@ -123,4 +127,31 @@ defmodule RDF.NQuads.EncoderTest do
"""
end
end
describe "stream/2" do
dataset =
Dataset.new([
{EX.S1, EX.p1(), EX.O1},
{EX.S2, EX.p2(), RDF.bnode("foo"), EX.G},
{EX.S3, EX.p3(), ~L"foo"},
{EX.S3, EX.p3(), ~L"foo"en, EX.G}
])
expected_result = """
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> .
<http://example.org/#S3> <http://example.org/#p3> "foo" .
<http://example.org/#S2> <http://example.org/#p2> _:foo <http://example.org/#G> .
<http://example.org/#S3> <http://example.org/#p3> "foo"@en <http://example.org/#G> .
"""
assert NQuads.Encoder.stream(dataset, mode: :string)
|> Enum.to_list()
|> IO.iodata_to_binary() ==
expected_result
assert NQuads.Encoder.stream(dataset, mode: :iodata)
|> Enum.to_list()
|> IO.iodata_to_binary() ==
expected_result
end
end