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