State diagram mappings - introduction (Java code)
When ACS generates a Class that owns a State Diagram through a State Machine, ACS generates state machine code to implement the State Diagram unless the «Java Class» stereotype has been applied to the Class and the tagged value of the Java Generate State Machine tag definition set to FALSE.
When you choose to generate code for State Diagrams, the ACS generates attributes, typedefs and operations in the code to implement the State Diagrams associated with the Classes that are being generated.
When ACS generates an attribute, typedef or operation in a code file, that attribute, typedef or operation is not associated with an Attribute, Typedef or Operation item in the model. To help you identify these code elements in the code files, ACS prefixes code elements generated for State Diagrams with 'Rts', except for the constructor and destructor operations. In addition, the code elements are conditional through RTS_SIMULATINON, so that they are not reversed into the Model as model items.
This topic uses an example to provide an overview of the Attributes, Typedefs and Operations that are generated from a simple State Diagram.
For entering the state machine
ACS generates a constructor operation to initialize the Class.
// \ART_SMG :: Constructor
public cBarCodeReader()
{
SimulationDelegate.GetSimDelegate().RtsNotifySynchronous(this, SimulationDelegate.RtsNotifyMSG.CREATE,"ed3a61ca-de1e-4382-a2fc-121821aa5a7e",0, null);
RtsBusy = new Semaphore(1);
try
{
RtsBusy.acquire();
}
catch (InterruptedException e)
{}
/* Initialising State Vectors to NotIn */
RtsCurrent_cBarCodeReader=RtscBarCodeReader_States.RtscBarCodeReader_States_NotIn_cBarCodeReader;
while(RtsRunToCompletion());
RtsBusy.release();
}
For setting and reading the current state of the state machine:
ACS generates a current state attribute to store the current state of the Class.
private RtscBarCodeReader_States RtsCurrent_cBarCodeReader
The preceding attribute can be set to Idle, Reading or NotIn_sdBCReader. NotIn_sdBCReader indicates that the state machine is not active.
public enum RtscBarCodeReader_States
{
RtscBarCodeReader_States_Idle
RtscBarCodeReader_States_Reading
RtscBarCodeReader_States_NotIn_cBarCodeReader
}
For entering each state
ACS generates an enter state operation to initialize each State (Idle and Reading) on entry.
private void RtsEnter_Idle()
{
/* Simulation support : Notification function call for entering state : Idle */
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.ENTER,"9f8b4d95-eb35-488e-99dc-feb9262fa535",0,null);
RtsCurrent_cBarCodeReader = RtscBarCodeReader_States.RtscBarCodeReader_States_Idle;
private void RtsEnter_Reading()
{
/* Simulation support : Notification function call for entering state : Reading */
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.ENTER,"ccb16de5-4672-4163-baed-db75b16ae6f5",0,null);
RtsCurrent_cBarCodeReader = RtscBarCodeReader_States.RtscBarCodeReader_States_Reading;
For exiting from each state
ACS generates an exit state operation to run code on exit from each State (Idle and Reading).
private void RtsExit_Idle()
{
if (RtsCurrent_cBarCodeReader == RtscBarCodeReader_States.RtscBarCodeReader_States_Idle)
{
/* Simulation support : Notification function call for exiting state : Idle */
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.EXIT,"9f8b4d95-eb35-488e-99dc-feb9262fa535",0,null);
}
}
private void RtsExit_Reading()
{
if (RtsCurrent_cBarCodeReader == RtscBarCodeReader_States.RtscBarCodeReader_States_Reading)
{
/* Simulation support : Notification function call for exiting state : Reading */
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.EXIT,"ccb16de5-4672-4163-baed-db75b16ae6f5",0,null);
}
}
For handling each event
ACS generates a signal event handler operation to handle the event.
public void RtsBarCodeDetected()
{
try
{
RtsBusy.acquire();
}
catch (InterruptedException e)
{}
RtsBarCodeDetected_In_cBarCodeReader();
while(RtsRunToCompletion());
RtsBusy.release();
}
For ensuring that calls are not executed when the state machine is busy
ACS generates a busy attribute to lock the state machine and each state when it is busy.
private Semaphore RtsBusy
For handling transitions that do not require further triggers
ACS generates a run to completion operation to handle all transitions that do not require further triggers.
private boolean RtsRunToCompletion()
{
if (RtsCurrent_cBarCodeReader == RtscBarCodeReader_States.RtscBarCodeReader_States_NotIn_cBarCodeReader)
{
/* Simulation support : Notification function calls for Entry and Exit of Initial state */
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.ENTER,"06329424-7e37-4104-a3c8-633678edd5ea",0,null);
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.EXIT,"06329424-7e37-4104-a3c8-633678edd5ea",0,null);
/* Simulation support : Notification function calls for transition */
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.TRANSITION,"b7365e29-8c1c-4b5a-860f-46aed984bf32",0,null);
RtsEnter_Idle();
return true;
}
if (RtsCurrent_cBarCodeReader == RtscBarCodeReader_States.RtscBarCodeReader_States_Reading)
{
RtsExit_Reading();
/* Simulation support : Notification function calls for transition */
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.TRANSITION,"54d4e919-d41b-44ea-a6bf-abb9f592f940",0,null);
RtsCurrent_cBarCodeReader = RtscBarCodeReader_States.RtscBarCodeReader_States_NotIn_cBarCodeReader;
/* Simulation support : Notification function call for entering final state */
SimulationDelegate.GetSimDelegate().RtsNotify(this, SimulationDelegate.RtsNotifyMSG.ENTER,"fab28f48-d2ff-41ab-9f8c-f541b6d6502e",0,null);
}
return false;
}
For exiting the state machine
A Destructor Operation to destroy the Class.
public void SMGDestroy()
{
RtsBusy = null;
}
For simulation purposes only
ACS generates the following operations for simulation purposes only - RtsGetAttributeValue, RtsSetAttributeValue and RtsInjectEvent.
ACS can create additional code elements that are not included in the preceding example.
ACS generates attributes for the following purposes:
• Storing the current state of each Sequential State and Concurrent State (current state attribute).
• Storing the last current state of a Sequential State or Concurrent State (history state attribute).
• Storing the handle for a timer (timer attribute).
ACS generates operations for the following purposes:
• Determining which State a Sequential State or Concurrent State starts in (default entry operation).
• Setting the default entry state of each Sequential State and Concurrent State (history state operation).
• Reading the current state of each Sequential State and Concurrent State (current state operation).
• Call events (Call Event Operation) and Time events (timer event handler operation).
• Setting the current State of a State to that recorded in a History State.