Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2003 v4.1 01/01/2003 E2-1 Essential Rhapsody in C++ Part Two Case Study.

Similar presentations


Presentation on theme: "Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2003 v4.1 01/01/2003 E2-1 Essential Rhapsody in C++ Part Two Case Study."— Presentation transcript:

1

2 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-1 Essential Rhapsody in C++ Part Two Case Study

3 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-2 Case Study : Cash Register

4 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-3 Cash Register Requirements A cash register needs to be designed that reads barcodes of products that a customer has selected. When a product has been identified, its name and price are displayed on a display. If the barcode cannot be read automatically then the message "Unknown product" will be displayed and the barcode can be entered via the cashier’s keyboard. When all the selected products have been read, a ticket is generated containing the list of all the selected products with the unit price, quantity and total price. Some products can be on special offer. It should be possible to add special offers such as "Buy one get one free", "10% off“ or "Buy 3 for 1 Euro". A keyboard allows the cashier to start a session, cancel the last selected product, end a session and issue a ticket.

5 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-4 Part Two Case Study Use Case Analysis

6 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-5 Rapid Object oriented Process for Embedded Systems (ROPES) In order to implement this Cash Register, we are going to be using the ROPES process: More information on the ROPES process can be found in the book “Doing Hard Time” by Bruce Powel Douglass. ISBN

7 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-6 Before we start During this case study, there will be times where we need to enter several non-trivial descriptions and operation implementations. In order to reduce the typing, a report of the final model “CashRegister” can be supplied so that descriptions and implementations can be cut and pasted from this file. During the course in order to optimize the allocated time, it is not necessary to enter descriptions, but in a real project it is very important to enter them. If no descriptions are entered then the final documentation will be a lot less useful!

8 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-7 Use cases and actors From the requirements find use cases and actors. Group Exercise!

9 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-8 Project Description Create a new project called CashRegister. Note that we could add a description for the project.

10 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-9 Hyperlinks We could also add hyperlinks into our description in order to reference external documents or web pages:

11 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-10 Principal Use Cases Add a use case diagram entitled “Principal Uses” Add use cases and actors : Use Case Actor

12 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-11 Actors description We could add descriptions for the actors: –Customer: –Cashier: –Manager:

13 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-12 Principal use case description We could add a description to our principal use case “Sum the cost of selected products”:

14 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-13 Principal use case description We could also add a description to our other principal use case “Configure the products”:

15 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-14 Secondary Use Cases Add another use case diagram entitled “Secondary Uses”. Add the three new use cases and dependencies Drag this use case from the browser Double-click on each dependency and set the stereotype >

16 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-15 The browser The browser should now look like this:

17 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-16 Secondary Use cases 1: For the secondary use case “Keep count of selected products” we could add the description:

18 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-17 Secondary Use cases 2: For the secondary use case “Generate a ticket showing total cost” we could add the description:

19 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-18 Secondary Use cases 3: For the secondary use case “Manage special offers” we could add the following description:

20 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-19 Analysis Package All the use cases, actors, and diagrams that we have currently created are in a package called “Default”, we can rename this to something more meaningful. Use the features menu to rename the package “Default” to “AnalysisPkg” A package in UML is similar to a folder in Windows. It is used to organize the model. Packages can be nested. Model elements can be moved from one package to another.

21 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-20 Navigation In order to navigate within our model, we can set the main diagram for our main use case to be the “Secondary Uses” diagram. Now from our “Principal Uses” diagram we can navigate to our “Secondary Uses” diagram. We can also set the main diagram for our AnalysisPkg to be the “Principal Uses” diagram.

22 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-21 Part Two Case Study Finding Objects

23 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-22 Brainstorming to find objects Spend a few minutes and come up with a list of possible objects: Find some classes and a short description for each one about their responsibility Think about possible relations between these classes Should we use generalization? Are any classes reactive? Are any classes active? Group Exercise!

24 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-23 Use case driven approach We’ll attack this problem one use case at a time. Doing the use cases in this order: 1.Configure the products 2.Keep count of selected products 3.Generate a ticket showing total cost 4.Manage special offers When doing this case study, each student should progress at their own speed and in the allocated time should be able to complete at least the first two use cases. The last two use cases are really extended exercises for the faster students.

25 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-24 CashRegister package Add a new package called “CashRegisterPkg” We have an Object Model Diagram that we have not used. Rename it to “CashRegister Overview” and then move it into our package “CashRegisterPkg” To move the OMD, simply grab it in the browser and drag it onto the CashRegisterPkg package.

