Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

State Machine Complier And Asp.net

, 20 Oct 2008
This article demonstrate the implementation of State Machine Compiler in .NET. This is small proof of concept for developing state machine workflow activity.
SMC_Source.zip
SMC_Source
Smc.jar
SMC_Demo
DataAccess
DataAccess
Properties
Domain
Domain
bin
Debug
Domain.dll
Properties
ServicesProvider
ServicesProvider
Properties
SMCWebsite
App_Data
Bin
DataAccess.dll
Domain.dll
ServicesProvider.dll
StateMachineWorkflowActivity.dll
statemap.dll
SMC_Demo
SMC_Demo.suo
StateMachineWorkflowActivity
bin
Debug
DataAccess.dll
DataAccess.pdb
Domain.dll
Domain.pdb
ServicesProvider.dll
ServicesProvider.pdb
StateMachineWorkflowActivity.dll
StateMachineWorkflowActivity.pdb
statemap.dll
Interface
obj
Debug
Refactor
ResolveAssemblyReference.cache
StateMachineWorkflowActivity.dll
StateMachineWorkflowActivity.pdb
TempPE
Properties
StateDefination
CarWF.sm
StateMachineWorkflowActivity.csproj.user
SQL Database Script
SMC_Source_1.zip
Smc.jar
Domain.dll
DataAccess.dll
Domain.dll
ServicesProvider.dll
StateMachineWorkflowActivity.dll
statemap.dll
SMC_Demo.suo
DataAccess.dll
DataAccess.pdb
Domain.dll
Domain.pdb
ServicesProvider.dll
ServicesProvider.pdb
StateMachineWorkflowActivity.dll
StateMachineWorkflowActivity.pdb
statemap.dll
ResolveAssemblyReference.cache
StateMachineWorkflowActivity.dll
StateMachineWorkflowActivity.pdb
CarWF.sm
StateMachineWorkflowActivity.csproj.user
using System;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using System.Collections;
using ServicesProvider;
using Domain;

using StateMachineWorkflowActivity;
namespace StateMachineWorkflowActivity
{
    [Serializable]
    public sealed class CarWFContext :statemap.FSMContext,ISerializable
    {
    //---------------------------------------------------------------
    // Properties.
    //

        public event EventHandler StateChanged;

        public CarWFState State
        {
            get
            {
                if (_state == null)
                {
                    throw(
                        new statemap.StateUndefinedException());
                }

                return ((CarWFState) _state);
            }
            set
            {
                _state = value;
                if (StateChanged != null)
                {
                    StateChanged(this, EventArgs.Empty);
                }
            }
        }

        public CarWF Owner
        {
            get
            {
                return (_owner);
            }
            set
            {
                _owner = value;
            }
        }

    //---------------------------------------------------------------
    // Member methods.
    //

        public CarWFContext(CarWF owner) :
            base ()
        {
        _owner = owner;
            _state = MainMap.Start;
            MainMap.Start.Entry(this);
        }

        public CarWFContext(SerializationInfo info, StreamingContext context) :
            base ()
        {
            int stackSize;
            int stateId;

            stackSize = info.GetInt32("stackSize");
            if (stackSize > 0)
            {
                int index;
                String name;

                for (index = (stackSize - 1); index >= 0; --index)
                {
                    name = "stackIndex" + index;
                    stateId = info.GetInt32(name);
                    PushState(_States[stateId]);
                }
            }

            stateId = info.GetInt32("state");
            PushState(_States[stateId]);
        }

        public void SubmitRun()
        {
            _transition = "SubmitRun";
            State.SubmitRun(this);
            _transition = "";
            return;
        }

        public void SubmitSlowDown()
        {
            _transition = "SubmitSlowDown";
            State.SubmitSlowDown(this);
            _transition = "";
            return;
        }

        public void SubmitStop()
        {
            _transition = "SubmitStop";
            State.SubmitStop(this);
            _transition = "";
            return;
        }

        public CarWFState valueOf(int stateId)
        {
            return(_States[stateId]);
        }

        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            int stackSize = 0;

            if (_stateStack != null)
            {
                stackSize = _stateStack.Count;
            }

            info.AddValue("stackSize", stackSize);

            if (stackSize > 0)
            {
                int index = 0;
                String name;

                foreach (CarWFState state in _stateStack)
                {
                    name = "stackIndex" + index;
                    info.AddValue(name, state.Id);
                    ++index;
                }
            }

