Automata-based Programming in Visual Studio 2005: State Machine Designer Tool

This article introduces State Machine Designer tool for automata-based programming in Visual Studio 2005. State Machine Designer extends functionality of Visual Studio 2005 and gives developer more abilities for designing and realization of software products. This tool allows developer to create UML-like visual models of project, add automata behavior to any class in the project, and generate a part of source code on C# programming language.


Introduction
Currently it becomes obvious that there is no common way in developing of software products because of many different programming standards and techniques.Programming requires more and more specifications to make this process clear.Specification is a good practice but it is not enough because it does not reflect the code which developer will produce.Usage of visual models is a good way to bring source code concepts and specifications together.Modelling allows developer to see main structure of a project and main interactions between its components.The last problem is to make models and source code being related.UML-diagrams [1] may describe both of system behavior and project structure simultaneously.State Machine Designer allows usage of three UMLdiagrams during project developing.Those are Classes Diagram, Objects Diagram and State Machine Diagram.By using these three models a part of source code could be generated automatically.

The following UML-diagrams are used in State Machine Designer:
Classes Diagramdescribes classes, interfaces and its relations.
State Machine Diagramdescribes behavior of the entity whose lifecycle could be represented as a state machine.This state machine consists of finite count of states and transitions between them.States represent some stable states of the system and transitions occur on the specified system events and if corresponding guard conditions are satisfied [2].
Objects Diagramreflects instances of classes with initial values of its properties and aggregation relationships between these instances.

Automata-based programming
Automata-based programming is a programming technology where finite state machines are used for representing the whole program or some of its parts.
Finite state machines could be implemented using different methods such as state design pattern [3] or SWITCH-Technology [4] or any other well-known method.But the main idea remains the same: introduce finite number of states, provide transitions between these states on some system events and make specified actions on states entering, leaving and transitioning between them.

Automata-based
programming is helpful in development of compilers, automation solutions and every application whose logic could be represented as finite state machines.

Implementation
State Machine Designer tool developed as a plug-in for Visual Studio 2005 [5] and based on Domain-Specific Language Tools [6]

Classes Diagram
Classes Diagram is intended for visual creation of classes, interfaces of the project and setting of its relationships.IDE Visual Studio 2005 has its own classes diagram which also allows creating classes, interfaces and so on.It has some disadvantages though: it is not always synchronized with a source code.For example, if we create any class on standard class diagram and then delete this diagram created class will stay in the project.And wise versa if we create a class independently of the diagram this class will appear on the diagram only after diagram"s regeneration and saving.It is unacceptable to have a source code and diagrams which are different.Diagram and source code should always be synchronized to prevent misleading situations.
Classes Diagram introduced in this paper is completely synchronized with the code it reflects.This diagram also has possibilities for adding of automata behaviour for the classes on it.On adding automata behaviour from Classes Diagram to some class automatic transition to the State Machine Diagram is performed.
Classes Diagram also has validation mechanism which prevents creation of wrong constructions in meaning of C# language concepts.

State Machine Diagram
State Machine Diagram is intended for creation of visual models for the objects which lifecycle could be represented by finite count of states with guarded transitions between them.

State Machine Diagram has validation mechanism which allows guaranteeing the following rules:
1. State machine has only one initial state.

2.
State machine has at least one final state.

3.
Every transition has specified event on which this transition is performed.
4. Every state should be reachable from the initial state.

5.
Transitions with the same event and same source state should have orthogonal guard conditions.
The fifth rule is very hard and has no accurate solution in this work.

Objects Diagram
Object Diagram is intended for visual creation of starting configuration of the application.For each object on the diagram instance of specified type is created and developer are able to set initial values of properties for each object, set relationships between different objects.And if there is object with automata behaviour on the diagram, developer can mark corresponding state machine as a start point of the whole application.

Code generation text templates
Code generation for specified model is possible with DSL Tools.Code generation is performed using text template transformation.Text templates are different for each of the three diagrams.On every model change new source code is generated.Thanks to this approach source code always completely represent visual model for any of the three diagrams.
Developer should never modify auto generated source code because it will be overwritten on the next saving or compilation of the project.

Classes Diagram editor
For editing of Classes Diagram developer adds new item to the project with special extension ClassesLanguage.When developer double-clicks on such an item special editor is appeared in new document window (Figure 1).Implementation for all methods for all classes from the diagram could be done using additional code editor which is shown after double click on any method (Figure 4).

State Machine Diagram editor
For editing of State Machine Diagram developer adds new item to the project with special extension StateMachineLanguage.Such items also have their own editor (Figure 5).StateMachineLanguage item could be created automatically when developer adds automata behavior to some class on the classes diagram.

Figure 5. State Machine Diagram editing in
Visual Studio 2005.Digits on the picture mean the same areas as on Figure 1.Toolbox for state machine editor contains "Initial state", "State", "Final state" and "Transition" items.Using this items developer visually creates state machine.State Machine Diagram also supports validation.The following rules are always satisfied: there is only one initial state, there are no unreachable states and so on.

Objects Diagram editor
For editing of Objects Diagram developer adds new item to the project with special extension ObjectsLanguage (Figure 6).This diagram is useful when application has fixed count of different objects.In this case objects configuration could be represented on the diagram.And those objects which have automata behaviour will be having "A" icon in the top-left corner of the shape (Figure 7).

Figure 1 .
Figure 1.Classes Diagram editing in Visual Studio 2005.Digits on the picture specify the main windows of IDE during classes editing: 1. Solution Explorertree list of modules and files in the project.Using "Add New Item …" command developer can add new files to the project as well as files with ClassesLanguage, StateMachineLanguage and ObjectsLanguage extensions which represent supported by tool diagrams.2.Classes Diagram editor area.Developer is able toadd classes and interfaces to this area using extended toolbox.

3 . 4 .
Toolbox which contains items for adding classes, interfaces and inheritance relationships to Classes Diagram.Property window for the diagram"s active object.Using this diagram adding of automata behaviour to any class is possible.After right click on any class figure on the diagram context menu is shown (Figure 2).Developer can choose "Add/Edit automat behaviour" item and active window will be automatically switched to the State Machine Diagram for the chosen class.If there is no such a diagram it will be automatically created.

Figure 2 .
Figure 2. Context menu for the class onClasses Diagram.All classes which have its own state machine are drawn with "A" icon in the top-left corner (Figure3).

Figure 3 .
Figure 3. Class with automata behaviour.Implementation for all methods for all classes from the diagram could be done using additional code editor which is shown after double click on any method (Figure4).

Figure 4 .
Figure 4.Additional source code editor.Classes Diagram supports model validation.For example, it doesn"t allow inheritance of interface from the class or class from two or more classes because such constructions are wrong in .NET languages.If some of the restrictions are not satisfied message with corresponding error is shown in error window.

Figure 7 .
Figure 7. Object with automata behaviour.On Objects Diagram developer could assign some of state machines to start when the whole application is started.
This paper describes a tool for Microsoft Visual Studio 2005 which allows developer to model and develop application using three diagrams: Classes Diagram, State Machine Diagram and Objects Diagram.This tool extends abilities of Microsoft Visual Studio 2005 in applications designing and development.With this approach a part of the source code is generated automatically that follows to decreasing of errors count.Thanks to visual models application becomes more clear and logical.Developed tool could be used in development of any application but it is most helpful in cases of reactive systems[8].Usage of the tool in development helps programmer to see static and dynamic models of the application simultaneously that also makes development process easier.Currently there is no similar tool for Visual Studio 2005 which will help developer to combine traditional programming techniques with automata-based programming.
which is a part of Microsoft Visual Studio 2005 SDK.