core: NTriples encoder
This commit is contained in:
parent
cd7e91ec9d
commit
ec7f4b3e7e
2 changed files with 116 additions and 0 deletions
47
lib/rdf/serializations/ntriples_encoder.ex
Normal file
47
lib/rdf/serializations/ntriples_encoder.ex
Normal file
|
@ -0,0 +1,47 @@
|
|||
defmodule RDF.NTriples.Encoder do
|
||||
use RDF.Serialization.Encoder
|
||||
|
||||
alias RDF.{Literal, BlankNode}
|
||||
|
||||
@xsd_string RDF.Datatype.NS.XSD.string
|
||||
|
||||
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}) do
|
||||
"#{term(subject)} #{term(predicate)} #{term(object)} ."
|
||||
end
|
||||
|
||||
def term(%URI{} = uri) do
|
||||
"<#{to_string(uri)}>"
|
||||
end
|
||||
|
||||
def term(%Literal{value: value, language: language}) when not is_nil(language) do
|
||||
~s["#{value}"@#{language}]
|
||||
end
|
||||
|
||||
def term(%Literal{value: value, language: language}) when not is_nil(language) do
|
||||
~s["#{value}"@#{language}]
|
||||
end
|
||||
|
||||
def term(%Literal{datatype: @xsd_string} = literal) do
|
||||
~s["#{Literal.lexical(literal)}"]
|
||||
end
|
||||
|
||||
def term(%Literal{datatype: datatype} = literal) do
|
||||
~s["#{Literal.lexical(literal)}"^^<#{to_string(datatype)}>]
|
||||
end
|
||||
|
||||
def term(%BlankNode{} = bnode) do
|
||||
to_string(bnode)
|
||||
end
|
||||
|
||||
end
|
69
test/unit/ntriples_encoder_test.exs
Normal file
69
test/unit/ntriples_encoder_test.exs
Normal file
|
@ -0,0 +1,69 @@
|
|||
defmodule RDF.NTriples.EncoderTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias RDF.NTriples
|
||||
|
||||
doctest NTriples.Encoder
|
||||
|
||||
alias RDF.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 NTriples.Encoder.encode!(Graph.new) == ""
|
||||
end
|
||||
|
||||
test "statements with URIs only" do
|
||||
assert NTriples.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 NTriples.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 NTriples.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
|
||||
|
||||
end
|
Loading…
Reference in a new issue