Tutorial to create a simple finite-state-machine with NetEditor

This page contains a tutorial that permits to create a simple Finite State Machine based on the NetEditor library.

The definition of a new language's editor should follow the steps:

1. Definition of the language constructs

The maven module related to this step is: org.arakhne.neteditor.fsm:fsm-constructs.

The Finite State Machine language is composed of the constructs: state, transitions, start point, end point.

1.1. Definition of the abstraction for all the nodes of the FSM

The FSM diagram contains three types of nodes: state, end point, and start point. It is recommended to create an abstract class that is common to all the implementations of these nodes. Each node of an FSM contains only one anchor.

The AbstractFSMNode class extends the class StandardMonoAnchorNode, which is the implementation of a node with a single anchor inside. The generic parameters are the types of the graph, node, anchor and transition supported by the implementation (here the FSM classes). The constructor of the super class takes an instance of anchor as parameter. The FSMAnchor is used.

The AbstractFSMNode class permits to simplify the use of the generic parameters.

1.2. State Definition

The FSM state is defined in the class FSMState. A state has a name, an action to execute when entering inside the state, and action to execute when exiting from the state, and an action when staying in the state.

1.3.Transition Definition

The FSM transition is defined in the class FSMTransition. A transition has a name, an action to execute when transition is traversed, and a guard (a condition) that must be true to traverse the transition.

1.4. Start-Point Definition

The FSM start point is the point from which the simulation of the FSM must start. It is not a state by itself, but it is an AbstractFSMNode because it should be link to other FSM nodes with FSM transitions.

1.5. End-Point Definition

The FSM end point is the point at which the simulation of the FSM is stopping. It is not a state by itself, but it is an AbstractFSMNode because it should be link to other FSM nodes with FSM transitions.

1.6. Anchor Definition

The anchor is a construct provided by the NetEditor library to help to define how the edges and the nodes are connected. The implementation of the anchor must defined functions that permits to test if a node and an edge could be connected together.

1.7. Finite-State-Machine Graph Definition

The class that is representing the FSM is FiniteStateMachine. According to the NetEditor library, the class FiniteStateMachine is a type of graph and it extends StandardGraph with the appropriate generic parameters.

2. Definition of the language figures

The maven module related to this step is: org.arakhne.neteditor.fsm:fsm-figures.

All the language constructs should have a figure to be rendered.

2.1. Definition of figure for FSMEndPoint

The figure class FSMEndPoint extends the class CircleNodeFigure which the implementation of a circle provided by the NetEditor library.

2.1. Definition of the figures

The figures for the FSMStartPoint, FSMState, FSMTransition, and FSMAnchor are coded in a similar way.

3. Definition of the figure factory

The maven module related to this step is: org.arakhne.neteditor.fsm:fsm-figures.

The NetEditor viewer requires to have a figure factory to create the figures when they are not yet associated to graph elements that may be rendered. The class FSMFigureFactory is the implementation of a figure factory dedicated to the FSM classes. It extends the class AbstractStandardFigureFactory, whish provides a standard implementation for factories.

3. Definition of an FSM Editor

The maven module related to this step is: org.arakhne.neteditor.fsm:fsm-editor.

The FSM editor is a frame that contains the NetEditor viewer, which is an instance of JFigureViewer. The standard way to create the editor is illustrated in the following code.