2.4 Branch Operator
The BranchOperator is used to decide which path to run based on the input data.
Example, if you have two paths, you can decide which path to run based on the input data.
There are two ways to use the BranchOperator:
Build A BranchOperator With A Branch Mapping
Pass a dictionary of branch functions and task names to the BranchOperator constructor.
from dbgpt.core.awel import DAG, BranchOperator, MapOperator
def branch_even(x: int) -> bool:
return x % 2 == 0
def branch_odd(x: int) -> bool:
return not branch_even(x)
branch_mapping = {
branch_even: "even_task",
branch_odd: "odd_task"
}
with DAG("awel_branch_operator") as dag:
task = BranchOperator(branches=branch_mapping)
even_task = MapOperator(
task_name="even_task",
map_function=lambda x: print(f"{x} is even")
)
odd_task = MapOperator(
task_name="odd_task",
map_function=lambda x: print(f"{x} is odd")
)
In above example, the BranchOperator has two child tasks, even_task and odd_task.
The BranchOperator will decide which child task to run based on the input data.
So we pass a dictionary of branch functions and task names to the BranchOperator
constructor to define the branch mapping, in dictionary, the key is the branch function,
and the value is the task name, when run the branch task, all the branch function will
be executed, if the branch function return True, the task will be executed, otherwise,
it will be skipped.
Implement A Custom BranchOperator
Just override the branches method to return a dictionary of branch functions and task names.
from dbgpt.core.awel import DAG, BranchOperator, MapOperator
def branch_even(x: int) -> bool:
return x % 2 == 0
def branch_odd(x: int) -> bool:
return not branch_even(x)
class MyBranchOperator(BranchOperator[int]):
def __init__(self, even_task_name: str, odd_task_name: str, **kwargs):
self.even_task_name = even_task_name
self.odd_task_name = odd_task_name
super().__init__(**kwargs)
async def branches(self):
return {
branch_even: self.even_task_name,
branch_odd: self.odd_task_name
}
with DAG("awel_branch_operator") as dag:
task = MyBranchOperator(even_task_name="even_task", odd_task_name="odd_task")
even_task = MapOperator(
task_name="even_task",
map_function=lambda x: print(f"{x} is even")
)
odd_task = MapOperator(
task_name="odd_task",
map_function=lambda x: print(f"{x} is odd")
)