Change RDF.Turtle.Decoder to save the prefixes in the graph
This commit is contained in:
parent
195b967b93
commit
74490c96c9
4 changed files with 29 additions and 17 deletions
|
@ -13,7 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
|
|||
- prefix management of `RDF.Graph`s:
|
||||
- the structure now has `prefixes` field with an optional `RDF.PrefixMap`
|
||||
- new functions `add_prefixes/2`, `delete_prefixes/2` and `clear_prefixes/1`
|
||||
- configurable RDF.default_prefixes
|
||||
- configurable `RDF.default_prefixes`
|
||||
|
||||
|
||||
### Changed
|
||||
|
@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
|
|||
`prefixes` field
|
||||
- when `RDF.Graph.add` and `RDF.Graph.put` are called with another graph, its
|
||||
prefixes are merged
|
||||
- `RDF.Turtle.Decoder` saves the prefixes now
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ defmodule RDF.Turtle.Decoder do
|
|||
def parse(tokens), do: tokens |> :turtle_parser.parse
|
||||
|
||||
defp build_graph(ast, base) do
|
||||
{graph, _} =
|
||||
{graph, %State{namespaces: namespaces}} =
|
||||
Enum.reduce ast, {RDF.Graph.new, %State{base_iri: base}}, fn
|
||||
{:triples, triples_ast}, {graph, state} ->
|
||||
with {statements, state} = triples(triples_ast, state) do
|
||||
|
@ -56,9 +56,15 @@ defmodule RDF.Turtle.Decoder do
|
|||
|
||||
{:directive, directive_ast}, {graph, state} ->
|
||||
{graph, directive(directive_ast, state)}
|
||||
|
||||
end
|
||||
{:ok, graph}
|
||||
|
||||
{:ok,
|
||||
if Enum.empty?(namespaces) do
|
||||
graph
|
||||
else
|
||||
RDF.Graph.add_prefixes(graph, namespaces)
|
||||
end
|
||||
}
|
||||
rescue
|
||||
error -> {:error, Exception.message(error)}
|
||||
end
|
||||
|
|
|
@ -53,8 +53,12 @@ defmodule RDF.Turtle.W3C.Test do
|
|||
|
||||
test TestSuite.test_title(test_case), %{test_case: test_case} do
|
||||
with base = to_string(TestSuite.test_input_file(test_case)) do
|
||||
assert (TestSuite.test_input_file_path(test_case, "Turtle") |> Turtle.read_file!(base: base)) ==
|
||||
(TestSuite.test_result_file_path(test_case, "Turtle") |> NTriples.read_file!)
|
||||
assert (TestSuite.test_input_file_path(test_case, "Turtle")
|
||||
|> Turtle.read_file!(base: base)
|
||||
|> RDF.Graph.clear_prefixes()
|
||||
) ==
|
||||
(TestSuite.test_result_file_path(test_case, "Turtle")
|
||||
|> NTriples.read_file!)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
|
|
@ -218,7 +218,7 @@ defmodule RDF.Turtle.DecoderTest do
|
|||
assert Turtle.Decoder.decode!("""
|
||||
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|
||||
<http://example.org/#spiderman> <http://example.org/#p> "42"^^xsd:integer .
|
||||
""") == Graph.new({EX.spiderman, EX.p, RDF.literal(42)})
|
||||
""") == Graph.new({EX.spiderman, EX.p, RDF.literal(42)}, prefixes: %{xsd: XSD})
|
||||
end
|
||||
|
||||
test "a language tagged literal" do
|
||||
|
@ -270,38 +270,39 @@ defmodule RDF.Turtle.DecoderTest do
|
|||
|
||||
describe "prefixed names" do
|
||||
test "non-empty prefixed names" do
|
||||
prefixes = RDF.PrefixMap.new(ex: ~I<http://example.org/#>)
|
||||
assert Turtle.Decoder.decode!("""
|
||||
@prefix ex: <http://example.org/#> .
|
||||
ex:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Person .
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person})
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes)
|
||||
|
||||
assert Turtle.Decoder.decode!("""
|
||||
@prefix ex: <http://example.org/#> .
|
||||
ex:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Person .
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person})
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes)
|
||||
|
||||
assert Turtle.Decoder.decode!("""
|
||||
PREFIX ex: <http://example.org/#>
|
||||
ex:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Person .
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person})
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes)
|
||||
|
||||
assert Turtle.Decoder.decode!("""
|
||||
prefix ex: <http://example.org/#>
|
||||
ex:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Person .
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person})
|
||||
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes)
|
||||
end
|
||||
|
||||
test "empty prefixed name" do
|
||||
prefixes = RDF.PrefixMap.new("": ~I<http://example.org/#>)
|
||||
assert Turtle.Decoder.decode!("""
|
||||
@prefix : <http://example.org/#> .
|
||||
:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> :Person .
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person})
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes)
|
||||
|
||||
assert Turtle.Decoder.decode!("""
|
||||
PREFIX : <http://example.org/#>
|
||||
:Aaron <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> :Person .
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person})
|
||||
""") == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -318,14 +319,14 @@ defmodule RDF.Turtle.DecoderTest do
|
|||
{RDF.bnode("b1"), RDF.rest, RDF.bnode("b2")},
|
||||
{RDF.bnode("b2"), RDF.first, EX.c},
|
||||
{RDF.bnode("b2"), RDF.rest, RDF.nil},
|
||||
])
|
||||
], prefixes: %{"": ~I<http://example.org/#>})
|
||||
end
|
||||
|
||||
test "empty collection" do
|
||||
assert Turtle.Decoder.decode!("""
|
||||
@prefix : <http://example.org/#> .
|
||||
:subject :predicate () .
|
||||
""") == Graph.new({EX.subject, EX.predicate, RDF.nil})
|
||||
""") == Graph.new({EX.subject, EX.predicate, RDF.nil}, prefixes: %{"": ~I<http://example.org/#>})
|
||||
end
|
||||
|
||||
test "nested collection" do
|
||||
|
@ -343,7 +344,7 @@ defmodule RDF.Turtle.DecoderTest do
|
|||
{RDF.bnode("b1"), RDF.rest, RDF.bnode("b2")},
|
||||
{RDF.bnode("b2"), RDF.first, EX.c},
|
||||
{RDF.bnode("b2"), RDF.rest, RDF.nil},
|
||||
])
|
||||
], prefixes: %{"": ~I<http://example.org/#>})
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue