www.ilogix.com © i-logix 1999-2004 rhapsody c++ v5.0.1 17/01/2004 e1-1 exercise 2 : count down

52
www.ilogix.com © I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-1 Exercise 2 : Count Down

Post on 20-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-1

Exercise 2 : Count Down

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-2

Copying a Project

Select “File->Save As” Press to select the work folder Press to create a new folderשימו לב !!!!!!! Rename “New Folder” to “CountDown” Select the new folder “CountDown” Save the project as CountDown.rpy The new CountDown project is opened in Rhapsody with the

previous workspace preserved

Each time there is auto-save, Rhapsody will only save just what has changed since the last auto-save.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-3

Loading a Rhapsody Project

Either start Rhapsody and select “File->Open …” Or double-click on the CountDown.rpy file Or start Rhapsody and drag the .rpy file into Rhapsody

The Rhapsody.ini file determines which Rhapsody (C / C++ / J / Ada) will be opened on double-clicking the .rpy file.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-4

Adding an Attribute

To add an attribute double-click on the Display class to bring up the features and select the Attributes tab

Click on <New> to add an attribute count of type int Set the initial value to 0

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-5

Generated Code for an Attribute

Save then edit the code for the Display class

Accessor

Mutator

Initial ValueProtected attribute

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-6

What’s an Accessor and Mutator?

By default, all attribute data members in Rhapsody are protected

If other classes need access to these attributes, then they must use an Accessor ex: getCount() or Mutator ex: setCount()

This allows the designer of a class, the freedom to change the type of an attribute without having to alert all users of the class. The designer would just need to modify the accessor and mutator.

In most cases, attributes will not need accessors or mutators, we’ll see later how to stop them being generated

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-7

Attribute Visibility

Changing the Visibility in the Attribute features dialog changes the mutator and accessor visibility, (not the data member visibility).

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-8

CG:CGGeneral:GeneratedCodeInBrowser

Propertyברמת הפרויקט סמן/י את ה •CG:CGGeneral:GeneratedCodeInBrowser

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-9

Adding an operation

Using the features for the Display class, select the operations tab and add a new “Primitive Operation” called print

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-10

Arguments

Double-click on print to open the features for the print operation

Add an argument “n” of type int

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-11

Adding Implementation

Select the “Implementation” tab for the print operation and add:

std::cout << “Count = ” << n << std::endl;

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-12

Another print operation

In a similar way, add another operation called print, this time with an argument “s” of type char* and with implementation:

std::cout << s << std::endl;

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-13

Operation isDone()

Add another operation called isDone that returns a bool and has the following implementation: return (0==count);

By typing 0==count instead of count==0, enables the compiler to detect the common error of where = is typed instead of ==

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-14

Active Code View

Switch on the “Active Code View”

The active code view is context sensitive and is automatically updated as the model is changed. The window also changes dynamically to show the generated code for the highlighted model element.

Note that although leaving the active code view always open is useful, it does slow down model manipulation since the code will get regenerated anytime any model element gets modified.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-15

Using the print operation

In the Active Code View, (Make sure you’ve selected the Implementation), change the code for our constructor to use the print operation

Change the focus to another window such as the browser and check that this modification has been automatically round-tripped

Save Generate / Make / Run

Implementation

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-16

Adding a Statechart

We’d like to get our Display class to count down from 2 to 0 in intervals of 200ms

In order to do so, we need to give some behavior to our class, we can do this by adding a statechart

Right-Click on the Display class and select “New Statechart”

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-17

Simple Statechart

Draw the following statechart:

actions

timeout

guardsterminator

condition connector

transition

state

default transition2;

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-18

Transitions

Once a transition has been drawn, there are two ways in which to enter information

In text format ex: [isDone()]/print(“Done”); By the features of the transition (activated by double-clicking

or right-clicking on the transition)

An empty line forces the action to appear on a new line

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-19

The Timer Mechanism

Rhapsody provides a timer that can be used within the statecharts

tm(200) acts as an event that will be taken 200ms after the state has been entered

On entering into the state, the timer will be started On exiting from the state, the timer will be stopped

The timer uses the OS Tick and so will only generate timeouts that are a multiple of ticks.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-20

Timeouts

If we have a system tick of say 20ms and we ask for a timeout of 65ms, then the resulting timeout will actually be between 80ms and 100ms, depending on when the timeout is started relative to the system tick.

If precise timeouts are required, then it is recommended to use a hardware timer in combination with triggered operations. We’ll talk about this later

time

System tick20ms

80ms ≤ Timeout ≤ 100ms

Start timeout End timeout

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-21

Counting Down

Save Generate / Make / Run

constructor

default transition

Don’t forget to close this window, before doing another generate / make / run !

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-22

Statechart Symbol

Now that the Display class is “Reactive”, it has a special symbol in both the browser and the OMD.

Also note that the Statechart appears in the browser.

A Reactive class is one that reacts to receiving events or timeouts.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-23

Generated Code : Display.h

Use the “Active Code View” to examine the generated code for the Display class.

Note that the Display class inherits from OMReactive, which is one of Rhapsody’s framework base classes. This is a class that simply waits for timeouts or events. When it receives a timeout or an event, it calls the rootState_dispatchEvent() operation.

Framework includes

Framework class

Thread on which to wait

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-24

Generated Code : Display.cpp

