Add support for N-Quads-star decoding

This commit is contained in:
Marcel Otto 2021-11-10 00:48:33 +01:00
parent f9c72c1a35
commit c753a7a30e
11 changed files with 85 additions and 2 deletions

View file

@ -1,7 +1,7 @@
%% Grammar for N-Quads as specified in http://www.w3.org/TR/2014/REC-n-quads-20140225/
Nonterminals nquadsDoc nonEmptyNquadsDoc statement subject predicate object graphLabel literal eols.
Terminals iriref blank_node_label string_literal_quote langtag '^^' '.' eol.
Nonterminals nquadsDoc nonEmptyNquadsDoc statement subject predicate object graphLabel literal quoted_triple eols.
Terminals iriref blank_node_label string_literal_quote langtag '^^' '.' '<<' '>>' eol.
Rootsymbol nquadsDoc.
eols -> eols eol.
@ -25,10 +25,12 @@ statement -> subject predicate object '.' : { '$1', '$2', '$3' }.
subject -> iriref : to_iri('$1').
subject -> blank_node_label : to_bnode('$1').
subject -> quoted_triple : '$1'.
predicate -> iriref : to_iri('$1').
object -> iriref : to_iri('$1').
object -> blank_node_label : to_bnode('$1').
object -> literal : '$1'.
object -> quoted_triple : '$1'.
graphLabel -> iriref : to_iri('$1').
graphLabel -> blank_node_label : to_bnode('$1').
@ -36,6 +38,7 @@ literal -> string_literal_quote '^^' iriref : to_literal('$1', {datatype, to_iri
literal -> string_literal_quote langtag : to_literal('$1', {language, to_langtag('$2')}).
literal -> string_literal_quote : to_literal('$1').
quoted_triple -> '<<' subject predicate object '>>' : { '$2', '$3', '$4' }.
Erlang code.

View file

@ -0,0 +1,65 @@
defmodule RDF.Star.NQuads.TestSuite do
@moduledoc """
RDF-star N-Quads Test Suite.
This runs the official RDF-star N-Triples Test Suite and variations of its
test files with added graph names against the `RDF.NQuads.Decoder`.
"""
use ExUnit.Case, async: false
import RDF.Sigils
@ntriples_star_test_suite_path "rdf-star/nt/syntax"
@nquads_star_test_suite_path "rdf-star/nq/syntax"
@ntriples_star_test_suite Path.join(RDF.TestData.dir(), @ntriples_star_test_suite_path)
@nquads_star_test_suite Path.join(RDF.TestData.dir(), @nquads_star_test_suite_path)
ExUnit.Case.register_attribute(__ENV__, :nt_test)
ExUnit.Case.register_attribute(__ENV__, :nq_test)
@ntriples_star_test_suite
|> File.ls!()
|> Enum.filter(fn file -> Path.extname(file) == ".nt" end)
|> Enum.each(fn file ->
@nt_test file: Path.join(@ntriples_star_test_suite, file)
if file |> String.contains?("-bad-") do
test "Negative syntax test: #{file}", context do
assert {:error, _} = RDF.NQuads.read_file(context.registered.nt_test[:file])
end
else
test "Positive syntax test: #{file}", context do
assert {:ok, %RDF.Dataset{}} = RDF.NQuads.read_file(context.registered.nt_test[:file])
end
end
end)
@nquads_star_test_suite
|> File.ls!()
|> Enum.filter(fn file -> Path.extname(file) == ".nq" end)
|> Enum.each(fn file ->
@nq_test file: Path.join(@nquads_star_test_suite, file)
test "Positive syntax test: #{file}", context do
nq_test_file = context.registered.nq_test[:file]
assert {:ok, %RDF.Dataset{} = dataset1} = RDF.NQuads.read_file(nq_test_file)
nt_test_file =
nq_test_file
|> String.replace(
@nquads_star_test_suite_path <> "/nquads-",
@ntriples_star_test_suite_path <> "/ntriples-"
)
|> String.replace(~r/\.nq$/, ".nt")
assert {:ok, %RDF.Dataset{} = dataset2} = RDF.NQuads.read_file(nt_test_file)
assert RDF.Dataset.graph_count(dataset1) == 1
assert RDF.Dataset.graph_count(dataset2) == 1
assert %RDF.Graph{} = graph1 = RDF.Dataset.graph(dataset1, ~I<http://example/Graph>)
assert %RDF.Graph{} = graph2 = RDF.Dataset.default_graph(dataset2)
#
assert RDF.Graph.change_name(graph1, nil) == graph2
end
end)
end

View file

@ -0,0 +1,2 @@
_:b0 <http://example/p> <http://example/o> <http://example/Graph> .
<< _:b0 <http://example/p> <http://example/o> >> <http://example/q> "ABC" <http://example/Graph> .

View file

@ -0,0 +1,2 @@
<http://example/s> <http://example/p> _:b1 <http://example/Graph> .
<< <http://example/s> <http://example/p> _:b1 >> <http://example/q> "456"^^<http://www.w3.org/2001/XMLSchema#integer> <http://example/Graph> .

View file

@ -0,0 +1,3 @@
<http://example/s> <http://example/p> <http://example/o> <http://example/Graph> .
<< <http://example/s> <http://example/p> <http://example/o> >> <http://example/r> <http://example/z> <http://example/Graph> .
<< << <http://example/s> <http://example/p> <http://example/o> >> <http://example/r> <http://example/z> >> <http://example/q> "1"^^<http://www.w3.org/2001/XMLSchema#integer> <http://example/Graph> .

View file

@ -0,0 +1,3 @@
<http://example/s> <http://example/p> <http://example/o> <http://example/Graph> .
<http://example/a> <http://example/q> << <http://example/s> <http://example/p> <http://example/o> >> <http://example/Graph> .
<< <http://example/a> <http://example/q> << <http://example/s> <http://example/p> <http://example/o> >> >> <http://example/r> <http://example/z> <http://example/Graph> .

View file

@ -0,0 +1 @@
<< <http://example/s> <http://example/p> <http://example/o> >> <http://example/q> <http://example/z> <http://example/Graph> .

View file

@ -0,0 +1 @@
<http://example/x> <http://example/p> << <http://example/s> <http://example/p> <http://example/o> >> <http://example/Graph> .

View file

@ -0,0 +1 @@
<< <http://example/s1> <http://example/p1> <http://example/o1> >> <http://example/q> << <http://example/s2> <http://example/p2> <http://example/o2> >> <http://example/Graph> .

View file

@ -0,0 +1 @@
<<<http://example/s1><http://example/p1><http://example/o1>>><http://example/q><<<http://example/s2><http://example/p2><http://example/o2>>><http://example/Graph>.

View file

@ -0,0 +1 @@
<<<<<http://example/s1><http://example/p1><http://example/o1>>><http://example/q1><<<http://example/s2><http://example/p2><http://example/o2>>>>><http://example/q2><<<<<http://example/s3><http://example/p3><http://example/o3>>><http://example/q3><<<http://example/s4><http://example/p4><http://example/o4>>>>><http://example/Graph>.