2017-08-20 20:35:14 +00:00
|
|
|
defimpl Inspect, for: RDF.IRI do
|
|
|
|
def inspect(%RDF.IRI{value: value}, _opts) do
|
|
|
|
"~I<#{value}>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-19 15:23:54 +00:00
|
|
|
defimpl Inspect, for: RDF.BlankNode do
|
2020-07-08 20:02:58 +00:00
|
|
|
def inspect(%RDF.BlankNode{value: value}, _opts) do
|
|
|
|
"~B<#{value}>"
|
2017-05-19 15:23:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 09:15:53 +00:00
|
|
|
defimpl Inspect, for: RDF.Literal do
|
2020-04-10 21:40:33 +00:00
|
|
|
def inspect(literal, _opts) do
|
2020-06-29 08:37:42 +00:00
|
|
|
"%RDF.Literal{literal: #{inspect(literal.literal)}, valid: #{RDF.Literal.valid?(literal)}}"
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl Inspect, for: RDF.Description do
|
2020-11-02 14:17:18 +00:00
|
|
|
def inspect(description, opts) do
|
|
|
|
if opts.structs do
|
|
|
|
try do
|
|
|
|
limit = opts.limit < RDF.Description.statement_count(description)
|
|
|
|
|
|
|
|
description =
|
|
|
|
if limit do
|
|
|
|
description.subject
|
|
|
|
|> RDF.Description.new(init: Enum.take(description, opts.limit))
|
|
|
|
else
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
2022-03-29 21:30:15 +00:00
|
|
|
header = "#RDF.Description<subject: #{inspect(description.subject)}"
|
|
|
|
|
2022-04-05 20:07:38 +00:00
|
|
|
if RDF.Description.empty?(description) do
|
2022-03-30 19:40:18 +00:00
|
|
|
header <> ">"
|
|
|
|
else
|
|
|
|
body =
|
|
|
|
description
|
|
|
|
|> RDF.Turtle.write_string!(only: :triples, indent: 2)
|
|
|
|
|> String.trim_trailing()
|
2020-11-02 14:17:18 +00:00
|
|
|
|
2022-03-30 19:40:18 +00:00
|
|
|
"#{header}\n#{body}#{if limit, do: "..\n..."}\n>"
|
|
|
|
end
|
2020-11-02 14:17:18 +00:00
|
|
|
rescue
|
|
|
|
caught_exception ->
|
|
|
|
message =
|
|
|
|
"got #{inspect(caught_exception.__struct__)} with message " <>
|
2021-09-30 20:36:25 +00:00
|
|
|
"#{inspect(Exception.message(caught_exception))} while inspecting RDF.Description #{inspect(description.subject)}"
|
2020-11-02 14:17:18 +00:00
|
|
|
|
|
|
|
exception = Inspect.Error.exception(message: message)
|
|
|
|
|
|
|
|
if opts.safe do
|
|
|
|
Inspect.inspect(exception, opts)
|
|
|
|
else
|
|
|
|
reraise(exception, __STACKTRACE__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Inspect.Map.inspect(description, opts)
|
|
|
|
end
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl Inspect, for: RDF.Graph do
|
2020-11-02 14:17:18 +00:00
|
|
|
def inspect(graph, opts) do
|
|
|
|
if opts.structs do
|
|
|
|
try do
|
2020-11-12 21:22:04 +00:00
|
|
|
no_metadata = Keyword.get(opts.custom_options, :no_metadata, false)
|
|
|
|
|
2020-11-02 14:17:18 +00:00
|
|
|
limit = opts.limit < RDF.Graph.statement_count(graph)
|
|
|
|
|
|
|
|
graph =
|
|
|
|
if limit do
|
|
|
|
graph
|
|
|
|
|> RDF.Graph.clear()
|
|
|
|
|> RDF.Graph.add(Enum.take(graph, opts.limit))
|
|
|
|
else
|
|
|
|
graph
|
|
|
|
end
|
|
|
|
|
2021-02-28 12:29:10 +00:00
|
|
|
header = "#RDF.Graph<name: #{inspect(graph.name)}"
|
2020-11-02 14:17:18 +00:00
|
|
|
|
|
|
|
body =
|
|
|
|
graph
|
2021-02-28 12:29:10 +00:00
|
|
|
|> RDF.Turtle.write_string!(only: if(no_metadata, do: :triples), indent: 2)
|
2020-11-02 14:17:18 +00:00
|
|
|
|> String.trim_trailing()
|
|
|
|
|
2021-02-28 12:29:10 +00:00
|
|
|
"#{header}\n#{body}#{if limit, do: "..\n..."}\n>"
|
2020-11-02 14:17:18 +00:00
|
|
|
rescue
|
|
|
|
caught_exception ->
|
|
|
|
message =
|
|
|
|
"got #{inspect(caught_exception.__struct__)} with message " <>
|
2021-05-22 14:08:19 +00:00
|
|
|
"#{inspect(Exception.message(caught_exception))} while inspecting RDF.Graph #{graph.name}"
|
2020-11-02 14:17:18 +00:00
|
|
|
|
|
|
|
exception = Inspect.Error.exception(message: message)
|
|
|
|
|
|
|
|
if opts.safe do
|
|
|
|
Inspect.inspect(exception, opts)
|
|
|
|
else
|
|
|
|
reraise(exception, __STACKTRACE__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Inspect.Map.inspect(graph, opts)
|
|
|
|
end
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl Inspect, for: RDF.Dataset do
|
|
|
|
import Inspect.Algebra
|
|
|
|
|
2020-11-02 14:17:18 +00:00
|
|
|
def inspect(dataset, opts) do
|
|
|
|
map = [name: dataset.name, graph_names: Map.keys(dataset.graphs)]
|
|
|
|
open = color("%RDF.Dataset{", :map, opts)
|
|
|
|
sep = color(",", :map, opts)
|
|
|
|
close = color("}", :map, opts)
|
|
|
|
container_doc(open, map, close, opts, &Inspect.List.keyword/2, separator: sep, break: :strict)
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
end
|
2020-11-12 21:22:04 +00:00
|
|
|
|
|
|
|
defimpl Inspect, for: RDF.Diff do
|
|
|
|
def inspect(diff, opts) do
|
|
|
|
if opts.structs do
|
|
|
|
try do
|
|
|
|
{additions, deletions} = unify_metadata(diff.additions, diff.deletions)
|
|
|
|
|
|
|
|
"""
|
2021-02-28 12:29:10 +00:00
|
|
|
#RDF.Diff<
|
2020-11-12 21:22:04 +00:00
|
|
|
#{changes(additions, " + ", opts.limit)}
|
|
|
|
#{changes(deletions, " - ", opts.limit)}
|
2021-02-28 12:29:10 +00:00
|
|
|
>
|
2020-11-12 21:22:04 +00:00
|
|
|
"""
|
|
|
|
rescue
|
|
|
|
caught_exception ->
|
|
|
|
message =
|
|
|
|
"got #{inspect(caught_exception.__struct__)} with message " <>
|
|
|
|
"#{inspect(Exception.message(caught_exception))} while inspecting RDF.Diff"
|
|
|
|
|
|
|
|
exception = Inspect.Error.exception(message: message)
|
|
|
|
|
|
|
|
if opts.safe do
|
|
|
|
Inspect.inspect(exception, opts)
|
|
|
|
else
|
|
|
|
reraise(exception, __STACKTRACE__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Inspect.Map.inspect(diff, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp unify_metadata(additions, deletions) do
|
|
|
|
unified_base = unified_base(additions.base_iri, deletions.base_iri)
|
|
|
|
unified_prefixes = unified_prefixes(additions.prefixes, deletions.prefixes)
|
|
|
|
|
|
|
|
{
|
|
|
|
additions
|
|
|
|
|> RDF.Graph.set_base_iri(unified_base)
|
|
|
|
|> RDF.Graph.clear_prefixes()
|
|
|
|
|> RDF.Graph.add_prefixes(unified_prefixes),
|
|
|
|
deletions
|
|
|
|
|> RDF.Graph.set_base_iri(unified_base)
|
|
|
|
|> RDF.Graph.clear_prefixes()
|
|
|
|
|> RDF.Graph.add_prefixes(unified_prefixes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp unified_base(nil, nil), do: nil
|
|
|
|
defp unified_base(nil, deletions_base), do: deletions_base
|
|
|
|
defp unified_base(additions_base, _), do: additions_base
|
|
|
|
|
|
|
|
defp unified_prefixes(nil, nil), do: nil
|
|
|
|
defp unified_prefixes(additions_prefixes, nil), do: additions_prefixes
|
|
|
|
defp unified_prefixes(nil, deletions_prefixes), do: deletions_prefixes
|
|
|
|
|
|
|
|
defp unified_prefixes(additions_prefixes, deletions_prefixes) do
|
2021-03-15 19:06:02 +00:00
|
|
|
case RDF.PrefixMap.merge(additions_prefixes, deletions_prefixes, :ignore) do
|
|
|
|
{:ok, prefix_map} -> prefix_map
|
|
|
|
{:error, error} -> raise error
|
|
|
|
end
|
2020-11-12 21:22:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp changes(graph, prefix, limit) do
|
|
|
|
[_header | triples] =
|
|
|
|
graph
|
|
|
|
|> Kernel.inspect(limit: limit, custom_options: [no_metadata: true])
|
|
|
|
|> String.split("\n")
|
2021-02-28 12:29:10 +00:00
|
|
|
# remove the trailing ">"
|
|
|
|
|> List.delete_at(-1)
|
2020-11-12 21:22:04 +00:00
|
|
|
|
|
|
|
triples
|
|
|
|
|> Enum.map(&[prefix, &1])
|
|
|
|
|> Enum.intersperse("\n")
|
|
|
|
end
|
|
|
|
end
|