core: NQuads encoder
This commit is contained in:
parent
ec7f4b3e7e
commit
7a2c165bbb
2 changed files with 147 additions and 0 deletions
29
lib/rdf/serializations/nquads_encoder.ex
Normal file
29
lib/rdf/serializations/nquads_encoder.ex
Normal file
|
@ -0,0 +1,29 @@
|
|||
defmodule RDF.NQuads.Encoder do
|
||||
use RDF.Serialization.Encoder
|
||||
|
||||
def encode(data, opts \\ []) do
|
||||
result =
|
||||
data
|
||||
|> Enum.reduce([], fn (statement, result) ->
|
||||
[statement(statement) | result]
|
||||
end)
|
||||
|> Enum.reverse
|
||||
|> Enum.join("\n")
|
||||
{:ok, (if result == "", do: result, else: result <> "\n")}
|
||||
end
|
||||
|
||||
def statement({subject, predicate, object, nil}) do
|
||||
statement({subject, predicate, object})
|
||||
end
|
||||
|
||||
def statement({subject, predicate, object, graph}) do
|
||||
"#{term(subject)} #{term(predicate)} #{term(object)} #{term(graph)} ."
|
||||
end
|
||||
|
||||
def statement({subject, predicate, object}) do
|
||||
"#{term(subject)} #{term(predicate)} #{term(object)} ."
|
||||
end
|
||||
|
||||
defdelegate term(value), to: RDF.NTriples.Encoder
|
||||
|
||||
end
|
118
test/unit/nquads_encoder_test.exs
Normal file
118
test/unit/nquads_encoder_test.exs
Normal file
|
@ -0,0 +1,118 @@
|
|||
defmodule RDF.NQuads.EncoderTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias RDF.NQuads
|
||||
|
||||
doctest NQuads.Encoder
|
||||
|
||||
alias RDF.{Dataset, Graph}
|
||||
alias RDF.NS.XSD
|
||||
|
||||
import RDF.Sigils
|
||||
|
||||
use RDF.Vocabulary.Namespace
|
||||
|
||||
defvocab EX,
|
||||
base_uri: "http://example.org/#",
|
||||
terms: [], strict: false
|
||||
|
||||
|
||||
describe "serializing a graph" do
|
||||
test "an empty graph is serialized to an empty string" do
|
||||
assert NQuads.Encoder.encode!(Graph.new) == ""
|
||||
end
|
||||
|
||||
test "statements with URIs only" do
|
||||
assert NQuads.Encoder.encode!(Graph.new [
|
||||
{EX.S1, EX.p1, EX.O1},
|
||||
{EX.S1, EX.p1, EX.O2},
|
||||
{EX.S1, EX.p2, EX.O3},
|
||||
{EX.S2, EX.p3, EX.O4},
|
||||
]) ==
|
||||
"""
|
||||
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> .
|
||||
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O2> .
|
||||
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O3> .
|
||||
<http://example.org/#S2> <http://example.org/#p3> <http://example.org/#O4> .
|
||||
"""
|
||||
end
|
||||
|
||||
test "statements with literals" do
|
||||
assert NQuads.Encoder.encode!(Graph.new [
|
||||
{EX.S1, EX.p1, ~L"foo"},
|
||||
{EX.S1, EX.p1, ~L"foo"en},
|
||||
{EX.S1, EX.p2, 42},
|
||||
{EX.S2, EX.p3, RDF.literal("strange things", datatype: EX.custom)},
|
||||
]) ==
|
||||
"""
|
||||
<http://example.org/#S1> <http://example.org/#p1> "foo"@en .
|
||||
<http://example.org/#S1> <http://example.org/#p1> "foo" .
|
||||
<http://example.org/#S1> <http://example.org/#p2> "42"^^<#{XSD.integer}> .
|
||||
<http://example.org/#S2> <http://example.org/#p3> "strange things"^^<#{EX.custom}> .
|
||||
"""
|
||||
end
|
||||
|
||||
test "statements with blank nodes" do
|
||||
assert NQuads.Encoder.encode!(Graph.new [
|
||||
{EX.S1, EX.p1, RDF.bnode(1)},
|
||||
{EX.S1, EX.p1, RDF.bnode("foo")},
|
||||
{EX.S1, EX.p1, RDF.bnode(:bar)},
|
||||
]) ==
|
||||
"""
|
||||
<http://example.org/#S1> <http://example.org/#p1> _:1 .
|
||||
<http://example.org/#S1> <http://example.org/#p1> _:bar .
|
||||
<http://example.org/#S1> <http://example.org/#p1> _:foo .
|
||||
"""
|
||||
end
|
||||
end
|
||||
|
||||
describe "serializing a dataset" do
|
||||
test "an empty dataset is serialized to an empty string" do
|
||||
assert NQuads.Encoder.encode!(Dataset.new) == ""
|
||||
end
|
||||
|
||||
test "statements with URIs only" do
|
||||
assert NQuads.Encoder.encode!(Dataset.new [
|
||||
{EX.S1, EX.p1, EX.O1, EX.G},
|
||||
{EX.S1, EX.p1, EX.O2, EX.G},
|
||||
{EX.S1, EX.p2, EX.O3, EX.G},
|
||||
{EX.S2, EX.p3, EX.O4},
|
||||
]) ==
|
||||
"""
|
||||
<http://example.org/#S2> <http://example.org/#p3> <http://example.org/#O4> .
|
||||
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> <http://example.org/#G> .
|
||||
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O2> <http://example.org/#G> .
|
||||
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O3> <http://example.org/#G> .
|
||||
"""
|
||||
end
|
||||
|
||||
test "statements with literals" do
|
||||
assert NQuads.Encoder.encode!(Dataset.new [
|
||||
{EX.S1, EX.p1, ~L"foo", EX.G1},
|
||||
{EX.S1, EX.p1, ~L"foo"en, EX.G2},
|
||||
{EX.S1, EX.p2, 42, EX.G3},
|
||||
{EX.S2, EX.p3, RDF.literal("strange things", datatype: EX.custom), EX.G3},
|
||||
]) ==
|
||||
"""
|
||||
<http://example.org/#S1> <http://example.org/#p1> "foo" <http://example.org/#G1> .
|
||||
<http://example.org/#S1> <http://example.org/#p1> "foo"@en <http://example.org/#G2> .
|
||||
<http://example.org/#S1> <http://example.org/#p2> "42"^^<#{XSD.integer}> <http://example.org/#G3> .
|
||||
<http://example.org/#S2> <http://example.org/#p3> "strange things"^^<#{EX.custom}> <http://example.org/#G3> .
|
||||
"""
|
||||
end
|
||||
|
||||
test "statements with blank nodes" do
|
||||
assert NQuads.Encoder.encode!(Dataset.new [
|
||||
{EX.S1, EX.p1, RDF.bnode(1)},
|
||||
{EX.S1, EX.p1, RDF.bnode("foo"), EX.G},
|
||||
{EX.S1, EX.p1, RDF.bnode(:bar)},
|
||||
]) ==
|
||||
"""
|
||||
<http://example.org/#S1> <http://example.org/#p1> _:1 .
|
||||
<http://example.org/#S1> <http://example.org/#p1> _:bar .
|
||||
<http://example.org/#S1> <http://example.org/#p1> _:foo <http://example.org/#G> .
|
||||
"""
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue