Rhapsody Code Generation in C++ Exercise
This exercise will step you through a simple example of using Rhapsody's code generation facilities.
- Start Rhapsody for C++, and begin a new project named, Counter.
- Add a class called Counter.
- Right click on the class and select New Constructor. There will be no arguments for this constructor so OK that arguments dialog box.
- To be able to see the attributes and operations for a class, right click on the class and select Display Options. On the Attributes and Operations tabs select All. You will see the constructor in the class display. You can also see it listed in the leftside browser under Packages -> Default -> Classes -> Counter -> Operations.
- Double click the Counter constructor in either place. On the implementation tab enter an implementation for the constructor:
std::cout << "Starting the counters" << std::endl;
- Select the menu option View -> Active Code View to show the Active Code window at the bottom of the page. There is also a toolbar button to show this window. You should see code for the Counter class. To bring up an editor, right click on the Counter class and selecting Edit Code.
- Select the Counter code view tab. Make some modification to the print statement in the edit window. Transfer back to the class model and look at the implementation of the Counter constructor. You should see the roundtripping in action. Be aware that not everything can be roundtripped!
- Close the Active Code window at the screen bottom. It is sometimes useful to have it open but it slows down model manipulation because code is regenerated whenever you modify a model element.
- Right click the Counter class and open the Features dialog. On the Properties tab under CPP_CG -> Class add <iostream> to the ImpIncludes field to indicate that this class includes this header file.
- In the browser, the Components area lists things that you can build. There should be a DefaultComponent. Double click this configuration. On the General tab rename it to Test. Make sure that it's Type is Executable.
- The Test section in the browser lists different configurations that can be built such as for different environments or as release vs. debug versions. Double click the DefaultConfig and on the General tab rename it to MSCpp.
- On the Initialization tab, get a Counter object instantiated at startup by checking it under Default in Initial Instances.
- On the Settings tab under Environment Settings, select Microsoft for the Environment.
- In the browser under Object Model Diagrams, rename Model1 to Class.
- Save the project, and select Generate/Make/Run (GMR). You should see the build in progress and the executable will display its message in a console window. If an error occurs you can double click on the error and Rhapsody will take you to the location of the error -- most of the time. If the error is in an include file it will not recurse through to the exact location.
- Stop the executable by closing the console window or pressing the Stop button.
- In the leftside browser under the MSCpp configuration there should be hyperlinks to the main application file and the Makefile. Or view all the generated files in the directory ..\Counter\Test\MSCpp which is rooted in your Rhapsody directory.
- Now make a second configuration that is built for the Cygwin environment. Before you do GMR, you will need to either set this new configuration as the default (right click configuration and select Set as Active Configuration) or select it on the menu bar as the active configuration for Test.
- Look at the files for the Cygwin configuration.
- Add an integer attribute count to the Counter class with an initial value of 0. Look at the generated code. What is the visibility of the attribute, accessor, and mutator. There is an option to prevent generation of accessors and mutators. (BTW, in Rhapsody there is an option to set everything and anything. All told there are over 1,000 options that you can change!)
- Add a print operation that takes a single integer argument and prints out "Count = " followed by the argument and an end of line. Add a second print operation that takes a char* as an argument and prints that string on the line.
- Add a isDone operation that returns true when count is 0 and false otherwise.
- Change the constructor to use the new print operation.
- Save the project and GMR to make sure that there are no errors.
- Associate a statechart with the Counter class to define its behavior. The counter's behavior is:
- When the statechart starts, initialize count to 10 and print out "count initialized".
- Every 1 second print out the current value of count.
- If isDone returns true, print "Done" and terminate the statechart. Otherwise, decrement count.
- Use the MSCpp configuration as the active configuration. On the Settings tab for the configuration's Properties select the Statechart Implementation to be Reusable. This will trigger the use of the state pattern. Save the project and GMR to make sure there are no errors and that the counter works correctly.
- Look at the code that is generated. Then change to a Statechart Implementation of Flat. Generate the code and compare this switch statement implementation to the state pattern implementation.
- Rhapsody has the ability to capture program execution. Create a new configuration by highlighting an existing configuration and drag it to the Configurations folder while holding down the control key. Rename this configuration to Debug. On the Settings tab, set the Instrumentation Mode to Animation. Make this the active configuration
- Add a sequence diagram to the Counter project. Put in a system border and drag a Counter object onto the diagram. Keep the sequence diagram open.
- Save the project, and GMR. When the program runs you will see the Animation Tool Bar appear on top, and the Event Queue and Call Stack windows appear at the bottom.
- Press the Go Step button until the "Executable is Idle" message appears in the Animation Window. In the Packages section of the browser, you will see that Counter has an instance object. You can right click on this instance and select Features in New Window to get a window showing this objects instance data.
- Keep running the animation. You should see that an animated sequence diagram was created showing the details of program execution.
- Create two orthogonal regions within the statechart for the Counter class. Place the elements from the previous statechart in one region. In the second region create an identical statechart which uses a second counter attribute in the object and cycles once every 3 seconds. Initialize this counter to a value of 5. Add operations or modify existing ones in the class as needed to deal with this new counter.
- Save the model and GMR and make sure it works correctly.
Submission
Submit your final Counter source code file to the Code Generation tutorial dropbox. Make sure that your name and your partner's name are in the file, and that you enter your names in the description field when submitting the file to the dropbox.
$Id$