Change RDF.Turtle.Decoder to save the base IRI in the graph
This commit is contained in:
parent
b28e5e4744
commit
19e12909ca
3 changed files with 13 additions and 6 deletions
|
@ -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
|
- `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)
|
[Compare v0.6.1...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.6.1...HEAD)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ defmodule RDF.Turtle.Decoder do
|
||||||
def parse(tokens), do: tokens |> :turtle_parser.parse
|
def parse(tokens), do: tokens |> :turtle_parser.parse
|
||||||
|
|
||||||
defp build_graph(ast, base) do
|
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
|
Enum.reduce ast, {RDF.Graph.new, %State{base_iri: base}}, fn
|
||||||
{:triples, triples_ast}, {graph, state} ->
|
{:triples, triples_ast}, {graph, state} ->
|
||||||
with {statements, state} = triples(triples_ast, state) do
|
with {statements, state} = triples(triples_ast, state) do
|
||||||
|
@ -66,6 +66,7 @@ defmodule RDF.Turtle.Decoder do
|
||||||
else
|
else
|
||||||
RDF.Graph.add_prefixes(graph, namespaces)
|
RDF.Graph.add_prefixes(graph, namespaces)
|
||||||
end
|
end
|
||||||
|
|> RDF.Graph.set_base_iri(base_iri)
|
||||||
}
|
}
|
||||||
rescue
|
rescue
|
||||||
error -> {:error, Exception.message(error)}
|
error -> {:error, Exception.message(error)}
|
||||||
|
|
|
@ -360,31 +360,32 @@ defmodule RDF.Turtle.DecoderTest do
|
||||||
test "without explicit in-doc base, but document_base option given" do
|
test "without explicit in-doc base, but document_base option given" do
|
||||||
assert Turtle.Decoder.decode!("""
|
assert Turtle.Decoder.decode!("""
|
||||||
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
|
<#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
|
end
|
||||||
|
|
||||||
test "with @base given" do
|
test "with @base given" do
|
||||||
assert Turtle.Decoder.decode!("""
|
assert Turtle.Decoder.decode!("""
|
||||||
@base <http://example.org/> .
|
@base <http://example.org/> .
|
||||||
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
|
<#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!("""
|
assert Turtle.Decoder.decode!("""
|
||||||
@base <http://example.org/#> .
|
@base <http://example.org/#> .
|
||||||
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
|
<#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
|
end
|
||||||
|
|
||||||
test "with BASE given" do
|
test "with BASE given" do
|
||||||
assert Turtle.Decoder.decode!("""
|
assert Turtle.Decoder.decode!("""
|
||||||
BASE <http://example.org/>
|
BASE <http://example.org/>
|
||||||
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
|
<#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!("""
|
assert Turtle.Decoder.decode!("""
|
||||||
base <http://example.org/#>
|
base <http://example.org/#>
|
||||||
<#Aaron> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <#Person> .
|
<#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
|
end
|
||||||
|
|
||||||
test "when a given base is itself relative" do
|
test "when a given base is itself relative" do
|
||||||
|
|
Loading…
Reference in a new issue