< Previous | Next >

Lesson 5: Creating statecharts

In this lesson, you create two statecharts. Statecharts define the behavior of actors, use cases, and classes, including the states that they can enter over their lifetime and the events and operations that cause them to transition from state to state. Your statechart diagrams define the behavior for two class objects in your model: button and sensor. They show the transitions between each state, denoted by transition arrows.

To create the statechart for the sensor class:

  1. Start Rational® Rhapsody® and open the vehicle sensor model if they are not already open.
  2. In the Rational Rhapsody browser, expand the Design package, right-click the Sensor class, and select Add New > Diagrams > Statechart. The Statechart element is added to the browser.
  3. Click on the drawing toolbar.
  4. Draw a large rectangle by clicking near the upper left corner of the drawing area and dragging to the lower right corner.
  5. Replace the default name with Active and then press Enter.
  6. Draw two states inside the Active state and name them Idle and Sensing.
  7. Click the Default Transition button Transition buttonand click to the left of the Idle state.
  8. Connect the default transition line to the Idle state.
  9. Click the Transition button, then click the right border of the Idle state, and then draw a transition to the Sensing state.
  10. Name the transition evToggle and then press Ctrl+Enter.
  11. Draw another transition from Sensing to Idle and name it evToggle.
  12. Draw another transition line from the Sensing state back to itself.
  13. Double-click the transition line you drew in step 12 to open the Features dialog box.
  14. Type tm(1000) in the Trigger box.
  15. In the Action field, press Enter and then type these two lines of code: ++pulse; and itsDisplay.show(pulse);
  16. Click OK. Your diagram must resemble this figure:
    Sensor statechart diagram

Creating the Button statechart

Now you create a second statechart that defines the button class. The button has three states - up, down, and held.
  1. In the browser, expand the Design package, right click the Button class and select Add New > Diagrams > Statechart. The Statechart element is added in the browser.
  2. Draw three states and name them Up, Down, and Held.
  3. Add a default connector to the Up state.
  4. Add a transition from Up to Down and name it evPress
  5. Add a second transition from Down to Up and name it evRelease.
  6. Double-click the second transition to open the Features window.
  7. In the Action field, press Enter and then type itsSensor->GEN (evToggle);.
  8. Add a third transition named evRelease from Held to Up, then click OK.
  9. Add a fourth transition from Down to Held.
  10. Double-click the fourth transition to open the Features window.
  11. In the Trigger box, type tm(1000).
  12. In the Action field, press Enter to leave a space, then type itsSensor->GEN(evReset); and click OK.
Your diagram must resemble this figure:

Button statechart diagram
Lesson checkpoint

In this lesson, you learned:
  • How a statechart defines behavior in a model
  • How to create a statechart

In the next lesson, you generate code for your model.

< Previous | Next >

Feedback