Skip to content

Commit 1850f7e

Browse files
committed
Fixed GetNumOfEndStates util method to take care of SwitchState #127
Signed-off-by: manick02 <[email protected]>
1 parent 37d15bd commit 1850f7e

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

utils/src/main/java/io/serverlessworkflow/utils/WorkflowUtils.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
import io.serverlessworkflow.api.Workflow;
1919
import io.serverlessworkflow.api.actions.Action;
2020
import io.serverlessworkflow.api.branches.Branch;
21+
import io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition;
2122
import io.serverlessworkflow.api.events.EventDefinition;
2223
import io.serverlessworkflow.api.events.OnEvents;
2324
import io.serverlessworkflow.api.functions.FunctionDefinition;
2425
import io.serverlessworkflow.api.functions.FunctionRef;
2526
import io.serverlessworkflow.api.interfaces.State;
2627
import io.serverlessworkflow.api.start.Start;
2728
import io.serverlessworkflow.api.states.*;
29+
import io.serverlessworkflow.api.switchconditions.DataCondition;
30+
import io.serverlessworkflow.api.switchconditions.EventCondition;
2831
import java.util.*;
2932
import java.util.stream.Collectors;
3033

@@ -291,9 +294,40 @@ public static long getNumOfStates(Workflow workflow, DefaultState.Type type) {
291294
}
292295

293296
public static long getNumOfEndStates(Workflow workflow) {
294-
return hasStates(workflow)
295-
? workflow.getStates().stream().filter(state -> state.getEnd() != null).count()
296-
: DEFAULT_STATE_COUNT;
297+
if (hasStates(workflow)) {
298+
long count = workflow.getStates().stream().filter(state -> state.getEnd() != null).count();
299+
List<State> switchStates =
300+
workflow.getStates().stream()
301+
.filter(state -> state instanceof SwitchState)
302+
.collect(Collectors.toList());
303+
for (State state : switchStates) {
304+
SwitchState switchState = (SwitchState) state;
305+
List<EventCondition> eventConditions = switchState.getEventConditions();
306+
if (eventConditions != null) {
307+
count =
308+
count
309+
+ eventConditions.stream()
310+
.filter(eventCondition -> eventCondition.getEnd() != null)
311+
.count();
312+
}
313+
List<DataCondition> dataConditions = switchState.getDataConditions();
314+
if (dataConditions != null) {
315+
count =
316+
count
317+
+ dataConditions.stream()
318+
.filter(dataCondition -> dataCondition.getEnd() != null)
319+
.count();
320+
}
321+
DefaultConditionDefinition defaultCondition = switchState.getDefaultCondition();
322+
if (defaultCondition != null) {
323+
count = defaultCondition.getEnd() != null ? 1 : 0;
324+
}
325+
}
326+
327+
return count;
328+
} else {
329+
return DEFAULT_STATE_COUNT;
330+
}
297331
}
298332

299333
private static List<Action> getActionsWhichUsesFunctionDefinition(

0 commit comments

Comments
 (0)