Bootcamp:Chapter 2

This is Chapter 2. You should consider finishing Chapter 1 first.

Video 2: Auto-diagram, OCL, and Autoforms

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 41 - 75 Introduction Deleting and moving Association to self Auto-diagram and more deleting More Association OCL Autoforms

Chapter 2: Understanding Class-diagrams, Creating Classes, and Making Use of AutoForms and DefaultStringRepresentation

NOTE: Click on the step/number to access the corresponding images/GIFs.

41. Open the location where you saved your file and make a copy in a subfolder called "TasksUpTo40". This helps you keep track of your progress and easily access previous versions of the file.

  • Copy both the modlr file and the Start Prototype Data XML file.
    • These files contain the values that you see in the debugger for the instances of the classes in the model.

42. Create a new class Class1 on the diagram next to Person.

43. Drag on a relation (or association) from Class1 to Person.

  • This creates a relationship between the two classes.

44. Find Class1 in Tree and double-click to see the autodiagram.

45. Go back to the diagram, and select the relation by clicking the line. Press delete (del key). Deleting only removes the relation from the diagram, not from the model.

46. Search for Class1 in the Tree, click on the auto diagram, and observe that it is still there.

47. Go back to the diagram, right-click Class1, find Introduce&Remove, then select Re-introduce removed associations and click on it.

  • Notice that the association you removed is now back on the diagram.

48. Select the end of the association - note the blue circle. While pressing the ctrl key, click and hold the association end. Hold and drag the association end and let it go when you are over Car. You have now moved an association.

49. Check the AutoDiagram, click the different classes, and make sure it says the same thing as the diagram. Go back and forth a few times and move the association ends of this relation.

50. Select the Class1 - Press the delete key. Check the tree and auto diagram - it is still in the model. The delete key only removes things from the diagram - not from the model.

51. Find Class1 in Tree and click-drag it onto the diagram surface. Note that even its associations re-appear.

52. To delete something from a diagram and the model, you must press ctrl+delete.

  • Select the association by clicking the line.
  • Press ctrl+delete to delete.
  • Check the tree, and check that the autodiagram is gone.

53. Sometimes, you want a relation from a class back to the same class.

  • Create a relation by selecting the tool - click hold drag on Class1 - let go of the mouse button over the open diagram space.
  • Note the via point.
  • Move the mouse to yet a new position in the open diagram space - click to add yet a via point.
  • From here, move the mouse back over Class1 - click again. Note a relation to self has been created.

54. Check the autodiagram - note that Class1 now appears three times when shown. This is how an association to self is shown in an autodiagram.

55. The autodiagram always shows the selected class and its surroundings completely. A diagram only shows the things from the model you have explicitly added to it.

56. In the diagram, in Class1 - add two new attributes (right click - add Attribute - twice). Select attribute1 - press the delete key, and note that it disappears. Check the autodiagram - it still shows two attributes.

57. Go back to the diagram using the back-and-forth arrows next to the play button in the top toolbar.

58. Press ctrl-Z for undo - note that attribute1 re-appears.

59. Press ctrl-Y for redo - observe that attribute1 disappears again.

60. Right-click Class1, find Features/Pick Shown features, find attribute1, and move it back to shown - close the dialog.

  • Note that attribute1 is now back in the diagram Class1 display.

61. Select Class1 - press ctrl+delete. Now, Class1 is deleted, and it deleted its attributes and associations.

62. Create a new class and name it BrandOfCar. Remember to save your model often - ctrl-S.

63. Add an attribute of type string called Name.

64. Drag an association from Brand to Car. Note that the multiplicity marking on the association should say that Car can have 0..1 Brand, and Brand has 0..*(many) Cars.

65. Open the debugger - add Brand-instances (Open Brand seeker, Add new). Name them Volvo, BMW, and Tesla - save the Debugger data.

66. In the debugger, open Autoform of a Car.

  • Note the new ability to assign a Brand.
  • Use it to assign a Brand and save the data.

67. Go back to the diagram, (Close debugger + run dialog), select Car, and in the property inspection, find DefaultStringRepresentation. Press the ... to open the OCL Editor.

OCL is a language that allows you to navigate and pinpoint everything in the model. You can write freehand OCL or use the context help of the OCL editor. All snippets of OCL code are called expressions. Expressions are like math functions, not only with numbers but also with information. The information you use in the expressions must be present in your model. This makes it easy to ensure that all expressions are valid. When you save the model, we start a validation and you will see a red dot when there is an error.
  • OCL is object-oriented - and the self/this variable of OCL is called self.
  • Write self. RegistrationNumber+' of type '+self.BrandOfCar.Name in the expression for DefaultStringRepresentation.
  • Click "okay" in the OCL editor. Save. Check for red dots. Try to introduce an error by saying Name2 instead of Name - save - see the red dot warning. Fix the issue.

68. The DefaultStringRepresentation of an object is used whenever we are not explicit with an attribute and we need a representation in the string form of the object. The debugger Autoforms use this. Go into the debugger and see the header of Autoforms following your rule from above.

69. OCL is short for Object Constraints Language; many simple OCL tools use OCL only for constraints. In MDriven, we use OCL for almost everything.

  • Go to Car - find Constraint in the Property Inspector - click ... - Add a new Constraint.
  • The OCL expression of a constraint should validate to true; otherwise, the constraint is broken.
  • Add the constraint self.BrandOfCar->notempty.
  • This means that we will be true if we have a Brand associated and false if we do not.
  • Enter the description of the constraint: Cars MUST have a Brand! - say that this is of type Warning.

70. Go into debugger Autoforms. Check what happens when an object breaks the constraint (remove a brand of a car).

71. In MDriven, we can design ViewModels that use OCL expressions to build information elements that can be presented to a user. There is a quick way to do the default and show everything - it is called Autoforms.

  • Right-click the diagram surface and find Autoforms/Create-refresh-Autoforms.
  • Note the tree is filled up with ViewModels and Actions all tagged with AutoForm.

72. Right-click the diagram surface - find Autoforms/Drop-Autoforms.

  • Note that the tree is cleared of actions and ViewModels tagged with AutoForm.

73. Add back the AutoForms. Press Play Now instead of the debugger and pick Run Model and continue modeling.

  • Familiarize yourself with the Menu and forms.
  • Note that this system uses the same data as in the debugger.

74. AutoForms are just the beginning - though, they may feel a bit clunky. Reduce clunkiness by setting properties in the model.

  • For example, select association-end BrandOfCar, in propertyinspector, find AutoForms and change the value from SearchAndPick to Combobox.
  • Go to Autoforms, then Create/Refresh Autoforms.
  • Save the model.
  • Switch back to the WECPOF window that can be kept open while changing the model.
  • Press MODEL-REFRESH. Notice this different way to assign brands.

75. Assign a null brand. Do you see the broken constraint?

Next Chapter

Bootcamp:Chapter 3

This page was edited 58 days ago on 09/16/2024. What links here