Beispiel #1
0
        /// <summary>
        /// Add a state transition to the FSM
        /// </summary>
        /// <param name="state">The state</param>
        /// <param name="eventId">The transition event</param>
        public void AddTransition(FSMStateId fromState, FSMEventId eventId, IState <TContext> state)
        {
            var transitionKey = new KeyValuePair <FSMStateId, FSMEventId>(fromState, eventId);

            Debug.Assert(!transitions.ContainsKey(transitionKey), string.Format("Overwriting gesture {0}", eventId.ToString()));

            state.Controller = this;
            this.transitions[transitionKey] = state;
        }
Beispiel #2
0
        /// <summary>
        /// Performs a state transition for a gesture
        /// </summary>
        /// <param name="eventId">The gesture for which to change state</param>
        public virtual void PerformTransition(FSMEventId eventId)
        {
            FSMStateId        oldStateId    = this.currentState != null ? this.currentState.Id : FSMStateId.Unknown;
            var               transitionKey = this.GetTransitionKey(eventId);
            IState <TContext> oldState      = this.currentState;

            if (!transitions.ContainsKey(transitionKey))
            {
                throw new ApplicationException(string.Format("No transition for event {0} found", eventId.ToString()));
            }

            currentState = transitions[transitionKey];

            if (currentState == null)
            {
                throw new ApplicationException(string.Format("Invalid state for event {0}", eventId.ToString()));
            }

            // we only enter/exit states if anything really changed
            if (currentState.Id != transitionKey.Key)
            {
                if (oldState != null)
                {
                    oldState.StateExited();
                }

                currentState.StateEntered(this.sharedContext);

                if (this.StateChanged != null)
                {
                    this.StateChanged(this, new StateChangedEventArgs()
                    {
                        OldState = oldStateId, NewState = currentState.Id
                    });
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Loads the gesture to eventid mapping
        /// </summary>
        /// <param name="config">The configuration node</param>
        private void LoadGestureTransitions(XmlDocument config)
        {
            config.ProcessXmlNodes("//configuration/gesturetransitions/gesturetransition",
                                   transition =>
            {
                FSMStateId fromStateId = (FSMStateId)int.Parse(transition.Attributes["fromState"].Value);
                GestureId gestureId    = (GestureId)int.Parse(transition.Attributes["onGesture"].Value);
                FSMEventId eventId     = (FSMEventId)int.Parse(transition.Attributes["raiseStateEvent"].Value);

                if (!this.Gestures.ContainsKey(gestureId))
                {
                    throw new InvalidDataException(string.Format("Gesture {0} is not specified in the gestures section of the config file",
                                                                 gestureId.ToString()));
                }

                if (!this.States.ContainsKey(fromStateId))
                {
                    throw new InvalidDataException(string.Format("State {0} is not specified in the states section of the config file",
                                                                 fromStateId.ToString()));
                }

                this.GestureTransitions[new KeyValuePair <FSMStateId, GestureId>(fromStateId, gestureId)] = eventId;
            });
        }
Beispiel #4
0
        /// <summary>
        /// Loads the states transitions
        /// </summary>
        /// <param name="config">The configuration node</param>
        private void LoadStateTransitions(XmlDocument config)
        {
            config.ProcessXmlNodes("//configuration/statetransitions/transition",
                                   transition =>
            {
                FSMStateId fromStateId = (FSMStateId)int.Parse(transition.Attributes["fromState"].Value);
                FSMStateId toStateId   = (FSMStateId)int.Parse(transition.Attributes["moveToState"].Value);
                FSMEventId eventId     = (FSMEventId)int.Parse(transition.Attributes["onEvent"].Value);

                if (!this.States.ContainsKey(fromStateId) && fromStateId != FSMStateId.Unknown)
                {
                    throw new InvalidDataException(string.Format("State {0} is not specified in the states section of the config file",
                                                                 fromStateId.ToString()));
                }

                if (!this.States.ContainsKey(toStateId))
                {
                    throw new InvalidDataException(string.Format("State {0} is not specified in the states section of the config file",
                                                                 toStateId.ToString()));
                }

                this.StateTransitions[new KeyValuePair <FSMStateId, FSMEventId>(fromStateId, eventId)] = toStateId;
            });
        }
Beispiel #5
0
 /// <summary>
 /// Returns a key that can be used for identifying transitions in the FSM
 /// </summary>
 /// <param name="eventId">The event id</param>
 /// <returns>A transition key</returns>
 private KeyValuePair <FSMStateId, FSMEventId> GetTransitionKey(FSMEventId eventId)
 {
     return(new KeyValuePair <FSMStateId, FSMEventId>(this.currentState == null ? FSMStateId.Unknown : this.currentState.Id, eventId));
 }
 public GestureRecognizedEventArgs(IGesture gesture, FSMEventId eventId)
 {
     this.Gesture = gesture;
     this.Event   = eventId;
 }