- Write a code template for instances of Statemachine
Write a code template for Statemachines
- Create a package
ch.actifsource.tutorial.statemachine.template
- Select the new package and choose New->Template from the context menu.
- Insert StatemachineImpl as Template Name
- Choose the Base Type ch.actifsource.tutorial.statemachine.generic.Statemachine
- Click Finish
- Insert Statemachine.name on the Filename Line and make sure that the language (C++) is automatically detected.
- Write the skeleton for a class Statemachine.name
Next, we define an enumeration variable with the all the States of a Statemachine as enumerators. This variable stores the current state of a Statemachine
- Write the declaration of enumeration variable m_aState
- Insert a LineContext in the enumeration list and choose the Selector Statemachine.state with the support of the Content Assist
- Insert State.name in the newly created LineContext. Append a ','. Then mark the ',' and select NotLast to make sure that there is no comma after the last entry in the enumeration list.
We define a member function for each event of our Statemachine which will later handle all the possible transitions triggered by the event:
- Create a new LineContext and choose Statemachine.event as the selector of the line context
- Write the skeleton of a function returning void named Event.name
We write a switch-statement with the current state m_aState
as control variable and define a LineContext that iterates over all
Transitions referring to an Event through the relation Transition.event
- Create a switch-statement with the
m_aState
as control variable
- Create a LineContext inside the switch-statement
- Choose Event.-event as the Selector of the new LineContext
We create a LineContext that iterates over all States that refer to a Transition through the relation State.transition
- Create a LineContext on the same line as LineContext that we have crated before
- Choose Transition.-transition as the Selector of the new LineContext
We write a case-statement for each State that is (indirectly) referring to an Event through State.transition.event
- Insert a case State.name and add a break at the end of the case-statement
We update the current state as follows: We first select for an Event the Transitions that refer to the Event trough Transition.event For each
Transition we select the States that are connected to Transition by State.transition
For each State it holds that if the current state m_aState
is equal to State then the new State of the Statemachine is Transition.targetState
- Write code to assign Transition.targetState.name to the variable m_aState
In order to generate code from the template we have implemented before, we setup the project properties for Actifsource:
- Select the project ch.actifsource.tutorial.statemachine and choose Project->Properties from the main menu
- In the Properties dialog choose Actifsource and select the tab Target Folders
- In the dialog Select Target Folder, click on the button Create folder
- Check the settings on the Target Folders tab and close the dialog by clicking on OK
The code generator now applies the template StatemachineImpl to the two Statemachine instances and stores the resulting files to the src
folder:
- Open the
src
folder and check that the two files Statemachine1Impl.hpp
and Statemachine2Impl.hpp
have been generated
- If the files have not been generated, make sure that Generate Automatically is active under Project in the main menu
- Open the newly generated files and inspect and compare the code for the two Statemachines
- Learn how to extend the Statemachine by conditional transitions and actions executed together with a transition by working through the Actifsource Tutorial Code Snippet
- Complete the generated classes by adding a member function initialize()