Example #1
0
        static void Main(string[] args)
        {
            // initialize a stopwatch for diagnostic use
            Stopwatch stopwatch = new Stopwatch();

            // create two state machines based on the meta model
            MachineExecutor <GenericRuntimeState> machine1 = new MachineExecutor <GenericRuntimeState>(new PhaseChangerMachine1().GetMetaModel());
            MachineExecutor <GenericRuntimeState> machine2 = new MachineExecutor <GenericRuntimeState>(new PhaseChangerMachine2().GetMetaModel());
            MachineExecutor <GenericRuntimeState> machine3 = new MachineExecutor <GenericRuntimeState>(new PhaseChangerMachine3().GetMetaModel());

            // initialize the machines
            machine1.Initialize();
            machine2.Initialize();
            machine3.Initialize();

            // Machine 1
            stopwatch = Stopwatch.StartNew();
            Console.WriteLine("\n" + "Test of Machine1 started with: " + NEVENTS + " random events");

            // process the events in the generatedEvents list
            foreach (Event e in generatedEvents)
            {
                machine1.ProcessEvent(e);
            }
            stopwatch.Stop();
            TimeSpan ts1 = stopwatch.Elapsed; // the elapsed time in the experiment

            Console.WriteLine("Run time of Machine1: " + ts1);

            // Machine 2
            Console.WriteLine("\n" + "Test of Machine2 started with: " + NEVENTS + " random events");
            stopwatch = Stopwatch.StartNew();
            // process the events in the generatedEvents list
            foreach (Event e in generatedEvents)
            {
                machine2.ProcessEvent(e);
            }
            stopwatch.Stop();
            TimeSpan ts2 = stopwatch.Elapsed; // the elapsed time in the experiment

            Console.WriteLine("Run time of Machine2: " + ts2);

            // Machine 3
            Console.WriteLine("\n" + "Test of Machine3 started with: " + NEVENTS + " random events");
            stopwatch = Stopwatch.StartNew();
            // process the events in the generatedEvents list
            foreach (Event e in generatedEvents)
            {
                machine3.ProcessEvent(e);
            }
            stopwatch.Stop();
            TimeSpan ts3 = stopwatch.Elapsed; // the elapsed time in the experiment

            Console.WriteLine("Run time of Machine3: " + ts3);
            Console.ReadKey();
        }
Example #2
0
        /// <summary>
        /// Process event by testing isApplicable in sequence, and then performing the action which
        /// may have an effect and returns the name of a new state, if a transition is to be made.
        /// </summary>
        /// <param name="machine">the machine, which state is set</param>
        /// <param name="runtime">the runtime</param>
        /// <param name="e">the event to process</param>
        public void ProcessEvent(MachineExecutor <T> machine, T runtime, Event e)
        {
            List <Transition <T> > matches = transitions.GetValueOrDefault(e.Code());

            if (matches == null)
            {
                return;
            }
            foreach (Transition <T> transition in matches)
            {
                if (transition.IsApplicable(runtime))
                {
                    string possibleTransition = transition.Action(runtime);
                    if (possibleTransition != null)
                    {
                        machine.SetState(possibleTransition);
                    }
                    return;
                }
            }
        }