Predicator (Predicator v0.9.3) View Source
Documentation for Predicator.
Lexer and Parser currently compatible with 1.1.0 predicate syntax
Link to this section Summary
Types
The data map provided to predicates as they are evaluated.
Each instruction in encoded as a list. This is a list of these lists which can be
evaluated by a Predicator.Evaluator
.
The source code of the predicate.
This will be lexed, parsed and compiled into an array of instructions which can
be evaluated by a Predicator.Evaluator
.
Functions
Compiles the source
string into a list of instructions.
Compiles the source
string into a list of instructions.
Takes a predicate set, a context struct and options.
Compiles the source
into instructions, which are then evaluated, along with
the provided context
to return a boolean.
Compiles the source
into instructions, which are then evaluated, along with
the provided context
to return a boolean.
Evaluates the instructions
array, along with the provided context
, and
returns a boolean.
Evaluates the instructions
with the context
to return a boolean.
leex_and_parse/1 takes a string or charlist and does all lexing and parsing then returns the predicate.
leex_string/1 takes string or charlist and returns a lexed tuple for parsing.
Takes in a predicate and context and returns the match result.
Context can be either a list or map. Accepts same options as eval/3
.
parse_lexed/1 takes a leexed token(list or tup) and returns a predicate. It also
can take optional atom for type of token keys to return. options are :string_ey_inst
& :atom_key_inst
Link to this section Types
Specs
context() :: Map.t()
The data map provided to predicates as they are evaluated.
Specs
instructions() :: [list()]
Each instruction in encoded as a list. This is a list of these lists which can be
evaluated by a Predicator.Evaluator
.
Specs
Specs
source() :: String.t()
The source code of the predicate.
This will be lexed, parsed and compiled into an array of instructions which can
be evaluated by a Predicator.Evaluator
.
Specs
token_key_t() :: :atom_key_inst | :string_key_inst
Link to this section Functions
Compiles the source
string into a list of instructions.
These instructions can be evaluated by a Predicator.Evaluator
.
Examples
iex> Predicator.compile "score > 600 or income > 9000"
{:ok,
[
["load", "score"],
["lit", 600],
["compare", "GT"],
["jtrue", 4],
["load", "income"],
["lit", 9000],
["compare", "GT"]
]}
Compiles the source
string into a list of instructions.
Returns the unwarpped compiled instructions, or raises if an error occurs.
These instructions can be evaluated by a Predicator.Evaluator
.
Examples
iex> Predicator.compile! "score > 600 or income > 9000"
[
["load", "score"],
["lit", 600],
["compare", "GT"],
["jtrue", 4],
["load", "income"],
["lit", 9000],
["compare", "GT"]
]
Takes a predicate set, a context struct and options.
Eval options:
map_type
: type of keys for context map,:atom
or:string
. Defaults to:string
.nil_values
: list of values considered "blank" forisblank
comparison. Defaults to["", nil]
.
evaluate(source, context \\ Map.new(), opts \\ [map_type: :string, nil_values: ["", nil]])
View SourceSpecs
evaluate(source :: source(), context :: context(), opts :: Keyword.t()) :: {:ok, boolean()} | {:error, any()}
Compiles the source
into instructions, which are then evaluated, along with
the provided context
to return a boolean.
Options:
:map_type
: type of keys for context map,:atom
or:string
. Defaults to:string
.:nil_values
: list of values considered "blank" forisblank
comparison. Defaults to["", nil]
.
Examples
iex> Predicator.evaluate "score > 600", %{"score" => 590}
{:ok, false}
iex> Predicator.evaluate "score > 600", %{"score" => 610}
{:ok, true}
evaluate!(source, context \\ Map.new(), opts \\ [map_type: :string, nil_values: ["", nil]])
View SourceSpecs
Compiles the source
into instructions, which are then evaluated, along with
the provided context
to return a boolean.
Returns the unwarpped boolean, or raises if an error occurs.
Options:
:map_type
: type of keys for context map,:atom
or:string
. Defaults to:string
.:nil_values
: list of values considered "blank" forisblank
comparison. Defaults to["", nil]
.
Examples
iex> Predicator.evaluate! "score > 600", %{"score" => 590}
false
iex> Predicator.evaluate! "score > 600", %{"score" => 610}
true
evaluate_instructions(instructions, context \\ Map.new(), opts \\ [map_type: :string, nil_values: ["", nil]])
View SourceSpecs
evaluate_instructions( instructions :: instructions(), context :: context(), opts :: Keyword.t() ) :: {:ok, boolean()} | {:error, any()}
Evaluates the instructions
array, along with the provided context
, and
returns a boolean.
Options:
:map_type
: type of keys for context map,:atom
or:string
. Defaults to:string
.:nil_values
: list of values considered "blank" forisblank
comparison. Defaults to["", nil]
.
Examples
iex> Predicator.evaluate_instructions [["load", "score"], ["lit", 600], ["compare", "GT"]], %{"score" => 590}
{:ok, false}
iex> Predicator.evaluate_instructions [["load", "score"], ["lit", 600], ["compare", "GT"]], %{"score" => 610}
{:ok, true}
evaluate_instructions!(instructions, context \\ Map.new(), opts \\ [map_type: :string, nil_values: ["", nil]])
View SourceSpecs
evaluate_instructions!( instructions :: instructions(), context :: context(), opts :: Keyword.t() ) :: boolean()
Evaluates the instructions
with the context
to return a boolean.
Returns the unwarpped boolean, or raises if an error occurs.
Options:
:map_type
: type of keys for context map,:atom
or:string
. Defaults to:string
.:nil_values
: list of values considered "blank" forisblank
comparison. Defaults to["", nil]
.
Examples
iex> Predicator.evaluate_instructions! [["load", "score"], ["lit", 600], ["compare", "GT"]], %{"score" => 590}
false
iex> Predicator.evaluate_instructions! [["load", "score"], ["lit", 600], ["compare", "GT"]], %{"score" => 610}
true
leex_and_parse/1 takes a string or charlist and does all lexing and parsing then returns the predicate.
iex> leex_and_parse("13 > 12") [["lit", 13], ["lit", 12], ["compare", "GT"]]
iex> leex_and_parse('532 == 532', :atom_key_inst) [[:lit, 532], [:lit, 532], [:compare, :EQ]]
Specs
leex_string(predicate()) :: {:ok | :error, list() | tuple(), non_neg_integer()}
leex_string/1 takes string or charlist and returns a lexed tuple for parsing.
iex> leex_string('10 > 5') {:ok, [{:lit, 1, 10}, {:compare, 1, :GT}, {:lit, 1, 5}], 1}
iex> leex_string("apple > 5532") {:ok, [{:load, 1, :apple}, {:compare, 1, :GT}, {:lit, 1, 5532}], 1}
Takes in a predicate and context and returns the match result.
Context can be either a list or map. Accepts same options as eval/3
.
iex> matches?("fruit in ['apple', 'pear']", %{"fruit" => "watermelon"}, [map_type: :string])
false
iex> matches?("fruit in ['apple', 'pear']", [fruit: "pear"], [map_type: :atom])
true
iex> matches?("fruit is blank", [fruit: nil], [map_type: :atom, nil_values: [nil]])
true
Specs
parse_lexed(list(), token_key_t()) :: {:ok | :error, list() | tuple()}
parse_lexed/1 takes a leexed token(list or tup) and returns a predicate. It also
can take optional atom for type of token keys to return. options are :string_ey_inst
& :atom_key_inst
iex> parse_lexed({:ok, [{:load, 1, :apple}, {:compare, 1, :GT}, {:lit, 1, 5532}], 1})
{:ok, [["load", "apple"], ["lit", 5532], ["compare", "GT"]]}
iex> parse_lexed({:ok, [{:load, 1, :apple}, {:compare, 1, :GT}, {:lit, 1, 5532}], 1}, :string_key_inst)
{:ok, [["load", "apple"], ["lit", 5532], ["compare", "GT"]]}
iex> parse_lexed([{:load, 1, :apple}, {:compare, 1, :GT}, {:lit, 1, 5532}], :atom_key_inst)
{:ok, [[:load, :apple], [:lit, 5532], [:compare, :GT]]}