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.
Procedure
To modify the method and add an override:
Example
protected CreateRule getCreate_Rule(FeatureAdapter referenceAdapter) {
CreateRule rule = new CreateRule(
CREATE_RULE,
InsertOwnerMessages.Class2Class_Transform_Create_Rule,
this,
referenceAdapter,
UMLPackage.Literals.CLASS) {
@Override
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;
}