From 19e12909cadaf826cde0d86c1ac83fba8e0764df Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sat, 3 Aug 2019 00:41:06 +0200 Subject: [PATCH] Change RDF.Turtle.Decoder to save the base IRI in the graph --- CHANGELOG.md | 5 +++++ lib/rdf/serializations/turtle_decoder.ex | 3 ++- test/unit/turtle_decoder_test.exs | 11 ++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6daf7ac..881bfd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rdf/serializations/turtle_decoder.ex b/lib/rdf/serializations/turtle_decoder.ex index 394ce95..6c919aa 100644 --- a/lib/rdf/serializations/turtle_decoder.ex +++ b/lib/rdf/serializations/turtle_decoder.ex @@ -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)} diff --git a/test/unit/turtle_decoder_test.exs b/test/unit/turtle_decoder_test.exs index 8356de4..09a785c 100644 --- a/test/unit/turtle_decoder_test.exs +++ b/test/unit/turtle_decoder_test.exs @@ -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> <#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) end test "with @base given" do assert Turtle.Decoder.decode!(""" @base . <#Aaron> <#Person> . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, base_iri: ~I) assert Turtle.Decoder.decode!(""" @base . <#Aaron> <#Person> . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, base_iri: ~I) end test "with BASE given" do assert Turtle.Decoder.decode!(""" BASE <#Aaron> <#Person> . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, base_iri: ~I) assert Turtle.Decoder.decode!(""" base <#Aaron> <#Person> . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, base_iri: ~I) end test "when a given base is itself relative" do