Click here to Skip to main content
15,867,141 members
Articles / Web Development / ASP.NET
Article

State Machine Complier And Asp.net

Rate me:
Please Sign up or sign in to vote.
4.56/5 (4 votes)
20 Oct 2008CPOL4 min read 55.3K   307   31   6
This article demonstrate the implementation of State Machine Compiler in .NET. This is small proof of concept for developing state machine workflow activity.

Introduction

The workflow activity has become a core business process in various domains. There are many technologies that provide the solution to design workflow activity and a mechanism to process the work request as per the user role or state of activities. One of them is Windows Workflow foundation. WWF provides a solution to such problem, but with very complex architecture to achieve this .This involves significant amount of production time. The workflow activity is not static it varies with change in business process. To deal with this problem we have state machine complier which provides out of the box solution to it. SMC is an open source code. This article demonstrate and help one to understand the implementation workflow using State Machine Compiler in .Net.

Reference: State Machine Compiler

  • Today Java Net- SMC
  • http://smc.sourceforge.net/
  • System Requirement

  • Install Java SDK
  • statemap.dll
  • SMC.jar
  • Syntax: State Machine definition

    Here is the syntax to start writing state machine workflow definition.Below diagram is self explanatory.

    State Diagram

    [State]
    {
    [Transition Method]  	[Transition State]
    {
    	[Action Method]
    }
    }

    Semantics: State Machine definition

    The semantics below describes the use of gaurd condition,multiple transition and actions performed on state changed. These semantics thus solves lot of business problem statement.Below is the list of semantics required to write sm file.

    1) If there is no action for a given transition.

    StateName 
    	{
    	    TransitionMethodName() TransitionStateName 
        	    {}
    	}

    2) One can pass input arguments to transition method.

    StateName 
    	{
        		TransitionMethodName (userId: String) TransitionStateName 
    		{}
        	}

    3) One can have Guard Condition for multiple Transitions within a state definition.

    StateName 
    	{
        		TransitionMethodNameFalse()[!contextBusinessObj.IsState]         
                     TransitionStateName1
                     Action1	{}
    
    		TransitionMethodNameTrue()[contextBusinessObj.IsState]         
                     TransitionStateName2
                     Action2	{}
    
        	}

    4) If there is no action for a given transition.

    StateName 
    	{
        		TransitionMethodName ()         
                   Nil
              	{}
    
        	}

    Problem Statement

    Here we have to create state machine workflow for CAR-Vehicle. As car can have four state Start ,Run,SlowDown and Stop.Taking these state I have designed a small demo project which demonstrate how to use SMC in .net. The prior knowledge of workflow can help one to understand this demo quickly.Lets us follow the below step to create workflow for this problem statement.

    Steps To Integrate SMC With .Net

    Below diagram depicts the picture how state change occured in workflow.This diagram is state activity diagram which help us to understand the state transition from start to end.

    State Machine Flow Diagram

    State Machine Diagram

    1. Create State machine defination file called .SM file, for CAR workflow.

    Taking above semantics and syntax we can create sm file. Below is the sm created for CAR automation workflow.Here the CarEntity is the object of business entity Car. The action UpdateState has takes three argument one car object,Next state and Current state.So in UpdateState we specify the next possible state for given current state.

    Car State Machine Definition

    %class CarWF
    					
    		%package StateMachineWorkflowActivity
    		%import StateMachineWorkflowActivity
    		%start MainMap::Start
    		%map MainMap
    		%%		
    		Start
    		{
    			SubmitRun 	Run
    		     	{
    			  UpdateState(ctxt.CarEntity  , Utilities.CarState.Run, Utilities.CarState.Start);									
    		     	}
    	         	SubmitStop	Stop
    		      {
    			  UpdateState(ctxt.CarEntity , Utilities.CarState.Stop , Utilities.CarState.Start);								
    		      }
    		}
    		Run		
    		{
    	
    	         	SubmitSlowDown	SlowDown
    		      {
    			   UpdateState(ctxt.CarEntity , Utilities.CarState.SlowDown, Utilities.CarState.Run);								
    
    		      }
    		       SubmitStop	Stop
    		      {
    			  UpdateState(ctxt.CarEntity , Utilities.CarState.Stop , Utilities.CarState.Run);								
    		      }
    			
    		      
    		      
    		}
    		SlowDown
    		{
    		       SubmitStop	Stop
    		      {
    			  UpdateState(ctxt.CarEntity , Utilities.CarState.Stop , Utilities.CarState.SlowDown);								
    		      }
    
    		}
    		Stop
    		{
    			
    		}			
    		%%

    2. Run the Command To Generate the CSharp Class From SM File

    Run the below command in command prompt to generate workflow CSharp file.

    java -jar "D:\SMC_Demo\Smc.jar" -reflect -serial -csharp  "D:\SMC_Demo\CarWF.sm"

    3. Create Persistence mechanism In Database

    In workflow it is very important to implement persistence mechanism where we can store the context of each entity and activity in database.Here I design very simple workflow saving mechanism.Create database named CarAutomation. There will be three table in it. The significance is stated as follows:

  • 1. Car Table: This table will marked the entry of car details, its current and previous state and customer of the car.
  • State Machine Diagram

  • 2. StateMaster Table: This table will list all the states available for car automation.
  • State Machine Diagram

  • 3. StateAuditLog Table: This table maintains the history of states for each car created and workflow activity occurred at each state
  • State Machine Diagram

    Database Diagram

    State Machine Diagram

    4. Create Solution in .Net

    Refer source code for more information.

    Here is the class diagram for stateMachineWorkFlowActivity package.

    State Machine Diagram

    State Machine Diagram

    Create Car Dashboard.

    In UI mode user view workflow dashboard.This dashboard is the workflow activity screen where one initiates state transition and perform appropiate actions as defined in state definition file. User clicks on respective CAR ID and perform state transition for it. The URL for each state transition is maintain in database table.

    State Machine Diagram

    Once the user clicks on CAR ID ,he/she is redirected to state activity page for the current state. Here user can clicks on 'Run','Slowdown' or 'Stop'. If user clicks on Run and Stop ,the action is perform as it is correct rules as per .SM file definition.

    State Machine Diagram

    protected void btnRun_Click(object sender, EventArgs e)
        {
            try
            {
                Car car = new Car();
                car.CarID = Convert.ToInt32(ViewState["CAR_ID"]);
                car.Remark = "State Machine Compiler:Run Car";
                car.State = (Utilities.CarState)Convert.ToInt32(ViewState["State"]);
                car.User = 444;
    
    
                StateMachineWorkflowActivity.IWFFactory m_WFactory = new StateMachineWorkflowActivity.WFFactory();
                StateMachineWorkflowActivity.ICarWF m_CarWF = m_WFactory.StartWorkFlowStateMachine(car);
    
                m_CarWF.SubmitRun();
                Response.Redirect("CarDashboard.aspx", false);
            }
           catch (Exception ex)
            {
                if (((statemap.TransitionUndefinedException)(ex)) == ex)
                { 
                ClientScript.RegisterClientScriptBlock(GetType(),"Key1","alert('This is undefined Workflow State.')");
                }
            }
        }

    If user clicks on Slowdown for current state 'Start', it will prompt for undefined state transition.

    State Machine Diagram

    Conclusion

    Hope this article will help the developer who needs reference for State Machine Compiler. This is just initiative towards presenting one working model of SMC integration with ASP.net.

    License

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


    Written By
    Technical Lead
    Australia Australia
    Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
    www.santoshpoojari.blogspot.com

    Comments and Discussions

     
    GeneralGraphical state machine designer for .SM files - visual paradigm Pin
    Jeremy Thomas11-Dec-08 0:00
    Jeremy Thomas11-Dec-08 0:00 
    GeneralRe: Graphical state machine designer for .SM files - visual paradigm Pin
    santosh poojari22-Dec-08 21:39
    santosh poojari22-Dec-08 21:39 
    GeneralRe: Graphical state machine designer for .SM files - visual paradigm Pin
    Jeremy Thomas14-Jan-09 21:40
    Jeremy Thomas14-Jan-09 21:40 
    Not sure what you mean by 'currently working on state machine compiler'. I came across visual paradigm while I was looking for some way of generating a .NET state machine from a state diagram. There is are a lot of posts about and code for .NET state machines but none that work from a state diagram. The one exception I found, of course, is visual paradigm but no one has written about using it for that, strange given the SMC seems to be the most prominent state machine system I found. Maybe one day I'll write a survey of all the .NET state machine options I found out there.

    I'm not actively working on the application that started to require a state machine and it will be some time till I get back to it.
    Regards

    Jeremy Thomas

    GeneralState machine Designer steed.net Pin
    marc.thom28-Oct-08 8:07
    marc.thom28-Oct-08 8:07 
    GeneralRe: State machine Designer steed.net Pin
    santosh poojari29-Oct-08 22:15
    santosh poojari29-Oct-08 22:15 
    GeneralRe: State machine Designer steed.net Pin
    Southmountain17-Mar-17 8:15
    Southmountain17-Mar-17 8:15 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.