< Previous | Next >

Lesson 6: Create and refine a package-to-package mapping declaration

This lesson shows you how to create a package-to-package mapping declaration and multiple mapping rules. The mapping rules specify how the generated transform processes nested packages or class elements in the package that the source model contains.

The package input and output objects contain a feature called packagedElement. This feature is a collection that contains different types of valid UML objects. The mapping rules that you create in this lesson define how the transformation processes the collection elements that are of type package or class.

In this lesson, you create the following mapping rules:
  • A move mapping rule that creates a package in the target project; this package has the same name as the package in the source model
  • A submap mapping rule that invokes the Class2ClassTransform transform if the element is of type class
  • A submap mapping rule that invokes the Class2InterfaceTransform transform if the element is of type class
  • A submap mapping rule that invokes the Package2PackageTransform transform if the element is of type package

Lesson 3: Create and refine a class-to-class mapping declaration showed that for each move mapping rule in a mapping declaration, a rule that copies an attribute value from the source model to the target model is added to the generated transform source code. For each submap mapping rule, an extractor that extracts elements in the specified collection is generated in the transform source code. The submap rule is applied to an object if the current input object is an instance of the input type that is defined in the mapping declaration.

When you run the generated transformation, if the source model contains a package, the Package2PackageTransform transform is invoked and creates a package with the same name in the target model. The Package2PackageTransform transform traverses the collection of the packagedElement feature. For each collection element that is of type package, which means that the source model contains nested packages, the transform invokes the Package2PackageTransform transform. For each collection element that is of type class, the transform invokes the rules to transform the class into a corresponding class and interface in the target output model.

To create a package-to-package mapping declaration in the mapping model:

  1. If the Generalize_Classes.mapping file is not open in the transformation 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 Package2Package and click OK. The map is displayed in the Outline view, and opens in the editor area under the Mapping Root.

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

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

To add an input object and an output object to the package-to-package 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 Package, 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 Package, and click OK.
  5. Click File > Save.
You can now define the mapping rules between the features of the input and output objects.

Define mapping rules between attributes of the package input and output objects

After you add the package input and output objects to the mapping declaration, you can create the mapping rules between the attributes of the input and output objects.
Note: When you generate the transformation source code, a transform is generated for each mapping declaration. Each generated transform contains a getAccept_Condition() method. By default, this method returns true if the current input element is an instance of the input object that is defined in the mapping declaration. If the method returns false, the transform does not process the current input element. In this lesson, you can create input filters that also verify whether the input object is an instance of the input type. Although these input filters are redundant, they are also harmless, and only demonstrate how to create input filters. Input filters in submap mapping rules must be written in Java and return a Boolean value.

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

  1. Create a move mapping rule between the name attributes by dragging the handle of the name attribute of the input object to the name attribute in the output object.
  2. Create a submap mapping rule between the packagedElement attribute of the input and output object, and specify that this submap invokes the Class2ClassTransform transform if the element is of type class:
    1. Drag the handle of the packagedElement attribute of the input object to the packagedElement attribute in the output object. Because the packagedElement attribute is a collection, the mapping editor creates a submap mapping rule by default.
    2. In the editor area, 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 Class2Class.
    When you run the generated transformation, for each element in the input package, the transformation invokes the Class2ClassTransform transform, but only if the element is of type class.
  3. Optional: In the submap rule that you created in step 2, create an input filter that verifies whether the current object is an instance of a class:
    1. In the editor area, right-click the Submap element that you created in step 2.
    2. In the Properties view, on the Input Filter tab, select the Filter Input Elements check box.
    3. Click In-line.
    4. In the text area under the Code field, under the method signature, specify the following code: return packagedElement_src instanceof org.eclipse.uml2.uml.Class;
    5. Click Apply.
  4. Create a submap mapping rule between the packagedElement feature of the input and output object, and specify that if the current element in the packagedElement collection is of type class, the transform called Package2PackageTransform transforms the class into an interface in the target model.
    1. Repeat step 2a to create another submap between the packagedElement features.
    2. In the editor area, 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 Class2Interface.
  5. Optional: In the submap rule that you created in step 4, create an input filter that verifies whether the current object is an instance of an interface:
    1. In the editor area, right-click the Submap element that you created in 4; then click Show in Properties.
    2. Repeat steps 3b, 3c, 3d, and 3e to specify the same input filter.
  6. Create a submap mapping rule that processes nested packages. If the current element in the packagedElement collection is a package, the transformation invokes the Package2PackageTransform transform.
    1. Repeat step 2a to create another submap between the packagedElement features.
    2. In the editor area, 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.
  7. Optional: In the submap rule that you created in step 6, create an input filter that verifies if the current input element is an instance of a package:
    1. In the editor area, right-click the Submap element that you created in 6; then click Show in Properties.
    2. Repeat steps 3b and 3c to specify that this rule has an input filter.
    3. In the text area under the Code field, under the method signature, specify the following code: return packagedElement_src instanceof org.eclipse.uml2.uml.Package;
    4. Click Apply.
  8. Click File > Save.
The mapping declaration contains one move mapping rule between the name attributes, and multiple submap rules between the packagedElement attributes.
< Previous | Next >

Feedback