26 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-25 Use the Show option in the relation features window for the Role name CashRegister Overview Add classes and directed relations to the “CashRegister Overview” OMD. Note multiplicity * (many)

27 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-26 Classes Responsibilities We could add descriptions for these classes:

28 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-27 Part Two Use Case 1 “Configure the products”

29 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-28 Use case 1: Configure the products Within the CashRegisterPkg add a new OMD called “ProductDatabase Overview” Set up the main diagram for the ProductDatabase class to point to this diagram. (Useful for navigating)

30 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-29 Product attributes Add attributes to the Product class: For the attribute name, since const char* is not an existing type, you must untick the box and enter the declaration manually.

31 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-30 Product public constructor Add a constructor to initialize the Product: Add arguments and Initializer

32 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-31 Product protected constructor Add a protected constructor to stop the Product being created without any arguments: To stop the default constructor from being generated we could have set the property CG->Class->GenerateImplicitConstructors If you wanted to display the constructors like they’re shown here, then you could use the Display Options.

33 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-32 Generated code Switch on the “active code view” and select the relation between the ProductDatabase and the Product class. active code view

34 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-33 OMCollection pros/cons When the multiplicity for a relation is * (many) then Rhapsody generates a collection using the template class OMCollection This is similar to a bag. We can put many products in it and also take them out. Advantages: –Fast access to a particular element –Adding elements is fast, since memory pre-allocated –Safe, will dynamically allocate more memory if needed Disadvantages: –The order is not specified. –By default pre-allocates 20 pointers –Uses templates, which some compilers handle inefficiently The default number of pointers can of course be changed to a smaller or larger number.

35 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-34 OMCollection overview

36 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-35 Ordered property Set the Ordered property for the relation to True.

37 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-36 OMList pros/cons When the multiplicity for a relation is * (many) and the property Ordered is True, then Rhapsody generates a linked list using the template class OMList Advantages: –Efficient use of memory –Order is specified –Safe, will dynamically allocate more memory if needed Disadvantages: –Access to a particular element is slow –Adding elements is slow since memory must be allocated –Uses templates, which some compilers handle inefficiently

38 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-37 OMList overview

39 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-38 Qualified Association A responsibility of the ProductDatabase class is to allow other classes to find a product given a barcode. It is possible that we may have 1000’s of products and we will want to be able to lookup a product as quickly as possible. To do so we want to qualify our relation with the barcode. Tick this box Select the attribute barcode

40 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-39 Generated code It is not necessary to set the ordered property, this has no effect on a qualified association.

41 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-40 OMMap pros/cons When the multiplicity for a relation is * (many) and the relation is qualified with a key, then Rhapsody generates a map using the template class OMMap Advantages: –Lookup of an element using the key is fast –Efficient use of memory –Order is specified according to the value of the key –Safe, will dynamically allocate more memory if needed Disadvantages: –Adding elements is slow since memory must be allocated and the map rebalanced –Uses templates, which some compilers handle inefficiently

42 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-41 OMMap overview This is a balanced tree, so searching is as fast as possible.

43 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-42 Generated operations When we look at the code, Rhapsody has generated two useful operations –void addItsProduct(int,Product*) –Product* getItsProduct(int) We can use the first to add products to our map, and the second to get a product given a specific barcode.

44 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-43 Creating products Create a constructor for ProductDatabase that does the following:

45 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-44 Build Package In order to try this out let’s create a new package called “BuildPkg”. This package will contain the classes that are needed to build our CashRegister. Copy the “CashRegister overview” diagram from the CashRegisterPkg to the BuildPkg (hold the ctrl key down and drag it in the browser) Rename the copied diagram to “Build Overview”

46 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-45 Build Prototype Add a composite class called BuildPrototype around all our classes except the Product class on the “Build Overview” diagram.

47 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-46 Initial instances The responsibility of the BuildPrototype class is to instantiate our objects and initialize the relations between them. If any classes become reactive, it will also start their behavior. Any classes that are reactive will also run on the same thread as the composite class. If the composite class is deleted then it will also delete the objects that it created.

48 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-47 Creating initial instances Use the features menu for each class inside the composite class to create initial instances: Instances of the Product class will get created by the ProductDatabase class We don’t need to specify an instance name, Rhapsody will do that for us. Select multiplicity 1 for all classes inside the composite class

49 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-48 Composite class By default the relations between the composite and its components are directional. They can be made symmetrical through the browser.

