我开始学习liteflow,想知道是否可以将for_each
和if_
结合在一起。我尝试了以下方法:
from liteflow.core import *
class Hello(StepBody):
def run(self, context: StepExecutionContext) -> ExecutionResult:
print("Hello")
return ExecutionResult.next()
class DoStuff(StepBody):
def run(self, context: StepExecutionContext) -> ExecutionResult:
print(f"doing stuff...{context.execution_pointer.context_item}")
return ExecutionResult.next()
class Goodbye(StepBody):
def run(self, context: StepExecutionContext) -> ExecutionResult:
print("Goodbye")
return ExecutionResult.next()
class MyWorkflow(Workflow):
def id(self):
return "MyWorkflow"
def version(self):
return 1
def build(self, builder: WorkflowBuilder):
builder\
.start_with(Hello)\
.for_each(lambda data, context: ["abc", "def", "xyz"])\
.if_(lambda d, c: not d.startswith("x"))\
.do(lambda x: x.start_with(DoStuff))\
.then(Goodbye)
host = configure_workflow_host()
host.register_workflow(MyWorkflow())
host.start()
wid = host.start_workflow("MyWorkflow", 1, None)
input()
host.stop()
这不起作用('NoneType' object has no attribute 'startswith'
),如果我将条件替换为0 < 1
,DoStuff
将被执行一次,而不是没有if_
进行三次,甚至打印“错误”的输出。
在这个简单的示例(if_
)中,我知道可以通过在for_each
中使用LC来消除[x for x in ["abc", "def", "xyz"] if not x.startswith("x")]
,但是想象一个更复杂的工作流程。
因此,我如何结合for_each
和if_
,以便从for_each
中的if_
访问循环变量,并将正确的上下文传递给{{ 1}}?还是这不可能,我必须在我的步骤的do
方法中检查条件?