Exercise 3 : Dishwasher

advertisement
Exercise 3 : Dishwasher
active
off
evStart[IS_IN(normal)]/
setup();
on
washing
tm(1000)/
--washTime;
DONE
evOpen
open
evClose
rinsing
tm(1000)/
--rinseTime;
H
drying
[isDried()]/
cycles++;
tm(1000)/
--dryTime;
normal>
[isInNeedOfService()]
evService
service
DONE
mode
evMode
quick
faulty
evMode
intense
evService
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-1
www.ilogix.com
Exercise 3 : Dishwasher
active
off
evStart[IS_IN(normal)]/
setup();
on
washing
tm(1000)/
--washTime;
DONE
evOpen
open
evClose
rinsing
tm(1000)/
--rinseTime;
H
drying
[isDried()]/
cycles++;
tm(1000)/
--dryTime;
normal>
[isInNeedOfService()]
evService
service
DONE
mode
evMode
quick
faulty
evMode
intense
evService
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-2
www.ilogix.com
Dishwasher
 Create a new project “Dishwasher”, make sure that it is
created in its own folder.
 Draw a single class Dishwasher
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-3
www.ilogix.com
Attributes
 Add the following attributes all of which are of type int and
with initial value of 0
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-4
www.ilogix.com
Operations
 Add the following operations,
with appropriate return types
and implementations:
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-5
www.ilogix.com
Types
 In the implementation for the operation
isInNeedOfService() we used a
constant called MAX_CYCLES.
 Select the Default package, right-click
and select “Add New -> Type”
 Enter MAX_CYCLES as name and
declaration: const int %s = 3;
%s is a shortcut for the name of the type
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-6
www.ilogix.com
Creating a Component
 As in the previous exercise, rename the DefaultComponent
to Test and the DefaultConfig to Debug
 Select “Animation” and create an initial instance of
Dishwasher
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-7
www.ilogix.com
Save / Generate / Make
 Before adding a statechart, let’s make sure that we haven’t
made any errors by doing a build.
 Save
Generate / Make
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-8
www.ilogix.com
Creating a Statechart
 Add a Statechart to
the Dishwasher
class
 We are going to be
drawing a complex
diagram, it is highly
recommended to
maximise the
Statechart window
and close all other
windows.
 Draw a single state
as large as possible
called active
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-9
www.ilogix.com
Creating Concurrent States
 Use the and-line icon
This box with
“active”inside will
appear when the
and-lines are drawn
Concurrent States
to create concurrent states
To draw the AND line, either:
click and drag the line or
click to start the line then
double-click to end it
Draw this line first
Note the only way to move an AND-line, is to delete it and redraw it.
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-10
www.ilogix.com
Naming the Concurrent States
 Name the concurrent states using the
icon to running,
service and mode.
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-11
www.ilogix.com
Adding Nested States
 Add the following states:
To change the size of an outer state, without the inner states
changing, hold down the “Alt” key when changing the size
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-12
www.ilogix.com
Adding History and Diagram Connectors
 Add a History connector
to the on state
 Add two identical Diagram connectors
named DONE
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-13
www.ilogix.com
Adding Default Transitions
 Add the following four default transitions
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-14
www.ilogix.com
Adding the Transitions
 Add the following transitions and actions
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-15
www.ilogix.com
Action on Entry
 Finally in the normal state, add an “Action on entry” to set
cycles=0;
Once an action
has been set,
the symbol “>”
is shown.
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-16
www.ilogix.com
Start Behavior
‫ מסוג‬Instrumentation ‫ עם‬trace ‫ הנקראת‬configuration ‫ י‬/‫ צור‬
.Tracer
?Start Behavior -‫ מה מתבצע ב‬
debug ‫ הנקראת‬configuration ‫' ב‬/‫ מכאן ולהבא תשתמש‬
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-17
www.ilogix.com
Save / Generate / Make / Run
 Save
/ Generate / Make / Run
 Do “Go step”
