Table of Contents
List of Tables
Table of Contents
Qfsm is a graphical editor for finite state machines written in C++ using Qt the graphical Toolkit fromTrolltech.
Finite state machines are models to describe complex objects or systems in terms of the states they may be in. In practice they can be used to create regular expressions, scanners or other program code as well as for integrated curcuit design.
Current features of Qfsm are:
Copyright (C) 2000-2015 Stefan Duffner, Rainer Strobel
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
There exists a (graphical) Windows installer that can be downloaded from here. The installation should be straightforward and doesn't require any other software to be installed.
An RPM package for openSUSE is provided here. RPM packages for other Linux distributions might be provided on other websites.
Qfsm has to be compiled using the CMake buildsystem. Among the advantages of CMake are that it provides excellent cross-platform support, that it can create project files for various IDEs such as KDevelop, XCode and MS Visual Studio and that it builds out-of-source, keeping the source tree clean of temporary files.
tar xvfz qfsm-0.54.0-Source.tar.gz --directory .
and change to that directory.export PATH=/usr/lib/qt4/bin:$PATH
cmake .
make
make install
This will install the executable "qfsm" to /usr/bin/ and the documentation to /usr/share/doc/qfsm/.
If CMake fails to find any of the dependencies but you know you have the development headers/libraries installed, add -DLIBRARY_SEARCH_DIRS=<path/to/lib> and -DINCLUDE_SEARCH_DIRS=<path/to/include> to the cmake command.
If you want to create a project file for your IDE (e.g. KDevelop), add -GKDevelop3 to the cmake command above. This will create a .kdevelop file in the build directory which can be opened by KDevelop.
In order to build Qfsm under Windows you have to execute the program CMakeSetup, specify input (source) and output directory and click on "Configure". You may choose between different development environments (e.g. Visual Studio) and CMake will create the respective project files. You can then compile Qfsm as usual with your preferred build tool. For more details, refer to the CMake documentation: http://www.cmake.org/HTML/Documentation.html.
Table of Contents
To create a new file: | select “New”. For more details, see the section called “Creating and modifying a Finite State Machine” |
To open an existing Qfsm file: | select “Open”. |
To open the most recently opened Qfsm files: | select “Open Recent”. |
To save the current FSM to a Qfsm file: | select “Save”. |
To save the current FSM under a different name: | select “Save As”. |
To export the current FSM to a foreign file format: | select “Export”. For more details, see the section called “Exporting” |
To print the current FSM: | select “Print”. |
To open a new window with a separate working area where a different FSM can be edited: | select “New Window”. Note that you can copy, cut and paste states and transitions from/to different FSMs. |
To close the current FSM: | select “Close”. |
To quit Qfsm: | select “Quit”. |
To undo the last action: | select “Undo”. |
To cut the currently selected states and transitions to the clipboard: | select “Cut”. |
To copy the currently selected states and transitions to the clipboard: | select “Copy”. |
To paste the clipboard into the current FSM: | select “Paste”. |
To delete the currently selected states and transitions: | select “Delete”. |
To switch to the select mode: | select “Select”. For more details, see the section called “Using the Select mode” |
To select all states and transitions of the current FSM: | select “Select All”. |
To deselect all objects: | select “Deselect All”. |
To open the options dialog: | select “Options”. For more details, see the section called “Setting the options” |
To show/hide the state codes inside the states: | select “State Codes”. Each state has a unique identifier, called state code, which is an integer that is automatically determined by Qfsm. |
To show/hide the Moore outputs inside the states: | select “Moore Outputs”. Each state defines its Moore outputs which are the values that are sent to the outputs of the FSM when the respective state is reached. |
To show/hide the Mealy input conditions on the transitions: | select “Mealy Inputs”. Mealy inputs are asynchronous inputs to the FSM. They can trigger transitions from one state to another if the condition of the respective transition is satisfied. |
To show/hide the Mealy outputs on the transitions: | select “Mealy Outputs”. Mealy outputs are outputs of the FSM that were sent when a transition is triggered. Thus, each transition can define the Moore outputs that are sent when it is triggered. |
To show/hide the shadows of the states: | select “Shadows”. |
To show/hide the grid on the working area: | select “Grid”. |
To show/hide the window displaying the names of the inputs and outputs of a finite state machine: | select “IO View”. |
To switch to the pan mode: | select “Pan View”. For more details, see the section called “Using the Pan mode” |
To switch to the zoom mode: | select “Zoom”. For more details, see the section called “Using the Zoom mode” |
To zoom the view in: | select “zoom in”. The current zoom value is shown in the leftmost part of the status bar. |
To zoom the view out: | select “zoom out”. The current zoom value is shown in the leftmost part of the status bar. |
To set the zoom to the original value (100%): | select “Zoom 100%”. The current zoom value is shown in the leftmost part of the status bar. |
To modify the properties of the current FSM: | select “Edit”. It opens a dialog in which you can modify the properties of the FSM. For more details, see the section called “Creating and modifying a Finite State Machine” |
To correct automatically the codes (or identifiers) of all states such that each state code is unique: | select “Auto correct State Codes” |
To switch to the simulation mode: | select “Simulate”. For more details, see the section called “Using the Simulate mode”andthe section called “Simulating a FSM” |
To perform an integrity check on the current FSM: | select “Integrity Check”. For more details, see the section called “Checking the integrity of a FSM” |
To switch to the "add state" mode and create a new state: | select “New”. For more details, see the section called “Using the Add State mode” and the section called “Adding and modifying states” |
To modify the properties of the currently selected state: | select “Edit”. It opens a dialog in which you can modify the properties of the state. For more details, see the section called “Adding and modifying states” |
To define the currently selected state as the initial state of the FSM: | select “Set Initial State”. |
To define the currently selected state as a final or non-final state: | select “Toggle Final State”. |
To switch to the "add transition" mode and create a new transition: | select “New”. For more details, see the section called “Using the Add Transition mode” and the section called “Adding and modifying transitions” |
To modify the properties of the currently selected transition: | select “Edit”. It opens a dialog in which you can modify the properties of the transition. For more details, see the section called “Adding and modifying transitions” |
To straighten the currently selected transition: | select “Straighten”. |
Table 2.1. Attributes of a Finite State Machine
When you want to modify the properties of an existing FSM you can select Machine->Edit from the main menu and the same dialog box displays. As soon as you click OK the changes take effect.
The working area denotes the area of the Qfsm window that shows the state diagram. Once you have created a new FSM you see a blank working area and you are in the selectmode.
There are six different modes you can be in and which determine what happens when you click or drag the mouse inside the working area of Qfsm.
Only one mode can be activated at a time. To change the mode, click on one of the icons in the middle of the toolbar.
When a diagram is larger the working area, you have the possibility to move the view of a diagram. To move the view of a diagram, click on the middle mouse button and drag the mouse pointer. As soon as you release the middle mouse button the application reverts to the selected mode.
Alternatively, you can select the respective menu entry or press the respective short cut. The active mode is indicated by a highlighted toolbar button. In some modes, the form of the mouse cursor also changes, e.g. a magnifier for the zoom mode.
In this mode you can select, move or modify graphical objects.
To select a state or a transition, click on it with the left mouse button.
To select several states or transitions at the same time, hold down the shift key. You can then apply further actions on selected graphical objects, i.e. copy or edit, by using the menu.
To unselect all the selected graphical objects, click on the background.
To show the context menu for a state or a transition, click with the right mouse button on the state or transition.
To modify the properties of a state or a transition, double-click on the state or transition. It opens a dialog in which you can modify the properties.
To select multiple graphical objects, draw a rectangle holding the left mouse button around the graphical objects you want to select.
To move state or transitions, select them and drag them to the desired position.
To move the transition control points, select the respective transition and then drag one of the control points. The transition control points are indicated by small red and green points when a transition is selected. The red points control the form of the transition, i.e. the bend. The green ones are used to attach them to a starting and end state.
In this mode, you can move the view to a different part of your diagram when your diagram is larger than the working area of the window.
To move the view, drag the mouse pointer.
In this mode you can zoom in the view.
To zoom in the view, click with the left mouse button on the working area.
To zoom out, keep the CTRL key pressed at the same time you click.
In this mode you can add states and specify their properties.
To create a new state, click with the left mouse button and fill the dialog with the properties. For more details, see the section called “Adding and modifying states”
In this mode you can add transitions and specify their properties.
1) click on a state and drag the mouse pointer to another state.
2) fill the dialog with the properties of the transition.
For more details, see the section called “Adding and modifying transitions”
In the simulate mode, you can test the behaviour of your state machine with respect to external input.
When you enter this state, the simulator dialog opens and all interaction with the state diagram is disabled until you close the dialog. For more details, see the section called “Simulating a FSM”
1) first you need to:
- create a new FSM or;
- open an existing file.
To create a new FSM, select File->New (see the section called “Creating and modifying a Finite State Machine”)
To open an existing file, select File->Open
and choose the desired file.2) Switch to the "add state" mode (see the section called “Using the Add State mode”).
3) Click at the position of the working area where you want the new state to be.
The following dialog opens.
To specify or modify the properties of the state, fill the "State properties" dialog:
Table 2.2. Attributes of a state
1) switch to the "select mode" (see the section called “Using the Select mode”).
2) double-click on the respective state
Or
2) select one state and select State->Edit from the main menu.
1) you need first to:
- create a FSM or
- open an existing file.
To create a FSM, select File->New (see the section called “Creating and modifying a Finite State Machine”)
To open an existing file, select File->Open from the main menu and choose the desired file.
2) create at least one state (see the section called “Adding and modifying states”).
3) switch to the "add transition" mode (see the section called “Using the Add Transition mode”).
To create a transition from state A to state B, press and hold the left mouse button on state A and release it on state B.
To draw loops, i.e. transitions that go from one state to itself, press and release the mouse button on the same state.
The "Transition properties" dialog opens.To specify or modify the properties of the state, fill the dialog.
Table 2.3. Attributes of a transition
Condition Type | Choose the type of the condition. The type of the condition determines the format in which you enter the input condition in the next field. |
Input | If the condition type is binary, enter a string of zeros and ones representing the Mealy input that should trigger this transition. You can also use the character 'x' meaning: "don't care". You can also specify several alternative sets of inputs linked with "|" (OR). If the condition type is ASCII enter a character or specify an expression in a specific format which is explained in detail in the section called “Using input ASCII conditions”. For "free text" conditions enter any input character string. However, it has no logical meaning and won't be interpreted, for example when simulating the machine. For SMC export, put function names here (e.g. "suspend()"). |
Output | Enter the Mealy output sent from the FSM when the transition is activated, i.e. the input condition is satisfied. Depending on the type of the condition the format is either a string of zeros and ones (binary) a character (ASCII) or any character string (free text). Note that in case of an ASCII character it can also be an escape sequence. For more details on escape sequences, see the section called “Using input ASCII conditions” For SMC export, put function names here (e.g. "suspend()"). |
Description | You can enter a description of the transition. This is only for documentation purposes. |
To modify the properties of an existing transition:
1) switch to the "select mode" (see the section called “Using the Select mode”).
2) double-click on the respective transition
Or
2) select one transition and selectTransition->Editfrom the main menu.
To change the bend of the transition as well as its start state and end state:
1) switch to the “Select mode”
2) click on the transition.
Four control points appear. You can drag them around with the left mouse button.
The green ones allow you to change the start and end state. With the red ones you can change the bend of the transition.
When you create a transition of FSM that processes ASCII characters you have to enter an input condition. This condition can be a simple character, e.g. 'a
', or several characters that are expressed by a special notation explained in the following.
This is the most simple form of condition. It contains one ASCII character, e.g. 'a
' or 'z
'.
Note that for special characters, e.g. '-
' (minus sign) or the space character you need to use an escape sequence (see the section called “Escape sequences”).
If you want the condition to contain multiple characters, i.e. 'a
' or 'f
' or '+
' you just enter the string: 'af+
'.Clearly, the order is not important.
Note that it isnotpossible to use a concatenation of characters as input condition, for example 'print
' in order to recognize the word "print". To do this, you have to create a transition and a state for each character and build a chain with the respective characters.
Special characters like the newline character need to be escaped, i.e. backslash + some character. The following table shows the recognized escape sequences.
Table 2.4. Recognized escape sequences
escape sequence | meaning |
---|---|
\t
| tab |
\n
| newline |
\r
| carriage return |
\s
| space |
\-
| minus |
\d
| digit (0-9) |
Note that the last escape sequence '\d
' actually represents 10 characters.
Characters that are neither printable nor in the above table can be specified by '\0
' (backslash zero) followed by their hexadecimal code. For example, '\0CF
' would represent the ASCII character 207 (decimal).
You can further specify ranges by using the minus sign. Thus, 'a-z
' means one of the characters between 'a
' and 'z
' (including). Any character, even escaped ones, can be used as start or end point of a range.
To check the integrity of a FSM, select Machine->Integrity Check from the main menu.
The following tests are performed:
A log of the checks that have been performed is printed in the protocol field at the bottom of the window.
Once you have created a FSM with some states and transitions you can simulate its behaviour with respect to varying input signals.
To start the simulation of an FSM, select Machine->Simulate from the main menu.
The "simulator" dialog opens allowing you to input data to the machine while displaying its current state and output.
Input | 1) Enter the input data in the text field 2) select one of the formats: binary, hexadecimal or ASCII. 3) you can set or unset input bits using the buttons 0 to 15. When you choose the ASCII format you can also use escape sequences as detailed in the section called “Escape sequences”. However, you can only enter a single character. Thus, |
Output | Choose the format of the current FSM, i.e. binary, hexadecimal or ASCII. |
State Name and State Code fields | display the current state of the FSM. The red or green point next to it indicates if the FSM is in a final state or not, i.e. green for final state and red otherwise. |
There are two modes to send input data:
- click on the Send button or, press the Enter key)
In the clock mode, Qfsm periodically sends the data in the input text field on the top of the dialog to the FSM.
You can specify the frequency of the clock in the input field at the bottom right. For ASCII input, the input text field is cleared after each clock signal, which allows to simulate the behaviour of ASCII FSMs in real-time by pressing different keys.
To exit the Clock Mode, click once again on the Clock button.
To reset the FSM, click on the Reset Button.
This sets the FSM to its initial state.
There are several export functions in Qfsm.
To export, select File->Export in the main menu.
They can be divided into the following categories:
Here are some explanations about the different export functions:
State tables can be exported in the formats: ASCII (plain text), Latex or HTML. State tables show for each possible state and input (here calledevent) the respective resulting states.
To change the options concerning the layout of the state table, a dialog allows you to change some options.
There are two different types of files that can be used by other code-generating software:
For the ragel (.rl) file format only ASCII FSMs can be exported. The resulting file serves as an input for the ragel state machine compiler. The ragel state machine compiler is a compiler that generates code from a high-level state machine description language. In this way, you can create string parsers for example. For details refer to the ragel homepage.
A dialog allows you to create a so-called action file.
That means, the ragel state machine specification is divided into two files. One that contains the state machine logic (which I will call FSM file here) and an action file that contains the action definitions and a framework calling the state machine. Thus, the action file actually includes the FSM file. The name of the action file is determined automatically by appending'_action'
at the end of the file name.
The idea is that you create the action file only once at the beginning, and then edit this file manually to define the specific actions. You can then modify your FSM logic in Qfsm as many times as you want and regenerate the FSM file without losing your own action definitions.
Example: suppose you have created an ASCII FSM and you export it under the name myFSM.rl
. If you check the option 'Create action file' the action file will be created under the namemyFSM_actions.rl
.
Using ragel you can compile the action file: ragel -C -o myFSM.c myFSM_actions.rl
This will create a file, called myFSM.c with the C code of the FSM. It will contain a function:int parse(char* string)
that parses an input string and returns 1 if the FSM accepts it, i.e. finishes in a final state, and 0 otherwise.
Similarly to ragel, the State Machine Compiler (SMC) transforms a textual state machine description (a .sm file) into code in one of the following languages: C, C++, C#, Groovy, Java, JavaScript, Lua, Objective-C, Perl, PHP, Python, Ruby, Scala, TCL, VB.net. Again, there is a separation into two parts of code, i.e. two classes. One class defines the FSM and is generated by SMC from the .sm file. The other is called the application class, or "AppClass", which you have to write yourself and which should contain an instance of the FSM class. In the AppClass, you can issue events by calling the respective methods of your FSM class, i.e. the events/actions that you have defined as inputs in the FSM. And you have to define the actions/events (i.e. methods) that the FSM will call, i.e. the FSM transition outputs and entry/exit actions. For details refer to the SMC homepage.
Qfsm can generate the .sm file from your graphical state diagram. This only works for "free text" FSMs, and the inputs and outputs of the machine are not simple ASCII symbols or binary bits but method names, for example "run()" or "suspend()". Several output actions (or events) can be specified by a comma-separated list.
Qfsm also supports state entry and exit actions, as well as default transitions (but not default states). Transition guards can also be added after the input events, for example "doThis() [ctxt.isValid()]". The object "ctxt" refers to your AppClass.
Here is a screenshot of a FSM for the purpose of exporting to SMC.
Once you have exported your FSM to a .sm file, let's say FSM1.sm, you can compile it for example to Java code with the command:
java -jar /usr/local/bin/smc/bin/Smc.jar -java FSM1.sm
replacing the path of Smc.jar corresponding to your smc installation. This creates the file FSM1Context.java - your FSM class file.
Here is an example of how to write a corresponding application class (AppClass). Note that the name of the class ("FSM1") has to be the same as the FSM name.
public class FSM1 { private final FSM1Context fsm; public int a; FSM1() { fsm = new FSM1Context(this); } public void action1() { System.out.println("executing action1()"); } public void do1() { System.out.println("executing do1()"); } public void do2() { System.out.println("executing do2()"); } public void entering() { System.out.println("entering State"); } public void exiting() { System.out.println("exiting State"); } public void aa() { System.out.println("executing aa()"); } public void bb() { System.out.println("executing bb()"); } public void default_action() { System.out.println("executing default_action()"); } public void testit() { a=1; fsm.init(); fsm.run(); fsm.init(); } public static void main(String[] args) { FSM1 f = new FSM1(); f.testit(); } } |
You can then compile everything with the java compiler:
javac *.java -cp /usr/local/bin/smc/lib/statemap.jar
making sure to put statemap.jar in your class path.
The hardware test code is used together with the output of a hardware description export. The supported language for test code is VHDL. The VHDL testbench export will create a set of files that enables a hardware simulation and debugging software to analyse the VHDL code exported by Qfsm.
The following files are generated:
Per default, these files can be written to different directories within a clearly structured design directory. Therefore, in the export options a 'Base Directory' input string is asked, first:
Base_directory | +-src | fsmname.fsm | e_fsmname.vhd | a_fsmname_architecturename.vhd | t_fsmname.vhd | p_fsmname.vhd | +-stimuli | fsmname_stimuli.vec | +-log fsmname_result.log
When exporting the I/O description of the state diagram a file containing the names of all the inputs and outputs of the FSM is created. The description is stored in a comma separated value (CSV) format, where the separator is a semicolon and the values are inside double quotes. The first value of each line is a textual description of the values in that line, i.e. "Inputs", "Mealy Outputs" and so on.
The following lines are written:
A "free text" FSM can be exported to vvvv Automata code. This code can be used in conjunction with the tool vvvv, a toolkit for real-time video synthesis. This textual format is a list of quadrupels, where each item/line describes a transition and is composed of:
When selecting this export function from the menu a non-modal dialog box is opened that displays the resulting code. This is automatically updated as the diagram is modified. An additional reset transition can optionally be added for each state by checking the box at the bottom of the dialog. There you can also specify the name of the reset event and the name of the respective action to be triggered.
A "free text" FSM can be exported in the State Chart XML format as proposed by the W3C. The specification of SCXML can be found here. Note that each transition is triggered by an event. And the text that is entered as transition input is used as the name of the event. The text that is entered as the transition output is interpreted as the name of an event that is to be sent when the transition is triggered. Thus, if the output linked to a transition is non-empty a corresponding <send> tag will be written.
Qfsm can import files written in the DOT language using Graphviz.
To import a DOT file (with extension .gv), select File->Import->Graphviz... in the main menu.
Qfsm uses Graphviz to transform the textual description of the FSM into a graphical layout of the state diagram.
When writing a DOT file make sure you respect the following syntax rules:
Here is an example of a dot file describing a binary FSM. The file is included in the Qfsm package in the examples folder.
digraph binary_finite_state_machine { rankdir=LR; size="8,5" node [shape = doublecircle]; LR_0 LR_3; node [shape = circle]; LR_0 [ label = "State_1" moore = "01"]; LR_0 -> LR_1 [ label = "0000|0001###11" ]; LR_1 -> LR_2 [ label = "NOT 0000###00" ]; LR_1 -> LR_3 [ label = "DEF###01" ]; LR_0 -> LR_2 [ label = "ANY###10" ]; LR_2 -> LR_3 [ label = "1111###11" ]; }
Qfsm automatically determines the type of FSM: binary, ASCII, or Free Text.
In the above example, "LR_0", "LR_1", … denote the states. It can be given any name by specifying the label
attribute. If you don't do this, the node name (here: "LR_1", …) is used as the state name.
The moore
attribute is used to specify the Moore outputs of a state.
Transitions are written as
State1 -> State2;
or
State1 -> State2 [ label = "00###11" ];
where the label
attribute specifies the Mealy inputs, and optionally the Moore outputs separated by the string ###
. NOT
denotes negation, DEF
a default transition, and ANY
transitions that accept any input.
To define end states, use the shape = doublecircle
node attribute as illustrated in the above example.
1) select Edit>Options>General
2) select the language
3) click on OK
4) restart the application
The language change is now effective.
To change the display, select Edit>Options>Display from the main menu
If you have questions or suggestions concerning Qfsm feel free to contact me at:
<qfsm(at)duffner(dash)net(dot)de>
I'm also glad about any contribution you want to make to the project, e.g. code, bug fixes, documentation, packaging, testing etc.