You can use this first Bootcamp chapter to create a small object model in MDriven Designer, enter test data in the debugger, persist it as XML, and model a one-to-many association between people and cars.
What you will build
You will create two classes:
- Person, with a
Nameattribute. - Car, with a
RegistrationNumberattribute.
You will then connect them so that a person can have zero or more cars they previously owned, while each car has zero or one previous owner.
| Model element | Name | Meaning in this exercise |
|---|---|---|
| Class | Person
|
A type of object representing a person. |
| Attribute | Person.Name
|
A string value, such as Alex.
|
| Class | Car
|
A type of object representing a car. |
| Attribute | Car.RegistrationNumber
|
A string value, such as OldCar1.
|
| Association end | CarsIUsedToOwn
|
The cars associated with a person; cardinality 0..*.
|
| Association end | PreviousOwner
|
The person associated with a car; cardinality 0..1.
|
Before you start
- Complete the installation instructions in the Before Starting Guide.
- Start MDriven Designer.
- Create a new empty model.
- Save it in a location you can find again. This chapter uses
C:\temp\MDrivenEducation\Start.modlras an example; use an equivalent location if needed.
Save the model regularly with Ctrl+S. The model file stores the class definitions, attributes, and associations that you create. The test objects you create later are saved separately in the XML persistence file.
Create the Person class
- Create a class named
Person. - Add an attribute named
NametoPerson. - Set the attribute type to
string. - Save the model.
For example, a Person object can later have Name set to Alex.
Run the model with XML persistence
XML persistence stores the objects created during this exercise in an XML file on disk. It is appropriate here because you are learning the model and debugger workflow.
- Select the Play button at the left side of the top menu.
- Choose XML persistence.
- Select Start system.
- Select Show debugger.
Query objects in the debugger
The debugger lets you inspect and create objects while the model is running. In its Expression box, enter the expression used in this Bootcamp exercise:
person.allinstancesExecute the expression. The result list is empty because you have defined the Person class but have not created any Person objects yet.
Create and save Person objects
- In the debugger, open a Seeker. A Seeker is the search and result view for objects of a selected class.
- Select the
Personclass. The search result is empty. - Select Create New twice to create two rows.
- Select the first row, right-click it, and select Open.
- In the AutoForm, enter your name in the
Namefield. - Open the second row and enter another name, such as
SomeOtherDude. - In the debugger, use the save button to save the created objects.
Run the expression again:
person.allinstancesYou should now see two rows. This verifies that two Person objects exist in the running system.
Inspect the persisted XML
- Find the XML file on disk in the same location as the model file.
- Open the XML file in Notepad or another text editor.
- Search for one of the names you entered.
- Close the text editor without changing the file.
This confirms that XML persistence saved the test objects outside the model definition.
Verify that objects survive a restart
- Close the debugger.
- Close the Play dialog, also called the system prototyper.
- Close MDriven Designer and save if prompted.
- Start MDriven Designer again.
- Open the model from Recent files.
- Select Play, choose XML persistence, start the system, and open the debugger.
- Execute
person.allinstancesagain.
Verify that the two Person objects are still present. If the result is empty, check that you reopened the same model and started it with XML persistence.
Find model elements in the model tree
The model tree at the right side of MDriven Designer lets you find elements throughout the model.
- Close the debugger and the Play dialog.
- Type
Personin the search box above the model tree. - Observe the results. You should find both the
Personclass and itsNameproperty. - Select
Personin the tree. - In the property inspector below the tree, change its default color to cyan, or choose another color you can recognize easily.
- Press Escape in the search box, or clear it, to return to the complete model tree.
Use this search whenever you need to return to a class, attribute, diagram, or other model element.
Work with Diagram1
A diagram is the visual workspace where you arrange and connect model classes.
- Find
Diagram1in the model tree. - Double-click
Diagram1to open it. - If the diagram is empty, drag
Personfrom the model tree onto the diagram. - If the Start wizard is not visible, select Start! at the upper-left corner.
- Right-click the diagram and select Add class.
- Rename the new class to
Car. - Use the zoom buttons or Ctrl+mouse wheel to adjust the diagram view.
- Add an attribute named
RegistrationNumbertoCarand set its type tostring. - Save the model.
Create the Person-to-Car association
An association defines how objects of two classes relate. In this example, it records historic ownership.
- Enter AssociationMode by selecting the association arrow in the toolbar.
- Click, drag, and hold from
Person. - Move the pointer to
Carand release it to create the association. - Select the association end at
Person's side and name itCarsIUsedToOwn. - Set
CarsIUsedToOwncardinality to0..*. - Select the association end at
Car's side and name itPreviousOwner. - Set
PreviousOwnercardinality to0..1. - Save the model.
The cardinalities mean:
- A
Personmay be associated with no cars or with many cars throughCarsIUsedToOwn. - A
Carmay have no recorded previous owner or one recorded previous owner throughPreviousOwner.
Explore the auto diagram
- Search for
Carin the model tree. - Double-click
Car.
This opens an auto diagram rather than Diagram1. The auto diagram centers the selected class and shows connected classes, including Person.
- Select
Personin the auto diagram. - Observe that the auto diagram recenters on
Personand keepsCaras a connected class. - Use the model-tree search to find and reopen
Diagram1when you want to return to your manually arranged diagram.
Create cars for a person
- Select Play.
- Choose XML persistence and open the debugger.
- Execute
person.allinstances. - Select the person object that contains your name.
- Find and select the multi-link button for
CarsIUsedToOwn. - Select Add New three times to create three related cars.
- Open each car AutoForm and set
RegistrationNumberto the following values:
| Car | RegistrationNumber |
|---|---|
| First car | OldCar1
|
| Second car | OldCar2
|
| Third car | OldCar3
|
- Save the debugger data.
You have now created one person with three associated historic cars. The association is visible through the CarsIUsedToOwn multi-link on the selected person.
Checkpoint
Before continuing, verify all of the following:
- The model contains
PersonandCar. Personhas the string attributeName.Carhas the string attributeRegistrationNumber.- The association has the ends
CarsIUsedToOwn(0..*) andPreviousOwner(0..1). - XML persistence contains two saved
Personobjects. - One person has three cars with registration numbers
OldCar1,OldCar2, andOldCar3.
Next chapter
Continue with Training:Bootcamp:Chapter 2.
See also
- Training:Bootcamp:Chapter 2
- Training:Bootcamp:Chapter 3
- Training:Bootcamp:Chapter 5
- Training:Bootcamp:Chapter 7
Prerequisites
Prerequisites
Before starting the exercises, complete the installation instructions in the Before Starting Guide.
You will use MDriven Designer to create and save a model. In this chapter, you will also start the system with XML persistence and inspect the XML file created for the test objects.
When you create the new model, save it in a location you can find again. The chapter uses C:\temp\MDrivenEducation\Start.modlr as an example; an equivalent location is also acceptable.
Continue with Step 1 after completing the installation guidance.
