< Previous | Next >

Lesson 4: Create and refine a class-to-interface mapping declaration

This lesson shows you how to create a class-to-interface mapping declaration in the mapping model. This mapping declaration contains mapping rules that create an interface whose name is derived from the name of the class in the source model, and that copy only the public methods of the class in the source model.

To create a class-to-interface mapping declaration in the mapping model:

  1. If the Generalize_Classes.mapping file is not open in the mapping editor, in the Package Explorer view, double-click the file.
  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 Class2Interface; then click OK. The map is displayed in the Outline view, and opens in the editor area under the Mapping Root section.
    Note: To view the details of a mapping declaration that is not open in the editor area, in the Outline view, expand the name of the mapping declaration. To open a different mapping declaration, in the Outline view, click the name of the mapping declaration.

Add input and output objects to the class-to-interface mapping declaration

After you create the class-to-interface mapping declaration, you specify a UML class as the input object, and a UML interface as the output object.

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

  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 Class, 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 Interface, and click OK.
  5. Click File > Save.
You can now define mapping rules between the attributes of the class input object and the interface output object.

Define mapping rules between the attributes of input and output objects

After you add an input and output object to the class-to-interface mapping declaration, you can define the mapping rules between the attributes of the input and output objects.

For this lesson, you create a custom mapping rule that creates an interface called IClassName. You also create a submap mapping rule that invokes the operation-to-operation submap that you create in Lesson 3: Create and refine a class-to-class mapping declaration, and add an input filter to the submap mapping rule to specify that only public operations are transformed into the target interface.

To define the mapping rules between the attributes of the input and output objects:

  1. Create a custom mapping rule between the name attribute of the input and output object:
    1. In the editor area, in the class input object, click the name attribute.
    2. Drag the handle of the name attribute to the name attribute in the interface output object. A move mapping rule is created.
    3. On the Move element that connects the name attributes, 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, and in the text area under the method signature, type the following code: Interface_tgt.setName("I"+Class_src.getName());
      Note: To invoke the content assist functionality, as you type the code, press Ctrl+Space.
    6. Click Apply.
  2. Create a submap mapping rule between the ownedOperation attribute of the input and output object.

    1. In the editor area, in the class input object, click the ownedOperation attribute.
    2. Drag the handle of the ownedOperation attribute to the ownedOperation attribute in the interface output object. Because the ownedOperation attribute is a collection, a submap mapping rule is created by default.
    3. If the Properties view is not open, right-click the new Submap element; then click Show in Properties.
    4. If it is not selected already, in the Properties view, on the Details tab, from the Map list, select Operation2Operation.
  3. To the submap mapping rule that you create in step 2, add an input filter that transforms only the public operations into the target interface:
    1. In the Properties view, on the Input Filter tab, click Filter Input Elements, and click In-line.
    2. In the text area under the Code option, under the method signature, type the following code: if (ownedOperation_src.getVisibility().equals(VisibilityKind.PUBLIC_LITERAL)) { return true; } return false;
      Note: The code that you specify for an input or output filter must return a Boolean value. You can specify only the method body; the transformation authoring framework defines the method signature. To see a list of valid variable names, in the text area under the Inline button, press Ctrl+Space.
    3. Click Apply.
  4. Click File > Save.
In the next lesson, you create several mapping declarations in the Generalize_Classes.mapping model; these mapping declarations define how parameters and primitive types are transformed when you run the generated transformation.
< Previous | Next >

Feedback