Display::Display(OMThread* p_thread) The constructor needs to know on which thread to wait

Display::rootState_entDef() Will be called by startBehavior() to take the initial default transition

Display::rootState_dispatchEvent(short id) Gets called whenever the object receives an event or timeout

Display::initStatechart() Initializes the attributes used to manage the Statechart

Display::startBehavior() Kicks off the behavior of the Statechart, invokes the

rootState_entDef() operation

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-25

Statechart Implementation

Select the features for the configuration Release

Select the Settings tab and set “Statechart Implementation” from Flat to Reusable

Save / Generate / Examine code

The Rhapsody framework allows two ways of implementing statecharts:

• Reusable which is based on the state design pattern where each state is an object. Results in faster execution and if a lot of statecharts are inherited can result in smaller code.

• Flat which uses a switch statement. Results in less code that is easier to read, but is slower.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-26

Extended Exercise

Experiment with the line shape of transitions

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-27

Design Level Debugging

Up to now we have generated code and executed it, hoping that it works! However as the model gets more and more complicated we need to validate the model

From now on we are going to validate our model by doing design level debugging, this is known as “Animation”

(First we will use tracer)

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-28

Section 2

Basic levelTracing

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-29

New Configuration for Tracing

Create a new configuration Trace

Create an initial instance of Display

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-30

Tracing Settings

In the settings set the instrumentation to Tracing

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-31

Executing in Tracing Mode

Select the Trace configuration Save / Generate / Make / Run When the DOS Console window appears type help

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-32

Tracing Step by Step

The tracing mode can be used to control and to capture what happens during execution.

Type go idle to start the execution איזה שורת בTracer היו מופיעים עם לא היה Statechart? איזה שורת בTracer היו מופיעים עם ה Statechart?היה ריק

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-33

Time Stamping

Type timestamp to switch on the time stamping Type go idle

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-34

Tracing the Methods

Type trace #all method to trace just the executed methods Type go idle

By default Rhapsody traces everything. This is the same as the command trace #all all.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-35

Capturing the Trace

Type output myTrace.txt Type go idle

Type go Exit the DOS console window

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-36

Examining the Trace

Edit the generated trace file C:\Rhapsody\Work\Countdown\Test\Trace\myTrace.txt

Note that the isDone() operation is invoked before the print() operation.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-37

Extended Exercise

Rerun Test.exe and experiment with other trace commands such as:

show Display[0] attributes break Display[0] attribute[count] ... More detailed help about the trace mode can be found in the

reference guide.

The trace is useful for doing regression tests and for debugging purposes because it uses standard IO instead of TCP/IP.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-38

Animation

Create a new configuration by copying the Release configuration. Hold the control key and drag the Release configuration onto the “Configurations” folder

Rename the new configuration Debug Set the instrumentation to “Animation”

Animation

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-39

Multiple Configurations

Now that we have more than one configuration, we must select which one we want to use

There are two ways to do this: Either select the configuration using the following pull-down

box

Or right-click on the configuration and select “Set as Active Configuration”

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-40

Animating

Make sure that the active configuration is “Debug” before doing Save then Generate / Make / Run

Animation Bar

Event Queue

Call Stack

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-41

Animation Tool Bar

Go step Go Go Idle Go Event Animation Break Command Prompt Quit Animation Threads Breakpoints Event Generator Call Operations Watch – Display Continuous Update

When grayed out, indicates asingled-threaded application

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-42

Starting the Animation

Go Step Note that the Display constructor appears in the Call Stack Continue to Go Step until the “Executable is Idle”

message appears in the Animation window

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-43

Animated Browser

In the browser, note that there is now an instance of the Display class

Open “Features in New Window” for this instance and note that the attribute count has been initialized to 10

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-44

Animated Statechart

Right-click on the instance to “Open Instance Statechart”

Highlighted state

If you don’t see a highlighted state, then perhaps you are looking at the statechart of the class rather than the statechart of the instance

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-45

Go Idle / Go

Repeatedly do Go Idle or Go and watch the animation until the instance is destroyed.

Note that the value of the attribute count also changes and that the transition taken in the statechart is highlighted

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-46

Go Idle / Go

מה משמעות של הצבעים השונים בanimated statechart?

Note that the value of the attribute count also changes and that the transition taken in the statechart is highlighted

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-47

Destructor

Exit the animation and add a Destructor to the Display class with implementation

Save then Generate / Make / Run

print(“Destroyed”);

Make sure you enter the code into the Implementation and not the Description field !

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-48

Sequence Diagrams

From the browser, create a new sequence diagram called “execution”. We will use this sequence diagram to capture what happens as we execute.

For this example it doesn’t matter if we select “Analysis” or “Design” mode, we’ll explain the difference later

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-49

Adding Instances

Drag the Display class from the browser onto the sequence diagram and add a System Boundary

System Boundary

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-50

Drawing a Sequence Diagram

Normally we would describe an actual scenario not dissimilar to this one here, however in this case, we are more interested in capturing what actually happens, so we’ll leave that until tomorrow.

For our purposes, we only need the system border and the Display instance line. There’s no need to add any operations.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-51

Animated Sequence Diagrams

Start the animation If there is a sequence diagram open, then Rhapsody will

create a new animated sequence diagram based on it.

www.ilogix.com© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004 E1-52

Extended Exercise

Try adding an extra state “pausing”. Then you’ll be able to see the instance changing states.