Bootcamp:Chapter 9
No edit summary
(Step 294 Noticed code was lacking, added it for this step)
 
(16 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This is '''Chapter 9'''. Here is the first part: [[The 1000 steps program to MDriven Chapter 1|The 1000 steps program to MDriven: Chapter 1]], or see [[The 1000 steps program to MDriven Chapter 8|Chapter 8 (the previous chapter)]]
{{Edited|July|12|2025}}
<message>Write the content here to display this box</message>
This is '''Chapter 9'''. Here is the first part, [[Training:Bootcamp:Chapter 1|Chapter 1]], or see [[Training:Bootcamp:Chapter 8|Chapter 8 (the previous chapter)]]


=== '''Video 9: Steps 265 - 301''' ===
=== '''Video 9: Inheritance and Statemachines''' ===
<html>
<html>


Line 13: Line 15:
   </div>
   </div>
   <div class="video__navigation">
   <div class="video__navigation">
  <span data-video="MPqx5af3QCM" data-start="00" tabindex="0"> <strong> Inheritance and Statemachines </strong> </span>
  <span data-video="MPqx5af3QCM" data-start="00" tabindex="0"> <strong> Steps 265 - 301 </strong> </span>
     <span class="navigation-item" data-video="MPqx5af3QCM" data-start="00" tabindex="0"> Introduction </span>
     <span class="navigation-item" data-video="MPqx5af3QCM" data-start="00" tabindex="0"> Introduction </span>
     <span class="navigation-item" data-video="MPqx5af3QCM" data-start="27" tabindex="0"> Aligning items </span>
     <span class="navigation-item" data-video="MPqx5af3QCM" data-start="27" tabindex="0"> Aligning items </span>
Line 30: Line 32:


== Chapter 9: Inheritance and Statemachines ==
== Chapter 9: Inheritance and Statemachines ==
'''In this chapter, we introduce [[UML Inheritance|UML inheritance]] and [https://wiki.mdriven.net/index.php/UML_%E2%80%93_State_machines UML Statemachines].'''
'''In this chapter, we introduce [[Training:UML Inheritance|UML inheritance]] and [[Training:UML – State machines|UML Statemachines]].'''


265. Remove the 20-pixel margin on the buttons and apply the Align Items property end instead, to make everything end up on the bottom line.
265. Remove the 20-pixel margin on the buttons and apply the Align Items property end instead, to make everything end up on the bottom line.
Line 36: Line 38:
266. Test out combinations of Justify content and Align Items, and determine how to familiarize yourself with ways to control the flow of UI widgets.
266. Test out combinations of Justify content and Align Items, and determine how to familiarize yourself with ways to control the flow of UI widgets.


267. Introduce the new class <code>CarDealer</code>.
267. Introduce the new class '''CarDealer'''.


268. Introduce the new class <code>CarOwner</code>.  
268. Introduce the new class '''CarOwner'''.  


269. Add generalization arrows from CarDealer to CarOwner and from Person to CarOwner.
269. Add generalization arrows from '''CarDealer''' to '''CarOwner''' and from '''Person''' to '''CarOwner'''.


270. Add the Attribute <code>Name:String</code> to CarOwner and save.   
270. Add the Attribute <code><span class="col-black">'''Name:String'''</span></code> to '''CarOwner''' and save.   
* Notice the error.   
* Notice the error.   
* Make sure you understand why Name is reported as a doublet.   
* Make sure you understand why '''Name''' is reported as a doublet.   
* Fix it by removing Name of Person.  
* Fix it by removing '''Name of Person'''.  
271. Make sure you understand Subclass and Superclass. Look up and read through these articles on the Wiki:  
271. Move the multi-link association existing between the classes '''Car''' and '''Person''' (The one with the '''HistoricOwnership''' association class) to '''Car''' and '''CarOwner'''.
* [[UML Inheritance|https://wiki.mdriven.net/index.php/UML_Inheritance]]  
 
* [[UML School|https://wiki.mdriven.net/index.php/UML_School#Lesson_3:_UML_Inheritance]].
* Rename the '''Car''' association end from '''CarsPersonUsedToOwn''' to '''CarsOwnerUsedToOwn''' and apply changes.
272. Add a new association from CarOwner to Car. Name the Cars end as <code>CurrentlyOwnedCars</code>.  
* Notice the errors.
* Create and refresh autoforms to correct the errors.
* Select the class '''CarOwner''' and mark it as Abstract by going to the Property Inspector and selecting '''True''' from the '''IsAbstract''' dropdown. Notice now that the label on the '''CarOwner''' Class is now in italics.
 
Make sure you understand Subclass and Superclass. Look up and read through this article on the Wiki:  
* [[Training:UML Inheritance|UML_Inheritance]]
272. Add a new association from '''CarOwner''' to '''Car'''. Name the '''Car''' end as '''CurrentlyOwnedCars'''.  


273. Delete the old link with the currently owned car. Update the autoforms. Save and check for errors.   
273. Delete the old link with the currently owned car. Update the autoforms. Save and check for errors.   


274. Delete the Combobox that showed the now-removed currently owned car in the Person form.
274. Delete the Combobox that showed the now-removed currently owned car in the '''Person''' form.
 
275. Add a new class: '''CarTransferOwnershipDocument''', and add a new association from <code><span class="col-black">'''Car(0..1)'''</span></code> to <code><span class="col-black">'''CarTransferOwnershipDocument(*)'''</span></code>
 
276. Add an association from '''CarOwner''' to '''CarTransferOwnershipDocument'''. Call the link <code><span class="col-black">'''DocumentsForSales(*)'''</span></code> and <code><span class="col-black">'''Seller(0..1)'''</span></code>
 
277. Add another association from '''CarOwner''' to '''CarTransferOwnershipDocument'''. Call the link <code><span class="col-black">'''DocumentsForPurchase(*)'''</span></code> and <code><span class="col-black">'''Buyer(0..1)'''</span></code>
 
278. Add '''CarFactory''' and make it a subclass of '''CarOwner'''.
 
279. Add '''ScrapYard''' and make it a subclass of '''CarOwner'''.


275. Add a new class: <code>CarTransferOwnershipDocument</code>, and add a new association from <code>Car(0..1)</code> to <code>CarTransferOwnershipDocument(*)</code>
280. Add a statemachine on Class '''Car'''.


276. Add an association from CarOwner to CarTransferOwnershipDocument. Call the link <code>DocumentsForSales(*)</code> and <code>Seller(0..1)</code>
281. Add a state in the statemachine of the car called '''BrandNew'''.


277. Add another association from CarOwner to CarTransferOwnershipDocument. Call the link <code>DocumentsForPurchase(*)</code> and <code>Buyer(0..1)</code>
282. Add another state: '''InOwnershipTransaction'''.


278. Add <code>CarFactory</code> and make it a subclass of CarOwner.
283. Add a Transition arrow from '''BrandNew''' to '''InOwnershipTransaction'''.


279. Add <code>ScrapYard</code> and make it a subclass of CarOwner.
284. Call the trigger of the transition '''InitiateSale'''.


280. Add a statemachine on Class Car.
285. Add yet another state called '''OwnershipStable'''.


281. Add a state in the statemachine of the car called <code>BrandNew</code>.
286. Add a transition from '''InOwnershipTransaction''' to '''OwnershipStable'''. Name the trigger '''CloseSale'''.


282. Add another state: <code>InOwnershipTransaction</code>.
287. Add a transition from '''OwnershipStable''' to '''InOwnershipTransaction'''. Reuse the trigger '''InitiateSale''' on this transition.


283. Add a Transition arrow from <code>BrandNew</code> to <code>InOwnershipTransaction</code>.
288. Create an <u>Entry Action</u> in the state '''InOwnerShipTransaction'''.


284. Call the trigger of the transition <code>InitiateSale</code>.
* Click the '''InOwnerShipTransaction''' state to highlight it's Properties.
* In the Property Inspector click the three dots at the '''Entry''' Property to access the Action Editor Entry for '''InOwnerShipTransaction'''.
* Type the following Expression:
<syntaxhighlight>
let doc=CarTransferOwnershipDocument.Create in
(
self.CarTransferOwnershipDocuments.Add(doc);
doc.Seller:=self.CarOwner
)
</syntaxhighlight>The Expression above:  


285. Add yet another state called <code>OwnershipStable</code>.
* Creates a <code><span class="col-black">'''CarTransferOwnershipDocument'''</span></code> and adds it to the cars list of '''CarTransferOwnershipDocuments'''.


286. Add a transition from InOwnershipTransaction to OwnershipStable. Name the trigger <code>CloseSale</code>.
* Also, assigns the '''Seller''' of the new '''CarTransferOwnershipDocument''' to the current owner of the '''Car'''.
289. Add an association from '''CarTransferOwnershipDocumen'''t to '''Car'''. Call the single-end '''CurrentTransaction'''. Set the Car's end to '''not Navigable'''.  


287. Add a transition from OwnershipStable to InOwnershipTransaction. Reuse the trigger InitiateSale on this transition.
290. In the Entry Action, in the state '''InOwnerShipTransaction''', assign the '''CurrentTransaction''' to the created document.


288. Create an Entry Action in the state InOwnerShipTransaction. 
* Type the following Expression to achieve this :
* Create a <code>CarTransferOwnershipDocument</code> and add it to the cars list of CarTransferOwnershipDocuments.
<syntaxhighlight>
* Also, assign the Seller of the new CarTransferOwnershipDocument to the current owner of the Car.  
let doc=CarTransferOwnershipDocument.Create in
289. Add an association from CarTransferOwnershipDocument to Car. Call the single-end <code>CurrentTransaction</code>. Set the Car's end to not Navigable.
(
self.CarTransferOwnershipDocuments.Add(doc);
doc.Seller:=self.CarOwner;
self.CurrentTransaction:=doc
)


290. In the Entry Action, in the state InOwnerShipTransaction, assign the CurrentTransaction to the created document.


291. Go to Guard of CloseSale and check that the current transaction has a Buyer.
</syntaxhighlight>


292. In the OwnershipStable, add an Entry action that removes the Car from Seller's CurrentlyOwnedCars and adds it to the Buyer's CurrentlyOwnedCars.  
291. Go to Guard of CloseSale and check that the current transaction has a '''Buyer'''.  


293. In the OwnershipStable, add an Entry action and add an update of the Seller's CarsOwnerUsedToOwn.
* Type the following Expression into the Guard OCL Editor to achieve this :


294. On the transition between BrandNew and InOwnerShipTransaction, add a guard that checks that there is a CarOwner.
<code>self.CurrentTransaction.Buyer->notempty</code>


295. On the InitiateSale trigger, add a ClassAction for the trigger.
292. In the '''OwnershipStable''', add an Entry action that removes the '''Car''' from '''Seller'''<nowiki/>'s '''CurrentlyOwnedCars''' and adds it to the '''Buyer'''<nowiki/>'s '''CurrentlyOwnedCars'''.


296. On the CloseSale trigger, add a ClassAction for the trigger.
* Type the following Expression to achieve this :
<syntaxhighlight>
self.CurrentTransaction.Seller.CurrentlyOwnedCars.Remove(self);
self.CurrentTransaction.Buyer.CurrentlyOwnedCars.Add(self)
 
</syntaxhighlight>293. In the '''OwnershipStable''', add an Entry action and add an update of the '''Seller'''<nowiki/>'s '''CarsOwnerUsedToOwn'''.
 
* Update the previous Expression in step 292 as provided below :
<syntaxhighlight>
self.CurrentTransaction.Seller.CurrentlyOwnedCars.Remove(self);
self.CurrentTransaction.Buyer.CurrentlyOwnedCars.Add(self);
self.CurrentTransaction.Seller.CarsOwnerUsedToOwn.Add(self)
 
</syntaxhighlight>294. On the transition between '''BrandNew''' and '''InOwnerShipTransaction''', add a guard that checks that there is a '''CarOwner'''.
 
* Type the following Expression into the OCL Editor '''Guard BrandNew'''->'''InOwnershipTransaction''' to achieve this:
 
<code>self.CarOwner->notempty</code>
 
295. On the InitiateSale trigger, add a '''ClassAction''' for the trigger.
 
296. On the CloseSale trigger, add a '''ClassAction''' for the trigger.


297. Refresh the AutoForms.
297. Refresh the AutoForms.


298. Save and test on the web.  
298. Save and test on the web.  
* Create a <code>CarFactory</code>.  
* Create a '''CarFactory'''.  
* Call it BWM in Munich.  
* Call it '''BMW''' in '''Munich'''.  
* Add a Car to this CarFactory.
* Add a '''Car''' to this '''CarFactory'''.
299. Initiate the Sale and check that you get a CarTransferOwnershipDocument.
299. Initiate the Sale and check that you get a '''CarTransferOwnershipDocument'''.
 
300. Assign a buyer in the CarTransferOwnershipDocument, then close the Sale. Check that ownership has switched.  


301. Set the default representation of CarOwner to <code>self.Name</code>
300. Assign a buyer in the '''CarTransferOwnershipDocument''', then close the Sale. Check that ownership has switched.  


'''Next Chapter'''
301. Set the default representation of '''CarOwner''' to <code><span class="col-black">'''self.Name'''</span></code>


[[The 1000 steps program to MDriven Chapter 10|The_1000_steps_program_to_MDriven_Chapter_10]]
=== '''Next Chapter''' ===
[[Category:1000 Steps Program]]
[[Training:Bootcamp:Chapter 10|Bootcamp:Chapter 10]]
[[Category:Bootcamp]]

Latest revision as of 22:52, 25 March 2025

This page was created by Hans.karlsen@mdriven.net on 2022-10-23. Last edited by Martin@mdriven.net on 2025-03-25.


This is Chapter 9. Here is the first part, Chapter 1, or see Chapter 8 (the previous chapter)

Video 9: Inheritance and Statemachines

To make your experience smooth, we set the main tags mentioned in the video to the right bar menu of this mini-player. Choose an interesting subtitle on the list and immediately get to the exact theme navigation item place in the video. Now you can pick any topic to be instructed on without watching the whole video.

Steps 265 - 301 Introduction Aligning items Inheritance Abstract classes Repetition Statemachines Triggers and entry Guards Class actions for triggers Web application example

Chapter 9: Inheritance and Statemachines

In this chapter, we introduce UML inheritance and UML Statemachines.

265. Remove the 20-pixel margin on the buttons and apply the Align Items property end instead, to make everything end up on the bottom line.

266. Test out combinations of Justify content and Align Items, and determine how to familiarize yourself with ways to control the flow of UI widgets.

267. Introduce the new class CarDealer.

268. Introduce the new class CarOwner.

269. Add generalization arrows from CarDealer to CarOwner and from Person to CarOwner.

270. Add the Attribute Name:String to CarOwner and save.

  • Notice the error.
  • Make sure you understand why Name is reported as a doublet.
  • Fix it by removing Name of Person.

271. Move the multi-link association existing between the classes Car and Person (The one with the HistoricOwnership association class) to Car and CarOwner.

  • Rename the Car association end from CarsPersonUsedToOwn to CarsOwnerUsedToOwn and apply changes.
  • Notice the errors.
  • Create and refresh autoforms to correct the errors.
  • Select the class CarOwner and mark it as Abstract by going to the Property Inspector and selecting True from the IsAbstract dropdown. Notice now that the label on the CarOwner Class is now in italics.

Make sure you understand Subclass and Superclass. Look up and read through this article on the Wiki:

272. Add a new association from CarOwner to Car. Name the Car end as CurrentlyOwnedCars.

273. Delete the old link with the currently owned car. Update the autoforms. Save and check for errors.

274. Delete the Combobox that showed the now-removed currently owned car in the Person form.

275. Add a new class: CarTransferOwnershipDocument, and add a new association from Car(0..1) to CarTransferOwnershipDocument(*)

276. Add an association from CarOwner to CarTransferOwnershipDocument. Call the link DocumentsForSales(*) and Seller(0..1)

277. Add another association from CarOwner to CarTransferOwnershipDocument. Call the link DocumentsForPurchase(*) and Buyer(0..1)

278. Add CarFactory and make it a subclass of CarOwner.

279. Add ScrapYard and make it a subclass of CarOwner.

280. Add a statemachine on Class Car.

281. Add a state in the statemachine of the car called BrandNew.

282. Add another state: InOwnershipTransaction.

283. Add a Transition arrow from BrandNew to InOwnershipTransaction.

284. Call the trigger of the transition InitiateSale.

285. Add yet another state called OwnershipStable.

286. Add a transition from InOwnershipTransaction to OwnershipStable. Name the trigger CloseSale.

287. Add a transition from OwnershipStable to InOwnershipTransaction. Reuse the trigger InitiateSale on this transition.

288. Create an Entry Action in the state InOwnerShipTransaction.

  • Click the InOwnerShipTransaction state to highlight it's Properties.
  • In the Property Inspector click the three dots at the Entry Property to access the Action Editor Entry for InOwnerShipTransaction.
  • Type the following Expression:
let doc=CarTransferOwnershipDocument.Create in
(
self.CarTransferOwnershipDocuments.Add(doc);
doc.Seller:=self.CarOwner
)

The Expression above:  

  • Creates a CarTransferOwnershipDocument and adds it to the cars list of CarTransferOwnershipDocuments.
  • Also, assigns the Seller of the new CarTransferOwnershipDocument to the current owner of the Car.

289. Add an association from CarTransferOwnershipDocument to Car. Call the single-end CurrentTransaction. Set the Car's end to not Navigable.

290. In the Entry Action, in the state InOwnerShipTransaction, assign the CurrentTransaction to the created document.

  • Type the following Expression to achieve this :
let doc=CarTransferOwnershipDocument.Create in
(
self.CarTransferOwnershipDocuments.Add(doc);
doc.Seller:=self.CarOwner;
self.CurrentTransaction:=doc
)

291. Go to Guard of CloseSale and check that the current transaction has a Buyer.

  • Type the following Expression into the Guard OCL Editor to achieve this :

self.CurrentTransaction.Buyer->notempty

292. In the OwnershipStable, add an Entry action that removes the Car from Seller's CurrentlyOwnedCars and adds it to the Buyer's CurrentlyOwnedCars.

  • Type the following Expression to achieve this :
self.CurrentTransaction.Seller.CurrentlyOwnedCars.Remove(self);
self.CurrentTransaction.Buyer.CurrentlyOwnedCars.Add(self)

293. In the OwnershipStable, add an Entry action and add an update of the Seller's CarsOwnerUsedToOwn.

  • Update the previous Expression in step 292 as provided below :
self.CurrentTransaction.Seller.CurrentlyOwnedCars.Remove(self);
self.CurrentTransaction.Buyer.CurrentlyOwnedCars.Add(self);
self.CurrentTransaction.Seller.CarsOwnerUsedToOwn.Add(self)

294. On the transition between BrandNew and InOwnerShipTransaction, add a guard that checks that there is a CarOwner.

  • Type the following Expression into the OCL Editor Guard BrandNew->InOwnershipTransaction to achieve this:

self.CarOwner->notempty

295. On the InitiateSale trigger, add a ClassAction for the trigger.

296. On the CloseSale trigger, add a ClassAction for the trigger.

297. Refresh the AutoForms.

298. Save and test on the web.

  • Create a CarFactory.
  • Call it BMW in Munich.
  • Add a Car to this CarFactory.

299. Initiate the Sale and check that you get a CarTransferOwnershipDocument.

300. Assign a buyer in the CarTransferOwnershipDocument, then close the Sale. Check that ownership has switched.

301. Set the default representation of CarOwner to self.Name

Next Chapter

Bootcamp:Chapter 10