Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

An Object-oriented Approach to Finite State Automata

Rate me:
Please Sign up or sign in to vote.
4.85/5 (19 votes)
3 Feb 2009CPOL7 min read 61.8K   1.1K   55  
A brief introduction to FSA and a ready-to-use class library
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using FSA;

namespace FormsFSATest
{
	class MovingObjectFSA
	{
		public enum Output
		{
			TakeCursor = 0,
			NormalCursor = 1,
			MoveCursor = 2
		}
		public enum States
		{
			Normal = 0,
			OverObject = 1,
			Moving = 2
		}
		public enum Input
		{
			MouseLeave = 0,
			MouseEnter = 1, 
			MouseUp = 2, 
			MouseDown = 3, 
			MouseMove = 4, 
		}

		private const int TABLE_SIZE = 8;
		public static FiniteStateAutomaton GetFSA()
		{
			MooreAutomaton moore = new MooreAutomaton(new MooreState[] {
				// Normal
				new MooreState {
					Table = new TableGenerator {
						EmptyTransition = (int) States.Normal,
						HashTransitions = new Hashtable {
							{ Input.MouseEnter, States.OverObject }
						},
						Size = TABLE_SIZE
					}.GenerateTable(),
					Output = (int)Output.NormalCursor

				},
				// over object
				new MooreState {
					Table = new TableGenerator {
						EmptyTransition = (int) States.OverObject,
						HashTransitions=new Hashtable {
							{ Input.MouseLeave, States.Normal },
							{ Input.MouseDown,  States.Moving }
						}
					}.GenerateTable(),
					Output = (int) Output.TakeCursor
				}
				,
				// moving
				new MooreState {
					Table = new TableGenerator {
						EmptyTransition = (int) States.Moving,
						HashTransitions = new  Hashtable {
							{ Input.MouseUp, States.OverObject }
						}
					}.GenerateTable(),
					Output = (int) Output.MoveCursor
				}
			}
				);
			return moore;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Poland Poland
My name is Jacek. Currently, I am a Java/kotlin developer. I like C# and Monthy Python's sense of humour.

Comments and Discussions