section 4 : Advanced

Download Report

Transcript section 4 : Advanced

Essential Rhapsody in C++
Section 4
Advanced
Level
Adv-1
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4: Advanced
•
•
•
•
•
•
•
•
•
Triggered Operations
Event Parameters
Event Hierarchy
Strings / Containers & Iterators
Packages
Concurrency
Configuration Management
Useful tips
Qualified Associations
Adv-2
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Triggered Operations
Adv-3
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Triggered Operations
• Triggered operations are a way of tuning a
model for rapid response.
• They act as events except that they are
executed immediately rather than being
queued and executed later as for events.
• They do not have a body.
• The reply() operation allows the triggered
operation to return a value.
Care should be taken not to mix triggered operations
with events in the same statechart since there would
be potential race conditions.
Adv-4
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Race Condition
Adv-5
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Project Triggered
• Create a new project “Triggered” and in an
OMD, draw a composite class Motor
containing one instance of Sensor.
• Add a triggered operation opRead for the
Sensor class.
Adv-6
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
opRead
• Set the return type to int
Adv-7
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Sensor Statechart
• Add the following
statechart to the
Sensor class.
• Add a getStatus
operation to the
Sensor class that
returns a random
number as an int.
Adv-8
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Motor Statechart
• Add the following statechart to the Motor class.
• Also add an attribute status of type int
Adv-9
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Animating
• Save
/ Generate / Make / Run
• Check that the attribute status changes
every two seconds.
Adv-10
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Generated Code
Adv-11
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Extended Exercise
• If the triggered operation is called when in the
active state then we could return a status of -1.
• Add a reaction in state for the active state so
that if opRead is received, -1 is replied.
Adv-12
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Event Parameters
Adv-13
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Event Parameters
• Events ( and Triggered Operations ) can be
passed parameters
• Rhapsody provides a pointer called params to
access these parameters.
• For instance if an event evGo has a parameter
n then n can be accessed using params->n
Adv-14
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Project Parameters
• Create a new project “Parameters” and in an
OMD, draw a simple class Motor with
statechart as shown:
Note that GEN(evGo)
broadcasts the event
evGo to itself.!
Adv-15
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Adding a Parameter
• Add an argument n of type int to the event
evGo.
• Add an attribute cycles of type int.
• Create a constructor to initialize cycles to 0.
Adv-16
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Exercise
• Check that execution is as expected.
• Why does the output increment by 2 ?
Adv-17
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Event Hierarchy
Adv-18
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Event Hierarchy
• It is possible for events to inherit from
other events.
• In this example, evCommsError,
evDivZeroError and evOutOfMemError
all inherit from evError.
Adv-19
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Client Generating Events
• The client
sends
one of
each
events to
the server
Adv-20
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Server Handling Events
• Note how each event is handled.
Adv-21
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Strings
Adv-22
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
OMString Class
• Rhapsody provides a string class that we
have already used.
• This class allows operations such as:
• stringA = stringB
• stringA += stringB;
• length = string.GetLength();
• character = string[5];
Note that the GetLength operation starts with a
capital ‘G’ which is contrary to the normal style !
Adv-23
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
OMString Class
• This class reserves memory off the heap in
blocks of 256 bytes and will add extra memory
if needed during an operation such as
stringA += stringB.
• The operation setDefaultBlock(n) allows the
block size to be changed.
Adv-24
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Multiplicity
Adv-25
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-26
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Relations
• The following table shows how Rhapsody
implements relations:
Multiplicity
Property
Container Type
Implementation
0..1 or 1
scalar
Pointer
m..n, n or *
Unordered
OMCollection
n
Static Array
Array
Array of Pointers
m..n, n or *
ordered
Ordered
OMList
Qualified
OMMap
Qualifier
Adv-27
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
OMCollection
Adv-28
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
OMList
Adv-29
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
OMMap
Adv-30
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-31
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Using OMIterator
• Rhapsody provides an OMIterator class that
can be used as follows to iterate through a
container:
OMCollection<Sensor*> itsSensor; // a container
OMIterator<Sensor*> iSensor(itsSensor);
iSensor.reset();
// point to first
while ( *iSensor != NULL ) {
(*iSensor)->print();
// print
++iSensor;
// point to next
}
Adv-32
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-33
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Using OMUContainers
• To use the Rhapsody containers that don’t
use templates, select the OMUContainers:
Adv-34
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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<Sensor*>(*iSensor))->print();
++iSensor;
// point to next
}
Note the static_cast to convert from void* to Sensor*
Adv-35
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Collection
Adv-36
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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).
Adv-37
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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<Sensor*> itsSensor;
• Note also that there is an operation
addItsSensor(Sensor* p_Sensor);
Adv-38
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Adding to OMCollection
• In the motor constructor add Sensors:
– addItsSensor(new TemperatureSensor(“Sensor1”,CELSIUS));
– addItsSensor(new TemperatureSensor(“Sensor2”,FAHRENHEIT));
– addItsSensor(new PressureSensor(“Sensor3”) );
Adv-39
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-40
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-41
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Multiple Relation
• Save
/ Generate / Make / Run
• Show that the Motor has a collection of three
Sensors.
Adv-42
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Statechart
• Create a simple Statechart for the Motor class
that calls a pollSensors() routine every two
seconds.
Adv-43
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
pollSensors()
• Create the pollSensors() operation that will
poll all the Sensors in the collection:
OMIterator<Sensor*> iSensor(itsSensor);
for ( iSensor.reset(); *iSensor; ++iSensor ) {
(*iSensor)->print();
(*iSensor)->read();
}
cout << "---------------------" << endl;
Note that if any more Sensors of any other type are
added, the operation still functions!
Adv-44
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-45
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-46
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Sequence Diagram
Another way is to drag instances from the browser during
animation. (Drag the instances from TemperatureSensor
and the PressureSensor classes not the Sensor class).
Adv-47
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-48
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Comparing Sequence Diagrams
• Use “Tools>Sequence Diagram
Compare” to examine the
differences between “What we
expect” and “Animated What
we expect”
When executing the code, then only if
a SD is open, will an animated SD will
be created.
Adv-49
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
The Comparison
Note the differences are highlighted in Magenta.
Adv-50
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-51
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Using the Comparison Options
• We can set an option so that the constructors
are not compared.
Adv-52
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Using the Comparison Options
• We can set an option so that the constructors
are not compared.
Adv-53
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
The Modified Comparison
Note the differences are now grayed out.
Adv-54
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-55
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Packages
Adv-56
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Packages
• A package is a convenient way of grouping
classes together.
• It is good practice not to use the Default
package. (This makes it easier to import
packages from other models without getting
name conflicts).
• In our example we are going to put all the
Sensors in a Sensor Package “SensorPkg”
and the Motor in a Motor Package “MotorPkg”.
Adv-57
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Domain Diagram
• Starting with the previous project (Collection),
create a new Object Model Diagram called
“Domain diagram”
• Draw two packages called SensorPkg and
MotorPkg.
Adv-58
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Moving Classes Between Packages
• There are two ways of moving classes from
one package to another.
• Drag from the Browser onto the Domain
Diagram, the Sensor class into the SensorPkg
and the Motor class into the MotorPkg.
• Drag the TemperatureSensor and the
PressureSensor within the browser from the
default to the SensorPkg package
We do not have to show all the classes in a package
on an OMD
Adv-59
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
The Domain Diagram
Adv-60
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Completing the Relations
• Complete the relations in the Domain Diagram
Adv-61
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Configuration Scope
• Move the tTempUnits type to the
SensorPkg.
• We can modify the component
so that it includes just these new
packages and not the Default
package which should be empty.
Adv-62
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Diagram Default Package
• Note that all the OMD’s and SD’s can have a
default package. Setting this to a particular
package means two things:
• First all new classes drawn on that diagram
will be in that package.
• Secondly any class on the diagram, not in that
package, will display its package scope.
Not to be confused with the package called Default !
Adv-63
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Overview Diagram
• Change the Default package for the Overview
diagram to the SensorPkg.
Package scope
Adv-64
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Save / Generate / Make / Run
• Save
/ Generate / Make / Run
check that our model still works.
to
Note also that the sequence diagrams now show package scope.
Adv-65
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Abstract Class
• Set the read() operation of the Sensor class
to be Abstract by setting the property Kind:
Note that the read operation
is pure virtual. This means
that if we try to create an
instance with “new Sensor”,
the compiler will generate an
error.
Adv-66
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Stereotypes
• For the Sensor class set the Stereotype to
Interface
Adv-67
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Stereotypes
• For the Sensor class set the Stereotype to
Interface
Adv-68
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Nested Packages
• Packages can be nested inside other packages.
• Diagrams can also be moved inside packages
• In our example, create a package OurPkg and
move everything that we have done so far into it.
Nested packages
Diagrams in
a package
Adv-69
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Main Diagram
• For classes and packages, we can
select a Main Diagram, this is very
useful for navigation in a model.
• In our model, set the Main Diagram for
the SensorPkg and the MotorPkg to be
the Overview diagram.
• Now we can navigate from the Domain
Diagram to the Overview diagram, by
“Open Main Diagram”.
Adv-70
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Extended Exercise
• Set a namespace for the package
Adv-71
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Using Namespace
• Set the ImplementationProlog property for the
Motor class to :
using namespace SensorPkg;
• Save
Adv-72
/ Make / Generate / Run
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Concurrency
Adv-73
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Code Generation Per Package
• Generated code for each package can be
directed into separate directories:
Adv-74
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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:
Adv-75
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Active Classes
• With the browser, change the concurrency of
the Sensor class from sequential to active.
Adv-76
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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 indicating
inherited behavior
Adv-77
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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 !
Adv-78
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-79
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Suspending Threads
• Note that a thread can be suspended.
Adv-80
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Extended Exercise I
• Add two new operations to the Motor class
• deleteSensor & addSensor
Adv-81
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Extended Exercise II
• Modify the Motor Statechart to add two static
reactions in state for evAdd and evDelete.
Adv-82
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Extended Exercise III
• Save
/ Generate/Make/Run
• Inject events evAdd and evDelete
• Note that threads are created & deleted.
Adv-83
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Extended Exercise IV
• Try adding a SpeedSensor that is a new
type of Sensor.
Adv-84
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-85
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-86
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
The Improved Design
Adv-87
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
The Singleton Design Pattern I
Static attribute
Protected
constructor
Adv-88
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
The Singleton Design Pattern II
Static factory
operation
Calling the
createRandomSensor
operation
Adv-89
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
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.
Adv-90
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Setting Priority, Stack Size, ...
• For an active class, the stack size, priority,
name, etc can be set through the following
properties:
Adv-91
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced
Mutexes
Adv-92
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Mutexes
• Rhapsody provides OMProtected which is a
mutex (or binary semaphore) for protecting
consecutive access to devices.
• OMProtected can be used as follows:
– attribute
OMProtected myMutex;
– free
myMutex.free();
– lock
myMutex.lock();
Adv-93
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Animating Mutexes
• Rhapsody cannot display in animation, the
value of attributes of type OMProtected.
• In order to use animation, the property
AnimateAttributes for the attribute must be
set to FALSE.
Adv-94
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Animating Attributes
• For animating attributes of a user defined type,
the same problem can occur.
• To allow animation:
– either set the property AnimateAttributes to FALSE
– or add a global function operator<< for the user
defined type
ostream& operator<<(ostream& out, const tDate& d) {
out << d.day << "/" << d.month << "/" << d.year;
return out;
};
– Or use the InstrumentationFunctionName
Adv-95
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Instrumentation Function Name I
• To animate an attribute of type tTempUnits that
is an enum of CELSIUS and FAHRENHEIT :
• Add a function showTempUnits as follows :
Adv-96
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Instrumentation Function Name II
• Set the Animate and AnimateArguments
properties for this function to False :
Adv-97
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Instrumentation Function Name III
• For each attribute of type tTempUnits, set the
InstrumentationFunctionName to
showTempUnits
Adv-98
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Instrumentation Function Name IV
• To generate this
operation only during
animation, the
following properties
can be set for this
function.
New line
Adv-99
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Instrumentation Function Name V
Adv-100
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Unguarded Access to Adc::read()
• In the following example, each Sensor calls
the Adc::read() operation to measure Voltage
/ Temperature or Pressure. The read
operation selects the appropriate multiplexor
input and does eight serial reads on DataIn.
Adv-101
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Unguarded Behavior
Current read()
operation is
interrupted by
another.
Adv-102
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Guarding Operations
• Rhapsody can use the mutex OMProtected to
guard access to a particular operation to stop
concurrent access.
• This can be done by setting the property
concurrency from sequential to guarded.
All operations in a same class, that are
guarded will be guarded by the same mutex.
Adv-103
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Guarded Behavior
Concurrent
read()
operation is
blocked by
mutex and
is executed
when current
read() ends.
Adv-104
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Guarded Operation : Code
Adv-105
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Guarding Relations
• Rhapsody can also use the mutex
OMProtected to guard access to a particular
relation to stop concurrent access.
• This can be done by setting the property
isGuarded from none to all ( mutator &
accessor operations ) or just mutator.
Adv-106
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Configuration Management
Adv-107
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Configuration Management
• Rhapsody can attach to many CM tools such
as PVCS, ClearCase, SourceIntegrity …
• The connection can be done by using either
the SCC interface or the command line
interface. The selection is made in the project
properties (or setup in the site.prp file)
• File->Configuration Items… opens the CM
interface
• Rhapsody checks in and out parts of the
model, these parts are called Units.
• Units checked out without a lock are ReadOnly.
Adv-108
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
CM Units
• There are eight types of units that can be checked
in/out with the CM tool:
– packages
.sbs files
– object model diagrams
.omd files
– sequence diagrams
.msc files
– components
.cmp files
– use case diagrams
.ucd files
– classes, actors & use cases
.cls files
– collaboration diagrams
.clb files
– component diagrams
.ctd files
Adv-109
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Units
• Each unit can be seen in the browser as
having the symbol .
• By default classes are not “Units”, to be able
to check in/out a single class, select the class
in the browser, right-click and select “Edit
Unit”.
Class is a unit
Adv-110
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Using the SCC Interface
Adv-111
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Connecting to an archive
• The first time that the user connects
to an archive, the SCC tool will
allow a CM archive to be selected.
Some tools will also allow a new CM
archive to be created.
• Each time the Rhapsody project is
opened, it is necessary to reconnect
to the CM archive
.
Adv-112
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
SCC : CM Commands
•
•
•
•
•
•
•
•
•
Adv-113
Connect to Archive
Show items in Archive
Fetch from Archive
Checkout items
Uncheckout items
Check in items
Add items to Archive
File History for items
File properties for items
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Using the Command Line Interface
Adv-114
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
CM Commands
•
•
•
•
•
•
•
•
•
Adv-115
Connect to archive
List items in archive
Check out selected items
Check in selected items
Add items to archive
Synchronize items
Lock items
Unlock items
Diff with Rhapsody
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Directly calling CM Operations
Adv-116
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Diff and merge I
• The Diff and merge tool can be invoked
by running the executable
“DiffMerge.exe” or if the command line
interface is used, then can be invoked
directly from the CM Interface.
Adv-117
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Diff and merge II
Adv-118
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Diff and merge III
Adv-119
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Add to Model I
• Add to model can be used to import part of
another model.
• The model can either be added to the model
or simply referenced.
• By using packages other than the default
package, name clashes can be avoided
during adding to model.
Adv-120
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Add to Model II
• Bringing in part of the previous project
browse
Choice
of unit type
add
copy
or
reference
Adv-121
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Unresolved Parts of Model
• When using Configuration Management, it is
possible that there are some unresolved
references in the model.
• These are indicated by (U).
• In the following example on the next slide,
just the diagram and one package have been
added to a model. The other packages
referred to in the OMD are unresolved.
Adv-122
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Unresolved Parts of Model
(U)
Adv-123
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Useful Tips
Adv-124
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Useful Tips I
 Keep statecharts simple, if they are too complex then
