View
214
Download
1
Tags:
Embed Size (px)
Citation preview
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-1
Section 4
Advanced LevelConcurrency
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-2
Code Generation Per Package• Generated code for each package can be
directed into separate directories:
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-3
Concurrency
• We want each Sensor to run on its own thread (active class).
• To do so, we need each Sensor to be Reactive (class that waits for events).
• So we will create a Statechart for the base Sensor class as follows:
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-4
Active Classes
• With the browser, change the concurrency of the Sensor class from sequential to active.
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-5
Inheriting Behavior
• Open the Statecharts for the PressureSensor and TemperatureSensor. Note that they have inherited the base class Statechart.
• Specialize the behavior of the TemperatureSensor as below:
Grayed out indicatinginherited behavior
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-6
Starting the Behavior
• Add a call to startBehavior() from the TemperatureSensor and PressureSensor constructors to initialize the statecharts.
If we had used a composite class, Rhapsody would have done this for us, but that would have been too easy !
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-7
Multi-threads
• Save / Generate / Make / Run• Check that there are four active threads.• Setting the focus to a particular thread displays
the call stack and event queue for that thread.
There will always be one thread called mainThread.
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-8
Suspending Threads
• Note that a thread can be suspended.
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-9
Extended Exercise I
• Add two new operations to the Motor class• deleteSensor & addSensor
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-10
Extended Exercise II
• Modify the Motor Statechart to add two static reactions in state for evAdd and evDelete.
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-11
Extended Exercise III
• Save / Generate/Make/Run• Inject events evAdd and evDelete• Note that threads are created & deleted.
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-12
Extended Exercise IV
• Try adding a SpeedSensor that is a new type of Sensor.
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-13
Problems with the Design
• With the current design there are a few potential problems:– The Motor class needs to know about all
the different types of Sensor.– If another class wants access to the
Sensors, it too will need to depend upon all the different types of Sensor.
– Starting the behavior of a Sensor, in the constructor is not very elegant.
– Adding a new type of Sensor means finding and modifying all classes that use Sensor.
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-14
Improving the Design• Using the “factory method design pattern” will
solve all these concerns.• A SensorFactory class can be introduced that is
used by all classes ( ex: Motor ) that need to get a Sensor. This decouples the Motor class from the actual Sensors and can also start the behavior of the Sensors.
• The SensorFactory will be implemented using the “Singleton design pattern” (to ensure that there is only one instance of SensorFactory).
See the “SensorFactory” example.
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-15
The Improved Design
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-16
The Singleton Design Pattern I
Protectedconstructor
Static attribute
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-17
The Singleton Design Pattern II
Static factoryoperation
Calling thecreateRandomSensor
operation
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001Adv-18
More Design Improvements• In all the constructors of the sensors passing a
string by value is not very efficient, it would be better to pass a reference to a string.