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 exprin 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 state values using optional operators.
You can also refer to this as
from burr.core import whenin the API.Basic equality (unchanged from original):
when(foo="bar") # state["foo"] == "bar" when(foo="bar", baz="qux") # state["foo"] == "bar" AND state["baz"] == "qux"
Comparison operators via
__suffix:when(age__gt=18) # state["age"] > 18 when(age__gte=18) # state["age"] >= 18 when(age__lt=18) # state["age"] < 18 when(age__lte=18) # state["age"] <= 18 when(age__ne=0) # state["age"] != 0 when(age__eq=18) # state["age"] == 18 (explicit)
Membership operators:
when(status__in=["a", "b"]) # state["status"] in ["a", "b"] when(status__notin=["x", "y"]) # state["status"] not in ["x", "y"] when(tags__contains="python") # "python" in state["tags"]
Identity operators:
when(value__is=None) # state["value"] is None when(value__isnot=None) # state["value"] is not None
Multiple conditions are ANDed together:
when(age__gte=18, status="active") # age >= 18 AND status == "active"
- Parameters:
kwargs – Keyword arguments with optional
__operatorsuffixes- Returns:
A condition that checks all specified constraints (AND)