divide the object into several smaller ones or use substatecharts.
 Draw states as small as possible and as close as
possible to other states. This helps greatly during
animating, since they are more readable, especially
when there are several diagrams open at the same
time.
 Don’t call many actions on a statechart, it makes the
diagram very difficult to read. Instead call a new
operation that calls the actions.
Adv-125
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Useful Tips II
 Don’t start a project until the ‘unguided examples’ in
the “Specialist” section have been completed
successfully.
 Don’t give the same name to several different
elements of the model ex: package and class. The
generated files will overwrite each other.
 When working with large projects, it is essential to
divide up the model into domains. The interfaces
between these domains should be defined. Then work
can be carried out simultaneously and independently
by different teams on each domain.
Adv-126
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Useful Tips III
Don’t put everything onto a single diagram,
this can make reading the diagram difficult.
Using a couple of diagrams, depicting different
views can really help in understanding the
model.
As a rule of thumb, there should be 7 ± 5
graphical objects ( state / class ) per diagram.
To aid navigation within the model, set up a
main diagram for every class and package.
Adv-127
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Useful Tips IV
 Use an iterative approach similar to that taken during
this training, constantly model / save / generate / make
/ run. Don’t wait until the model is too large before
animating.
 Make use of the Framework Base classes (
OMReactive, OMEvent, … ) to help de-couple classes.
 Beware of using names such as res, id, execute, start,
