async Task IRuntimeWorkflowEngine.SendEventToNestedWorkflows(ActivityExecutionContext activityExecutionContext, Guid workflowId, string code, CancellationToken cancellationToken)
        {
            Guard.ArgumentNotNullOrEmpty(code, nameof(code));

            // obtain all the workflow instances of a specified nested workflow class
            var nestedWorkflowInstancesEnumerable = await WorkflowEngineBuilder.WorkflowStore.GetNestedWorkflowInstances(
                activityExecutionContext.StateExecutionContext.WorkflowContext.WorkflowInstance.Id, workflowId, cancellationToken).ConfigureAwait(false);

            if (null == nestedWorkflowInstancesEnumerable)
            {
                return;
            }

            var nestedWorkflowInstances = nestedWorkflowInstancesEnumerable.ToList();

            if (nestedWorkflowInstances.Count == 0)
            {
                return;
            }

            foreach (var nestedWorkflowInstance in nestedWorkflowInstances)
            {
                var parentToNestedEvent = new ParentToNestedEntityEvent(code, WorkflowHierarchyEventType.Application,
                                                                        nestedWorkflowInstance.RootWorkflowInstanceId, nestedWorkflowInstance.ParentWorkflowInstanceId, nestedWorkflowInstance, null);
                await PulseToNestedWorkflow(activityExecutionContext.StateExecutionContext.WorkflowContext, parentToNestedEvent, cancellationToken).ConfigureAwait(false);
            }
        }
        private Task <WorkflowProcessingResult> InitiateNestedWorkflowInternal(IWorkflowInstance workflowInstance, Guid nestedWorkflowId, IEntity nestedEntity, CancellationToken cancellationToken = default)
        {
            var parentToNestedEvent = new ParentToNestedEntityEvent(EventTypeConfiguration.ParentToNestedInitial.ToString("G"),
                                                                    WorkflowHierarchyEventType.Initialize,
                                                                    workflowInstance.RootWorkflowInstanceId, workflowInstance.Id, default(Guid), nestedEntity);

            return(((IWorkflowEngine)this).ProcessEvent(nestedWorkflowId, parentToNestedEvent, cancellationToken));
        }
        private Task PulseToNestedWorkflow(WorkflowContext workflowContext, ParentToNestedEntityEvent parentToNestedEntityEvent, CancellationToken cancellationToken = default)
        {
            // [Assumption #1] Failed event should be propagated with no additional checks for validity
            if (parentToNestedEntityEvent.HierarchyEventType == WorkflowHierarchyEventType.Failed)
            {
                return(((IWorkflowEngine)this).ProcessEvent(parentToNestedEntityEvent.TargetWorkflowInstance.WorkflowId, parentToNestedEntityEvent, cancellationToken));
            }

            // [Assumption #2] Nested workflow instance must be in [AwaitingEvent] state for application scope events to pass through
            if (parentToNestedEntityEvent.HierarchyEventType == WorkflowHierarchyEventType.Application &&
                parentToNestedEntityEvent.TargetWorkflowInstance.CurrentStateProgress != StateExecutionProgress.AwaitingEvent)
            {
                throw new WorkflowException(string.Format(CultureInfo.InvariantCulture,
                                                          "Nested Workflow instance [ID={0:D}] must be waiting for an event from parent workflow instance [ID={1:D}], but it is in [State={2}..{3:G}]",
                                                          parentToNestedEntityEvent.TargetWorkflowInstance.Id, workflowContext.WorkflowInstance.Id, parentToNestedEntityEvent.TargetWorkflowInstance.CurrentStateCode,
                                                          parentToNestedEntityEvent.TargetWorkflowInstance.CurrentStateProgress));
            }

            return(((IWorkflowEngine)this).ProcessEvent(parentToNestedEntityEvent.TargetWorkflowInstance.WorkflowId, parentToNestedEntityEvent, cancellationToken));
        }
        private async Task PulseFailedToNestedWorkflows(WorkflowContext workflowContext, CancellationToken cancellationToken)
        {
            // all the workflow instances of all the nested workflow classes ==> need to move to failed state
            var nestedWorkflowInstancesEnumerable = await WorkflowEngineBuilder.WorkflowStore.GetNestedWorkflowInstances(workflowContext.WorkflowInstance.Id, cancellationToken).ConfigureAwait(false);

            if (null == nestedWorkflowInstancesEnumerable)
            {
                return;
            }

            var nestedWorkflowInstances = nestedWorkflowInstancesEnumerable.ToList();

            if (nestedWorkflowInstances.Count == 0)
            {
                return;
            }

            foreach (var nestedWorkflowInstance in nestedWorkflowInstances)
            {
                var parentToNestedFailedEvent = new ParentToNestedEntityEvent(EventTypeConfiguration.ParentToNestedFailed.ToString("G"), WorkflowHierarchyEventType.Failed,
                                                                              nestedWorkflowInstance.RootWorkflowInstanceId, nestedWorkflowInstance.ParentWorkflowInstanceId, nestedWorkflowInstance, null);
                await PulseToNestedWorkflow(workflowContext, parentToNestedFailedEvent, cancellationToken).ConfigureAwait(false);
            }
        }