Add support for N-Triples-star decoding

This commit is contained in:
Marcel Otto 2021-09-28 20:57:34 +02:00
parent 2819092586
commit f835f6611e
17 changed files with 156 additions and 2 deletions

View file

@ -24,6 +24,8 @@ Rules.
{EOL} : {token, {eol, TokenLine}}. {EOL} : {token, {eol, TokenLine}}.
\. : {token, {'.', TokenLine}}. \. : {token, {'.', TokenLine}}.
\^\^ : {token, {'^^', TokenLine}}. \^\^ : {token, {'^^', TokenLine}}.
\<\< : {token, {'<<', TokenLine}}.
\>\> : {token, {'>>', TokenLine}}.
{WHITESPACE}+ : skip_token. {WHITESPACE}+ : skip_token.
{COMMENT} : skip_token. {COMMENT} : skip_token.

View file

@ -1,7 +1,7 @@
%% Grammar for N-Triples as specified in https://www.w3.org/TR/2014/REC-n-triples-20140225/ %% Grammar for N-Triples as specified in https://www.w3.org/TR/2014/REC-n-triples-20140225/
Nonterminals ntriplesDoc nonEmptyNtriplesDoc triple subject predicate object literal eols. Nonterminals ntriplesDoc nonEmptyNtriplesDoc triple subject predicate object literal quoted_triple eols.
Terminals iriref blank_node_label string_literal_quote langtag '^^' '.' eol. Terminals iriref blank_node_label string_literal_quote langtag '^^' '.' '<<' '>>' eol.
Rootsymbol ntriplesDoc. Rootsymbol ntriplesDoc.
eols -> eols eol. eols -> eols eol.
@ -24,15 +24,18 @@ triple -> subject predicate object '.' : { '$1', '$2', '$3' }.
subject -> iriref : to_iri('$1'). subject -> iriref : to_iri('$1').
subject -> blank_node_label : to_bnode('$1'). subject -> blank_node_label : to_bnode('$1').
subject -> quoted_triple : '$1'.
predicate -> iriref : to_iri('$1'). predicate -> iriref : to_iri('$1').
object -> iriref : to_iri('$1'). object -> iriref : to_iri('$1').
object -> blank_node_label : to_bnode('$1'). object -> blank_node_label : to_bnode('$1').
object -> literal : '$1'. object -> literal : '$1'.
object -> quoted_triple : '$1'.
literal -> string_literal_quote '^^' iriref : to_literal('$1', {datatype, to_iri('$3')}). literal -> string_literal_quote '^^' iriref : to_literal('$1', {datatype, to_iri('$3')}).
literal -> string_literal_quote langtag : to_literal('$1', {language, to_langtag('$2')}). literal -> string_literal_quote langtag : to_literal('$1', {language, to_langtag('$2')}).
literal -> string_literal_quote : to_literal('$1'). literal -> string_literal_quote : to_literal('$1').
quoted_triple -> '<<' subject predicate object '>>' : { '$2', '$3', '$4' }.
Erlang code. Erlang code.

View file

@ -0,0 +1,29 @@
defmodule RDF.Star.NTriples.W3C.TestSuite do
@moduledoc """
The official RDF-star N-Triples Test Suite.
from <https://w3c.github.io/rdf-star/tests/nt/syntax/>
"""
use ExUnit.Case, async: false
@ntriples_star_test_suite Path.join(RDF.TestData.dir(), "rdf-star/nt/syntax")
ExUnit.Case.register_attribute(__ENV__, :nt_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.NTriples.read_file(context.registered.nt_test[:file])
end
else
test "Positive syntax test: #{file}", context do
assert {:ok, %RDF.Graph{}} = RDF.NTriples.read_file(context.registered.nt_test[:file])
end
end
end)
end

View file

@ -0,0 +1,101 @@
## Distributed under both the "W3C Test Suite License" [1]
## and the "W3C 3-clause BSD License".
## [1] https://www.w3.org/Consortium/Legal/2008/04-testsuite-license
## [2] https://www.w3.org/Consortium/Legal/2008/03-bsd-license
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>
PREFIX rdft: <http://www.w3.org/ns/rdftest#>
PREFIX trs: <https://w3c.github.io/rdf-star/tests/nt/syntax#>
trs:manifest rdf:type mf:Manifest ;
rdfs:label "N-Triples-star Syntax Tests" ;
mf:entries
(
trs:ntriples-star-1
trs:ntriples-star-2
trs:ntriples-star-3
trs:ntriples-star-4
trs:ntriples-star-5
trs:ntriples-star-bnode-1
trs:ntriples-star-bnode-2
trs:ntriples-star-nested-1
trs:ntriples-star-nested-2
trs:ntriples-star-bad-1
trs:ntriples-star-bad-2
trs:ntriples-star-bad-3
trs:ntriples-star-bad-4
) .
trs:ntriples-star-1 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - subject quoted triple" ;
mf:action <ntriples-star-syntax-1.nt> ;
.
trs:ntriples-star-2 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - object quoted triple" ;
mf:action <ntriples-star-syntax-2.nt> ;
.
trs:ntriples-star-3 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - subject and object quoted triples" ;
mf:action <ntriples-star-syntax-3.nt> ;
.
trs:ntriples-star-4 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - whitespace and terms" ;
mf:action <ntriples-star-syntax-4.nt> ;
.
trs:ntriples-star-5 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - Nested, no whitespace" ;
mf:action <ntriples-star-syntax-5.nt> ;
.
# Blank nodes
trs:ntriples-star-bnode-1 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - Blank node subject" ;
mf:action <ntriples-star-bnode-1.nt> ;
.
trs:ntriples-star-bnode-2 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - Blank node object" ;
mf:action <ntriples-star-bnode-2.nt> ;
.
trs:ntriples-star-nested-1 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - Nested subject term" ;
mf:action <ntriples-star-nested-1.nt> ;
.
trs:ntriples-star-nested-2 rdf:type rdft:TestNTriplesPositiveSyntax ;
mf:name "N-Triples-star - Nested object term" ;
mf:action <ntriples-star-nested-2.nt> ;
.
## Bad syntax
trs:ntriples-star-bad-1 rdf:type rdft:TestNTriplesNegativeSyntax ;
mf:name "N-Triples-star - Bad - quoted triple as predicate" ;
mf:action <ntriples-star-bad-syntax-1.nt> ;
.
trs:ntriples-star-bad-2 rdf:type rdft:TestNTriplesNegativeSyntax ;
mf:name "N-Triples-star - Bad - quoted triple, literal subject" ;
mf:action <ntriples-star-bad-syntax-2.nt> ;
.
trs:ntriples-star-bad-3 rdf:type rdft:TestNTriplesNegativeSyntax ;
mf:name "N-Triples-star - Bad - quoted triple, literal predicate" ;
mf:action <ntriples-star-bad-syntax-3.nt> ;
.
trs:ntriples-star-bad-4 rdf:type rdft:TestNTriplesNegativeSyntax ;
mf:name "N-Triples-star - Bad - quoted triple, blank node predicate" ;
mf:action <ntriples-star-bad-syntax-4.nt> ;
.

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,3 @@
<http://example/s> <http://example/p> <http://example/o> .
<< <http://example/s> <http://example/p> <http://example/o> >> <http://example/r> <http://example/z> .
<< << <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> .

View file

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

View file

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

View file

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

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> >> .

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>>>.

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>>>>>.