Iterating Over Elements
The forEach
operation enables workflows to process collections of elements by executing a sub-workflow for each element in parallel1.
This is useful for any scenario where you need to apply the same workflow logic to multiple items.
The ForEach operation takes a collection of elements and embeds a sub-workflow execution for each element. The ForEach step completes when all sub-workflows have finished.
Usage Example
This element is one of the most complicated ones and requires you to configure quite a lot of various aspects:
type Element
type Input
def elementWorkflow: SubWorkflowContext.WIO[Element, Nothing, SubWorkflowState] = SubWorkflowContext.WIO.pure(SubWorkflowState()).autoNamed
def getElements(input: Input): Set[Element] = ???
def initialElementState: SubWorkflowState = ???
def initialInterimState(input: Input): MyState = ???
def updateInterimState(elem: Element, elemState: SubWorkflowState, state: MyState): MyState = ???
def buildOutput(input: Input, results: Map[Element, SubWorkflowState]): MyState = ???
def eventEmbedding: WorkflowEmbedding.Event[(Element, SubWorkflowEvent), MyEventBase] =
new WorkflowEmbedding.Event[(Element, SubWorkflowEvent), MyEventBase] {
override def convertEvent(e: (Element, SubWorkflowEvent)): MyEventBase = ???
override def unconvertEvent(e: MyEventBase): Option[(Element, SubWorkflowEvent)] = ???
}
def signalRouter: SignalRouter.Receiver[Element, MyState] = SimpleSignalRouter[Element]()
val forEachStep = WIO
.forEach[Input](getElements)
.execute[SubWorkflowContext.Ctx](elementWorkflow, initialElementState)
.withEventsEmbeddedThrough(eventEmbedding)
.withInterimState(initialInterimState)
.incorporatingChangesThrough(updateInterimState)
.withOutputBuiltWith(buildOutput)
.withSignalsWrappedWith(signalRouter)
.autoNamed()
Rendering Outputs
- Flowchart
- BPMN
- Model
{
"forEach" : {
"meta" : {
"name" : "Element Workflow",
"error" : null
},
"_type" : "Pure"
},
"meta" : {
"name" : "For Each Step"
},
"_type" : "ForEach"
}
Draft Mode
For quick prototyping, you can use the draft API:
val subWorkflow = WIO.draft.step()
val forEachDraft = WIO.draft.forEach(subWorkflow)
Rendering Outputs
- Flowchart
- BPMN
- Model
{
"forEach" : {
"meta" : {
"name" : "Sub Workflow",
"error" : null,
"description" : null
},
"_type" : "RunIO"
},
"meta" : {
"name" : "For Each Draft"
},
"_type" : "ForEach"
}