Example #1
0
        /// <summary>
        /// Executes the fsm with the given command
        /// </summary>
        /// <returns>Returns true if the transition goes correctly otherwise false</returns>
        public bool MoveNext(T command)
        {
            CommandTransition <K, T> transition = new CommandTransition <K, T>(CurrentState, command);
            Func <K> state;

            if (Transitions.TryGetValue(transition, out state))
            {
                if (!CurrentState.Equals(state))
                {
                    LastState    = CurrentState;
                    CurrentState = state.Invoke();

                    if (!CurrentCommand.Equals(command))
                    {
                        CurrentCommand = command;

                        if (OnCommandChanged != null)
                        {
                            OnCommandChanged.Invoke(CurrentCommand);
                        }
                    }

                    if (OnStateChanged != null)
                    {
                        OnStateChanged.Invoke(CurrentState);
                    }

                    return(true);
                }
            }

            return(false);
        }
Example #2
0
        public void AddTransition(K CurrentEvent, T condition, Func <K> result)
        {
            CommandTransition <K, T> transition = new CommandTransition <K, T>(CurrentEvent, condition);

            if (!Transitions.ContainsKey(transition))
            {
                Transitions.Add(transition, result);
            }
            else
            {
                throw new ArgumentException("CommandFSM: Transition already added");
            }
        }
Example #3
0
        public override bool Equals(object obj)
        {
            CommandTransition <K, T> other = (CommandTransition <K, T>)obj;

            if (other == null)
            {
                return(false);
            }

            int cond_  = (Convert.ToInt32(Condition) & Convert.ToInt32(other.Condition));
            int state_ = (Convert.ToInt32(State) & Convert.ToInt32(other.State));

            return(cond_ != 0 & state_ != 0);
        }