Previous Next
vpuml_user_guide Chapter 12 - State Machine Diagram Code Generation

Chapter 12 - State Machine Diagram Code Generation

VP-UML can assist you in drawing a State Machine Diagram, as well as generate a State Machine Diagram Code.
 Figure 12.1 - State Machine Diagram

Drawing State Machine Diagram

To generate a state machine diagram, you should first have a Class on the class diagram. Select Sub Diagrams > State Machine Diagram > Create State Machine Diagram from the pop-down menu of the class to create a sub diagram.
Figure 12.2 - Select Create State Machine Diagram

You will see an initial pseudo state on the state machine diagram.
Figure 12.3 - State machine diagram with an initial pseudo state

Drag a State from the diagram toolbar and drop the diagram.
Figure 12.4 - Drag and drop a State

Alternatively, you can use the resources of the initial pseudo state.
Figure 12.5 - Using resources

A diagram can then be created.
 Figure 12.6 - Diagram created

Generate State Machine Code

The process of creating a State Machine Code is simple.
To generate a state machine code:
1. Select Tools > State Machine Code >Generate Code from the main menu.
Figure 12.7 - Select Generate Code...

2. The Generate state machine code dialog is displayed.
Figure 12.8 - Generate state machine code dialog

3. Select a language to generate the code in from the drop-down menu.
 Figure 12.9 - Select Language

4. Configure an output path by selecting ... or type in the path in the text box, and configure the options in the dialog.
 Figure 12.10 - Configure an output path

5. The process of generation is shown. You may choose to Close Dialog when finished progress by checking the check box.
 Figure 12.11 - Process of generation

6. The code is generated.
Figure 12.12 - Code generated

Programming with Generated State Machine Code

VP-UML supports the generation of different types of state machine code. Since the steps for generation of codes in different languages are similar, Java will be used as an example to illustrate the steps.
Before generating the code, you may want to configure the properties of states and transitions.

For state, configure by selecting Open Specification...from the pop-up menu.
Figure 12.13 - Select Open Specification...

The State Specification dialog box is now open, and you can configure the state properties here. You may edit the Entry property by clicking Edit... .
Figure 12.14 - State Specification dialog box

This is the Activity Specification(Entry) dialog box. After editing to suit your needs, click OK to confirm.
Figure 12.15 - Activity Specification(Entry) dialog box

State's property has been edited.
Figure 12.16 - State's property edited

Similarly, you can edit the transition by selecting the Open Specification... from the pop-up menu.
Figure 12.17 - Open Specification...

Then, edit the properties of transition in the Transition Specification dialog box.
Figure 12.18 - Transition Specification dialog box

The states and transitions properties are configured.
Figure 12.19 - State Machine Diagram

Running sample application

After generating Java files, you can compile and run them.  Command Prompt in windows is used as an example to demonstrate the actions.
1. Run the compiler.  Change the directory to the directory where the code file is generated.  Type in the command for compiling.  In this example, the command is javac Name\*java .
Figure 12.20 - Compile Java


2. After compiling, you can enter another command for running Java.  For Windows, the command is java -cp . telephone.TelephoneSample .
 Figure 12.21 - Run Java

3. The Java is run.
 Figure 12.22 - Sample Java

Insert Implementation

If there is no implementation, the Java cannot be run because there is an error.
 Figure 12.23 - Error message box
.
The original state machine diagram code generated did not have any implementation.
 Figure 12.24 - No implementation

You can add the implementation in the source.
 Figure 12.25 - With implementation

The diagram sample is run with the implementation.
Figure 12.26 - Java sample

Reverse State Machine Code

Apart from generation of state machine code, VP-UML also supports the reversal of the state machine code file, with extension .sm.
Figure 12.26 - A .sm file

To reverse the .sm file:
1. Select Tools > State Machine Code > Reverse Code... from the main menu .
Figure 12.27 - Select Reverse Code...

2. The Reverse State Machine Code dialog box is displayed. Select the Class and State Diagram.
Figure 12.28 - Reverse State Machine Code dialog box

3. Configure the input file path by typing in the text box or select ... .Select OK to confirm.
Figure 12.29 - Configure the input file path

4. The progress of reversal is shown. You can select to Close Dialog when finished progress by checking the check box.
Figure 12.30- Progress of reversing

5. The state machine code is now reversed.
Figure 12.31 - State machine code is reversed

Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com