public virtual bool SwitchState(int stateKey, IStateContext context = null)
        {
            int oldStateKey = m_cCurState == null ? -1 : m_cCurState.key;

            if (null != OnBeforeSwitchState)
            {
                OnBeforeSwitchState(oldStateKey, stateKey);
            }
            if (m_cCurState != null)
            {
                m_cCurState._OnExit();
            }
            m_cCurState = GetState(stateKey);
            bool succ = false;

            if (m_cCurState != null)
            {
                m_cCurState._OnEnter(context);
                succ = true;
            }
            if (null != OnAfterSwitchState)
            {
                OnAfterSwitchState(oldStateKey, stateKey);
            }
            return(succ);
        }
Example #2
0
 private void ExecuteExitActions(IStateContext <TState, TEvent> stateContext)
 {
     foreach (var actionHolder in this.ExitActions)
     {
         this.ExecuteExitAction(actionHolder, stateContext);
     }
 }
Example #3
0
        public void Handle(IStateContext <StateTypeA> context)
        {
            Console.WriteLine(StateType.ToString() + " is handling context.");

            // change context state
            context.SetState(new CloseState());
        }
Example #4
0
 /// <summary>
 /// Exits all sub-states up the hierarchy up to the <paramref name="origin"/> state.
 /// </summary>
 /// <param name="origin">The origin.</param>
 /// <param name="stateContext">The event context.</param>
 private void UnwindSubStates(IState <TState, TEvent> origin, IStateContext <TState, TEvent> stateContext)
 {
     for (IState <TState, TEvent> o = origin; o != this.Source; o = o.SuperState)
     {
         o.Exit(stateContext);
     }
 }
Example #5
0
        public override void ProcessState(IStateContext context)
        {
            IStateContext newcontext = new StateContext(context.GetParseString(), context);

            GetParent().ChangeContext(newcontext);
            ChangeState(new InitState(GetParent()));
        }
 protected BotStateMachine(IStateContext <TState> stateContext,
                           IStateContextRepository <TState> stateRepository,
                           Action <StateMachine <TState, string> > configureDelegate)
     : this(stateContext, stateRepository)
 {
     Configure(configureDelegate);
 }
Example #7
0
        public void ShowMenu(IStateContext context)
        {
            Console.Clear();
            Console.WriteLine("MainMenu\n");
            Console.WriteLine("1. Reserve book/s");
            Console.WriteLine("2. Return book/s");
            Console.WriteLine("3. Exit\n");
            Console.Write("Choice: ");

            int choice;

            int.TryParse(Console.ReadLine(), out choice);

            switch (choice)
            {
            case 1:
                context.SetState(new ReserveBookState());
                break;

            case 2:
                context.SetState(new ReturnBook());
                break;

            case 3:
                Environment.Exit(0);
                break;

            default:
                context.SetState(new MainMenu());
                break;
            }
        }
Example #8
0
        /// <summary>
        /// Enters this state is deep mode:
        /// The entry action is executed and the initial state is entered in deep mode if there is one.
        /// </summary>
        /// <param name="stateContext">The event context.</param>
        /// <returns>The active state.</returns>
        public IState <TState, TEvent> EnterDeep(IStateContext <TState, TEvent> stateContext)
        {
            this.Entry(stateContext);

            return(this.LastActiveState == null ?
                   this :
                   this.LastActiveState.EnterDeep(stateContext));
        }
Example #9
0
        /// <summary>
        /// Enters this state is shallow mode:
        /// The entry action is executed and the initial state is entered in shallow mode if there is one.
        /// </summary>
        /// <param name="stateContext">The event context.</param>
        /// <returns>The entered state.</returns>
        public IState <TState, TEvent> EnterShallow(IStateContext <TState, TEvent> stateContext)
        {
            this.Entry(stateContext);

            return(this.initialState == null ?
                   this :
                   this.initialState.EnterShallow(stateContext));
        }
Example #10
0
        /// <summary>
        /// Enters this state.
        /// </summary>
        /// <param name="stateContext">The event context.</param>
        public void Entry(IStateContext <TState, TEvent> stateContext)
        {
            Ensure.ArgumentNotNull(stateContext, "stateContext");

            stateContext.AddRecord(this.Id, RecordType.Enter);

            this.ExecuteEntryActions(stateContext);
        }
Example #11
0
 /// <summary>
 /// Enters this instance with history type = none.
 /// </summary>
 /// <param name="stateContext">The state context.</param>
 /// <returns>The entered state.</returns>
 private IState <TState, TEvent> EnterHistoryNone(IStateContext <TState, TEvent> stateContext)
 {
     return(this.initialState != null
                ?
            this.initialState.EnterShallow(stateContext)
                :
            this);
 }
Example #12
0
 /// <summary>
 /// Enters this instance with history type = shallow.
 /// </summary>
 /// <param name="stateContext">The state context.</param>
 /// <returns>The entered state.</returns>
 private IState <TState, TEvent> EnterHistoryShallow(IStateContext <TState, TEvent> stateContext)
 {
     return(this.LastActiveState != null
                ?
            this.LastActiveState.EnterShallow(stateContext)
                :
            this);
 }