50 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-49 Links If a relation is drawn inside the composite class, then that relation will be automatically initialized at run time by Rhapsody. This is known as a link. Rhapsody shows all these links in the browser.

51 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-50 Component Rename the existing component to RhapsodyCashRegister Rename the configuration to Debug Create an initial instance of BuildPrototype and select the tick box Derived Set Environment to Microsoft Set Instrumentation to Animation

52 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-51 Save / Generate / Make / Run Save & Generate / Make / Run

53 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-52 Initialized objects Check that we have an initial instance of the BuildPrototype and instances of all the other classes. Note the names of the instances that are inside the composite instance. We should also have five instances of Product

54 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-53 ProductDatabase The ProductDatabase should know about all the five instances of Product, each of which should be initialized correctly.

55 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-54 Part Two Use Case 2 “Keep count of selected products”

56 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-55 Scenarios For this use case we can create some sequence diagrams which will help us understand how the Cash Register works: For example we’ll look at the following scenarios: 1.Selecting products 2.Cancelling products 3.Manually entering a barcode

57 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-56 Scenario selecting products Create in the CashRegisterPkg a “Design” sequence diagram entitled “Scenario selecting products”. Drag the classes onto the diagram as shown below:

58 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-57 Starting the scenario Add a condition mark called idle and two partition lines with associated text.

59 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-58 Adding a message Draw a message evStart diagonally from the Keyboard to the CashRegister. Answer Yes to the question.

60 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-59 Adding more operations Add the operations startSession() and show() Add a condition mark “active” Answer Yes to realize the messages

61 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-60 Event or Primitive operation Since we drew the message evStart diagonally, Rhapsody has added the message evStart to the CashRegister as an event. If you drew the message show horizontally then the message will have been added as a primitive operation. Make sure that evStart is an event and that startSession and show are both primitive operations.

62 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-61 Operation show() Edit the show operation so that it takes an argument aMsg of type const char*

63 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-62 Adding more operations Add another partition line and more operations Answer Yes to realize the operations

64 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-63 Return value The operation getProduct returns a Product, we can indicate this by using the features of this message

65 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-64 Adding more operations Add arguments to evBarcode, identifyProduct, getProduct and addProduct. Add return value for the getProduct operation Answer Yes to realize the operations

66 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-65 Finishing the scenario Complete the scenario by adding a partition line, an event, three more operations and a condition mark.

67 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-66 Browser Now that the scenario is completed, we have populated our browser, which should look similar to this:

68 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-67 Scenario cancelling products Add another “Design” sequence diagram entitled: “Scenario cancelling products”

69 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-68 Browser This scenario should have added just one more event evCancel, as well as operations removeLastProduct() and isNoMoreProducts() to our browser. Make sure that evCancel is an event

70 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-69 Implementation We need to start to implement the operations, let’s start with the Display class Add the following implementation for the show operation:

71 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-70 Include Since we have used cout, we should do an include of “iostream.h” or We can use the property ImpIncludes to do this. Set the value of this property for the Display class to With the Visual C++ compiler, we don’t have to do this, but we do for other compilers such as gnu.

72 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-71 CashRegister statechart From our scenarios, we have noted that the CashRegister receives events. We also added a couple of condition marks indicating states. So we need to add a Statechart to the CashRegister. Add the following Statechart, note that since the events already exist, we can right-click and select them from the list.

73 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-72 Completing the Statechart Complete the Statechart: Remember that params is a Rhapsody generated pointer that allows access to the arguments of the event.

74 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-73 Ordered Relation Since we need to be able to remove the last product added, the order of our products is important so we need to make our relation ordered. For the relation itsProduct, set the property ordered to True

75 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-74 {Ordered} To indicate that this relation is ordered, we can add a Note to indicate this. Add a Note using the icon. As we saw previously, this will generate a linked list.

76 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-75 CashRegister addProduct() Add the implementation to the addProduct() operation of the CashRegister class

77 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-76 Include stdio.h Since we have used sprintf, we need to include stdio.h, so we can again use the ImpIncludes property to do this for the CashRegister class

78 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-77 CashRegister endSession() Add the implementation for the endSession() operation: clearItsProduct() is an operation that gets generated automatically and clears (or empties) the linked list.

79 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-78 CashRegister identifyProduct() Add the implementation for the identifyProduct() operation:

80 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-79 CashRegister isNoMoreProducts() Ensure that the isNoMoreProducts() operation returns an int and make it inline and constant. Add the implementation: Operation of OMList to check if list is empty.

