Пример #1
0
        public void QHsm_InitWithMessageMap_Ok()
        {
            var person = new PersonMachine();

            person.TraceEvent = Program.Trace;
            person.Quiescent += (obj, arg) => { Console.WriteLine("STATEMACHINE IS OFFLINE"); };
            person.Start();

            var messages = new object[]
            {
                new PokeMessage(),
                new TiredMessage(),
            };

            foreach (var msg in messages)
            {
                person.Dispatch(msg);
            }

            //It should also be possible to provide the user signal directly...
            person.Dispatch(PersonSignals.Punch);
        }
Пример #2
0
        public static void Main(string[] args)
        {
            var person = new PersonMachine();

            person.TraceEvent = Trace;
            person.Stopped   += (obj, arg) => { Console.WriteLine("STATEMACHINE IS OFFLINE"); };
            person.Start();

            char input = 'a';

            do
            {
                Console.WriteLine("Input an event: (P)oke, (T)ired, (N)oise, P(u)nch or E(x)it\n");
                var keyInfo = Console.ReadKey();
                Console.WriteLine();
                input = Char.ToLowerInvariant(keyInfo.KeyChar);
                object evt = null;
                switch (input)
                {
                case 'p':
                    evt = new PokeMessage();
                    break;

                case 't':
                    evt = new TiredMessage();
                    break;

                case 'n':
                    evt = new NoiseMessage();
                    break;

                case 'u':
                    evt = new PunchMessage();
                    break;

                case 'x':
                    return;

                default:
                    Console.WriteLine("Command '{0}' is unrecognized input", input);
                    break;
                }
                person.Dispatch(evt);
            } while (true);
        }
Пример #3
0
        public void QHsm_InitWithMessageMap_Ok()
        {
            var person = new PersonMachine();

            person.TraceEvent = Program.Trace;
            person.Stopped   += (obj, arg) => { Console.WriteLine("STATEMACHINE IS OFFLINE"); };
            person.Start();

            var messages = new object[]
            {
                new PokeMessage(),
                new TiredMessage(),
            };

            foreach (var msg in messages)
            {
                person.Dispatch(msg);
            }
        }
Пример #4
0
        public async Task QHsm_InitWithCompletionNotice_Ok()
        {
            var person = new PersonMachine();

            person.TraceEvent = Program.Trace;
            person.Quiescent += (obj, arg) => { Console.WriteLine("STATEMACHINE IS OFFLINE"); };
            person.Start();
            Task completion = null;

            var messages = new object[]
            {
                new TiredMessage(),
                new TiredMessage(),
            };

            foreach (var msg in messages)
            {
                completion = person.Dispatch(msg);
            }

            bool isSuccess = await Task.WhenAny(completion, Task.Delay(80)) == completion;

            Assert.True(isSuccess && completion.Status == TaskStatus.RanToCompletion);
        }
Пример #5
0
        public static void Main(string[] args)
        {
            var person = new PersonMachine();

            person.TraceEvent = Trace;
            person.Quiescent += (obj, arg) => { Console.WriteLine("STATEMACHINE IS OFFLINE"); };
            person.Failed    += (obj, arg) => { Console.WriteLine("STATEMACHINE EXCEPTION"); };
            person.Start();

            PersonState state = null;
            char        input = 'a';

            do
            {
                Console.WriteLine("Input an event: (P)oke, (T)ired, (N)oise, P(u)nch, (R)estore or E(x)it\n");
                var keyInfo = Console.ReadKey();
                Console.WriteLine();
                input = Char.ToLowerInvariant(keyInfo.KeyChar);
                object evt = null;
                switch (input)
                {
                case 'p':
                    evt = new PokeMessage();
                    break;

                case 't':
                    evt = new TiredMessage();
                    break;

                case 'n':
                    evt = new NoiseMessage();
                    break;

                case 'u':
                    evt = new PunchMessage();
                    break;

                case 'x':
                    return;

                case 'r':
                    //Simulate stopping, persisting, and restoring the state-machine.
                    person.Stop();
                    state             = person.CurrentState;
                    person            = new PersonMachine();
                    person.TraceEvent = Trace;
                    //Normally only the state-machine is permitted to change these values,
                    //but for testing purposes on the "moody person" machine, this is ok.
                    state.DB = 100;
                    person.Start(state);
                    break;

                default:
                    Console.WriteLine("Command '{0}' is unrecognized input", input);
                    break;
                }
                if (evt != null)
                {
                    person.Dispatch(evt);
                }
            } while (true);
        }