ez-flow – Typescript library for a workflow engine
Roberto B. Stanziale
Posted on February 25, 2024
This library was heavily inspired to j-easy/easy-flows in the Java world.
Definitions
A Workflow is a collection of Work Units must be run tu achieve a given purpose.
Each Work unit implements a behaviour and interacts with outer environment and the other units by the means of a Context exchanged between them. You can place each unit in a flow applying some logic to control it.
That is you can place unit in a sequential flow, in a conditional one, in a parallel one and so on.
At the end of this "chain" we obtain the final result.
In this library are actually available the following flow constructs:
Sequential Flow
Conditional Flow
Iterative Flow
Parallel Flow
How to apply a flow?
Suppose you have defined a work unit called PrintMessageWork that implements Work. It takes a message and its call method prints it:
To test the library, a use case scenario was built in which, given a list of Italian cities, I wish to perform certain operations on each city and produce results.
To achieve this, it was important to first define the available inputs and what the workflow wanted to obtain, in this case the cumulative data on the regions to which the cities belong.
Installation
npm install @rs-box/ez-flow
Workflows definition
Specifically, three workflows were built:
Main workflow
Loop over cities workflow
Operations workflow
Main
This is the main workflow, a Sequential Flow that performs two operations:
Validate the context given as input (ValidateContext unit).
Calling the Loop over cities workflow.
Loop over cities
This is a Repeat Flow, it will run until the IsNotLastCity predicate returns a negative value
Operations city
This is a Parallel Flow that executes the list of units passed as input, in the specific case of this test it will perform the following actions:
Print the city name
Update the cumulative region data.
Complete
This is the complete workflow resulting from the combination of those shown above.
Result
Finally, the workflow described above is translated as follows:
constworkflow=SequentialFlow.Builder.newFlow().withName('Main workflow').addWork(newValidateContextWork()).addWork(RepeatFlow.Builder.newFlow().withName('Loop over cities').withWork(ParallelFlow.Builder.newFlow().withName('Operations city').withWorks([newPrintCityWork(),newRegionCity()]).build(),).until(newIsNotLastCityPredicate()).build(),).build();
Source code
You can find the full source code for this scenario here:
This application test aims to demonstrate @rs-box/ez-flow functions.
Use case
To test the library, a use case scenario was built in which, given a list of Italian cities, I wish to perform certain operations on each city and produce results.
To achieve this, it was important to first define the available inputs and what the workflow wanted to obtain, in this case the cumulative data on the regions to which the cities belong.
Workflows definition
Specifically, three workflows were built:
Main workflow
Loop over cities workflow
Operations workflow
Main
This is the main workflow, a Sequential Flow that performs two operations:
Validate the context given as input (ValidateContext unit).
Calling the Loop over cities workflow.
Loop over cities
This is a Repeat Flow, it will run until the IsNotLastCity predicate returns a negative value
Operations city
This is a Parallel Flow that executes the list of units…