            info.AddValue("state", _state.Id);

            return;
        }

    //---------------------------------------------------------------
    // Member data.
    //

        [NonSerialized]
        private CarWF _owner;

        // Map state IDs to state objects.
        // Used to deserialize an FSM.
        [NonSerialized]
        private static CarWFState[] _States =
        {
            MainMap.Start,
            MainMap.Run,
            MainMap.SlowDown,
            MainMap.Stop
        };

    //---------------------------------------------------------------
    // Inner classes.
    //

        public abstract class CarWFState : statemap.State
        {
        //-----------------------------------------------------------
        // Properties.
        //

            public abstract IDictionary Transitions
            {
                get;
            }

        //-----------------------------------------------------------
        // Member methods.
        //

            internal CarWFState(string name, int id) :base (name, id)
            {}

            protected internal virtual void Entry(CarWFContext context)
            {}

            protected internal virtual void Exit(CarWFContext context)
            {}

            protected internal virtual void SubmitRun(CarWFContext context)
            {
                Default(context);
            }

            protected internal virtual void SubmitSlowDown(CarWFContext context)
            {
                Default(context);
            }

            protected internal virtual void SubmitStop(CarWFContext context)
            {
                Default(context);
            }

            protected internal virtual void Default(CarWFContext context)
            {
                throw (
                    new statemap.TransitionUndefinedException(
                        "State: " +
                        context.State.Name +
                        ", Transition: " +
                        context.GetTransition()));
            }
        }

        internal abstract class MainMap
        {
        //-----------------------------------------------------------
        // Member methods.
        //

        //-----------------------------------------------------------
        // Member data.
        //

            //-------------------------------------------------------
            // Statics.
            //

            internal static readonly MainMap_Default.MainMap_Start Start = new MainMap_Default.MainMap_Start("MainMap.Start", 0);
     
            internal static readonly MainMap_Default.MainMap_Run Run =
                new MainMap_Default.MainMap_Run("MainMap.Run", 1);
         
            internal static readonly MainMap_Default.MainMap_SlowDown SlowDown =
                new MainMap_Default.MainMap_SlowDown("MainMap.SlowDown", 2);
        
            internal static readonly MainMap_Default.MainMap_Stop Stop =
                new MainMap_Default.MainMap_Stop("MainMap.Stop", 3);
         
            private static readonly MainMap_Default Default = new MainMap_Default("MainMap.Default", -1);

        }

        internal class MainMap_Default :CarWFState
        {
        //-----------------------------------------------------------
        // Properties.
        //

            public override IDictionary Transitions
            {
                get
                {
                    return (_transitions);
                }
            }

        //-----------------------------------------------------------
        // Member methods.
        //

            internal MainMap_Default(string name, int id) :base (name, id)
            {}

        //-----------------------------------------------------------
        // Inner classes.
        //

            internal class MainMap_Start : MainMap_Default
            {
            //-------------------------------------------------------
            // Properties.
            //

                public override IDictionary Transitions
                {
                    get
                    {
                        return (_transitions);
                    }
                }

            //-------------------------------------------------------
            // Member methods.
            //

                internal MainMap_Start(string name, int id) :  base (name, id)
                {}

                protected internal override void SubmitRun(CarWFContext context)
                {

                    CarWF ctxt = context.Owner;


                    context.State.Exit(context);
                    context.ClearState();

                    try
                    {
                        ctxt.UpdateState(ctxt.CarEntity, Utilities.CarState.Run, Utilities.CarState.Start);
                    }
                    finally
                    {
                        context.State = MainMap.Run;
                        context.State.Entry(context);
                    }

                    return;
                }

                protected internal override void SubmitStop(CarWFContext context)
                {

                    CarWF ctxt = context.Owner;


                    context.State.Exit(context);
                    context.ClearState();

                    try
                    {
                        ctxt.UpdateState(ctxt.CarEntity, Utilities.CarState.Stop, Utilities.CarState.Start);
                    }
                    finally
                    {
                        context.State = MainMap.Stop;
                        context.State.Entry(context);
                    }

                    return;
                }

            //-------------------------------------------------------
            // Member data.
            //

                //---------------------------------------------------
                // Statics.
                //
                new private static IDictionary _transitions;

