Creating objects without direct correspondences to source objects

By default, the mapping editor enables you to map from elements in a single source container to elements in a single target container. However, you can modify the generated transformation so that you can also create objects in a different target container than the one being mapped to.

About this task

Specifically, you can modify the getCreateRule method in the generated transformation and, in the new implementation, override the insertEObject method for the CreateRule constructor.


To modify the method and add an override:

  1. In the mapping editor, for the submap that defines a transform between the source and the target, locate the reference to a mapping declaration. For example, if you map from typeDefinitions to packagedElements, you might have the mapping declaration typeDef2Class.
  2. Generate the transformation source code. The mapping declaration has a transform generated for it (for example, typeDef2ClassTransform), and the transform contains a getCreate_Rule method.
  3. In the getCreate_Rule method, in the new CreateRule constructor that is instantiated, override the default implementation of the insertEObject method.

    The insertEObject method takes as arguments the already created object and a target container object. The override code can check for the existence of the target container object that you want as the owner. Create the target container object if it does not exist, and retain the default implementation for the actual insert, passing the new owner as the target container.


The following example shows an owner package inserted in a UML Class2Class transform. The example uses a UML package as the target container, but you can use the same technique with any type of target container; that is, any item that could be an output feature that connects to a submap.
     protected CreateRule getCreate_Rule(FeatureAdapter referenceAdapter) {
        CreateRule rule = new CreateRule(
            UMLPackage.Literals.CLASS) {
        protected void insertEObject(EObject targetContainer, EObject target) {
          final String ownerName = "owner";
          if (targetContainer instanceof Model) {
            org.eclipse.uml2.uml.Package owner = ((Model)targetContainer).getNestedPackage(ownerName);
            if (owner == null) {
              owner = ((Model)targetContainer).createNestedPackage(ownerName);
            targetContainer = owner;
          super.insertEObject(targetContainer, target);
        return rule;