Example #13
0
 static void Main(string[] args)
 {
     context = new StateContext();
     while (true)
     {
         context.ShowMenu();
     }
 }
Example #14
0
        /// <summary>
        /// Exits this state, executes the exit action and sets the <see cref="LastActiveState"/> on the super-state.
        /// </summary>
        /// <param name="stateContext">The event context.</param>
        public void Exit(IStateContext <TState, TEvent> stateContext)
        {
            Ensure.ArgumentNotNull(stateContext, "stateContext");

            stateContext.AddRecord(this.Id, RecordType.Exit);

            this.ExecuteExitActions(stateContext);
            this.SetThisStateAsLastStateOfSuperState();
        }
Example #15
0
        public void Handle(IStateContext <StateTypeA> context)
        {
            var inputA = context.Input as InputA;

            Console.WriteLine("Input Param:" + inputA.Name);
            Console.WriteLine(StateType.ToString() + " is handling context.");

            // change context state
            context.SetState(new QueryState());
        }
Example #16
0
 private void ExecuteExitAction(IActionHolder actionHolder, IStateContext <TState, TEvent> stateContext)
 {
     try
     {
         actionHolder.Execute();
     }
     catch (Exception exception)
     {
         this.HandleExitActionException(stateContext, exception);
     }
 }
Example #17
0
 public void _OnDispose()
 {
     OnBeforeEnter = null;
     OnAfterEnter  = null;
     OnBeforeExit  = null;
     OnAfterExit   = null;
     OnDispose();
     m_nKey        = -1;
     m_cContext    = null;
     mStateEnabled = false;
 }
Example #18
0
 public GeneratedState(
     IState current,
     StateNode stateNode,
     IStateContext stateContext,
     ISessionStateAttributesService sessionStateAttributesService)
 {
     _current      = current;
     _stateNode    = stateNode;
     _stateContext = stateContext;
     _sessionStateAttributesService = sessionStateAttributesService;
 }
Example #19
0
	// Use this for initialization
	protected void Start () {

		ComponentTransform = transform;

		StateContext = new StateContext<Soldier> ();

		//add Target
		GameObject objPlayer = GameObject.FindGameObjectWithTag("Player");
		var targetObj = (AISystem.GameObjectParameter) runtimeController.controller.GetParameter("Target");
		targetObj.Value = objPlayer;
		TargetObj = objPlayer;
	}
Example #20
0
        /// <summary>
        /// Enters the initial state that was previously set with <see cref="Initialize(TState)"/>.
        /// </summary>
        public void EnterInitialState()
        {
            this.CheckThatStateMachineIsInitialized();

            this.extensions.ForEach(extension => extension.EnteringInitialState(this, this.initialStateId.Value));

            IStateContext <TState, TEvent> stateContext = this.factory.CreateStateContext(null, this);

            this.EnterInitialState(this.states[this.initialStateId.Value], stateContext);

            this.extensions.ForEach(extension => extension.EnteredInitialState(this, this.initialStateId.Value, stateContext));
        }
Example #21
0
        public INode Parse(string a)
        {
            IParserState state;

            _context = new StateContext(new ParseString(a.ToUpper()), null);
            ChangeState(new InitState(this));
            while (_states.Count > 0)
            {
                state = _states.Pop();
                state.ProcessState(_context);
            }
            return(_context.GetRoot());
        }
Example #22
0
        public override void ProcessState(IStateContext context)
        {
            IParseString ps = context.GetParseString();

            if (!ps.HasChar())
            {
                if (context.GetCurrentConj() != null)
                {
                    ChangeState(new AddConjunctionState(GetParent()));
                    return;
                }
                else if (context.GetRoot() == null)
                {
                    GetParent().SetError("Unexpected } of line at position" + ps.GetPosition().ToString());
                    ChangeState(new ErrorState(GetParent()));
                }
                return;
            }
            switch (ps.GetChar())
            {
            case 'X':
                ChangeState(new StartTermState(GetParent()));
                break;

            case '+':
                if (context.GetCurrentConj() != null)
                {
                    ChangeState(new AddConjunctionState(GetParent()));
                    return;
                }
                else
                {
                    GetParent().SetError("Unexpected char position " + ps.GetPosition().ToString());
                    ChangeState(new ErrorState(GetParent()));
                }
                break;

            case '(':
                ChangeState(new StartExprState(GetParent()));
                break;

            case ')':
                ChangeState(new EndExprState(GetParent()));
                break;

            default:
                GetParent().SetError("Unexpected char at position " + (ps.GetPosition() - 1).ToString());
                ChangeState(new ErrorState(GetParent()));
                break;
            }
        }
Example #23
0
 public void _OnEnter(IStateContext context = null)
 {
     if (null != OnBeforeEnter)
     {
         OnBeforeEnter(this);
     }
     m_cContext = context;
     OnEnter();
     mStateEnabled = true;
     if (null != OnAfterEnter)
     {
         OnAfterEnter(this);
     }
 }
