2017-03-12 13:27:52 +00:00
|
|
|
defmodule RDF.Vocabulary.Namespace do
|
|
|
|
@moduledoc """
|
2017-06-10 19:52:16 +00:00
|
|
|
A RDF vocabulary as a `RDF.Namespace`.
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
`RDF.Vocabulary.Namespace` modules represent a RDF vocabulary as a `RDF.Namespace`.
|
|
|
|
They can be defined with the `defvocab/2` macro of this module.
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
RDF.ex comes with predefined modules for some fundamentals vocabularies in
|
|
|
|
the `RDF.NS` module.
|
|
|
|
Furthermore, the [rdf_vocab](https://hex.pm/packages/rdf_vocab) package
|
|
|
|
contains predefined modules for popular vocabularies.
|
2017-03-12 13:27:52 +00:00
|
|
|
"""
|
|
|
|
|
2017-06-11 01:05:23 +00:00
|
|
|
alias RDF.Utils.ResourceClassifier
|
|
|
|
|
2017-03-12 13:27:52 +00:00
|
|
|
@vocabs_dir "priv/vocabs"
|
2017-06-14 03:19:18 +00:00
|
|
|
@big_vocab_threshold 300
|
2017-03-12 13:27:52 +00:00
|
|
|
|
|
|
|
defmacro __using__(_opts) do
|
|
|
|
quote do
|
|
|
|
import unquote(__MODULE__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Defines a `RDF.Namespace` module for a RDF vocabulary.
|
|
|
|
"""
|
2017-03-12 13:34:12 +00:00
|
|
|
defmacro defvocab(name, opts) do
|
2017-05-29 21:12:50 +00:00
|
|
|
strict = strict?(opts)
|
2017-03-12 13:27:52 +00:00
|
|
|
base_uri = base_uri!(opts)
|
2017-05-26 11:54:04 +00:00
|
|
|
file = filename!(opts)
|
2017-05-29 21:12:50 +00:00
|
|
|
{terms, data} =
|
|
|
|
case source!(opts) do
|
|
|
|
{:terms, terms} -> {terms, nil}
|
|
|
|
{:data, data} -> {rdf_data_vocab_terms(data, base_uri), data}
|
2017-06-10 19:52:16 +00:00
|
|
|
end
|
2017-05-29 21:12:50 +00:00
|
|
|
|
2017-06-14 03:19:18 +00:00
|
|
|
if data && RDF.Data.subject_count(data) > @big_vocab_threshold do
|
|
|
|
IO.puts("Compiling vocabulary namespace for #{base_uri} may take some time")
|
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
terms =
|
|
|
|
terms
|
|
|
|
|> term_mapping!(opts)
|
|
|
|
|> validate_terms!(opts)
|
|
|
|
|> validate_case!(data, base_uri, opts)
|
2017-03-12 13:27:52 +00:00
|
|
|
case_separated_terms = group_terms_by_case(terms)
|
2017-05-26 11:54:04 +00:00
|
|
|
lowercased_terms = Map.get(case_separated_terms, :lowercased, %{})
|
2017-03-12 13:27:52 +00:00
|
|
|
|
|
|
|
quote do
|
|
|
|
vocabdoc = Module.delete_attribute(__MODULE__, :vocabdoc)
|
|
|
|
|
|
|
|
defmodule unquote(name) do
|
|
|
|
@moduledoc vocabdoc
|
|
|
|
|
|
|
|
@behaviour RDF.Namespace
|
|
|
|
|
|
|
|
if unquote(file) do
|
|
|
|
@external_resource unquote(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
@base_uri unquote(base_uri)
|
|
|
|
def __base_uri__, do: @base_uri
|
|
|
|
|
|
|
|
@strict unquote(strict)
|
|
|
|
def __strict__, do: @strict
|
|
|
|
|
2017-05-26 11:54:04 +00:00
|
|
|
@terms unquote(Macro.escape(terms))
|
2017-05-26 20:24:44 +00:00
|
|
|
def __terms__, do: @terms |> Map.keys
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns all known URIs of the vocabulary.
|
|
|
|
"""
|
|
|
|
def __uris__ do
|
|
|
|
@terms
|
|
|
|
|> Enum.map(fn
|
|
|
|
{term, true} -> term_to_uri(@base_uri, term)
|
|
|
|
{_alias, term} -> term_to_uri(@base_uri, term)
|
|
|
|
end)
|
|
|
|
|> Enum.uniq
|
|
|
|
end
|
2017-03-12 13:27:52 +00:00
|
|
|
|
|
|
|
define_vocab_terms unquote(lowercased_terms), unquote(base_uri)
|
|
|
|
|
2017-05-26 11:54:04 +00:00
|
|
|
def __resolve_term__(term) do
|
|
|
|
case @terms[term] do
|
|
|
|
nil ->
|
|
|
|
if @strict do
|
|
|
|
raise RDF.Namespace.UndefinedTermError,
|
|
|
|
"undefined term #{term} in strict vocabulary #{__MODULE__}"
|
|
|
|
else
|
|
|
|
term_to_uri(@base_uri, term)
|
|
|
|
end
|
|
|
|
true ->
|
2017-03-12 13:27:52 +00:00
|
|
|
term_to_uri(@base_uri, term)
|
2017-05-26 11:54:04 +00:00
|
|
|
original_term ->
|
|
|
|
term_to_uri(@base_uri, original_term)
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
2017-05-26 11:54:04 +00:00
|
|
|
end
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2017-05-26 11:54:04 +00:00
|
|
|
if not @strict do
|
2017-05-25 11:34:42 +00:00
|
|
|
def unquote(:"$handle_undefined_function")(term, []) do
|
2017-03-12 13:27:52 +00:00
|
|
|
term_to_uri(@base_uri, term)
|
|
|
|
end
|
2017-05-25 11:34:42 +00:00
|
|
|
|
|
|
|
def unquote(:"$handle_undefined_function")(term, [subject | objects]) do
|
|
|
|
RDF.Description.new(subject, term_to_uri(@base_uri, term), objects)
|
|
|
|
end
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
@doc false
|
2017-05-26 11:54:04 +00:00
|
|
|
defmacro define_vocab_terms(terms, base_uri) do
|
|
|
|
terms
|
|
|
|
|> Stream.map(fn
|
|
|
|
{term, true} -> {term, term}
|
|
|
|
{term, original_term} -> {term, original_term}
|
|
|
|
end)
|
|
|
|
|> Enum.map(fn {term, uri_suffix} ->
|
2017-06-11 01:05:23 +00:00
|
|
|
uri = term_to_uri(base_uri, uri_suffix)
|
|
|
|
quote do
|
|
|
|
@doc "<#{unquote(to_string(uri))}>"
|
|
|
|
def unquote(term)(), do: unquote(Macro.escape(uri))
|
|
|
|
|
|
|
|
@doc "`RDF.Description` builder for `#{unquote(term)}/0`"
|
|
|
|
def unquote(term)(subject, object) do
|
|
|
|
RDF.Description.new(subject, unquote(Macro.escape(uri)), object)
|
|
|
|
end
|
2017-05-26 11:54:04 +00:00
|
|
|
|
2017-06-11 01:05:23 +00:00
|
|
|
# Is there a better way to support multiple objects via arguments?
|
|
|
|
@doc false
|
|
|
|
def unquote(term)(subject, o1, o2),
|
|
|
|
do: unquote(term)(subject, [o1, o2])
|
|
|
|
@doc false
|
|
|
|
def unquote(term)(subject, o1, o2, o3),
|
|
|
|
do: unquote(term)(subject, [o1, o2, o3])
|
|
|
|
@doc false
|
|
|
|
def unquote(term)(subject, o1, o2, o3, o4),
|
|
|
|
do: unquote(term)(subject, [o1, o2, o3, o4])
|
|
|
|
@doc false
|
|
|
|
def unquote(term)(subject, o1, o2, o3, o4, o5),
|
|
|
|
do: unquote(term)(subject, [o1, o2, o3, o4, o5])
|
|
|
|
end
|
|
|
|
end)
|
2017-05-26 11:54:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp strict?(opts),
|
|
|
|
do: Keyword.get(opts, :strict, true)
|
|
|
|
|
2017-03-12 13:27:52 +00:00
|
|
|
defp base_uri!(opts) do
|
|
|
|
base_uri = Keyword.fetch!(opts, :base_uri)
|
2017-05-21 21:28:34 +00:00
|
|
|
unless is_binary(base_uri) and String.ends_with?(base_uri, ["/", "#"]) do
|
2017-03-12 13:27:52 +00:00
|
|
|
raise RDF.Namespace.InvalidVocabBaseURIError,
|
|
|
|
"a base_uri without a trailing '/' or '#' is invalid"
|
|
|
|
else
|
|
|
|
base_uri
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp source!(opts) do
|
2017-03-12 13:27:52 +00:00
|
|
|
cond do
|
2017-05-29 21:12:50 +00:00
|
|
|
Keyword.has_key?(opts, :file) -> {:data, filename!(opts) |> load_file}
|
|
|
|
rdf_data = Keyword.get(opts, :data) -> {:data, raw_rdf_data(rdf_data)}
|
|
|
|
terms = Keyword.get(opts, :terms) -> {:terms, terms_from_user_input!(terms)}
|
2017-03-12 13:27:52 +00:00
|
|
|
true ->
|
|
|
|
raise KeyError, key: ~w[terms data file], term: opts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp terms_from_user_input!(terms) do
|
|
|
|
# TODO: find an alternative to Code.eval_quoted - We want to support that the terms can be given as sigils ...
|
|
|
|
{terms, _ } = Code.eval_quoted(terms, [], rdf_data_env())
|
|
|
|
Enum.map terms, fn
|
|
|
|
term when is_atom(term) -> term
|
|
|
|
term when is_binary(term) -> String.to_atom(term)
|
|
|
|
term ->
|
|
|
|
raise RDF.Namespace.InvalidTermError,
|
|
|
|
"'#{term}' is not a valid vocabulary term"
|
|
|
|
end
|
2017-05-26 11:54:04 +00:00
|
|
|
end
|
|
|
|
|
2017-06-11 01:05:23 +00:00
|
|
|
defp raw_rdf_data(%RDF.Description{} = rdf_data), do: rdf_data
|
2017-05-29 21:12:50 +00:00
|
|
|
defp raw_rdf_data(%RDF.Graph{} = rdf_data), do: rdf_data
|
2017-06-11 01:05:23 +00:00
|
|
|
defp raw_rdf_data(%RDF.Dataset{} = rdf_data), do: rdf_data
|
2017-05-29 21:12:50 +00:00
|
|
|
defp raw_rdf_data(rdf_data) do
|
2017-05-26 11:54:04 +00:00
|
|
|
# TODO: find an alternative to Code.eval_quoted
|
|
|
|
{rdf_data, _} = Code.eval_quoted(rdf_data, [], rdf_data_env())
|
2017-05-29 21:12:50 +00:00
|
|
|
rdf_data
|
2017-05-26 11:54:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp term_mapping!(terms, opts) do
|
2017-05-26 11:54:04 +00:00
|
|
|
terms = Map.new terms, fn
|
|
|
|
term when is_atom(term) -> {term, true}
|
|
|
|
term -> {String.to_atom(term), true}
|
|
|
|
end
|
|
|
|
Keyword.get(opts, :alias, [])
|
|
|
|
|> Enum.reduce(terms, fn {alias, original_term}, terms ->
|
|
|
|
term = String.to_atom(original_term)
|
|
|
|
cond do
|
2017-05-26 19:22:26 +00:00
|
|
|
not valid_term?(alias) ->
|
|
|
|
raise RDF.Namespace.InvalidAliasError,
|
|
|
|
"alias '#{alias}' contains invalid characters"
|
|
|
|
|
2017-05-26 11:54:04 +00:00
|
|
|
Map.get(terms, alias) == true ->
|
|
|
|
raise RDF.Namespace.InvalidAliasError,
|
|
|
|
"alias '#{alias}' already defined"
|
|
|
|
|
|
|
|
strict?(opts) and not Map.has_key?(terms, term) ->
|
|
|
|
raise RDF.Namespace.InvalidAliasError,
|
|
|
|
"term '#{original_term}' is not a term in this vocabulary"
|
|
|
|
|
|
|
|
Map.get(terms, term, true) != true ->
|
|
|
|
raise RDF.Namespace.InvalidAliasError,
|
|
|
|
"'#{original_term}' is already an alias"
|
|
|
|
|
|
|
|
true ->
|
|
|
|
Map.put(terms, alias, to_string(original_term))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp aliased_terms(terms) do
|
|
|
|
terms
|
|
|
|
|> Map.values
|
|
|
|
|> MapSet.new
|
|
|
|
|> MapSet.delete(true)
|
|
|
|
|> Enum.map(&String.to_atom/1)
|
|
|
|
end
|
|
|
|
|
2017-05-26 19:22:26 +00:00
|
|
|
defp validate_terms!(terms, opts) do
|
|
|
|
if (handling = Keyword.get(opts, :invalid_characters, :fail)) == :ignore do
|
|
|
|
terms
|
|
|
|
else
|
|
|
|
terms
|
2017-05-29 21:12:50 +00:00
|
|
|
|> detect_invalid_terms
|
|
|
|
|> handle_invalid_terms(handling, terms)
|
2017-05-26 19:22:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp detect_invalid_terms(terms) do
|
|
|
|
aliased_terms = aliased_terms(terms)
|
|
|
|
Enum.filter_map terms,
|
|
|
|
fn {term, _} ->
|
|
|
|
not term in aliased_terms and not valid_term?(term)
|
|
|
|
end,
|
|
|
|
fn {term, _} -> term end
|
2017-05-26 19:22:26 +00:00
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp handle_invalid_terms([], _, terms), do: terms
|
2017-05-26 19:22:26 +00:00
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp handle_invalid_terms(invalid_terms, :fail, _) do
|
2017-05-26 19:22:26 +00:00
|
|
|
raise RDF.Namespace.InvalidTermError, """
|
|
|
|
The following terms contain invalid characters:
|
|
|
|
|
|
|
|
- #{Enum.join(invalid_terms, "\n- ")}
|
|
|
|
|
|
|
|
You have the following options:
|
|
|
|
|
|
|
|
- if you are in control of the vocabulary, consider renaming the resource
|
|
|
|
- define an alias with the :alias option on defvocab
|
|
|
|
- change the handling of invalid characters with the :invalid_characters option on defvocab
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp handle_invalid_terms(invalid_terms, :warn, terms) do
|
2017-05-26 19:22:26 +00:00
|
|
|
Enum.each invalid_terms, fn term ->
|
|
|
|
IO.warn "'#{term}' is not valid term, since it contains invalid characters"
|
|
|
|
end
|
|
|
|
terms
|
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp valid_term?(term) when is_atom(term),
|
|
|
|
do: valid_term?(Atom.to_string(term))
|
|
|
|
defp valid_term?(term),
|
|
|
|
do: Regex.match?(~r/^[a-zA-Z_]\w*$/, term)
|
|
|
|
|
|
|
|
defp validate_case!(terms, nil, _, _), do: terms
|
|
|
|
defp validate_case!(terms, data, base_uri, opts) do
|
|
|
|
if (handling = Keyword.get(opts, :case_violations, :warn)) == :ignore do
|
|
|
|
terms
|
|
|
|
else
|
|
|
|
terms
|
|
|
|
|> detect_case_violations(data, base_uri)
|
|
|
|
|> group_case_violations
|
|
|
|
|> handle_case_violations(handling, terms, base_uri, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp detect_case_violations(terms, data, base_uri) do
|
|
|
|
aliased_terms = aliased_terms(terms)
|
|
|
|
Enum.filter terms, fn
|
|
|
|
{term, true} ->
|
|
|
|
if not term in aliased_terms do
|
|
|
|
proper_case?(term, base_uri, Atom.to_string(term), data)
|
|
|
|
end
|
|
|
|
{term, original_term} ->
|
|
|
|
proper_case?(term, base_uri, original_term, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp proper_case?(term, base_uri, uri_suffix, data) do
|
2017-06-11 01:05:23 +00:00
|
|
|
case ResourceClassifier.property?(term_to_uri(base_uri, uri_suffix), data) do
|
2017-05-29 21:12:50 +00:00
|
|
|
true -> not lowercase?(term)
|
|
|
|
false -> lowercase?(term)
|
|
|
|
nil -> lowercase?(term)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp group_case_violations(violations) do
|
|
|
|
violations
|
|
|
|
|> Enum.group_by(fn
|
|
|
|
{term, true} ->
|
|
|
|
if lowercase?(term),
|
|
|
|
do: :lowercased_term,
|
|
|
|
else: :capitalized_term
|
|
|
|
{term, _original} ->
|
|
|
|
if lowercase?(term),
|
|
|
|
do: :lowercased_alias,
|
|
|
|
else: :capitalized_alias
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_case_violations(%{} = violations, _, terms, _, _) when map_size(violations) == 0,
|
|
|
|
do: terms
|
|
|
|
|
|
|
|
defp handle_case_violations(violations, :fail, _, base_uri, _) do
|
|
|
|
resource_name_violations = fn violations ->
|
|
|
|
violations
|
|
|
|
|> Enum.map(fn {term, true} -> term_to_uri(base_uri, term) end)
|
|
|
|
|> Enum.map(&to_string/1)
|
|
|
|
|> Enum.join("\n- ")
|
|
|
|
end
|
|
|
|
alias_violations = fn violations ->
|
|
|
|
violations
|
|
|
|
|> Enum.map(fn {term, original} ->
|
|
|
|
"alias #{term} for #{term_to_uri(base_uri, original)}"
|
|
|
|
end)
|
|
|
|
|> Enum.join("\n- ")
|
|
|
|
end
|
|
|
|
|
|
|
|
violation_error_lines =
|
|
|
|
violations
|
|
|
|
|> Enum.map(fn
|
|
|
|
{:capitalized_term, violations} ->
|
|
|
|
"""
|
|
|
|
Terms for properties should be lowercased, but the following properties are
|
|
|
|
capitalized:
|
|
|
|
|
|
|
|
- #{resource_name_violations.(violations)}
|
|
|
|
|
|
|
|
"""
|
|
|
|
{:lowercased_term, violations} ->
|
|
|
|
"""
|
|
|
|
Terms for non-property resource should be capitalized, but the following
|
|
|
|
non-properties are lowercased:
|
|
|
|
|
|
|
|
- #{resource_name_violations.(violations)}
|
|
|
|
|
|
|
|
"""
|
|
|
|
{:capitalized_alias, violations} ->
|
|
|
|
"""
|
|
|
|
Terms for properties should be lowercased, but the following aliases for
|
|
|
|
properties are capitalized:
|
|
|
|
|
|
|
|
- #{alias_violations.(violations)}
|
|
|
|
|
|
|
|
"""
|
|
|
|
{:lowercased_alias, violations} ->
|
|
|
|
"""
|
|
|
|
Terms for non-property resource should be capitalized, but the following
|
|
|
|
aliases for non-properties are lowercased:
|
|
|
|
|
|
|
|
- #{alias_violations.(violations)}
|
|
|
|
|
|
|
|
"""
|
|
|
|
end)
|
|
|
|
|> Enum.join
|
|
|
|
|
|
|
|
raise RDF.Namespace.InvalidTermError, """
|
|
|
|
Case violations detected
|
|
|
|
|
|
|
|
#{violation_error_lines}
|
|
|
|
You have the following options:
|
|
|
|
|
|
|
|
- if you are in control of the vocabulary, consider renaming the resource
|
|
|
|
- define a properly cased alias with the :alias option on defvocab
|
|
|
|
- change the handling of case violations with the :case_violations option on defvocab
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
defp handle_case_violations(violations, :warn, terms, base_uri, _) do
|
|
|
|
for {type, violations} <- violations,
|
|
|
|
{term, original} <- violations do
|
|
|
|
case_violation_warning(type, term, original, base_uri)
|
|
|
|
end
|
|
|
|
terms
|
|
|
|
end
|
|
|
|
|
|
|
|
defp case_violation_warning(:capitalized_term, term, _, base_uri) do
|
|
|
|
IO.warn "'#{term_to_uri(base_uri, term)}' is a capitalized property"
|
|
|
|
end
|
|
|
|
|
|
|
|
defp case_violation_warning(:lowercased_term, term, _, base_uri) do
|
|
|
|
IO.warn "'#{term_to_uri(base_uri, term)}' is a lowercased non-property resource"
|
|
|
|
end
|
|
|
|
|
|
|
|
defp case_violation_warning(:capitalized_alias, term, _, _) do
|
|
|
|
IO.warn "capitalized alias '#{term}' for a property"
|
|
|
|
end
|
|
|
|
|
2017-06-16 22:44:11 +00:00
|
|
|
defp case_violation_warning(:lowercased_alias, term, _, _) do
|
2017-05-29 21:12:50 +00:00
|
|
|
IO.warn "lowercased alias '#{term}' for a non-property resource"
|
2017-05-26 19:22:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
defp filename!(opts) do
|
2017-05-26 11:54:04 +00:00
|
|
|
if filename = Keyword.get(opts, :file) do
|
2017-03-12 13:27:52 +00:00
|
|
|
cond do
|
2017-05-26 11:54:04 +00:00
|
|
|
File.exists?(filename) ->
|
|
|
|
filename
|
|
|
|
File.exists?(expanded_filename = Path.expand(filename, @vocabs_dir)) ->
|
|
|
|
expanded_filename
|
2017-03-12 13:27:52 +00:00
|
|
|
true ->
|
2017-05-26 11:54:04 +00:00
|
|
|
raise File.Error, path: filename, action: "find", reason: :enoent
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp load_file(file) do
|
2017-06-10 16:02:51 +00:00
|
|
|
# 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
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
|
|
|
|
2017-05-26 11:54:04 +00:00
|
|
|
defp rdf_data_env do
|
2017-05-29 21:12:50 +00:00
|
|
|
import RDF.Sigils
|
2017-03-12 13:27:52 +00:00
|
|
|
__ENV__
|
|
|
|
end
|
|
|
|
|
2017-05-26 11:54:04 +00:00
|
|
|
defp rdf_data_vocab_terms(data, base_uri) do
|
2017-03-12 13:27:52 +00:00
|
|
|
data
|
2017-06-05 00:58:49 +00:00
|
|
|
|> RDF.Data.resources
|
2017-03-12 13:27:52 +00:00
|
|
|
# filter URIs
|
|
|
|
|> Stream.filter(fn
|
|
|
|
%URI{} -> true
|
|
|
|
_ -> false
|
|
|
|
end)
|
2017-05-29 21:12:50 +00:00
|
|
|
|> Stream.map(&URI.to_string/1)
|
2017-03-12 13:27:52 +00:00
|
|
|
|> Stream.map(&(strip_base_uri(&1, base_uri)))
|
2017-05-26 11:54:04 +00:00
|
|
|
|> Stream.filter(&vocab_term?/1)
|
|
|
|
|> Enum.map(&String.to_atom/1)
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp group_terms_by_case(terms) do
|
2017-05-26 11:54:04 +00:00
|
|
|
terms
|
|
|
|
|> Enum.group_by(fn {term, _} ->
|
|
|
|
if lowercase?(term),
|
|
|
|
do: :lowercased,
|
|
|
|
else: :capitalized
|
|
|
|
end)
|
|
|
|
|> Map.new(fn {group, term_mapping} ->
|
|
|
|
{group, Map.new(term_mapping)}
|
|
|
|
end)
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp lowercase?(term) when is_atom(term),
|
|
|
|
do: Atom.to_string(term) |> lowercase?
|
|
|
|
defp lowercase?(term),
|
|
|
|
do: term =~ ~r/^\p{Ll}/u
|
|
|
|
|
|
|
|
defp strip_base_uri(uri, base_uri) do
|
|
|
|
if String.starts_with?(uri, base_uri) do
|
|
|
|
String.replace_prefix(uri, base_uri, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-26 19:22:26 +00:00
|
|
|
defp vocab_term?(""), do: false
|
2017-03-12 13:27:52 +00:00
|
|
|
defp vocab_term?(term) when is_binary(term) do
|
|
|
|
not String.contains?(term, "/")
|
|
|
|
end
|
|
|
|
defp vocab_term?(_), do: false
|
|
|
|
|
|
|
|
@doc false
|
2017-05-29 21:12:50 +00:00
|
|
|
def term_to_uri(base_uri, term) when is_atom(term),
|
|
|
|
do: term_to_uri(base_uri, Atom.to_string(term))
|
|
|
|
def term_to_uri(base_uri, term),
|
|
|
|
do: URI.parse(base_uri <> term)
|
2017-03-12 13:27:52 +00:00
|
|
|
|
|
|
|
end
|