Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-1 Section 4 Advanced Level Multiplicity.

Similar presentations


Presentation on theme: "Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-1 Section 4 Advanced Level Multiplicity."— Presentation transcript:

1 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-1 Section 4 Advanced Level Multiplicity

2 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-2 Containers A container is a collection of objects. Rhapsody uses containers for managing all “1 to many” and “many to many” relations. The containers are implemented using template classes. The Rhapsody containers are called OMContainers. Rhapsody also provides containers called OMUContainers that don’t use templates.

3 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-3 Relations The following table shows how Rhapsody implements relations:

4 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-4 OMCollection

5 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-5 OMList

6 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-6 OMMap

7 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-7 Iterators An iterator is a friend class to a container, that is used to keep a pointer to the current element in the container. Iterators are needed so that many classes can have concurrent access to the same container.

8 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-8 Using OMIterator Rhapsody provides an OMIterator class that can be used as follows to iterate through a container: OMCollection itsSensor; // a container OMIterator iSensor(itsSensor); iSensor.reset(); // point to first while ( *iSensor != NULL ) { (*iSensor)->print();// print ++iSensor;// point to next }

9 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-9 Other Libraries Other libraries can be used instead of OMContainers and OMString, such as : –STL (Standard Template Library) ( See the appropriate Specialist section for more information ). –Rhapsody OMUContainers ( This is a container set similar to OMContainers except that templates are not used, this can help reduce code size ). It is also possible to add your own container set or integrate a third party container set such as the RogueWave libraries.

10 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-10 Using OMUContainers To use the Rhapsody containers that don’t use templates, select the OMUContainers:

11 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-11 Using OMUIterator Rhapsody provides an OMUIterator class that can be used as follows to iterate through a container: OMUCollection itsSensor; // a container OMUIterator iSensor(itsSensor); iSensor.reset(); // point to first while ( *iSensor != NULL ) { (static_cast (*iSensor))->print(); ++iSensor; // point to next } Note the static_cast to convert from void* to Sensor*

12 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-12 Section 4 Advanced Level Collection

13 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-13 Collection of Sensors Load the “Virtual” project and save as “Collection” Delete from Model the relations between the Motor and the Sensors. Check in the Browser that these relations have been deleted from the model not just the view. Add a directed aggregation itsSensor from the Motor to the Sensor. Set Multiplicity to * (many).

14 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-14 OMCollection Delete implementation of Motor constructor Save / Generate / Examine code for Motor Note that the relation has been implemented as a collection of Sensors: OMCollection itsSensor; Note also that there is an operation addItsSensor(Sensor* p_Sensor);

15 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-15 Adding to OMCollection In the motor constructor add Sensors: –addItsSensor(new TemperatureSensor(“Sensor1”,CELSIUS)); –addItsSensor(new TemperatureSensor(“Sensor2”,FAHRENHEIT)); –addItsSensor(new PressureSensor(“Sensor3”) );

16 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-16 Dependencies In order to compile, the Motor needs to include the Pressure and Temperature Sensors header files. To do this we will add dependencies from the Motor to those classes: Double-click on each dependency and select the stereotype Usage.

17 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-17 Implementation Includes Alternatively instead of drawing dependencies, we can just modify the properties for the Motor class and for CPP_CG->Class->ImpIncludes add TemperatureSensor.h,PressureSensor.h CPP_CG means C++ Code Generation.

18 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-18 Multiple Relation Save / Generate / Make / Run Show that the Motor has a collection of three Sensors.

19 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-19 Statechart Create a simple Statechart for the Motor class that calls a pollSensors() routine every two seconds.

20 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-20 pollSensors() Create the pollSensors() operation that will poll all the Sensors in the collection: Note that if any more Sensors of any other type are added, the operation still functions! OMIterator iSensor(itsSensor); for ( iSensor.reset(); *iSensor; ++iSensor ) { (*iSensor)->print(); (*iSensor)->read(); } cout << "---------------------" << endl;

21 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-21 Output Save / Generate / Make / Run Verify the output The syntax for an iterator is a little complex, but it’s used in the same way every time.

22 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-22 Sequence Diagram Create a sequence diagram entitled “What we expect” as shown in the next slide. Create the instances by dragging the classes onto the sequence diagram from the browser. Right click on each TemperatureSensor line to bring up the features where the instance name can be entered. Note that an other way is to drag instances rather than classes onto the sequence diagram. Now that we have several instances of a class, on a sequence diagram we have to indicate the instance name and not just the class.

23 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-23 Another way is to drag instances from the browser during animation. (Drag the instances from TemperatureSensor and the PressureSensor classes not the Sensor class). Sequence Diagram

24 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-24 Animated Sequence Diagram Save / Generate / Make / Run. Animate and check that what we expect is what we get. When executing the code, then only if a SD is open, will an animated SD will be created.

25 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-25 When executing the code, then only if a SD is open, will an animated SD will be created. Comparing Sequence Diagrams Use “Tools>Sequence Diagram Compare” to examine the differences between “What we expect” and “Animated What we expect”

26 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-26 Note the differences are highlighted in Magenta. The Comparison

27 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-27 Interpreting the Comparison As we can see the result is not as we expected, (the order of constructing the instances is different). This is because, Rhapsody does not consider an object to exist until it’s constructor has terminated, this is why all the constructors come from the system border.

28 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-28 Using the Comparison Options We can set an option so that the constructors are not compared.

29 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-29 Using the Comparison Options We can set an option so that the constructors are not compared.

30 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-30 The Modified Comparison Note the differences are now grayed out.

31 Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-31 Extended Exercise For the Sensor class, add a static attribute numberOfSensors of type int with initial value 0. In the Sensor Constructor add numberOfSensors++; For the Sensor class, add a virtual Destructor with implementation : numberOfSensors--; Add the following to pollSensors() cout << “Number of sensors = “ << Sensor::getNumberOfSensors() << endl; Generate code and execute to check that numberOfSensors = 3.


Download ppt "Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001 Adv-1 Section 4 Advanced Level Multiplicity."

Similar presentations


Ads by Google