protected override void OnActivityChangeAdd(ActivityExecutionContext executionContext, Activity addedActivity) { if (executionContext == null) { throw new ArgumentNullException("executionContext"); } if (addedActivity == null) { throw new ArgumentNullException("addedActivity"); } if (addedActivity.Enabled && (executionContext.Activity.ExecutionStatus == ActivityExecutionStatus.Executing)) { EventDrivenActivity eventDriven = addedActivity as EventDrivenActivity; if (eventDriven != null) { StateMachineSubscriptionManager.ChangeEventDrivenQueueState(executionContext, eventDriven, false); StateMachineExecutionState state = StateMachineExecutionState.Get(StateMachineHelpers.GetRootState(executionContext.Activity as StateActivity)); if (StateMachineHelpers.GetCurrentState(executionContext) != null) { state.SubscriptionManager.ReevaluateSubscriptions(executionContext); state.LockQueue(); state.ProcessActions(executionContext); } } } }
internal void ProcessQueue(ActivityExecutionContext context) { StateActivity currentState = StateMachineHelpers.GetCurrentState(context); if (((this.EventQueue.Count != 0) && !this.ExecutionState.HasEnqueuedActions) && (!this.ExecutionState.SchedulerBusy && (currentState != null))) { this.Dequeue().ProcessEvent(context); } }
internal void ProcessTransitionRequest(ActivityExecutionContext context) { if (!string.IsNullOrEmpty(this.NextStateName)) { StateActivity currentState = StateMachineHelpers.GetCurrentState(context); this.CalculateStateTransition(currentState, this.NextStateName); this.LockQueue(); this.NextStateName = null; } }
internal virtual void Execute(ActivityExecutionContext context) { if (context == null) { throw new ArgumentNullException("context"); } this._state = (StateActivity)context.Activity; this._currentState = StateMachineHelpers.GetCurrentState(context); StateActivity rootState = StateMachineHelpers.GetRootState(this._state); this._executionState = StateMachineExecutionState.Get(rootState); this._subscriptionManager = this._executionState.SubscriptionManager; }
internal void ReevaluateSubscriptions(ActivityExecutionContext context) { Dictionary <IComparable, StateMachineSubscription> subscriptionsShallowCopy = this.GetSubscriptionsShallowCopy(); List <IComparable> list = new List <IComparable>(); for (StateActivity activity = StateMachineHelpers.GetCurrentState(context); activity != null; activity = activity.Parent as StateActivity) { foreach (Activity activity2 in activity.EnabledActivities) { EventDrivenActivity eventDriven = activity2 as EventDrivenActivity; if (eventDriven != null) { IComparable queueName = StateMachineHelpers.GetEventActivity(eventDriven).QueueName; if (queueName != null) { StateMachineSubscription subscription; subscriptionsShallowCopy.TryGetValue(queueName, out subscription); EventActivitySubscription subscription2 = subscription as EventActivitySubscription; if (subscription2 != null) { if (subscription2.EventDrivenName.Equals(eventDriven.QualifiedName)) { list.Add(queueName); continue; } if (subscription2.StateName.Equals(activity.QualifiedName)) { throw new InvalidOperationException(SR.GetStateAlreadySubscribesToThisEvent(activity.QualifiedName, queueName)); } if (this.IsParentState(activity, subscription2.StateName)) { UnsubscribeAction action = new UnsubscribeAction(subscription2.StateName, subscription2.EventDrivenName); this.ExecutionState.EnqueueAction(action); subscriptionsShallowCopy.Remove(queueName); } } if (!list.Contains(queueName)) { SubscribeAction action2 = new SubscribeAction(activity.QualifiedName, eventDriven.QualifiedName); this.ExecutionState.EnqueueAction(action2); list.Add(queueName); } } } } } DisableQueuesAction action3 = new DisableQueuesAction(StateMachineHelpers.GetCurrentState(context).QualifiedName); this.ExecutionState.EnqueueAction(action3); }
internal virtual void Execute(ActivityExecutionContext context) { if (context == null) { throw new ArgumentNullException("context"); } Debug.Assert(context.Activity.QualifiedName.Equals(this.StateName)); _state = (StateActivity)context.Activity; _currentState = StateMachineHelpers.GetCurrentState(context); StateActivity rootState = StateMachineHelpers.GetRootState(_state); _executionState = StateMachineExecutionState.Get(rootState); _subscriptionManager = _executionState.SubscriptionManager; }
internal override void ProcessEvent(ActivityExecutionContext context) { SetStateEventArgs args = context.GetService <WorkflowQueuingService>().GetWorkflowQueue("SetStateQueue").Dequeue() as SetStateEventArgs; StateActivity currentState = StateMachineHelpers.GetCurrentState(context); if (currentState == null) { throw new InvalidOperationException(SR.GetStateMachineWorkflowMustHaveACurrentState()); } StateMachineExecutionState state = StateMachineExecutionState.Get(StateMachineHelpers.GetRootState((StateActivity)context.Activity)); SetStateAction action = new SetStateAction(currentState.QualifiedName, args.TargetStateName); state.EnqueueAction(action); state.ProcessActions(context); }
internal void ProcessQueue(ActivityExecutionContext context) { StateActivity currentState = StateMachineHelpers.GetCurrentState(context); if (this.EventQueue.Count == 0 || this.ExecutionState.HasEnqueuedActions || this.ExecutionState.SchedulerBusy || currentState == null) { return; } StateMachineSubscription subscription = Dequeue(); subscription.ProcessEvent(context); }
protected override void OnActivityChangeAdd(ActivityExecutionContext executionContext, Activity addedActivity) { if (executionContext == null) { throw new ArgumentNullException("executionContext"); } if (addedActivity == null) { throw new ArgumentNullException("addedActivity"); } if (!addedActivity.Enabled) { return; } if (executionContext.Activity.ExecutionStatus != ActivityExecutionStatus.Executing) { return; // activity is not executing } EventDrivenActivity eventDriven = addedActivity as EventDrivenActivity; if (eventDriven == null) { return; } // Activity we added is an EventDrivenActivity // First we disable the queue StateMachineSubscriptionManager.ChangeEventDrivenQueueState(executionContext, eventDriven, false); StateActivity rootState = StateMachineHelpers.GetRootState(executionContext.Activity as StateActivity); StateMachineExecutionState executionState = StateMachineExecutionState.Get(rootState); StateActivity currentState = StateMachineHelpers.GetCurrentState(executionContext); if (currentState == null) { return; // Dynamic update happened before we entered the initial state } StateMachineSubscriptionManager subscriptionManager = executionState.SubscriptionManager; subscriptionManager.ReevaluateSubscriptions(executionContext); executionState.LockQueue(); executionState.ProcessActions(executionContext); }
internal override void ProcessEvent(ActivityExecutionContext context) { WorkflowQueuingService workflowQueuingService = context.GetService <WorkflowQueuingService>(); WorkflowQueue workflowQueue = workflowQueuingService.GetWorkflowQueue(StateMachineWorkflowActivity.SetStateQueueName); SetStateEventArgs eventArgs = workflowQueue.Dequeue() as SetStateEventArgs; StateActivity currentState = StateMachineHelpers.GetCurrentState(context); if (currentState == null) { throw new InvalidOperationException(SR.GetStateMachineWorkflowMustHaveACurrentState()); } StateActivity rootState = StateMachineHelpers.GetRootState((StateActivity)context.Activity); StateMachineExecutionState executionState = StateMachineExecutionState.Get(rootState); SetStateAction action = new SetStateAction(currentState.QualifiedName, eventArgs.TargetStateName); Debug.Assert(!executionState.HasEnqueuedActions); executionState.EnqueueAction(action); executionState.ProcessActions(context); }
internal void ReevaluateSubscriptions(ActivityExecutionContext context) { Dictionary <IComparable, StateMachineSubscription> subscriptions = this.GetSubscriptionsShallowCopy(); List <IComparable> subscribed = new List <IComparable>(); StateActivity state = StateMachineHelpers.GetCurrentState(context); while (state != null) { foreach (Activity activity in state.EnabledActivities) { EventDrivenActivity eventDriven = activity as EventDrivenActivity; if (eventDriven == null) { continue; } IEventActivity eventActivity = StateMachineHelpers.GetEventActivity(eventDriven); IComparable queueName = eventActivity.QueueName; if (queueName == null) { continue; } StateMachineSubscription subscription; subscriptions.TryGetValue(queueName, out subscription); EventActivitySubscription eventActivitySubscription = subscription as EventActivitySubscription; if (eventActivitySubscription != null) { if (eventActivitySubscription.EventDrivenName.Equals(eventDriven.QualifiedName)) { // this EventDriven is already subscribed subscribed.Add(queueName); continue; } else { // Check if this state already subscribe to this event // if so, throws, since it is not valid to subscribe to the // same event twice if (eventActivitySubscription.StateName.Equals(state.QualifiedName)) { throw new InvalidOperationException(SR.GetStateAlreadySubscribesToThisEvent(state.QualifiedName, queueName)); } // some other EventDriven is subscribed, so we need to unsubscribe if // the event driven belongs to one of our parents if (IsParentState(state, eventActivitySubscription.StateName)) { UnsubscribeAction unsubscribe = new UnsubscribeAction(eventActivitySubscription.StateName, eventActivitySubscription.EventDrivenName); this.ExecutionState.EnqueueAction(unsubscribe); subscriptions.Remove(queueName); } } } // Tests if a child state already subscribes to this event // is so, skip, since the child takes precedence if (subscribed.Contains(queueName)) { continue; } SubscribeAction subscribe = new SubscribeAction(state.QualifiedName, eventDriven.QualifiedName); this.ExecutionState.EnqueueAction(subscribe); subscribed.Add(queueName); } state = state.Parent as StateActivity; } StateActivity currentState = StateMachineHelpers.GetCurrentState(context); DisableQueuesAction disableQueues = new DisableQueuesAction(currentState.QualifiedName); this.ExecutionState.EnqueueAction(disableQueues); }