A Moore Machine

I think someone found my blog today by Googling for a Moore machine example in C#, lol.  I will admit a Moore machine is the best kind of machine, no offense Turing and f%*k Mealy.  As for explanation of what’s a Moore machine your SOL, even I don’t have the time or energy to try present this one but here are few resources to get you going.

Resource #1 – Wikipedia

Despite what most people will say about Wikipedia, you can a wealth of information their.  Side note, support Wikipedia if you can, it’s kinda sad to think about but that the site will more than likely be taken down in my life time.  Haters gonna hate.

Wiki’s Description:

Moore machine is a finite-state machine, whose output values are determined solely by its current state.

Wow, that’s helpful.  Alright they have a tiny bit more under the formal definition but not much.  But what you do get are nice diagrams to help you out.

Resource #2 – Youtube

There are some good finite machine tutorials avaible on the intertubes if you look for them.  Here is one I found:

[youtube:http://www.youtube.com/watch?v=PQMtZkkFFv0%5D

 

Resource #3 – Google

This one should be self-explanatory.  Anything you can’t find use Google, just switch to another country’s Google page 😉

Found this C# example of a Moore machine hosted on codeproject.  I have no idea how good of an example it is, maybe later I’ll try tinkering with it.

 

Moore machine with C#

By: Alexander Müller

http://www.codeproject.com/Articles/35843/Moore-machine-with-C

*Note: Don’t let url scare you.  It’s C# not C.

using System;
using System.Threading;
 
namespace visusNET.FiniteStateMachine {
    public class Test {
        [STAThread]
        public static void Main() {
            string[] sigma = new string[] { "ABC", "XYZ", "123" };
 
            State state1 = new State(State1, "State 1");
            State state2 = new State(State2, "State 2");
            State state3 = new State(State3, "State 3");
            State[] S = { state1, state2, state3 };
 
            TransitionFunction delta = new TransitionFunction();
 
            delta.AddTransition(state1, sigma[0], state2); // state1 --ABC--> state2
            delta.AddTransition(state2, sigma[1], state3); // state2 --XYZ--> state3
            delta.AddTransition(state3, sigma[2], state1); // state3 --123--> state1
 
            MooreMachine machine = new MooreMachine(sigma, S, state1, delta);
            machine.Delay = 1000;
            machine.Interval = 5000;
 
            machine.Analyze();
 
            Thread machineThread = new Thread(new ThreadStart(machine.Run));
            machineThread.Start();
 
            while (true) {
                machine.SetInput("ABC");
                Thread.Sleep(1000);
                machine.SetInput("XYZ");
                Thread.Sleep(1000);
                machine.SetInput("123");
                Thread.Sleep(1000);
            }
        }
 
        public static void State1() {
            Console.WriteLine("State 1 is running...");
        }
 
        public static void State2() {
            Console.WriteLine("State 2 is running...");
        }
 
        public static void State3() {
            Console.WriteLine("State 3 is running...");
        }
    }
}

Standard Out:

Checking State 1 with ABC... Found
Checking State 1 with XYZ... Not found
Checking State 1 with 123... Not found
Checking State 2 with ABC... Not found
Checking State 2 with XYZ... Found
Checking State 2 with 123... Not found
Checking State 3 with ABC... Not found
Checking State 3 with XYZ... Not found
Checking State 3 with 123... Found
This Moore-Machine is a partially-specified deterministic finite state machine.
Following transitions are missing:
(State 1, "XYZ") -> undefined
(State 1, "123") -> undefined
(State 2, "ABC") -> undefined
(State 2, "123") -> undefined
(State 3, "ABC") -> undefined
(State 3, "XYZ") -> undefined
State 1 is running...
Got input: XYZ
State 1 is running...
Got input: ABC
State 2 is running...
Got input: 123
State 2 is running...
Got input: XYZ
State 3 is running...
Got input: ABC
State 3 is running...
Got input: 123
State 1 is running...
Got input: XYZ
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s