|
19 | 19 | import io.serverlessworkflow.api.actions.Action;
|
20 | 20 | import io.serverlessworkflow.api.branches.Branch;
|
21 | 21 | import io.serverlessworkflow.api.events.EventDefinition;
|
| 22 | +import io.serverlessworkflow.api.events.OnEvents; |
| 23 | +import io.serverlessworkflow.api.functions.FunctionDefinition; |
| 24 | +import io.serverlessworkflow.api.functions.FunctionRef; |
22 | 25 | import io.serverlessworkflow.api.interfaces.State;
|
23 | 26 | import io.serverlessworkflow.api.start.Start;
|
24 | 27 | import io.serverlessworkflow.api.states.*;
|
@@ -157,12 +160,12 @@ private static List<EventDefinition> getWorkflowEventDefinitions(
|
157 | 160 |
|
158 | 161 | List<String> uniqueWorkflowEventsFromStates = getUniqueWorkflowEventsFromStates(workflow);
|
159 | 162 | List<EventDefinition> definedConsumedEvents = getDefinedEvents(workflow, eventKind);
|
160 |
| - if(definedConsumedEvents == null) { |
| 163 | + if (definedConsumedEvents == null) { |
161 | 164 | return null;
|
162 | 165 | }
|
163 | 166 | return definedConsumedEvents.stream()
|
164 |
| - .filter(definedEvent -> uniqueWorkflowEventsFromStates.contains(definedEvent.getName())) |
165 |
| - .collect(Collectors.toList()); |
| 167 | + .filter(definedEvent -> uniqueWorkflowEventsFromStates.contains(definedEvent.getName())) |
| 168 | + .collect(Collectors.toList()); |
166 | 169 | }
|
167 | 170 |
|
168 | 171 | /** Returns a list of unique event names from workflow states */
|
@@ -240,6 +243,106 @@ public static int getWorkflowProducedEventsCount(Workflow workflow) {
|
240 | 243 | return workflowProducedEvents == null ? 0 : workflowProducedEvents.size();
|
241 | 244 | }
|
242 | 245 |
|
| 246 | + /** @return Returns function definition for actions */ |
| 247 | + public static FunctionDefinition getFunctionDefinitionsForAction( |
| 248 | + Workflow workflow, String action) { |
| 249 | + if (!hasFunctionDefs(workflow)) return null; |
| 250 | + FunctionRef functionRef = getFunctionRefFromAction(workflow, action); |
| 251 | + if (functionRef == null) return null; |
| 252 | + final Optional<FunctionDefinition> functionDefinition = |
| 253 | + workflow.getFunctions().getFunctionDefs().stream() |
| 254 | + .filter(functionDef -> functionDef.getName().equals(functionRef.getRefName())) |
| 255 | + .distinct() |
| 256 | + .findFirst(); |
| 257 | + |
| 258 | + return functionDefinition.isPresent() ? functionDefinition.get() : null; |
| 259 | + } |
| 260 | + |
| 261 | + private static FunctionRef getFunctionRefFromAction(Workflow workflow, String action) { |
| 262 | + if (!hasStates(workflow)) return null; |
| 263 | + |
| 264 | + for (State state : workflow.getStates()) { |
| 265 | + if (state instanceof EventState) { |
| 266 | + EventState eventState = (EventState) state; |
| 267 | + List<OnEvents> onEvents = eventState.getOnEvents(); |
| 268 | + if (onEvents != null) { |
| 269 | + for (OnEvents onEvent : onEvents) { |
| 270 | + if (onEvent != null) { |
| 271 | + List<Action> onEventActions = onEvent.getActions(); |
| 272 | + if (onEventActions != null) { |
| 273 | + for (Action onEventAction : onEventActions) { |
| 274 | + if (onEventAction != null |
| 275 | + && onEventAction.getName() != null |
| 276 | + && onEventAction.getName().equals(action)) |
| 277 | + return onEventAction.getFunctionRef(); |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + } else if (state instanceof CallbackState) { |
| 284 | + CallbackState callbackState = (CallbackState) state; |
| 285 | + final Action callbackStateAction = callbackState.getAction(); |
| 286 | + if (callbackStateAction != null |
| 287 | + && callbackStateAction.getName() != null |
| 288 | + && callbackStateAction.getName().equals(action)) { |
| 289 | + return callbackStateAction.getFunctionRef(); |
| 290 | + } |
| 291 | + |
| 292 | + } else if (state instanceof OperationState) { |
| 293 | + OperationState operationState = (OperationState) state; |
| 294 | + final List<Action> operationStateActions = operationState.getActions(); |
| 295 | + if (operationStateActions != null) { |
| 296 | + for (Action operationStateAction : operationStateActions) { |
| 297 | + if (operationStateAction != null |
| 298 | + && operationStateAction.getName() != null |
| 299 | + && operationStateAction.getName().equals(action)) { |
| 300 | + return operationStateAction.getFunctionRef(); |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + } else if (state instanceof ParallelState) { |
| 305 | + ParallelState parallelState = (ParallelState) state; |
| 306 | + List<Branch> parallelStateBranches = parallelState.getBranches(); |
| 307 | + if (parallelStateBranches != null) { |
| 308 | + for (Branch branch : parallelStateBranches) { |
| 309 | + List<Action> branchActions = branch.getActions(); |
| 310 | + if (branchActions != null) { |
| 311 | + for (Action branchAction : branchActions) { |
| 312 | + if (branchAction != null |
| 313 | + && branchAction.getName() != null |
| 314 | + && branchAction.getName().equals(action)) { |
| 315 | + return branchAction.getFunctionRef(); |
| 316 | + } |
| 317 | + } |
| 318 | + } |
| 319 | + } |
| 320 | + } |
| 321 | + } else if (state instanceof ForEachState) { |
| 322 | + ForEachState forEachState = (ForEachState) state; |
| 323 | + List<Action> forEachStateActions = forEachState.getActions(); |
| 324 | + if (forEachStateActions != null) { |
| 325 | + for (Action forEachStateAction : forEachStateActions) { |
| 326 | + if (forEachStateAction != null |
| 327 | + && forEachStateAction.getName() != null |
| 328 | + && forEachStateAction.getName().equals(action)) { |
| 329 | + return forEachStateAction.getFunctionRef(); |
| 330 | + } |
| 331 | + } |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + |
| 336 | + return null; |
| 337 | + } |
| 338 | + |
| 339 | + private static boolean hasFunctionDefs(Workflow workflow) { |
| 340 | + return workflow != null |
| 341 | + && workflow.getFunctions() != null |
| 342 | + && workflow.getFunctions().getFunctionDefs() != null |
| 343 | + && !workflow.getFunctions().getFunctionDefs().isEmpty(); |
| 344 | + } |
| 345 | + |
243 | 346 | /** Returns true if workflow has states, otherwise false */
|
244 | 347 | private static boolean hasStates(Workflow workflow) {
|
245 | 348 | return workflow != null && workflow.getStates() != null && !workflow.getStates().isEmpty();
|
|
0 commit comments