Click here to Skip to main content
12,403,983 members (62,712 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

171.3K views
968 downloads
132 bookmarked
Posted

A .NET State Machine Toolkit - Part III

, 26 Oct 2006 CPOL
Using code generation with the .NET state machine toolkit.
StateMachineToolkit
StateMachineBuilderDemo
App.ico
StateMachineBuilderDemo.csproj.user
StateMachineDemo
App.ico
StateMachineDemo.csproj.user
StateMachineMaker
App.ico
StateMachineMaker.csproj.user
StateMachineToolkit
bin
Debug
LSCollections.dll
CodeGeneration
StateMachineBuilder
StateMachineToolkit.csproj.user
StateMachineToolkitPartIII_src
CodeGeneration
namespace StateMachineDemo
{
    
    
    public abstract class TrafficLightBase : StateMachineToolkit.StateMachine
    {
        
        private StateMachineToolkit.State stateOn;
        
        private StateMachineToolkit.State stateOff;
        
        private StateMachineToolkit.State stateRed;
        
        private StateMachineToolkit.State stateYellow;
        
        private StateMachineToolkit.State stateGreen;
        
        private StateMachineToolkit.GuardHandler guardCounterEquals4;
        
        private StateMachineToolkit.GuardHandler guardCounterEquals2;
        
        private StateMachineToolkit.ActionHandler actionIncrementCounter;
        
        private StateMachineToolkit.ActionHandler actionResetCounter;
        
        public TrafficLightBase(StateMachineToolkit.EventQueue queue) : 
                base(queue)
        {
            this.InitializeStates();
            this.InitializeGuards();
            this.InitializeActions();
            this.InitializeTransitions();
            this.InitializeRelationships();
            this.InitializeHistoryTypes();
            this.InitializeInitialStates();
            this.Initialize(this.stateOff);
        }
        
        private void InitializeStates()
        {
            StateMachineToolkit.EntryHandler enOn = new StateMachineToolkit.EntryHandler(this.EntryOn);
            StateMachineToolkit.ExitHandler exOn = new StateMachineToolkit.ExitHandler(this.ExitOn);
            this.stateOn = new StateMachineToolkit.State(3, enOn, exOn);
            StateMachineToolkit.EntryHandler enOff = new StateMachineToolkit.EntryHandler(this.EntryOff);
            StateMachineToolkit.ExitHandler exOff = new StateMachineToolkit.ExitHandler(this.ExitOff);
            this.stateOff = new StateMachineToolkit.State(3, enOff, exOff);
            StateMachineToolkit.EntryHandler enRed = new StateMachineToolkit.EntryHandler(this.EntryRed);
            StateMachineToolkit.ExitHandler exRed = new StateMachineToolkit.ExitHandler(this.ExitRed);
            this.stateRed = new StateMachineToolkit.State(3, enRed, exRed);
            StateMachineToolkit.EntryHandler enYellow = new StateMachineToolkit.EntryHandler(this.EntryYellow);
            StateMachineToolkit.ExitHandler exYellow = new StateMachineToolkit.ExitHandler(this.ExitYellow);
            this.stateYellow = new StateMachineToolkit.State(3, enYellow, exYellow);
            StateMachineToolkit.EntryHandler enGreen = new StateMachineToolkit.EntryHandler(this.EntryGreen);
            StateMachineToolkit.ExitHandler exGreen = new StateMachineToolkit.ExitHandler(this.ExitGreen);
            this.stateGreen = new StateMachineToolkit.State(3, enGreen, exGreen);
        }
        
        private void InitializeGuards()
        {
            this.guardCounterEquals4 = new StateMachineToolkit.GuardHandler(this.CounterEquals4);
            this.guardCounterEquals2 = new StateMachineToolkit.GuardHandler(this.CounterEquals2);
        }
        
        private void InitializeActions()
        {
            this.actionIncrementCounter = new StateMachineToolkit.ActionHandler(this.IncrementCounter);
            this.actionResetCounter = new StateMachineToolkit.ActionHandler(this.ResetCounter);
        }
        
        private void InitializeTransitions()
        {
            StateMachineToolkit.Transition trans;
            trans = new StateMachineToolkit.Transition(null, null, this.stateOn);
            this.stateOff.Transitions.Add(((int)(EventType.TurnOn)), trans);
            trans = new StateMachineToolkit.Transition(null, null, this.stateOff);
            this.stateOn.Transitions.Add(((int)(EventType.TurnOff)), trans);
            trans = new StateMachineToolkit.Transition(this.guardCounterEquals4, this.actionResetCounter, this.stateGreen);
            this.stateRed.Transitions.Add(((int)(EventType.TimerElapsed)), trans);
            trans = new StateMachineToolkit.Transition(null, this.actionIncrementCounter, null);
            this.stateRed.Transitions.Add(((int)(EventType.TimerElapsed)), trans);
            trans = new StateMachineToolkit.Transition(this.guardCounterEquals2, this.actionResetCounter, this.stateRed);
            this.stateYellow.Transitions.Add(((int)(EventType.TimerElapsed)), trans);
            trans = new StateMachineToolkit.Transition(null, this.actionIncrementCounter, null);
            this.stateYellow.Transitions.Add(((int)(EventType.TimerElapsed)), trans);
            trans = new StateMachineToolkit.Transition(this.guardCounterEquals4, this.actionResetCounter, this.stateYellow);
            this.stateGreen.Transitions.Add(((int)(EventType.TimerElapsed)), trans);
            trans = new StateMachineToolkit.Transition(null, this.actionIncrementCounter, null);
            this.stateGreen.Transitions.Add(((int)(EventType.TimerElapsed)), trans);
        }
        
        private void InitializeRelationships()
        {
            this.stateOn.Substates.Add(this.stateRed);
            this.stateOn.Substates.Add(this.stateYellow);
            this.stateOn.Substates.Add(this.stateGreen);
        }
        
        private void InitializeHistoryTypes()
        {
            this.stateOn.HistoryType = StateMachineToolkit.HistoryType.Shallow;
            this.stateOff.HistoryType = StateMachineToolkit.HistoryType.None;
            this.stateRed.HistoryType = StateMachineToolkit.HistoryType.None;
            this.stateYellow.HistoryType = StateMachineToolkit.HistoryType.None;
            this.stateGreen.HistoryType = StateMachineToolkit.HistoryType.None;
        }
        
        private void InitializeInitialStates()
        {
            this.stateOn.InitialState = this.stateRed;
        }
        
        protected virtual void EntryOn()
        {
        }
        
        protected virtual void EntryOff()
        {
        }
        
        protected virtual void EntryRed()
        {
        }
        
        protected virtual void EntryYellow()
        {
        }
        
        protected virtual void EntryGreen()
        {
        }
        
        protected virtual void ExitOn()
        {
        }
        
        protected virtual void ExitOff()
        {
        }
        
        protected virtual void ExitRed()
        {
        }
        
        protected virtual void ExitYellow()
        {
        }
        
        protected virtual void ExitGreen()
        {
        }
        
        protected abstract bool CounterEquals4(object[] args);
        
        protected abstract bool CounterEquals2(object[] args);
        
        protected abstract void IncrementCounter(object[] args);
        
        protected abstract void ResetCounter(object[] args);
        
        public enum EventType
        {
            
            TurnOn,
            
            TurnOff,
            
            TimerElapsed,
        }
    }
}

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)

Share

About the Author

Leslie Sanford
United States United States
Aside from dabbling in BASIC on his old Atari 1040ST years ago, Leslie's programming experience didn't really begin until he discovered the Internet in the late 90s. There he found a treasure trove of information about two of his favorite interests: MIDI and sound synthesis.

After spending a good deal of time calculating formulas he found on the Internet for creating new sounds by hand, he decided that an easier way would be to program the computer to do the work for him. This led him to learn C. He discovered that beyond using programming as a tool for synthesizing sound, he loved programming in and of itself.

Eventually he taught himself C++ and C#, and along the way he immersed himself in the ideas of object oriented programming. Like many of us, he gotten bitten by the design patterns bug and a copy of GOF is never far from his hands.

Now his primary interest is in creating a complete MIDI toolkit using the C# language. He hopes to create something that will become an indispensable tool for those wanting to write MIDI applications for the .NET framework.

Besides programming, his other interests are photography and playing his Les Paul guitars.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 26 Oct 2006
Article Copyright 2005 by Leslie Sanford
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid