Change RDF.Turtle.Decoder to save the base IRI in the graph

This commit is contained in:
Marcel Otto 2019-08-03 00:41:06 +02:00
parent b28e5e4744
commit 19e12909ca
3 changed files with 13 additions and 6 deletions

View file

@ -15,6 +15,11 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- `RDF.Graph.clear_metadata/1` which clears the base IRI and the prefixes
### Changed
- `RDF.Turtle.Decoder` saves the base IRI now
[Compare v0.6.1...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.6.1...HEAD)

View file

@ -49,7 +49,7 @@ defmodule RDF.Turtle.Decoder do
def parse(tokens), do: tokens |> :turtle_parser.parse
defp build_graph(ast, base) do
{graph, %State{namespaces: namespaces}} =
{graph, %State{namespaces: namespaces, base_iri: base_iri}} =
Enum.reduce ast, {RDF.Graph.new, %State{base_iri: base}}, fn
{:triples, triples_ast}, {graph, state} ->
with {statements, state} = triples(triples_ast, state) do
@ -66,6 +66,7 @@ defmodule RDF.Turtle.Decoder do
else
RDF.Graph.add_prefixes(graph, namespaces)
end
|> RDF.Graph.set_base_iri(base_iri)
}
rescue
error -> {:error, Exception.message(error)}

View file

@ -360,31 +360,32 @@ defmodule RDF.Turtle.DecoderTest do
test "without explicit in-doc base, but document_base option given" do
assert Turtle.Decoder.decode!("""
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
""", base: "http://example.org/") == Graph.new({EX.Aaron, RDF.type, EX.Person})
""", base: "http://example.org/") ==
Graph.new({EX.Aaron, RDF.type, EX.Person}, base_iri: ~I<http://example.org/>)
end
test "with @base given" do
assert Turtle.Decoder.decode!("""
@base <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}, base_iri: ~I<http://example.org/>)
assert Turtle.Decoder.decode!("""
@base <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}, base_iri: ~I<http://example.org/#>)
end
test "with BASE given" do
assert Turtle.Decoder.decode!("""
BASE <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}, base_iri: ~I<http://example.org/>)
assert Turtle.Decoder.decode!("""
base <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}, base_iri: ~I<http://example.org/#>)
end
test "when a given base is itself relative" do