Example #24
0
 public void _OnExit()
 {
     if (null != OnBeforeExit)
     {
         OnBeforeExit(this);
     }
     OnExit();
     m_cContext    = null;
     mStateEnabled = false;
     if (null != OnAfterExit)
     {
         OnAfterExit(this);
     }
 }
Example #25
0
        private void HandleExitActionException(IStateContext <TState, TEvent> stateContext, Exception exception)
        {
            this.extensionHost.ForEach(
                extension =>
                extension.HandlingExitActionException(
                    this.stateMachineInformation, this, stateContext, ref exception));

            HandleException(exception, stateContext);

            this.extensionHost.ForEach(
                extension =>
                extension.HandledExitActionException(
                    this.stateMachineInformation, this, stateContext, exception));
        }
Example #26
0
        public override void ProcessState(IStateContext context)
        {
            IParseString ps = context.GetParseString();

            if (ps.HasChar())
            {
                if (Char.IsDigit(ps.PeekChar()))
                {
                    ps.CollectTerm();
                    ChangeState(new CollectTermState(GetParent()));
                    return;
                }
            }
            ChangeState(new AddTermState(GetParent()));
        }
        public Task Set(IStateContext <TestState> stateContext, CancellationToken cancellationToken = default)
        {
            var context = new StateContextBase <TestState>(stateContext.TelegramId, stateContext.CurrentState);

            if (_items.ContainsKey(stateContext.TelegramId))
            {
                _items[stateContext.TelegramId] = context;
            }
            else
            {
                _items.Add(stateContext.TelegramId, stateContext);
            }

            return(Task.CompletedTask);
        }
        protected BotStateMachine(IStateContext <TState> stateContext,
                                  IStateContextRepository <TState> stateRepository)
        {
            if (stateContext == null)
            {
                throw new ArgumentNullException(nameof(stateContext));
            }

            StateRepository = stateRepository ?? throw new ArgumentNullException(nameof(stateRepository));

            State      = stateContext.CurrentState;
            TelegramId = stateContext.TelegramId;

            StateMachine = new StateMachine <TState, string>(
                () => State,
                s => State = s, FiringMode.Immediate);
        }
        public void ShowMenu(IStateContext context)
        {
            Console.Clear();
            Console.WriteLine("Booking Book Menu");

            IList <IBook> availableBooks = context.Library.GetAvailabeBooks();

            foreach (IBook book in availableBooks)
            {
                Console.WriteLine($"ISBN: {book.ISBN}\tTitel: {book.Title}");
            }
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("(0 = Finish booking)");
            Console.WriteLine(" ");

            int res = 0;

            do
            {
                res = Convert.ToInt32(Console.ReadLine());
                if (res != 0)
                {
                    IBook book = context.Library.GetBookFromISBN(res);
                    books.Add(book);
                    Console.WriteLine("The book with the ISBN; " + book.ISBN + " has been added to your bookings");
                }
            } while (res != 0);

            Console.WriteLine(" ");
            Console.WriteLine("Enter the membership number");
            int memberNr;

            int.TryParse(Console.ReadLine(), out memberNr);
            int librarienNr = context.logIn.Librarian.LibrNr;

            IBooking booking = context.Library.ReservBook(books, librarienNr, memberNr);

            Console.WriteLine(" ");
            Console.WriteLine("BookingNr: " + booking.BookingNr);
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("Press enter to get back to the main menu");
            Console.WriteLine();

            context.SetState(new MainMenu());
        }
Example #30
0
        public void ShowMenu(IStateContext context)
        {
            while (context.logIn == null)
            {
                Console.Clear();
                Console.WriteLine("Library Log-In/n");

                int libNr;
                Console.Write("LibrarienNr: ");
                int.TryParse(Console.ReadLine(), out libNr);

                Console.WriteLine("Password: ");
                string password = Console.ReadLine();

                context.logIn = context.Library.LogingIn(libNr, password);
            }
            context.SetState(new MainMenu());
        }
Example #31
0
        public override void ProcessState(IStateContext context)
        {
            IParseString ps = context.GetParseString();

            if (ps.HasChar())
            {
                switch (ps.GetChar())
                {
                case 'X':
                    ChangeState(new StartTermState(GetParent()));
                    break;

                case '+':
                    if (!ps.HasChar() || ps.PeekChar() != 'X' && ps.PeekChar() != '(')
                    {
                        GetParent().SetError("second term isn't found at position " + ps.GetPosition().ToString());
                        ChangeState(new ErrorState(GetParent()));
                    }
                    else
                    {
                        ChangeState(new AddConjunctionState(GetParent()));
                    }
                    break;

                case '(':
                    ChangeState(new StartExprState(GetParent()));
                    break;

                case ')':
                    ChangeState(new EndExprState(GetParent()));
                    break;

                default:

                    GetParent().SetError("Unexpected char at position " + (ps.GetPosition() - 1).ToString());
                    ChangeState(new ErrorState(GetParent()));
                    break;
                }
            }
            else
            {
                ChangeState(new AddConjunctionState(GetParent()));
            }
        }
Example #32
0
	// Use this for initialization
	void Start () {
		StateContext = new StateContext<EnemyCar> ();
	}