< Previous | Next >

Lesson 7: Create and refine a model-to-model mapping declaration

This lesson shows you how to create a model-to-model mapping declaration in the mapping model. This mapping declaration contains a custom mapping rule that, when you run the generated transformation, creates a target output model whose name is derived from the source input model. This lesson also shows you how to change the order in which the transformation processes the mapping declarations in the mapping model.

About this task

To rename the target model that the transformation generates, you can create a custom mapping rule between the input and output objects, instead of creating a mapping rule between the features of the input and output objects.

To create a model-to-model mapping declaration:

Procedure

  1. If the mapping model is not open in the transformation mapping editor, in the Package Explorer view, double-click the .mapping file in the model folder.
  2. In the transformation mapping editor, in the Mapping Root section, right-click Generalize_Classes; then click Create Map.
  3. In the New Map window, in the Map name field, type Model2Model and click OK. The map is displayed in the Outline view, and opens under the Mapping Root in the editor area.

Add input and output objects to the model-to-model mapping declaration

About this task

After you create the model-to-model mapping declaration, you must add an input and output object to it. In this lesson, you specify a model as the input and output object.

To add an input object and an output object to the model-to-model mapping declaration:

Procedure

  1. Click the Add an input object icon, which is the leftmost icon on the toolbar of the map that you are editing.
  2. In the Add Input window, in the Element pane, expand uml, click Model, and click OK.
  3. Click the Add an output object icon, which is the second icon from the left on the toolbar of the map that you are editing.
  4. In the Add Output window, in the Element pane, expand uml, click Model, and click OK.
  5. Click File > Save.

Results

You can now define the mapping rules between the input and output objects.

Define the custom and submap mapping rules

About this task

In this lesson, you create the following mapping rules:
  • A custom mapping rule that renames the target model that the transformation generates
  • A submap mapping rule that invokes the Package2PackageTransform transform, to transform the packages in the model

To create the mapping rules in the mapping declaration:

Procedure

  1. Create a custom mapping rule that renames the target model:
    1. In the editor area, click the top compartment of the Model input object. The entire model input object is highlighted.
    2. Drag the handle of the Model input object to the Model output object.
    3. On the new Submap element, click the down arrow and click Custom.
    4. Right-click the Custom element; then click Show in Properties.
    5. In the Properties view, on the Details tab, click In-line.
    6. In the text area below the Code field, under the method signature, specify the following code, which implements the custom mapping rule when you run the transformation: Model_tgt.setName(Model_src.getName()+"TgtModel");
    7. Click Apply.
    Note: To rename the target model, you can also create a custom mapping rule between the name attribute of the input object and output object, and specify the same code on the Details tab, instead of creating a mapping rule between the input and output objects.
  2. Create a submap mapping rule that invokes the Package2Package mapping that you created in Lesson 6: Create and refine a package-to-package mapping declaration:
    1. Drag the handle of the packagedElement feature to the packagedElement attribute in the model output object.
    2. Right-click the new Submap element; then click Show in Properties.
    3. In the Properties view, on the Details tab, from the Map list, select Package2Package.
  3. Click File > Save.

Results

You have created all the mapping declarations and mapping rules that are required for this tutorial. You can now specify the processing order of the mapping declarations.

Change the processing order of mapping declarations

About this task

You can change the processing order of mapping declarations in a mapping model. The Outline view lists the mapping declarations in the order that they are processed when you run the generated transformation code. By changing the processing order, you can specify processing instructions for input objects that might be processed and consumed by a less specific mapping declaration.

For this tutorial, specify that the generated transformation processes the mapping declarations in the following order:
  • Model2Model
  • Package2Package
  • Class2Class
  • Class2Interface
  • Operation2Operation
  • Parameter2Parameter
  • Primitive2Primitive
Note: In this tutorial, the order of the Model2Model and Package2Package mapping declarations is most important. When you run the generated transformation, because a model is a package, the Package2Package transform accepts a model or a package. If the Package2Package transform is the first transform listed in the transformation, it processes and consumes the model input object. In this scenario, the Model2Model transform does not process the model input object; therefore, the custom rule that you created in the Model2Model trasnform does not run.

To change the processing order of the mapping declarations:

Procedure

  1. If the Outline view is not visible, click Window > Show View > Outline.
  2. In the Outline view, right-click the Model2Model mapping declaration; then click Execution Order, and click Move up. Repeat this step until the Model2Model mapping declaration is at the top of the list.
  3. Repeat step 2 for each mapping declaration until the mapping declarations are in the same order as the list above.
  4. Click File > Save.

Results

You can now generate the transformation source code.
< Previous | Next >

Feedback