Conditions/Transitions¶
Conditions represent choices to move between actions – these are read by the application builder when executing the graph.
Note that these will always be specified in order – the first condition that evaluates to True
will be the selected action.
- class burr.core.action.Condition(
- keys: List[str],
- resolver: Callable[[State], bool],
- name: str = None,
- optional_keys: List[str] = None,
- __init__(
- keys: List[str],
- resolver: Callable[[State], bool],
- name: str = None,
- optional_keys: List[str] = None,
Base condition class. Chooses keys to read from the state and a resolver function. If you want a condition that defaults to true, use Condition.default or just default.
Note that you can use a few fundamental operators to build more complex conditions:
~
operator allows you to automatically invert the condition.|
operator allows you to OR two conditions together.&
operator allows you to AND two conditions together.
- Parameters:
keys – Keys to read from the state
resolver – Function to resolve the condition to True or False
name – Name of the condition
- __and__(other: Condition) Condition ¶
Combines two conditions with an AND operator. This will return a new condition that is the AND of the two conditions.
To check if both foo is bar and baz is qux:
condition = Condition.when(foo="bar") & Condition.when(baz="qux") # equivalent to condition = Condition.when(foo="bar", baz="qux")
- Parameters:
other – Other condition to AND with
- Returns:
A new condition that is the AND of the two conditions
- __or__(other: Condition) Condition ¶
Combines two conditions with an OR operator. This will return a new condition that is the OR of the two conditions.
To check if either foo is bar or baz is qux:
condition = Condition.when(foo="bar") | Condition.when(baz="qux")
- Parameters:
other – Other condition to OR with
- Returns:
A new condition that is the OR of the two conditions
- static expr(expr: str) Condition ¶
Returns a condition that evaluates the given expression. Expression must use only state variables and Python operators. Do not trust that anything else will work.
Do not accept expressions generated from user-inputted text, this has the potential to be unsafe.
You can also refer to this as
from burr.core import expr
in the API.- Parameters:
expr – Expression to evaluate
- Returns:
A condition that evaluates the given expression
- static lmda(
- resolver: Callable[[State], bool],
- state_keys: List[str],
Returns a condition that evaluates the given function of State. Note that this is just a simple wrapper over the Condition object.
This does not (yet) support optional (default) arguments.
- Parameters:
fn
state_keys
- Returns:
- property reads: list[str]¶
Returns the keys from the state that this function reads
- Returns:
A list of keys
- run(state: State, **run_kwargs) dict ¶
Runs the function on the given state and returns the result. The result is just a key/value dictionary.
- Parameters:
state – State to run the function on
run_kwargs – Additional arguments to the function passed at runtime.
- Returns:
Result of the function
- classmethod when(**kwargs)¶
Returns a condition that checks if the given keys are in the state and equal to the given values.
You can also refer to this as
from burr.core import when
in the API.- Parameters:
kwargs – Keyword arguments of keys and values to check – will be an AND condition
- Returns:
A condition that checks if the given keys are in the state and equal to the given values