2017-04-12 09:15:53 +00:00
|
|
|
defmodule RDF.InspectHelper do
|
|
|
|
@moduledoc false
|
|
|
|
|
|
|
|
import Inspect.Algebra
|
|
|
|
|
|
|
|
def objects_doc(objects, opts) do
|
|
|
|
objects
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Enum.map(fn {object, _} -> to_doc(object, opts) end)
|
|
|
|
|> fold_doc(fn object, acc -> line(object, acc) end)
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def predications_doc(predications, opts) do
|
|
|
|
predications
|
|
|
|
|> Enum.map(fn {predicate, objects} ->
|
2020-06-29 08:37:42 +00:00
|
|
|
to_doc(predicate, opts)
|
|
|
|
|> line(objects_doc(objects, opts))
|
|
|
|
|> nest(4)
|
|
|
|
end)
|
|
|
|
|> fold_doc(fn predication, acc ->
|
|
|
|
line(predication, acc)
|
|
|
|
end)
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def descriptions_doc(descriptions, opts) do
|
|
|
|
descriptions
|
|
|
|
|> Enum.map(fn {subject, description} ->
|
2020-06-29 08:37:42 +00:00
|
|
|
to_doc(subject, opts)
|
|
|
|
|> line(predications_doc(description.predications, opts))
|
|
|
|
|> nest(4)
|
|
|
|
end)
|
|
|
|
|> fold_doc(fn predication, acc ->
|
|
|
|
line(predication, acc)
|
|
|
|
end)
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
2019-02-15 01:30:59 +00:00
|
|
|
|
|
|
|
def surround_doc(left, doc, right) do
|
|
|
|
concat(concat(left, nest(doc, 1)), right)
|
|
|
|
end
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
import Inspect.Algebra
|
|
|
|
import RDF.InspectHelper
|
|
|
|
|
|
|
|
def inspect(%RDF.Description{subject: subject, predications: predications}, opts) do
|
|
|
|
doc =
|
2017-08-20 20:35:14 +00:00
|
|
|
space("subject:", to_doc(subject, opts))
|
2017-04-12 09:15:53 +00:00
|
|
|
|> line(predications_doc(predications, opts))
|
|
|
|
|> nest(4)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2019-02-15 01:30:59 +00:00
|
|
|
surround_doc("#RDF.Description{", doc, "}")
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl Inspect, for: RDF.Graph do
|
|
|
|
import Inspect.Algebra
|
|
|
|
import RDF.InspectHelper
|
|
|
|
|
|
|
|
def inspect(%RDF.Graph{name: name, descriptions: descriptions}, opts) do
|
|
|
|
doc =
|
2017-08-20 20:35:14 +00:00
|
|
|
space("name:", to_doc(name, opts))
|
2017-04-12 09:15:53 +00:00
|
|
|
|> line(descriptions_doc(descriptions, opts))
|
|
|
|
|> nest(4)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2019-02-15 01:30:59 +00:00
|
|
|
surround_doc("#RDF.Graph{", doc, "}")
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl Inspect, for: RDF.Dataset do
|
|
|
|
import Inspect.Algebra
|
2019-02-15 01:30:59 +00:00
|
|
|
import RDF.InspectHelper
|
2017-04-12 09:15:53 +00:00
|
|
|
|
2017-06-08 19:11:56 +00:00
|
|
|
def inspect(%RDF.Dataset{name: name} = dataset, opts) do
|
2017-04-12 09:15:53 +00:00
|
|
|
doc =
|
2017-08-20 20:35:14 +00:00
|
|
|
space("name:", to_doc(name, opts))
|
2017-06-08 19:11:56 +00:00
|
|
|
|> line(graphs_doc(RDF.Dataset.graphs(dataset), opts))
|
2017-04-12 09:15:53 +00:00
|
|
|
|> nest(4)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2019-02-15 01:30:59 +00:00
|
|
|
surround_doc("#RDF.Dataset{", doc, "}")
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp graphs_doc(graphs, opts) do
|
|
|
|
graphs
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Enum.map(fn graph -> to_doc(graph, opts) end)
|
|
|
|
|> fold_doc(fn graph, acc -> line(graph, acc) end)
|
2017-04-12 09:15:53 +00:00
|
|
|
end
|
|
|
|
end
|