suspend, resume, event, since OMThread &
OMReactive use these operations. This can lead to
problems that are very hard to track down.
Adv-128
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Useful Naming Conventions
Start Event names with ev
– ex: evStart
Start Triggered operation with op
– ex: opPress
Start Condition operations with is
– ex: isPressed()
Start Interface classes with a I
– ex: IHardware
These conventions help when reading diagrams.
Adv-129
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Remote Animation
Adv-130
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Remote Animation
• An executable can be run
on a remote PC from the
host connected via a TCP/IP
link.
• In the properties sheet for
Microsoft, set the Remote
Host, to either the IP
address for the host, or to
the host name as shown in
the next slide.
• Set UseRemoteHost to
TRUE.
Adv-131
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Remote Animation
• Generate an executable and transfer it to the
target PC.
• With the project loaded into Rhapsody on the
host, start the executable on the target
• The animation bar should appear on the host
and animation can proceed as if the code was
running on the host
• This can be very easily demonstrated with two
networked PC’s
Adv-132
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced Level
Qualified Associations
Adv-133
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Qualified Associations
• Create a new project called “Qualified”
• Create an OMD containing two classes Airline
and Member.
• Add attributes name, frequentFlyerNumber and
miles to the Member class.
• Add a directed association from the Airline to
the member with multiplicity *.
• Set the Qualifier to be the
frequentFlyerNumber
attribute.
Adv-134
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Qualified Associations
Adv-135
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Qualified Association
• Create a component
and configuration and
generate code.
• Examine the code.
map
Adv-136
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Using Qualified Associations
• Knowing the frequentFlyerNumber, the Airline
class can access the appropriate Member as
follows.
Adv-137
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Prioritized List Example
• The example PrioritizedList shows how a
qualified association can be used to manage a
list of prioritized commands:
• In this example, the Writer creates commands
with a specific priority and passes them to a
CommandList class that inserts them into a list
according to the priority.
• The Reader asks the CommandList to read
the next highest priority message.
• The Reader and Writer run on separate
threads with different priorities.
Adv-138
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Prioritized List Example
Note that the property
UseAsExternal is set to
TRUE.
Mutex used to protect
from simultaneous access
Qualified
association
Adv-139
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Displaying Bitmaps
• To display a bitmap on a class,
add a stereotype ex: Coffee
• Add a bitmap with the same name as the
stereotype ex: Coffee.bmp to the folder
Rhapsody\Share\PredefinedPictures
• With the Display Options for the class, set
Show stereotype to Icon
Adv-140
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Section 4
Advanced
Global Instances
Adv-141
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Global Instances
• There are two ways of creating global
instances.
– In the browser, for a package, add a Global
instance
– Create a class and check the box “This box is also
an instance”.
• Examine the Rhapsody generated code in the
package file: default.cpp
• Any relations between global instances will be
initialized by Rhapsody.
Note that the second “visual” method is preferred.
Adv-142
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Global Instances
Instance added
directly in
browser
Instance added
via OMD
Adv-143
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001
Adv-144
Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2000 v3.0 1/29/2001