to create a Dishwasher instance
‫“ כמה פעמים עד ש‬Go Step” ‫י על‬/‫ לחצ‬. Call Stack ‫י את ההודעות ב‬/‫ הסביר‬
‫י את ההודעות ב‬/‫ הסביר‬.“Output Window” -‫“ מופיע ב‬Executable Idle”
Call Stack
If there is no instance created, then it is probable that an initial
instance of Dishwasher was not selected in the configuration.
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-18
www.ilogix.com
Animated Statechart
 Check that there are three concurrent animated states
1
3
2
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-19
www.ilogix.com
Injecting Events
 The Dishwasher is in an idle state waiting for some events
 Generate the event evStart by right-clicking anywhere on
the animated statechart and selecting “Generate Event”
 The event evStart should appear in the event queue
Events can also be
generated via the
Command prompt
or via the Event
Generator
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-20
www.ilogix.com
Design Level Debugging
 Do “Go”
and watch the animation
 Does your Dishwasher work as expected?
 What happens if you open the door when it’s on, does it
remember what state it was in?
 Why does the Dishwasher become faulty after four cycles?
 Can you get the Dishwasher back to the normal state?
!3 ‫עד כאן תרגיל‬
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-21
www.ilogix.com
Intense / Quick
 Modify the setup operation so
that the timings are different
in the quick and intense
modes.
 Save / Generate / Make / Run
 It should now be quicker to
get the Dishwasher into the
“faulty” state.
IS_IN is a macro that tests to see if the object is in a
particular state.
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-22
www.ilogix.com
Extended Exercise
 Using the IS_IN macro and a guard, prevent the Dishwasher
from being started if it is faulty.
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-23
www.ilogix.com
Breakpoints
 Setting breakpoints can be done in a similar way to injecting
events by right-clicking on a state in the animated statechart
Breakpoints can be added/removed via the breakpoint
icon
on the animation toolbar.
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-24
www.ilogix.com
Using the Simulated Time Model
‫השכלה כללית בלבד‬
 At the moment, we are using the System tick for all our
timeouts and so a timeout of 1000ms takes 1000ms, which
means that all simulations can be long.
 There is an alternative time model that we can use that is
the “Simulated” time model. When we use this, all the
timeouts are executed in the appropriate order, but rather
than waiting, the shortest timeout immediately times out.
This means that we can test models such as this one much
quicker.
 Make a copy of the Debug configuration, rename it to Sim
and set the “Time Model” to Simulated
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-25
www.ilogix.com
The Command Prompt
 Events and breakpoints can also be generated through the
command prompt
 For example, we could inject the evStart by typing
“Dishwasher->GEN(evStart)” in the command window
 More useful perhaps is that we can use the command
window to invoke scripts
GEN is a macro that will create the event before sending
it to the object. GEN(evStart) ≈ gen( new evStart )
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-26
www.ilogix.com
Creating a Test Script
 One way of testing our Dishwasher is by running a script
 We can create this script inside Rhapsody in the following
way:
 In the Test Component, select “Add New File”
 Name the file TestScript.cfg
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-27
www.ilogix.com
TestScript.cfg
 Set the Path to “..\..”
 Set the File Type to “Other”
The script we’re about
to create, will be contained
inside the model and will now be
generated every time that code
is generated
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-28
www.ilogix.com
Script Contents
 Select “Add text”, enter “script contents” as the name
 And then press
to start the text editor
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-29
www.ilogix.com
The Script
 Enter the following:
This script logs
everything that happens
including timing information to
a file called TestScript.log
For more information on the
scripting language, search for
example “timestamp” in the
user reference manual
Don’t leave any blank lines or
any leading spaces!
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-30
www.ilogix.com
Running the Script
 Save
/ Generate / Make / Run
 Use the command prompt
to run the script
 Once the script has run and terminated, stop the animation
 The script should have created a file called TestScript.log,
we can edit this file from within Rhapsody by adding another
file called TestScript.log of type Other and Path ..\..
If the script is called “OMAnimator.cfg”
then on starting the animation, the script will be
automatically run. This script must be located in
the project directory.
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-31
www.ilogix.com
Reading TestScript.log
 Select the TestScript.log file in the browser, right-click and
select “Edit File”
Elapsed Time
© I-Logix 1999-2004 Rhapsody C++ V5.0.1 17/01/2004
E1-32
www.ilogix.com
Download