2020-06-11 22:19:31 +00:00
|
|
|
defmodule RDF.Query.Builder do
|
2020-06-16 10:05:44 +00:00
|
|
|
@moduledoc false
|
|
|
|
|
2020-06-11 22:19:31 +00:00
|
|
|
alias RDF.Query.BGP
|
2020-10-13 08:43:05 +00:00
|
|
|
alias RDF.{IRI, BlankNode, Literal, Namespace, PropertyMap}
|
2020-06-11 22:19:31 +00:00
|
|
|
import RDF.Utils.Guards
|
2020-09-30 10:45:33 +00:00
|
|
|
import RDF.Utils
|
2020-06-11 22:19:31 +00:00
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
def bgp(query, opts \\ []) do
|
|
|
|
property_map = if context = Keyword.get(opts, :context), do: PropertyMap.new(context)
|
|
|
|
|
|
|
|
with {:ok, triple_patterns} <- triple_patterns(query, property_map) do
|
2020-06-11 22:19:31 +00:00
|
|
|
{:ok, %BGP{triple_patterns: triple_patterns}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
def bgp!(query, opts \\ []) do
|
|
|
|
case bgp(query, opts) do
|
2020-06-11 22:19:31 +00:00
|
|
|
{:ok, bgp} -> bgp
|
|
|
|
{:error, error} -> raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
defp triple_patterns(query, property_map) when is_list(query) or is_map(query) do
|
2020-09-30 10:45:33 +00:00
|
|
|
flat_map_while_ok(query, fn triple ->
|
2020-10-13 08:43:05 +00:00
|
|
|
with {:ok, triple_pattern} <- triple_patterns(triple, property_map) do
|
2020-09-30 10:45:33 +00:00
|
|
|
{:ok, List.wrap(triple_pattern)}
|
|
|
|
end
|
2020-06-11 22:19:31 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
defp triple_patterns({subject, predicate, objects}, property_map) do
|
2020-09-30 14:38:29 +00:00
|
|
|
with {:ok, subject_pattern} <- subject_pattern(subject) do
|
2020-10-13 08:43:05 +00:00
|
|
|
do_triple_patterns(subject_pattern, {predicate, objects}, property_map)
|
2020-06-11 22:19:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
defp triple_patterns({subject, predications}, property_map) when is_map(predications) do
|
|
|
|
triple_patterns({subject, Map.to_list(predications)}, property_map)
|
2020-09-30 14:38:29 +00:00
|
|
|
end
|
2020-06-11 22:19:31 +00:00
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
defp triple_patterns({subject, predications}, property_map) do
|
2020-09-30 14:38:29 +00:00
|
|
|
with {:ok, subject_pattern} <- subject_pattern(subject) do
|
|
|
|
predications
|
|
|
|
|> List.wrap()
|
2020-10-13 08:43:05 +00:00
|
|
|
|> flat_map_while_ok(&do_triple_patterns(subject_pattern, &1, property_map))
|
2020-09-30 14:38:29 +00:00
|
|
|
end
|
|
|
|
end
|
2020-06-11 22:19:31 +00:00
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
defp do_triple_patterns(subject_pattern, {predicate, objects}, property_map) do
|
|
|
|
with {:ok, predicate_pattern} <- predicate_pattern(predicate, property_map) do
|
2020-09-30 14:38:29 +00:00
|
|
|
objects
|
|
|
|
|> List.wrap()
|
|
|
|
|> map_while_ok(fn object ->
|
|
|
|
with {:ok, object_pattern} <- object_pattern(object) do
|
|
|
|
{:ok, {subject_pattern, predicate_pattern, object_pattern}}
|
2020-06-11 22:19:31 +00:00
|
|
|
end
|
2020-09-30 14:38:29 +00:00
|
|
|
end)
|
2020-06-11 22:19:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp subject_pattern(subject) do
|
|
|
|
value = variable(subject) || resource(subject)
|
|
|
|
|
|
|
|
if value do
|
|
|
|
{:ok, value}
|
|
|
|
else
|
2020-06-29 08:37:42 +00:00
|
|
|
{:error,
|
|
|
|
%RDF.Query.InvalidError{
|
|
|
|
message: "Invalid subject term in BGP triple pattern: #{inspect(subject)}"
|
|
|
|
}}
|
2020-06-11 22:19:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
defp predicate_pattern(predicate, property_map) do
|
|
|
|
value = variable(predicate) || resource(predicate) || property(predicate, property_map)
|
2020-06-11 22:19:31 +00:00
|
|
|
|
|
|
|
if value do
|
|
|
|
{:ok, value}
|
|
|
|
else
|
2020-06-29 08:37:42 +00:00
|
|
|
{:error,
|
|
|
|
%RDF.Query.InvalidError{
|
|
|
|
message: "Invalid predicate term in BGP triple pattern: #{inspect(predicate)}"
|
|
|
|
}}
|
2020-06-11 22:19:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp object_pattern(object) do
|
|
|
|
value = variable(object) || resource(object) || literal(object)
|
|
|
|
|
|
|
|
if value do
|
|
|
|
{:ok, value}
|
|
|
|
else
|
2020-06-29 08:37:42 +00:00
|
|
|
{:error,
|
|
|
|
%RDF.Query.InvalidError{
|
|
|
|
message: "Invalid object term in BGP triple pattern: #{inspect(object)}"
|
|
|
|
}}
|
2020-06-11 22:19:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp variable(var) when is_atom(var) do
|
|
|
|
var_string = to_string(var)
|
|
|
|
|
|
|
|
if String.ends_with?(var_string, "?") do
|
|
|
|
var_string
|
|
|
|
|> String.slice(0..-2)
|
|
|
|
|> String.to_atom()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp variable(_), do: nil
|
|
|
|
|
|
|
|
defp resource(%IRI{} = iri), do: iri
|
|
|
|
defp resource(%URI{} = uri), do: IRI.new(uri)
|
|
|
|
defp resource(%BlankNode{} = bnode), do: bnode
|
|
|
|
|
|
|
|
defp resource(var) when is_ordinary_atom(var) do
|
|
|
|
case to_string(var) do
|
|
|
|
"_" <> bnode ->
|
|
|
|
BlankNode.new(bnode)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
case Namespace.resolve_term(var) do
|
|
|
|
{:ok, iri} -> iri
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp resource(_), do: nil
|
|
|
|
|
2020-10-13 08:43:05 +00:00
|
|
|
defp property(:a, _), do: RDF.type()
|
|
|
|
|
|
|
|
defp property(term, property_map) when is_atom(term) and not is_nil(property_map) do
|
|
|
|
PropertyMap.iri(property_map, term)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp property(_, _), do: nil
|
2020-06-11 22:19:31 +00:00
|
|
|
|
|
|
|
defp literal(%Literal{} = literal), do: literal
|
|
|
|
defp literal(value), do: Literal.coerce(value)
|
|
|
|
|
2020-06-12 00:53:29 +00:00
|
|
|
def path(query, opts \\ [])
|
|
|
|
|
|
|
|
def path(query, _) when is_list(query) and length(query) < 3 do
|
2020-06-29 08:37:42 +00:00
|
|
|
{:error,
|
|
|
|
%RDF.Query.InvalidError{
|
|
|
|
message: "Invalid path expression: must have at least three elements"
|
|
|
|
}}
|
2020-06-12 00:53:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def path([subject | rest], opts) do
|
|
|
|
path_pattern(subject, rest, [], 0, Keyword.get(opts, :with_elements, false))
|
2020-10-13 08:43:05 +00:00
|
|
|
|> bgp(opts)
|
2020-06-12 00:53:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def path!(query, opts \\ []) do
|
|
|
|
case path(query, opts) do
|
|
|
|
{:ok, bgp} -> bgp
|
|
|
|
{:error, error} -> raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp path_pattern(subject, [predicate, object], triple_patterns, _, _) do
|
|
|
|
[{subject, predicate, object} | triple_patterns]
|
|
|
|
|> Enum.reverse()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp path_pattern(subject, [predicate | rest], triple_patterns, count, with_elements) do
|
|
|
|
object = if with_elements, do: :"el#{count}?", else: RDF.bnode(count)
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
path_pattern(
|
|
|
|
object,
|
|
|
|
rest,
|
|
|
|
[{subject, predicate, object} | triple_patterns],
|
|
|
|
count + 1,
|
|
|
|
with_elements
|
|
|
|
)
|
2020-06-12 00:53:29 +00:00
|
|
|
end
|
2020-06-11 22:19:31 +00:00
|
|
|
end
|