Click here to Skip to main content
12,407,037 members (61,702 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

17.9K views
398 downloads
22 bookmarked
Posted

Moore machine with C#

, 23 Apr 2009 Public Domain
Rate this:
Please Sign up or sign in to vote.
A simple Moore machine implemented in C#.

Introduction

Some time ago, I had to solve a synchronization problem using serial ports for communication with mobile phones. I solved this with an implementation of a Moore machine.

Background

What is a Moore machine and what is a finite state machine? A finite state machine can be visualized by a graph. Each node is a state . Only one state can be active at a time, whereas time is actually irrelevant. If the state machine's active state is , it receives the input character (words, symbols, messages, a.s.o.), there is a transition function defined, then the machine changes its active state to .

The Moore machine and other finite state machines (e.g., Mealy machines) are well documented on Wikipedia:

Using the code

Here is a very simple usage example:

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...");
        }
    }
}

And this is the (verbose) output:

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

Points of interest

The funny thing about this code is that I wrote it in about an hour without testing it. It worked at first go.

History

  • Version 1.0 - 23/04/2009.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Alexander Müller
Software Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 23 Apr 2009
Article Copyright 2009 by Alexander Müller
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid