Tree Traversal in Workflow Definition
Your workflow is represented as a tree structure according to the specific definition. There can be multiple motivations for navigating through this tree. It is important to be aware that performing this operation requires a significant amount of processing power, so it is advisable to undertake it only when absolutely necessary. Additionally, it is recommended to utilize a cache to store the outcomes of the traversal.
if
|___ true
|___ someTask
|___ false
|___ someOtherTask
Get Parents
To retrieve the ancestors of a particular step, you can invoke the getStepParents()
method of the Designer
class.
Designer.getStepParents(needle: Sequence | Step | string): (Step | string)[];
This function can accept either a step, a step id or a sequence as its input. Its result is an array that includes all the parent steps and the parent branch names, arranged in a top-to-bottom sequence. The final element in the array corresponds to the step provided as input.
const designer = Designer.create(/* ... */);
const step: Step = /* ... */;
const result: (Step | string)[] = designer.getStepParents(step);
// result[0] = { componentType: 'switch', type: 'if', ... }
// result[1] = 'true'
// result[2] = step
By applying a filtering mechanism to the resulting array, you have the ability to conveniently exclude branch names if needed.
const parents: Step[] = result.filter((item) => typeof item === 'object');
React, Angular and Back-end
To get parents of a step in React, Angular or back-end, you need to use the DefinitionWalker
class from the sequential-workflow-model
package.
import { DefinitionWalker } from 'sequential-workflow-model';
const walker = new DefinitionWalker();
Now you need to call the getParents()
method. This method expects two arguments.
DefinitionWalker.getParents(definition: Definition, needle: Sequence | Step | string): (Step | string)[];
The first one is the current definition, the second is a step, a step id or a sequence that you want to get parents of.
walker.getParents(definition, step);
For Each
It's possible to iterate through all the steps in a definition by using the forEach()
method of the DefinitionWalker
class.
import { DefinitionWalker } from 'sequential-workflow-model';
const walker = new DefinitionWalker();
walker.forEach(definition, (step: Step, index: number, parentSequence: Sequence) => {
if (check(step)) {
return false; // stop iteration
}
// ...
});
The method accepts two arguments - the definition and a callback function. The callback function is invoked for each step in the definition. When the callback function returns false
, the iteration is stopped.