Skip to content

Commit 29639cd

Browse files
committed
feat(aws-stepfunctions): a method to retrieve all possible next states from a given initial state
1 parent 7695f2b commit 29639cd

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

packages/@aws-cdk/aws-stepfunctions/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,14 @@ new stepfunctions.Parallel(this, 'All jobs')
728728
.branch(new MyJob(this, 'Slow', { jobFlavor: 'slow' }).prefixStates());
729729
```
730730

731+
To retrieve all possible subsequent states from an initial state, you can use the
732+
method `State.findReachableStates`:
733+
734+
```typescript
735+
import * as sf from '@aws-cdk/aws-stepfunctions';
736+
const nextState = sf.State.findReachableStates(startState); // All reachable / next states
737+
```
738+
731739
## Activity
732740

733741
**Activities** represent work that is done on some non-Lambda worker pool. The

packages/@aws-cdk/aws-stepfunctions/lib/states/state.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,28 @@ export abstract class State extends cdk.Construct implements IChainable {
7373
}
7474
}
7575

76+
/**
77+
* Find the set of states reachable through transitions from the given start state
78+
*/
79+
public static findReachableStates(start: State, options: FindStateOptions = {}): State[] {
80+
const visited = new Set<State>();
81+
const ret = new Set<State>();
82+
const queue = [start];
83+
while (queue.length > 0) {
84+
const state = queue.splice(0, 1)[0]!;
85+
if (visited.has(state)) { continue; }
86+
visited.add(state);
87+
const outgoing = state.outgoingTransitions(options);
88+
queue.push(...outgoing);
89+
ret.add(state);
90+
}
91+
return Array.from(ret);
92+
}
93+
7694
/**
7795
* Find the set of end states states reachable through transitions from the given start state
7896
*/
79-
public static findReachableEndStates(start: State, options: FindStateOptions = {}) {
97+
public static findReachableEndStates(start: State, options: FindStateOptions = {}): State[] {
8098
const visited = new Set<State>();
8199
const ret = new Set<State>();
82100
const queue = [start];

packages/@aws-cdk/aws-stepfunctions/test/states-language.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,26 @@ describe('States Language', () => {
614614
expect(() => new stepfunctions.Parallel(stack, 'Parallel')
615615
.branch(state1.next(state2))
616616
.branch(state2)).toThrow();
617+
}),
618+
619+
test('Can retrieve possible states from initial state', () => {
620+
// GIVEN
621+
const stack = new cdk.Stack();
622+
const state1 = new stepfunctions.Pass(stack, 'State1');
623+
const state2 = new stepfunctions.Pass(stack, 'State2');
624+
const state3 = new stepfunctions.Pass(stack, 'State3');
625+
626+
const definition = state1
627+
.next(state2)
628+
.next(state3);
629+
630+
// WHEN
631+
const states = stepfunctions.State.findReachableStates(definition.startState);
632+
633+
// THEN
634+
expect(state1.toStateJson()).toStrictEqual(states[0].toStateJson());
635+
expect(state2.toStateJson()).toStrictEqual(states[1].toStateJson());
636+
expect(state3.toStateJson()).toStrictEqual(states[2].toStateJson());
617637
});
618638
});
619639

0 commit comments

Comments
 (0)