81 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-80 CashRegister removeLastProduct() For this operation, we need to iterate through the linked list to get the last product. To do this, we need to use an “iterator”, this is a friend class that allows us to manipulate the list. We will use the following declaration OMIterator iter(itsProduct); This declares iter as being an iterator to a collection/list of Products and is automatically initialized to point to the first Product. At first glance, the syntax for iterators may seem a little complex, but it is soon mastered.

82 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-81 CashRegister removeLastProduct() Add the implementation for the removeLastProduct() operation: removeItsProduct() is an operation that gets generated automatically.

83 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-82 CashRegister startSession() Add the implementation for the startSession() operation:

84 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-83 ProductDatabase getProduct() Finally add an implementation for the getProduct() operation of the ProductDatabase class. Check that it returns a Product* getItsProduct() is an operation that gets generated automatically.

85 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-84 Save / Generate / Make / Run Close all diagrams Save Generate / Make / Run Go Idle Note: Animation View

86 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-85 Running a scenario Open an animated sequence diagram based on “Scenario selecting products”

87 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-86 Injecting events Check the scenario by injecting events evStart, followed by evBarcode(12345), evBarcode(12346), evCancel and finally evEnd to the CashRegister instance. You won’t see the events, since we didn’t add a system border to this sequence diagram.

88 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-87 Automating the tests We can check that our model is correct by manually injecting events and observing the animated diagrams. Ideally we’d like to automate these tests, this can be done ideally by using the Test Conductor (an add-on product to Rhapsody) Another way to automate these tests is to use an actor to drive our model. We will use our actor “Cashier” to execute several scenarios.

89 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-88 Actor Driven overview Add the following Object Model Diagram to the CashRegisterPkg entitled “Actor Driven Overview” The simplest way to do this, is to drag the actor and classes from the browser onto the OMD.

90 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-89 Actor Statechart Add the following Statechart to the Cashier actor is a junction connector This transition was drawn from the state to the junction connector, then the line shape was changed to rectilinear and a reroute done.

91 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-90 A Sub-Statechart Add the following sub-Statechart for the state sd1_adding_products Use the sub-state connector and name it SD1_COMPLETE

92 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-91 Another sub-Statechart Add the following sub-Statechart for the state sd2_cancelling_products Use the sub-state connector and name it SD2_COMPLETE

93 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-92 Sub-Statechart stub connectors Add the Sub-Statechart Stub connectors SD1_COMPLETE and SD2_COMPLETE to the top- level Statechart. Note that the … indicates that there is a sub-state. To connect to the sub-state connector, connect to the centre of the connector.

94 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-93 Keyboard operations Add operations startSession(), endSession() and cancelProduct() to the Keyboard class

95 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-94 BarcodeReader operation Add operation detect(int aCode) to the BarcodeReader class with implementation:

96 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-95 Creating an instance of an actor Copy the existing configuration Debug then rename to ActorDriven Create an initial instance of Cashier and select “Generate Code For Actors” Make sure that we still have the initial instance of BuildPrototype

97 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-96 Initializing the relations We must initialize the relations between our actor and the Keyboard and BarcodeReader Set the ActorDriven configuration as the active configuration Save / Generate and Make (don’t run it’ll crash!) Select the ActorDriven configuration and edit the main

98 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-97 Editing the main Add the extra two lines to initialize the relations and then click on the browser to automatically roundtrip this code into the configuration. For a relation ex: itsKeyboard, operations getItsKeyboard() and setItsKeyboard() will always be generated automatically.

99 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-98 Save / generate / make / run Start the animation and check that there is an instance of the Cashier and its relations are initialized.

100 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-99 Running a scenario Open an animated sequence diagram based on “Scenario selecting products” Generate event evS1 to the Cashier object and hit

101 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-100 Saving the diagram Stop the animation Close the animated sequence diagram and click Yes to save it

102 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-101 Comparing sequence diagrams We can now compare the expected scenario against the actual scenario Select “Sequence Diagram Compare” Select the diagrams to compare Click OK

103 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-102 The Comparison One major difference!

104 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-103 Correcting the problem Which diagram is wrong? Correct the error and try another comparison Check out the other Actor driven scenario Does our model conform to both scenarios?

105 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-104 Manually entering a barcode We can now start to look at how we can manually enter a barcode. To facilitate the implementation we will introduce a new class KeyReader which has the following description. Add a new Object Model Diagram entitled “Keyboard Overview”, drag the class Keyboard onto it and add a new class KeyReader. Add a composition relation between them. Set up the main diagram for Keyboard

