A Class Element, which can be executed with Cameo Simulation Toolkit, must have its Classifier Behavior defined. The Classifier Behavior can be any kind of Behavior such as Activity, State Machine, and Interaction. This tutorial uses the State Machine Behavior to define the Behavior of the stopwatch.
To define the Classifier Behavior for the StopWatch Class, you have to create the Behavior on the StopWatch, which will be assigned later as the Classifier Behavior of the StopWatch automatically.
To define the Classifier Behavior for the StopWatch Class
- Right-click the StopWatch symbol on the Class diagram and select New Diagram > State-Machine Diagram.... A new State Machine Element will be created under the StopWatch Class with a new State Machine diagram.
- Name the State Machine diagram "StopWatch".
- Make sure that the StopWatch State machine is the Classifier Behavior of the StopWatch Class. To see the Classifier Behavior of the StopWatch Class, right-click the StopWatch symbol on the Class diagram and select Specification from the context menu. The Specification window will open.
- Select All from the Properties drop-down menu.
- Scroll down to see the ClassifierBehavior row, and you will see that the Classifier Behavior of the StopWatch Class is defined by the StopWatch State Machine.
Next, we are going to create the StopWatch initial and final stages, and the four States (Ready, Running, Paused, and Stopped) in the initial stage as well.
To add an Initial stage to the StopWatch State Machine
- Click Initial on the State Machine diagram toolbar, and then click any area on the State Machine diagram to place the Initial stage.
To add the ready, running, paused, and stopped States
- Click State on the State Machine diagram toolbar, and then click any area on the diagram to place a State. Name the created State "ready".
- Repeat Step 1 to create the "running", "paused", and "stopped" States.
To add a final stage to the State machine diagram
- Click Final State on the State Machine diagram toolbar, and then click any area on the diagram to place the final stage.
Now that the initial and final stages have been created, you will need to add the transitions between them and among the four States in the initial stage.
To add a transition from the Initial stage to the ready State
- Click a stage (the Initial stage in this example) that will be the source of the transition to be created. The smart manipulator toolbar will open.
- Click the Transition icon on the smart manipulator toolbar.
- Click a State (the ready State in this example) that will be the target of the transition.
- Repeat Steps 1 to 3 to create more transitions between the following States:
from the ready to running State
from the running to paused State
from the running to stopped State
from the paused to running State
from the paused to stopped State
from the stopped to ready State
from the stopped State to the Final stage - Create a looping transition to the running State by clicking the Transitiontoself icon on the smart manipulator toolbar.
- Now your StopWatch State machine will look similar to the one shown in the following figure
You will see that some of the created transitions have the SignalEvent triggers. These triggers need signal Elements and a package that is required to store those signal Elements. You can add a package and create the signal Elements by using the browser context menu.
To add a new package for the signal Elements
- Right-click the Model node in the containment browser and select New Element > Package.
- Name the created package "signals".
To create signal Elements
- Right-click the signals package and select New Element > Signal.
- Name the created signal "reset". The reset signal will be created and stored in the signals package.
- Repeat Steps 1 to 2 to create the split, start, stop, and unsplit signal Elements.
To create signal Events for the transitions on the StopWatch State Machine diagram
- Drag a signal Element from the containment browser to a transition on the State Machine diagram. The signal Event will later be specified as the trigger of the transition.
To create a signal Event for each transition
- Drag the start signal to the transition between the ready and running States.
- Drag the split signal to the transition between the running and paused States.
- Drag the stop signal to the transition between the running and stopped States.
- Drag the unsplit signal to the transition between the paused and running States.
- Drag the stop signal to the transition between the paused and stopped States.
- Drag the reset signal to the transition between the stopped and ready States.
- Drag the stop signal to the transition between the stopped and Final States.
Now each and every transition between the States has its own signal Event. Next, you will also need to create a signal Event for the transitiontoself of the running State. The signal Event that will be created as the trigger for the transitiontoself is called TimeEvent.
To create a signal Event for the transition to self of the running State
- Right-click the Transitiontoself of the running State and select Specification to open its Specification window.
- Click the Categorized View button to open the properties of the transition in the categorized view.
- Click the EventType row in the Trigger category and select TimeEvent from the drop-down menu.
- Click the When row in the Trigger category and type "1s" to specify that the trigger will send the time Event every second.
- Click Close. You will see all of the created stages, States, transitions, and signal Events are shown on the StopWatch State Machine diagram.