|
19 | 19 | import io.serverlessworkflow.api.events.EventDefinition;
|
20 | 20 | import io.serverlessworkflow.api.interfaces.State;
|
21 | 21 | import io.serverlessworkflow.api.start.Start;
|
| 22 | +import io.serverlessworkflow.api.states.CallbackState; |
22 | 23 | import io.serverlessworkflow.api.states.DefaultState;
|
| 24 | +import io.serverlessworkflow.api.states.EventState; |
| 25 | +import io.serverlessworkflow.api.states.SwitchState; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.HashSet; |
23 | 28 | import java.util.List;
|
| 29 | +import java.util.Set; |
24 | 30 | import java.util.stream.Collectors;
|
25 | 31 |
|
26 | 32 | /** Provides common utility methods to provide most often needed answers from a workflow */
|
@@ -120,4 +126,80 @@ public static int getDefinedConsumedEventsCount(Workflow workflow) {
|
120 | 126 | public static int getDefinedProducedEventsCount(Workflow workflow) {
|
121 | 127 | return getDefinedEventsCount(workflow, EventDefinition.Kind.PRODUCED);
|
122 | 128 | }
|
| 129 | + |
| 130 | + /** |
| 131 | + * Gets Consumed Events of parent workflow Iterates through states in parent workflow and collects |
| 132 | + * all the ConsumedEvents. Sub Workflows of the Workflow <strong>are not</strong> considered for |
| 133 | + * getting Consumed Events |
| 134 | + * |
| 135 | + * @return Returns {@code List<EventDefinition>} |
| 136 | + */ |
| 137 | + public static List<EventDefinition> getWorkflowConsumedEvents(Workflow workflow) { |
| 138 | + return getWorkflowEventDefinitions(workflow, EventDefinition.Kind.CONSUMED); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Gets Produced Events of parent workflow Iterates through states in parent workflow and collects |
| 143 | + * all the Produced Events. |
| 144 | + * |
| 145 | + * @return Returns {@code List<EventDefinition>} |
| 146 | + */ |
| 147 | + public static List<EventDefinition> getWorkflowProducedEvents(Workflow workflow) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Gets Events of parent workflow matching {@code EventDefinition.Kind} Iterates through states in |
| 153 | + * parent workflow and collects all the events matching {@code EventDefinition.Kind} . |
| 154 | + * |
| 155 | + * @return Returns {@code List<EventDefinition>} |
| 156 | + */ |
| 157 | + private static List<EventDefinition> getWorkflowEventDefinitions( |
| 158 | + Workflow workflow, EventDefinition.Kind eventKind) { |
| 159 | + if (workflow == null || workflow.getStates() == null || workflow.getStates().size() == 0) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + List<EventDefinition> definedConsumedEvents = getDefinedEvents(workflow, eventKind); |
| 163 | + if (definedConsumedEvents == null) return null; |
| 164 | + Set<String> uniqEventReferences = new HashSet<>(); |
| 165 | + List<String> eventReferencesFromState = getWorkflowConsumedEventsFromState(workflow); |
| 166 | + uniqEventReferences.addAll(eventReferencesFromState); |
| 167 | + return definedConsumedEvents.stream() |
| 168 | + .filter(x -> uniqEventReferences.contains(x.getName())) |
| 169 | + .collect(Collectors.toList()); |
| 170 | + } |
| 171 | + |
| 172 | + private static List<String> getWorkflowConsumedEventsFromState(Workflow workflow) { |
| 173 | + List<String> eventReferences = new ArrayList<>(); |
| 174 | + for (State state : workflow.getStates()) { |
| 175 | + if (state instanceof SwitchState) { |
| 176 | + SwitchState switchState = (SwitchState) state; |
| 177 | + if (switchState.getEventConditions() != null) { |
| 178 | + switchState |
| 179 | + .getEventConditions() |
| 180 | + .forEach(eventCondition -> eventReferences.add(eventCondition.getEventRef())); |
| 181 | + } |
| 182 | + } else if (state instanceof CallbackState) { |
| 183 | + CallbackState callbackState = (CallbackState) state; |
| 184 | + eventReferences.add(callbackState.getEventRef()); |
| 185 | + } else if (state instanceof EventState) { |
| 186 | + EventState eventState = (EventState) state; |
| 187 | + if (eventState.getOnEvents() != null) { |
| 188 | + eventState |
| 189 | + .getOnEvents() |
| 190 | + .forEach(onEvents -> eventReferences.addAll(onEvents.getEventRefs())); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + return eventReferences; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @return Returns {@code int } Count of the workflow consumed events. <strong>Does not</strong> |
| 199 | + * consider sub-workflows |
| 200 | + */ |
| 201 | + public static int getWorkflowConsumedEventsCount(Workflow workflow) { |
| 202 | + List<EventDefinition> workflowConsumedEvents = getWorkflowConsumedEvents(workflow); |
| 203 | + return workflowConsumedEvents == null ? 0 : workflowConsumedEvents.size(); |
| 204 | + } |
123 | 205 | }
|
0 commit comments