106 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-105 Scenario manually entering barcode Add a new sequence diagram “Scenario manually entering a barcode”

107 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-106 Keyboard Statechart Add the following Statechart to the Keyboard

108 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-107 Keyboard attributes/operations Add an attribute barcode of type int Add implementation for the enterBarcode operation

109 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-108 Active class We need the KeyReader class to wait for keys, by using “cin >> c” However calling an operation like this will block the execution, so this is a good candidate for making the KeyReader class “active” (so that it runs on it’s own thread). Set the concurrency for the KeyReader class to “active” instead of sequential. The thick border indicates an “active” class

110 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-109 OMThread Switch on the “Active code view” and note that the KeyReader class inherits from OMThread, this is a Rhapsody framework class. Rhapsody will create a thread for us that will run an operation in OMThread called execute(). We need to overload this operation so that it constantly reads the keys.

111 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-110 Overloading execute() Add an operation execute to the KeyReader class that returns “OMReactive*” and that has the following implementation: We must return “OMReactive*” since this operation must have the same signature as the execute operation in OMThread.

112 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-111 Read operation Add a read(char c) operation with implementation: (c – ‘0’) simply converts an ASCII character between ‘0’ and ‘9’ to an integer between 0 and 9.

113 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-112 Switching off animation Since the execute operation never ends, it doesn’t make sense to animate it. Use the Animate property to disable the execute operation from being animated

114 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-113 Starting the thread To start our KeyReader thread, we must call the start operation which is part of OMReactive. We can add a constructor to the Keyboard class to do this. If an active class has a Statechart, then there is no need to overload the execute operation or call the start operation, this gets done automatically for us.

115 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-114 Thread properties If we wanted to execute our model on a real target, then we could set various properties for the thread of our KeyReader class such as the priority, name, stack size, message queue size …

116 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-115 Execution Save / Generate / Make / Run / Go In the DOS console window enter: s 12345b 12347b c e We can now use the keyboard to run scenarios. Check that the model works as expected.

117 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-116 Threads Now that we have an active class we can use the thread button to inspect the threads. We can set the focus of the call stack and event queue to any one thread. Set the focus to the KeyReader thread set focus The call stack shows the actions and the event queue shows the pending events just for the current thread. The animation must be paused to get access to the thread button.

118 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-117 Setting a breakpoint Use the breakpoint command to set a breakpoint when the KeyReader thread gets control Go, then in the DOS console window enter command : c

119 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-118 Updating the use case Now that we have created some sequence diagrams, we can associate them with the use case “Keep count of selected products”.

120 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-119 Referenced Sequence Diagrams Select the use case “Keep count of selected products” in the browser and add the following sequence diagrams as “Referenced Sequence Diagrams”

121 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-120 Part Two Use Case 3 “Generate a ticket showing total cost”

122 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-121 “Generate a ticket” Use Case In order to generate a ticket, we need to be able to count how many of each product there are. To do so we’ll introduce a new class CountedProduct. Create a new OMD entitled “Product Overview” Set this to be the main diagram for the class Product

123 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-122 Product Overview Drag the classes CashRegister and Product onto the OMD and add the class CountedProduct Add inheritance and Qualified association Add attribute count of type int Add operation getPrice that returns an int Add operation increment Set Multiplicity *

124 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-123 CountedProduct For the CountedProduct class: –Add description –Add implementations Initialize count to 1

125 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-124 CountedProduct constructor We will be creating CountedProducts from Products so we need a constructor that takes a “Product*” as an argument. Add the constructor

126 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-125 Another Product constructor Add another constructor to the Product class that is protected and that takes a Product* as an argument

127 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-126 Adding a scenario Add the Sequence Diagram entitled “Scenario generating a ticket” Drag the classes from the browser onto the diagram Double-click on each instance of CountedProduct to enter the specific name of the instance

128 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-127 Completing the scenario constructor destructor

129 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-128 Printer Implement the print operation for the Printer class Set property ImpIncludes so that the Printer class will include

130 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-129 countProducts() operation For the CashRegister class add the implementation for the countProducts() operation Don’t forget that to save typing, text can be cut and pasted from the report.

131 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-130 generateTicket() operation For the CashRegister class add the implementation for the generateTicket() operation

132 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-131 endSession() operation We need to modify the endSession() operation so that we remove all the CountedProducts and delete them

133 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-132 Printing a ticket Save Generate / Make / Run Use the DOS console to start a session, enter several barcodes and then end a session