                static MainMap_Start()
                {
                    _transitions = new Hashtable();
                _transitions.Add("SubmitRun", 1);
                _transitions.Add("SubmitSlowDown", 0);
                _transitions.Add("SubmitStop", 1);
                }
            }

            internal class MainMap_Run :
                MainMap_Default
            {
            //-------------------------------------------------------
            // Properties.
            //

                public override IDictionary Transitions
                {
                    get
                    {
                        return (_transitions);
                    }
                }

            //-------------------------------------------------------
            // Member methods.
            //

                internal MainMap_Run(string name, int id) :
                    base (name, id)
                {}

                protected internal override void SubmitSlowDown(CarWFContext context)
                {

                    CarWF ctxt = context.Owner;


                    context.State.Exit(context);
                    context.ClearState();

                    try
                    {
                        ctxt.UpdateState(ctxt.CarEntity, Utilities.CarState.SlowDown, Utilities.CarState.Run);
                    }
                    finally
                    {
                        context.State = MainMap.SlowDown;
                        context.State.Entry(context);
                    }

                    return;
                }

                protected internal override void SubmitStop(CarWFContext context)
                {

                    CarWF ctxt = context.Owner;


                    context.State.Exit(context);
                    context.ClearState();

                    try
                    {
                        ctxt.UpdateState(ctxt.CarEntity, Utilities.CarState.Stop, Utilities.CarState.Run);
                    }
                    finally
                    {
                        context.State = MainMap.Stop;
                        context.State.Entry(context);
                    }

                    return;
                }

            //-------------------------------------------------------
            // Member data.
            //

                //---------------------------------------------------
                // Statics.
                //
                new private static IDictionary _transitions;

                static MainMap_Run()
                {
                    _transitions = new Hashtable();
                _transitions.Add("SubmitRun", 0);
                _transitions.Add("SubmitSlowDown", 1);
                _transitions.Add("SubmitStop", 1);
                }
            }

            internal class MainMap_SlowDown :
                MainMap_Default
            {
            //-------------------------------------------------------
            // Properties.
            //

                public override IDictionary Transitions
                {
                    get
                    {
                        return (_transitions);
                    }
                }

            //-------------------------------------------------------
            // Member methods.
            //

                internal MainMap_SlowDown(string name, int id) :
                    base (name, id)
                {}

                protected internal override void SubmitStop(CarWFContext context)
                {

                    CarWF ctxt = context.Owner;


                    context.State.Exit(context);
                    context.ClearState();

                    try
                    {
                        ctxt.UpdateState(ctxt.CarEntity, Utilities.CarState.Stop, Utilities.CarState.SlowDown);
                    }
                    finally
                    {
                        context.State = MainMap.Stop;
                        context.State.Entry(context);
                    }

                    return;
                }

            //-------------------------------------------------------
            // Member data.
            //

                //---------------------------------------------------
                // Statics.
                //
                new private static IDictionary _transitions;

                static MainMap_SlowDown()
                {
                    _transitions = new Hashtable();
                _transitions.Add("SubmitRun", 0);
                _transitions.Add("SubmitSlowDown", 0);
                _transitions.Add("SubmitStop", 1);
                }
            }

            internal class MainMap_Stop :
                MainMap_Default
            {
            //-------------------------------------------------------
            // Properties.
            //

                public override IDictionary Transitions
                {
                    get
                    {
                        return (_transitions);
                    }
                }

            //-------------------------------------------------------
            // Member methods.
            //

                internal MainMap_Stop(string name, int id) :
                    base (name, id)
                {}

            //-------------------------------------------------------
            // Member data.
            //

                //---------------------------------------------------
                // Statics.
                //
                new private static IDictionary _transitions;

                static MainMap_Stop()
                {
                    _transitions = new Hashtable();
                _transitions.Add("SubmitRun", 0);
                _transitions.Add("SubmitSlowDown", 0);
                _transitions.Add("SubmitStop", 0);
                }
            }

        //-----------------------------------------------------------
        // Member data.
        //

            //-------------------------------------------------------
            // Statics.
            //
            private static IDictionary _transitions;

            static MainMap_Default()
            {
                _transitions = new Hashtable();
            _transitions.Add("SubmitRun", 0);
            _transitions.Add("SubmitSlowDown", 0);
            _transitions.Add("SubmitStop", 0);
            }
        }
    }

}

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)

About the Author

santosh poojari
Technical Lead
India India
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

| Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 21 Oct 2008
Article Copyright 2008 by santosh poojari
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid