if / elif / else
Use if / elif / else when you want priority-ordered decisions. It is equivalent to match().case() in hit_first mode.
Scenario: grade by score
python
from agently import TriggerFlow, TriggerFlowEventData
flow = TriggerFlow()
@flow.chunk
def input_score(_: TriggerFlowEventData):
return {"score": 82}
@flow.chunk
def grade_a(_: TriggerFlowEventData):
return "A"
@flow.chunk
def grade_b(_: TriggerFlowEventData):
return "B"
@flow.chunk
def grade_c(_: TriggerFlowEventData):
return "C"
@flow.chunk
def print_result(data: TriggerFlowEventData):
print(data.value)
(
flow.to(input_score)
.if_condition(lambda d: d.value["score"] >= 90)
.to(grade_a)
.elif_condition(lambda d: d.value["score"] >= 80)
.to(grade_b)
.else_condition()
.to(grade_c)
.end_condition()
.to(print_result)
)
flow.start(wait_for_result=False)Output:
text
BScenario: nested rules (primary rule + sub-rule)
python
from agently import TriggerFlow, TriggerFlowEventData
flow = TriggerFlow()
@flow.chunk
def input_score(_: TriggerFlowEventData):
return {"score": 95, "vip": False}
@flow.chunk
def to_grade(data: TriggerFlowEventData):
return {"grade": "A", "vip": data.value["vip"]}
@flow.chunk
def grade_a_plus(_: TriggerFlowEventData):
return "A+"
@flow.chunk
def grade_a(_: TriggerFlowEventData):
return "A"
@flow.chunk
def grade_b(_: TriggerFlowEventData):
return "B"
@flow.chunk
def print_result(data: TriggerFlowEventData):
print(data.value)
(
flow.to(input_score)
.if_condition(lambda d: d.value["score"] >= 90)
.to(to_grade)
.if_condition(lambda d: d.value["vip"])
.to(grade_a_plus)
.else_condition()
.to(grade_a)
.end_condition()
.else_condition()
.to(grade_b)
.end_condition()
.to(print_result)
)
flow.start(wait_for_result=False)Output:
text
A