134 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-133 Using the Actor to print a ticket Add a new state sd3_generating_a_ticket to the Cashier actor so that we can easily test out our model Instead of drawing this new state and sub-state chart, a quicker way is to simply copy one of the other states, then paste it and rename it.

135 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-134 Adding the sub-Statechart Don’t forget that to save typing, text can be cut and pasted from the report.

136 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-135 Executing the scenario Save Generate / Make / Run Barcode 12344

137 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-136 Referenced Sequence Diagrams Add the sequence diagram “Scenario generating a ticket”, to the use case “Generate a ticket showing total cost”

138 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-137 Part Two Use Case 4 “Manage special offers”

139 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-138 Special Offer Use case Open the Product Overview OMD and add a class entitled “ISpecialOffer” Add an aggregation from Product to ISpecialOffer Select the Interface stereotype for this class The > stereotype is just extra information informing the reader that this class is pure- virtual and must be sub-classed.

140 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-139 Directed aggregation Change the relation to be directional and not symmetrical Change the role name to be itsSpecialOffer Set multiplicity 0..1

141 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-140 Abstract operation Add an operation getSpecialPrice() which returns an int that is “Virtual” and “Abstract” Add arguments unitPrice and quantity, both int’s

142 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-141 Special Offers Overview Add a new OMD entitled “Special Offers Overview” Set the main diagram for ISpecialOffer to this diagram

143 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-142 BuyOneGetOneFree Select the BuyOneGetOneFree class, right-click and select Implement Base Classes

144 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-143 Implement Base Classes We must implement the getSpecialPrice() operation, so tick the operation and select “Edit Code…” to enter the implementation.

145 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-144 Using the special offer Modify the “ProductDatabase Overview” OMD Add the BuyOneGetOneFree class with a dependency from ProductDatabase to it. With the features for this dependency, set the stereotype to Usage. The > stereotype generates a #include “BuyOneGetOneFree.h”

146 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-145 Applying the special offer Modify the implementation of the ProductDatabase constructor

147 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-146 Product constructors Modify the Product constructors Set default value for aSpecialOffer to NULL

148 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-147 CountedProduct getPrice() Modify the implementation of the getPrice() operation of the CountedProduct class

149 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-148 generateTicket() Modify the generateTicket() operation of the CashRegister class

150 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-149 Figs are “buy one get one free!” Save / generate / make / run / go Send evS3 to the Cashier

151 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-150 Optimization : get / set We’ve implemented all our use cases and everything seems ok, it’s now time to do a little optimization Rhapsody generates by default a get and set for every attribute. We can tell Rhapsody not to generate these get / set operations and then generate them just when we need them. Set the following properties: Make sure that you select the properties for the entire project

152 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-151 Regenerating the code Select Code->Re Generate Make There will be errors, since we have used several accessors such as getBarcode()

153 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-152 Generating accessors Set the AccessorGenerate property to True for all the attributes in the Product and CountedProduct classes

154 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-153 Optimization : avoiding new We can also tell Rhapsody to not use ”new” when it creates instances of classes inside composite classes For example the Keyboard class currently creates an instance of the KeyReader class like this:

155 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-154 Embedded classes Set the property CPP_CG:Class:Embeddable for the entire project and regenerate code Note that there is no longer a pointer to the KeyReader class and also that there is no longer a newItsKeyReader() operation

156 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-155 Correcting the errors Do a Make Correct the error in the Keyboard class by changing the -> to a.

157 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-156 Embedded classes in the main Set the property CPP_CG:Configuration:InitializeEmbeddableObjects ByValue to True and regenerate code

158 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-157 Correcting the errors Do a Make Correct the errors in the main by changing the -> to. Note that new is no longer used.

159 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-158 Generating for another OS If we wanted to get this model to run on a target running an OS such as VxWorks, then all we need to do is copy the configuration, rename it and select the appropriate environment

160 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-159 Cross compiling We can generate and make this component

161 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-160 Running on the target 66K ROM Rhapsody Tasks

162 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-161 Generating a report Finally we can generate a report: For example an HTML documentation of just our use cases, actors and use case diagrams

163 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-162 Pre-defined report We can select a predefined report:

164 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-163 Final Report

165 Rhapsody in C++ Tool Training "Essential" © I-Logix v4.1 01/01/2003 E2-164


Download ppt "Rhapsody in C++ Tool Training "Essential" © I-Logix 1999-2003 v4.1 01/01/2003 E2-1 Essential Rhapsody in C++ Part Two Case Study."

Similar presentations


Ads by Google