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 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);
        }
예제 #3
0
        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);
        }
 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);
 }