rdf-ex/lib/rdf/query/bgp.ex

46 lines
1.4 KiB
Elixir
Raw Normal View History

2020-06-08 09:36:22 +00:00
defmodule RDF.Query.BGP do
@moduledoc """
A struct for Basic Graph Pattern queries.
See `RDF.Query` and its functions on how to construct this query struct and
apply it on `RDF.Graph`s.
"""
2020-06-08 09:36:22 +00:00
@enforce_keys [:triple_patterns]
defstruct [:triple_patterns]
2020-06-29 08:37:42 +00:00
@type variable :: String.t()
2020-06-08 09:36:22 +00:00
@type triple_pattern :: {
2020-06-29 08:37:42 +00:00
subject :: variable | RDF.Term.t(),
predicate :: variable | RDF.Term.t(),
object :: variable | RDF.Term.t()
}
2020-06-08 09:36:22 +00:00
@type triple_patterns :: list(triple_pattern)
@type t :: %__MODULE__{triple_patterns: triple_patterns}
2020-06-09 23:13:01 +00:00
@doc """
Return a list of all variables in a BGP.
"""
@spec variables(any) :: [atom]
def variables(bgp)
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)
|> 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