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-23 15:25:27 +00:00
|
|
|
RDF.ex comes with predefined modules for some fundamental vocabularies in
|
2017-06-10 19:52:16 +00:00
|
|
|
the `RDF.NS` module.
|
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"
|
|
|
|
|
|
|
|
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-08-20 20:35:14 +00:00
|
|
|
base_iri = base_iri!(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}
|
2017-08-20 20:35:14 +00:00
|
|
|
{:data, data} -> {rdf_data_vocab_terms(data, base_iri), data}
|
2017-06-10 19:52:16 +00:00
|
|
|
end
|
2017-05-29 21:12:50 +00:00
|
|
|
|
2017-08-21 19:24:05 +00:00
|
|
|
IO.puts("Compiling vocabulary namespace for #{base_iri}")
|
2017-06-14 03:19:18 +00:00
|
|
|
|
2017-06-21 21:28:23 +00:00
|
|
|
ignored_terms = ignored_terms!(opts)
|
2017-05-29 21:12:50 +00:00
|
|
|
terms =
|
|
|
|
terms
|
|
|
|
|> term_mapping!(opts)
|
2017-06-21 21:28:23 +00:00
|
|
|
|> Map.drop(MapSet.to_list(ignored_terms))
|
2017-06-23 15:25:27 +00:00
|
|
|
|> validate_terms!
|
|
|
|
|> validate_characters!(opts)
|
2017-08-20 20:35:14 +00:00
|
|
|
|> validate_case!(data, base_iri, 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
|
|
|
|
|
2018-03-09 22:57:20 +00:00
|
|
|
@behaviour Elixir.RDF.Namespace
|
2017-03-12 13:27:52 +00:00
|
|
|
|
|
|
|
if unquote(file) do
|
|
|
|
@external_resource unquote(file)
|
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
@base_iri unquote(base_iri)
|
2019-04-01 00:04:51 +00:00
|
|
|
def __base_iri__, do: @base_iri
|
2017-03-12 13:27:52 +00:00
|
|
|
|
|
|
|
@strict unquote(strict)
|
|
|
|
def __strict__, do: @strict
|
|
|
|
|
2017-05-26 11:54:04 +00:00
|
|
|
@terms unquote(Macro.escape(terms))
|
2018-09-17 00:08:16 +00:00
|
|
|
@impl Elixir.RDF.Namespace
|
2017-05-26 20:24:44 +00:00
|
|
|
def __terms__, do: @terms |> Map.keys
|
|
|
|
|
2017-06-21 21:28:23 +00:00
|
|
|
@ignored_terms unquote(Macro.escape(ignored_terms))
|
|
|
|
|
2017-05-26 20:24:44 +00:00
|
|
|
@doc """
|
2017-08-20 20:35:14 +00:00
|
|
|
Returns all known IRIs of the vocabulary.
|
2017-05-26 20:24:44 +00:00
|
|
|
"""
|
2017-08-20 20:35:14 +00:00
|
|
|
def __iris__ do
|
2017-05-26 20:24:44 +00:00
|
|
|
@terms
|
|
|
|
|> Enum.map(fn
|
2017-08-20 20:35:14 +00:00
|
|
|
{term, true} -> term_to_iri(@base_iri, term)
|
|
|
|
{_alias, term} -> term_to_iri(@base_iri, term)
|
2017-05-26 20:24:44 +00:00
|
|
|
end)
|
|
|
|
|> Enum.uniq
|
|
|
|
end
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
define_vocab_terms unquote(lowercased_terms), unquote(base_iri)
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2018-09-17 00:08:16 +00:00
|
|
|
@impl Elixir.RDF.Namespace
|
2017-05-26 11:54:04 +00:00
|
|
|
def __resolve_term__(term) do
|
|
|
|
case @terms[term] do
|
|
|
|
nil ->
|
2017-06-21 21:28:23 +00:00
|
|
|
# TODO: Why does this MapSet.member? call produce a warning? It does NOT always yield the same result!
|
|
|
|
if @strict or MapSet.member?(@ignored_terms, term) do
|
2018-03-09 22:57:20 +00:00
|
|
|
raise Elixir.RDF.Namespace.UndefinedTermError,
|
2017-05-26 11:54:04 +00:00
|
|
|
"undefined term #{term} in strict vocabulary #{__MODULE__}"
|
|
|
|
else
|
2017-08-20 20:35:14 +00:00
|
|
|
term_to_iri(@base_iri, term)
|
2017-05-26 11:54:04 +00:00
|
|
|
end
|
|
|
|
true ->
|
2017-08-20 20:35:14 +00:00
|
|
|
term_to_iri(@base_iri, term)
|
2017-05-26 11:54:04 +00:00
|
|
|
original_term ->
|
2017-08-20 20:35:14 +00:00
|
|
|
term_to_iri(@base_iri, 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-06-21 21:28:23 +00:00
|
|
|
if MapSet.member?(@ignored_terms, term) do
|
|
|
|
raise UndefinedFunctionError
|
|
|
|
else
|
2017-08-20 20:35:14 +00:00
|
|
|
term_to_iri(@base_iri, term)
|
2017-06-21 21:28:23 +00:00
|
|
|
end
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
2017-05-25 11:34:42 +00:00
|
|
|
|
|
|
|
def unquote(:"$handle_undefined_function")(term, [subject | objects]) do
|
2017-06-21 21:28:23 +00:00
|
|
|
if MapSet.member?(@ignored_terms, term) do
|
|
|
|
raise UndefinedFunctionError
|
|
|
|
else
|
2017-08-20 20:35:14 +00:00
|
|
|
RDF.Description.new(subject, term_to_iri(@base_iri, term), objects)
|
2017-06-21 21:28:23 +00:00
|
|
|
end
|
2017-05-25 11:34:42 +00:00
|
|
|
end
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
@doc false
|
2017-08-20 20:35:14 +00:00
|
|
|
defmacro define_vocab_terms(terms, base_iri) do
|
2017-05-26 11:54:04 +00:00
|
|
|
terms
|
2017-06-23 15:25:27 +00:00
|
|
|
|> Stream.filter(fn
|
2017-08-11 16:33:12 +00:00
|
|
|
{term, true} -> valid_term?(term)
|
|
|
|
{_, _} -> true
|
2017-06-23 15:25:27 +00:00
|
|
|
end)
|
2017-05-26 11:54:04 +00:00
|
|
|
|> Stream.map(fn
|
|
|
|
{term, true} -> {term, term}
|
|
|
|
{term, original_term} -> {term, original_term}
|
|
|
|
end)
|
2017-08-20 20:35:14 +00:00
|
|
|
|> Enum.map(fn {term, iri_suffix} ->
|
|
|
|
iri = term_to_iri(base_iri, iri_suffix)
|
2017-06-11 01:05:23 +00:00
|
|
|
quote do
|
2017-08-20 20:35:14 +00:00
|
|
|
@doc "<#{unquote(to_string(iri))}>"
|
|
|
|
def unquote(term)(), do: unquote(Macro.escape(iri))
|
2017-06-11 01:05:23 +00:00
|
|
|
|
|
|
|
@doc "`RDF.Description` builder for `#{unquote(term)}/0`"
|
|
|
|
def unquote(term)(subject, object) do
|
2017-08-20 20:35:14 +00:00
|
|
|
RDF.Description.new(subject, unquote(Macro.escape(iri)), object)
|
2017-06-11 01:05:23 +00:00
|
|
|
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-08-20 20:35:14 +00:00
|
|
|
defp base_iri!(opts) do
|
|
|
|
base_iri = Keyword.fetch!(opts, :base_iri)
|
|
|
|
unless is_binary(base_iri) and String.ends_with?(base_iri, ["/", "#"]) do
|
|
|
|
raise RDF.Namespace.InvalidVocabBaseIRIError,
|
|
|
|
"a base_iri without a trailing '/' or '#' is invalid"
|
2017-03-12 13:27:52 +00:00
|
|
|
else
|
2017-08-20 20:35:14 +00:00
|
|
|
base_iri
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-29 21:12:50 +00:00
|
|
|
defp source!(opts) do
|
2017-03-12 13:27:52 +00:00
|
|
|
cond do
|
2018-03-09 20:26:15 +00:00
|
|
|
Keyword.has_key?(opts, :file) -> {:data, filename!(opts) |> RDF.read_file!()}
|
2017-05-29 21:12:50 +00:00
|
|
|
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-06-23 15:25:27 +00:00
|
|
|
defp ignored_terms!(opts) do
|
|
|
|
# TODO: find an alternative to Code.eval_quoted - We want to support that the terms can be given as sigils ...
|
|
|
|
with terms = Keyword.get(opts, :ignore, []) do
|
|
|
|
{terms, _ } = Code.eval_quoted(terms, [], rdf_data_env())
|
|
|
|
terms
|
|
|
|
|> Enum.map(fn
|
|
|
|
term when is_atom(term) -> term
|
|
|
|
term when is_binary(term) -> String.to_atom(term)
|
|
|
|
term -> raise RDF.Namespace.InvalidTermError, inspect(term)
|
|
|
|
end)
|
|
|
|
|> MapSet.new
|
|
|
|
end
|
|
|
|
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-06-23 15:25:27 +00:00
|
|
|
not valid_characters?(alias) ->
|
2017-05-26 19:22:26 +00:00
|
|
|
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-06-23 15:25:27 +00:00
|
|
|
@invalid_terms MapSet.new ~w[
|
|
|
|
and
|
|
|
|
or
|
|
|
|
xor
|
|
|
|
in
|
|
|
|
fn
|
|
|
|
def
|
|
|
|
when
|
|
|
|
if
|
|
|
|
for
|
|
|
|
case
|
|
|
|
with
|
|
|
|
quote
|
|
|
|
unquote
|
|
|
|
unquote_splicing
|
|
|
|
alias
|
|
|
|
import
|
|
|
|
require
|
|
|
|
super
|
|
|
|
__aliases__
|
|
|
|
]a
|
|
|
|
|
|
|
|
def invalid_terms, do: @invalid_terms
|
|
|
|
|
|
|
|
defp validate_terms!(terms) do
|
2017-08-11 16:33:12 +00:00
|
|
|
with aliased_terms = aliased_terms(terms) do
|
2018-08-16 21:32:28 +00:00
|
|
|
for {term, _} <- terms, term not in aliased_terms and not valid_term?(term) do
|
2017-08-11 16:33:12 +00:00
|
|
|
term
|
|
|
|
end
|
|
|
|
|> handle_invalid_terms!
|
|
|
|
end
|
2017-06-23 15:25:27 +00:00
|
|
|
|
|
|
|
terms
|
|
|
|
end
|
|
|
|
|
|
|
|
defp valid_term?(term) do
|
|
|
|
not MapSet.member?(@invalid_terms, term)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_invalid_terms!([]), do: nil
|
|
|
|
|
|
|
|
defp handle_invalid_terms!(invalid_terms) do
|
|
|
|
raise RDF.Namespace.InvalidTermError, """
|
|
|
|
The following terms can not be used, because they conflict with the Elixir semantics:
|
|
|
|
|
|
|
|
- #{Enum.join(invalid_terms, "\n- ")}
|
|
|
|
|
|
|
|
You have the following options:
|
|
|
|
|
|
|
|
- define an alias with the :alias option on defvocab
|
|
|
|
- ignore the resource with the :ignore option on defvocab
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
defp validate_characters!(terms, opts) do
|
2017-05-26 19:22:26 +00:00
|
|
|
if (handling = Keyword.get(opts, :invalid_characters, :fail)) == :ignore do
|
|
|
|
terms
|
|
|
|
else
|
|
|
|
terms
|
2017-06-23 15:25:27 +00:00
|
|
|
|> detect_invalid_characters
|
|
|
|
|> handle_invalid_characters(handling, terms)
|
2017-05-26 19:22:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-23 15:25:27 +00:00
|
|
|
defp detect_invalid_characters(terms) do
|
2017-08-11 16:33:12 +00:00
|
|
|
with aliased_terms = aliased_terms(terms) do
|
2018-08-16 21:32:28 +00:00
|
|
|
for {term, _} <- terms, term not in aliased_terms and not valid_characters?(term),
|
2017-08-11 16:33:12 +00:00
|
|
|
do: term
|
|
|
|
end
|
2017-05-26 19:22:26 +00:00
|
|
|
end
|
|
|
|
|
2017-06-23 15:25:27 +00:00
|
|
|
defp handle_invalid_characters([], _, terms), do: terms
|
2017-05-26 19:22:26 +00:00
|
|
|
|
2017-06-23 15:25:27 +00:00
|
|
|
defp handle_invalid_characters(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
|
2017-06-23 15:25:27 +00:00
|
|
|
- ignore the resource with the :ignore option on defvocab
|
2017-05-26 19:22:26 +00:00
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2017-06-23 15:25:27 +00:00
|
|
|
defp handle_invalid_characters(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-06-23 15:25:27 +00:00
|
|
|
defp valid_characters?(term) when is_atom(term),
|
|
|
|
do: valid_characters?(Atom.to_string(term))
|
|
|
|
defp valid_characters?(term),
|
2017-05-29 21:12:50 +00:00
|
|
|
do: Regex.match?(~r/^[a-zA-Z_]\w*$/, term)
|
|
|
|
|
|
|
|
defp validate_case!(terms, nil, _, _), do: terms
|
2017-08-20 20:35:14 +00:00
|
|
|
defp validate_case!(terms, data, base_iri, opts) do
|
2017-05-29 21:12:50 +00:00
|
|
|
if (handling = Keyword.get(opts, :case_violations, :warn)) == :ignore do
|
|
|
|
terms
|
|
|
|
else
|
|
|
|
terms
|
2017-08-20 20:35:14 +00:00
|
|
|
|> detect_case_violations(data, base_iri)
|
2017-05-29 21:12:50 +00:00
|
|
|
|> group_case_violations
|
2017-08-20 20:35:14 +00:00
|
|
|
|> handle_case_violations(handling, terms, base_iri, opts)
|
2017-05-29 21:12:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defp detect_case_violations(terms, data, base_iri) do
|
2017-05-29 21:12:50 +00:00
|
|
|
aliased_terms = aliased_terms(terms)
|
2017-06-23 15:23:14 +00:00
|
|
|
terms
|
|
|
|
|> Enum.filter(fn {term, _} ->
|
|
|
|
not(Atom.to_string(term) |> String.starts_with?("_"))
|
|
|
|
end)
|
|
|
|
|> Enum.filter(fn
|
|
|
|
{term, true} ->
|
2018-08-16 21:32:28 +00:00
|
|
|
if term not in aliased_terms do
|
2017-08-20 20:35:14 +00:00
|
|
|
proper_case?(term, base_iri, Atom.to_string(term), data)
|
2017-06-23 15:23:14 +00:00
|
|
|
end
|
|
|
|
{term, original_term} ->
|
2017-08-20 20:35:14 +00:00
|
|
|
proper_case?(term, base_iri, original_term, data)
|
2017-06-23 15:23:14 +00:00
|
|
|
end)
|
2017-05-29 21:12:50 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defp proper_case?(term, base_iri, iri_suffix, data) do
|
|
|
|
case ResourceClassifier.property?(term_to_iri(base_iri, iri_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
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defp handle_case_violations(violations, :fail, _, base_iri, _) do
|
2017-05-29 21:12:50 +00:00
|
|
|
resource_name_violations = fn violations ->
|
|
|
|
violations
|
2017-08-20 20:35:14 +00:00
|
|
|
|> Enum.map(fn {term, true} -> term_to_iri(base_iri, term) end)
|
2017-05-29 21:12:50 +00:00
|
|
|
|> Enum.map(&to_string/1)
|
|
|
|
|> Enum.join("\n- ")
|
|
|
|
end
|
|
|
|
alias_violations = fn violations ->
|
|
|
|
violations
|
|
|
|
|> Enum.map(fn {term, original} ->
|
2017-08-20 20:35:14 +00:00
|
|
|
"alias #{term} for #{term_to_iri(base_iri, original)}"
|
2017-05-29 21:12:50 +00:00
|
|
|
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
|
2017-06-23 15:25:27 +00:00
|
|
|
- ignore the resource with the :ignore option on defvocab
|
2017-05-29 21:12:50 +00:00
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defp handle_case_violations(violations, :warn, terms, base_iri, _) do
|
2017-05-29 21:12:50 +00:00
|
|
|
for {type, violations} <- violations,
|
|
|
|
{term, original} <- violations do
|
2017-08-20 20:35:14 +00:00
|
|
|
case_violation_warning(type, term, original, base_iri)
|
2017-05-29 21:12:50 +00:00
|
|
|
end
|
|
|
|
terms
|
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defp case_violation_warning(:capitalized_term, term, _, base_iri) do
|
|
|
|
IO.warn "'#{term_to_iri(base_iri, term)}' is a capitalized property"
|
2017-05-29 21:12:50 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defp case_violation_warning(:lowercased_term, term, _, base_iri) do
|
|
|
|
IO.warn "'#{term_to_iri(base_iri, term)}' is a lowercased non-property resource"
|
2017-05-29 21:12:50 +00:00
|
|
|
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
|
|
|
|
|
2017-05-26 11:54:04 +00:00
|
|
|
defp rdf_data_env do
|
2018-03-09 22:57:20 +00:00
|
|
|
import RDF.Sigils # TODO: Can we get rid of the warning about this line somehow? It is plain false.
|
2017-03-12 13:27:52 +00:00
|
|
|
__ENV__
|
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defp rdf_data_vocab_terms(data, base_iri) 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
|
|
|
|> Stream.filter(fn
|
2017-08-20 20:35:14 +00:00
|
|
|
%RDF.IRI{} -> true
|
|
|
|
_ -> false
|
2017-03-12 13:27:52 +00:00
|
|
|
end)
|
2017-08-20 20:35:14 +00:00
|
|
|
|> Stream.map(&to_string/1)
|
|
|
|
|> Stream.map(&(strip_base_iri(&1, base_iri)))
|
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),
|
2017-06-23 15:23:14 +00:00
|
|
|
do: term =~ ~r/^(_|\p{Ll})/u
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
defp strip_base_iri(iri, base_iri) do
|
|
|
|
if String.starts_with?(iri, base_iri) do
|
|
|
|
String.replace_prefix(iri, base_iri, "")
|
2017-03-12 13:27:52 +00:00
|
|
|
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-08-20 20:35:14 +00:00
|
|
|
def term_to_iri(base_iri, term) when is_atom(term),
|
|
|
|
do: term_to_iri(base_iri, Atom.to_string(term))
|
|
|
|
def term_to_iri(base_iri, term),
|
|
|
|
do: RDF.iri(base_iri <> term)
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2019-03-30 01:01:30 +00:00
|
|
|
@doc false
|
|
|
|
def vocabulary_namespace?(name) do
|
2019-09-06 13:05:40 +00:00
|
|
|
Code.ensure_compiled?(name) && function_exported?(name, :__base_iri__, 0)
|
2019-03-30 01:01:30 +00:00
|
|
|
end
|
|
|
|
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|