From 79044763216e9f307f0092cd76463a127876f0ca Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sat, 10 Jun 2017 18:02:51 +0200 Subject: [PATCH] core: RDF.Vocabulary.Namespaces can be constructed from NQuad files --- lib/rdf/vocabulary_namespace.ex | 9 +++++- test/data/vocab_ns_example.nq | 2 ++ ...cab_ns_example2.nt => vocab_ns_example.nt} | 0 test/unit/vocabulary_namespace_test.exs | 28 ++++++++++++++----- 4 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 test/data/vocab_ns_example.nq rename test/data/{vocab_ns_example2.nt => vocab_ns_example.nt} (100%) diff --git a/lib/rdf/vocabulary_namespace.ex b/lib/rdf/vocabulary_namespace.ex index bc0eb7b..ced683f 100644 --- a/lib/rdf/vocabulary_namespace.ex +++ b/lib/rdf/vocabulary_namespace.ex @@ -462,7 +462,14 @@ defmodule RDF.Vocabulary.Namespace do end defp load_file(file) do - RDF.NTriples.read_file!(file) # TODO: support other formats + # TODO: support other formats + cond do + String.ends_with?(file, ".nt") -> RDF.NTriples.read_file!(file) + String.ends_with?(file, ".nq") -> RDF.NQuads.read_file!(file) + true -> + raise ArgumentError, + "unsupported file type for #{file}: vocabulary namespaces can currently be created from NTriple and NQuad files" + end end defp rdf_data_env do diff --git a/test/data/vocab_ns_example.nq b/test/data/vocab_ns_example.nq new file mode 100644 index 0000000..a3d0256 --- /dev/null +++ b/test/data/vocab_ns_example.nq @@ -0,0 +1,2 @@ + . + . diff --git a/test/data/vocab_ns_example2.nt b/test/data/vocab_ns_example.nt similarity index 100% rename from test/data/vocab_ns_example2.nt rename to test/data/vocab_ns_example.nt diff --git a/test/unit/vocabulary_namespace_test.exs b/test/unit/vocabulary_namespace_test.exs index 4f16891..1a1632f 100644 --- a/test/unit/vocabulary_namespace_test.exs +++ b/test/unit/vocabulary_namespace_test.exs @@ -35,7 +35,11 @@ defmodule RDF.Vocabulary.NamespaceTest do defvocab ExampleFromNTriplesFile, base_uri: "http://example.com/from_ntriples/", - file: "test/data/vocab_ns_example2.nt" + file: "test/data/vocab_ns_example.nt" + + defvocab ExampleFromNQuadsFile, + base_uri: "http://example.com/from_nquads/", + file: "test/data/vocab_ns_example.nq" defvocab StrictExampleFromTerms, base_uri: "http://example.com/strict_from_terms#", @@ -351,12 +355,22 @@ defmodule RDF.Vocabulary.NamespaceTest do assert RDF.uri(Example1.foo) in Example1.__uris__ assert RDF.uri(Example1.Bar) in Example1.__uris__ - alias TestNS.StrictExampleFromAliasedTerms, as: Example2 - assert length(Example2.__uris__) == 4 - assert RDF.uri(Example2.Term1) in Example2.__uris__ - assert RDF.uri(Example2.term2) in Example2.__uris__ - assert RDF.uri(Example2.Term3) in Example2.__uris__ - assert RDF.uri(Example2.term4) in Example2.__uris__ + alias TestNS.ExampleFromNTriplesFile, as: Example2 + assert length(Example2.__uris__) == 2 + assert RDF.uri(Example2.foo) in Example2.__uris__ + assert RDF.uri(Example2.Bar) in Example2.__uris__ + + alias TestNS.ExampleFromNQuadsFile, as: Example3 + assert length(Example3.__uris__) == 2 + assert RDF.uri(Example3.foo) in Example3.__uris__ + assert RDF.uri(Example3.Bar) in Example3.__uris__ + + alias TestNS.StrictExampleFromAliasedTerms, as: Example4 + assert length(Example4.__uris__) == 4 + assert RDF.uri(Example4.Term1) in Example4.__uris__ + assert RDF.uri(Example4.term2) in Example4.__uris__ + assert RDF.uri(Example4.Term3) in Example4.__uris__ + assert RDF.uri(Example4.term4) in Example4.__uris__ end