Add support for N-Triples-star encoding

This commit is contained in:
Marcel Otto 2021-09-29 22:43:01 +02:00
parent f835f6611e
commit 8b609233fa
2 changed files with 29 additions and 0 deletions

View file

@ -57,6 +57,10 @@ defmodule RDF.NTriples.Encoder do
to_string(bnode)
end
def term({s, p, o}) do
"<< #{term(s)} #{term(p)} #{term(o)} >>"
end
@spec iolist_statement(Triple.t()) :: iolist
def iolist_statement({subject, predicate, object}) do
[iolist_term(subject), " ", iolist_term(predicate), " ", iolist_term(object), " .\n"]

View file

@ -0,0 +1,25 @@
defmodule RDF.Star.NTriples.EncoderTest do
use RDF.Test.Case
alias RDF.NTriples
test "annotations of triples on subject position" do
assert NTriples.Encoder.encode!(graph_with_annotation()) ==
"""
<< <http://example.com/S> <http://example.com/P> "Foo" >> <http://example.com/ap> <http://example.com/ao> .
"""
end
test "annotations of triples on object position" do
assert NTriples.Encoder.encode!(
Graph.new([
statement(),
{EX.AS, EX.ap(), statement()},
])
) ==
"""
<http://example.com/AS> <http://example.com/ap> << <http://example.com/S> <http://example.com/P> "Foo" >> .
<http://example.com/S> <http://example.com/P> "Foo" .
"""
end
end