You can use this chapter to understand how MDriven Designer diagrams relate to the model, write your first OCL expressions and constraint, and generate AutoForms from the model. It is for learners who have completed Bootcamp Chapter 1 and have the Person and Car model with saved XML prototype data.
Before you begin
Complete Chapter 1 first. This chapter continues from its model, including:
- The classes Person and Car.
- The RegistrationNumber attribute on Car.
- Saved debugger XML data, including Person and Car instances.
Save frequently with Ctrl+S. When you save, MDriven Designer validates the model. A red error indicator means that an expression or other model definition is invalid.
Make a checkpoint copy
- Open the folder where you saved the
.modlrfile. - Create a subfolder named
TasksUpTo40. - Copy both of these files into that folder:
- Your model file (
.modlr). - The Start Prototype Data XML file.
- Your model file (
The XML file stores the object values displayed in the debugger. Keeping both files in the checkpoint folder lets you return to the state completed in Chapter 1.
Diagrams and the model
A diagram is a selected visual presentation of model elements. The model contains the classes, attributes, and associations themselves. Removing an element from a diagram does not necessarily delete it from the model.
An AutoDiagram is generated from the selected class and its connected surroundings. Unlike a manually maintained diagram, it automatically shows the selected class and its relevant associations.
Create and inspect an association
- In your existing diagram, create a class next to Person. Name it
Class1. - Create an association from Class1 to Person by using the association tool and dragging from Class1 to Person.
- In the model tree, search for
Class1and double-click it to open its AutoDiagram.
The AutoDiagram shows Class1 and the association to Person because the association is present in the model.
Remove an association from a diagram only
- Return to your manually maintained diagram.
- Select the association line between Class1 and Person.
- Press
Delete. - Search for Class1 in the tree and open its AutoDiagram again.
The association still appears in the AutoDiagram. Delete removed its visual representation from the current diagram; it did not remove the association from the model.
To show the removed association in the diagram again:
- Return to the manually maintained diagram.
- Right-click Class1.
- Select Introduce&Remove, then Re-introduce removed associations.
Move an association end
- Select an end of the association. The selected end has a blue circle.
- Hold
Ctrl. - Click and hold the association end, drag it to Car, and release it over Car.
- Open the AutoDiagram for Class1 and inspect the result.
The association now connects Class1 and Car. Compare the manually maintained diagram with the AutoDiagram while moving association ends. The AutoDiagram reflects the current model; the manual diagram shows only elements you have introduced to it.
Remove and restore a class on a diagram
- In the manual diagram, select Class1 and press
Delete. - Check the model tree and the AutoDiagram.
- Drag Class1 from the tree onto the diagram surface.
Class1 and its associations return to the diagram because Class1 remained in the model.
Delete an association from the model
To remove an element from both the diagram and the model, use Ctrl+Delete.
- Select the association line you created.
- Press
Ctrl+Delete. - Check the tree and the AutoDiagram.
The association is gone because it was deleted from the model.
Create a self-association
A self-association is an association from a class back to the same class. For example, a Person model could use one to express that one person manages another person.
- Select the association tool.
- Click and drag from Class1 into empty diagram space, then release the mouse button.
- Move the pointer to another empty location and click to add another via point.
- Move the pointer back over Class1 and click.
- Open the AutoDiagram for Class1.
The AutoDiagram displays Class1 three times to show the two roles and their self-association. This is expected for a self-association.
Show and hide features without deleting them
- In the manual diagram, right-click Class1 and select Add Attribute twice.
- Select
attribute1and pressDelete. - Open the AutoDiagram for Class1.
The AutoDiagram still shows both attributes. The attribute was hidden in the current diagram, not removed from the model.
Use the back and forward arrows next to the Play button in the top toolbar to return to the diagram if needed.
- Press
Ctrl+Zto undo the hide operation. The attribute reappears. - Press
Ctrl+Yto redo it. The attribute disappears from the manual diagram again. - Right-click Class1 and select Features/Pick Shown features.
- Find
attribute1, move it to the shown features list, and close the dialog.
To delete Class1 and everything it owns in the model:
- Select Class1.
- Press
Ctrl+Delete.
This deletes Class1, its attributes, and its associations.
Add car brands and prototype data
- Create a new class named
BrandOfCar. - Add an attribute named
Namewith typestring. - Create an association from BrandOfCar to Car.
- Verify the multiplicities express this business rule: a Car has zero or one BrandOfCar, and a BrandOfCar can be associated with zero or many Cars.
- Save the model with
Ctrl+S.
Create BrandOfCar instances
- Start the debugger.
- Open a seeker for BrandOfCar.
- Use Add New to create BrandOfCar instances.
- Set their Name values to
Volvo,BMW, andTesla. - Save the debugger data.
- Open the AutoForm for a Car and assign one of the brands.
- Save the debugger data again.
The Car AutoForm now exposes the association so that you can assign a BrandOfCar instance.
Use OCL for object text
OCL (Object Constraint Language) is the expression language used to navigate and calculate from information in your model. An OCL expression evaluates in a context. In an expression on Car, self means the current Car object.
DefaultStringRepresentation defines the text used when MDriven needs a string representation of an object, including AutoForm headers.
- Close the debugger and the Run dialog.
- Select Car in the diagram.
- In the Property Inspector, find DefaultStringRepresentation.
- Click the
...button to open the OCL Editor. - Enter this expression:
self.RegistrationNumber+' of type '+self.BrandOfCar.Name- Click OK and save the model.
- Check that no red validation indicator appears.
For example, a Car with RegistrationNumber OldCar1 and BrandOfCar Name Volvo is represented as OldCar1 of type Volvo.
To see validation in action, temporarily replace Name with Name2, then save. The model reports an error because Name2 is not an attribute in the model. Restore Name and save again.
- Start the debugger and open a Car AutoForm.
- Verify that its header uses the DefaultStringRepresentation expression.
Add an OCL constraint
A constraint is an OCL expression that must evaluate to true. If it evaluates to false, the object breaks the rule. A constraint can be reported as a warning.
- Close the debugger and Run dialog.
- Select Car.
- In the Property Inspector, find Constraint and click
.... - Add a new constraint.
- Set its OCL expression to:
self.BrandOfCar->notempty- Set the description to
Cars MUST have a Brand!. - Set the constraint type to Warning.
- Save the model.
This expression is true when the Car has a BrandOfCar association and false when it does not.
- Open a Car AutoForm in the debugger.
- Remove the car's brand assignment.
- Observe the warning for the broken constraint.
Generate and test AutoForms
A ViewModel defines information and actions that can be presented to a user. AutoForms generate default ViewModels and actions from the model, giving you a quick interface for working with modeled data.
Create and remove generated AutoForms
- Right-click empty space on the diagram.
- Select Autoforms/Create-refresh-Autoforms.
- Inspect the tree. Generated ViewModels and Actions are tagged
AutoForm. - Right-click empty space on the diagram again and select Autoforms/Drop-Autoforms.
Dropping AutoForms removes the generated AutoForm-tagged ViewModels and Actions from the tree.
Run the generated interface
- Create the AutoForms again with Autoforms/Create-refresh-Autoforms.
- Press Play Now rather than opening the debugger.
- Choose Run Model and continue modeling.
- Use the generated menus and forms to inspect and edit the same prototype data used by the debugger.
Change the BrandOfCar editor
AutoForms use model properties to determine how an association is edited. The default BrandOfCar editor is SearchAndPick. Change it to a combo box for this association.
- Return to the diagram and select the association end named
BrandOfCaron Car. - In the Property Inspector, find AutoForms.
- Change the value from
SearchAndPicktoCombobox. - Right-click the diagram surface and select Autoforms/Create-refresh-Autoforms.
- Save the model.
- Return to the open web client window and select MODEL-REFRESH.
- Open a Car and inspect the BrandOfCar editor.
The association is now presented as a combo box rather than the SearchAndPick editor.
- Assign no brand to a Car.
- Confirm that the
Cars MUST have a Brand!warning is shown.
What you learned
- A manual diagram is a visual selection of model elements; an AutoDiagram shows a selected class and its surroundings.
Deleteremoves an item from the current diagram, whileCtrl+Deleteremoves it from the model.- OCL expressions can navigate associations, calculate display text, and define constraints.
- DefaultStringRepresentation controls the default text representation of an object.
- AutoForms generate ViewModels and actions from your model, and model properties can change the generated editor behavior.
Next chapter
Continue with Bootcamp Chapter 3.
