2020-06-08 09:36:22 +00:00
|
|
|
defmodule RDF.Query.BGP do
|
|
|
|
@enforce_keys [:triple_patterns]
|
|
|
|
defstruct [:triple_patterns]
|
|
|
|
|
2020-06-09 23:13:01 +00:00
|
|
|
|
2020-06-08 09:36:22 +00:00
|
|
|
@type variable :: String.t
|
|
|
|
@type triple_pattern :: {
|
|
|
|
subject :: variable | RDF.Term.t,
|
|
|
|
predicate :: variable | RDF.Term.t,
|
|
|
|
object :: variable | RDF.Term.t
|
|
|
|
}
|
|
|
|
@type triple_patterns :: list(triple_pattern)
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{triple_patterns: triple_patterns}
|
2020-06-08 19:56:50 +00:00
|
|
|
|
|
|
|
|
2020-06-09 23:13:01 +00:00
|
|
|
@doc """
|
|
|
|
Return a list of all variables in a BGP.
|
|
|
|
"""
|
2020-06-08 19:56:50 +00:00
|
|
|
def variables(%__MODULE__{triple_patterns: triple_patterns}), do: variables(triple_patterns)
|
|
|
|
|
|
|
|
def variables(triple_patterns) when is_list(triple_patterns) do
|
|
|
|
triple_patterns
|
2020-06-11 22:19:31 +00:00
|
|
|
|> Enum.flat_map(&variables/1)
|
2020-06-08 19:56:50 +00:00
|
|
|
|> Enum.uniq()
|
|
|
|
end
|
|
|
|
|
|
|
|
def variables({s, p, o}) when is_atom(s) and is_atom(p) and is_atom(o), do: [s, p, o]
|
|
|
|
def variables({s, p, _}) when is_atom(s) and is_atom(p), do: [s, p]
|
|
|
|
def variables({s, _, o}) when is_atom(s) and is_atom(o), do: [s, o]
|
|
|
|
def variables({_, p, o}) when is_atom(p) and is_atom(o), do: [p, o]
|
|
|
|
def variables({s, _, _}) when is_atom(s), do: [s]
|
|
|
|
def variables({_, p, _}) when is_atom(p), do: [p]
|
|
|
|
def variables({_, _, o}) when is_atom(o), do: [o]
|
|
|
|
|
|
|
|
def variables(_), do: []
|
2020-06-08 09:36:22 +00:00
|
|
|
end
|