Unique Instantiation

This pattern is one of the fundamental MT design patterns.

Summary

To avoid duplicate creation of objects in the target model, a check can be made that an object satisfying specified properties does not already exist, before such an object is created.

Application conditions

Required when duplicated copies of objects in the target model are forbidden, eg., because a target entity TE possesses an identifier (primary key) attribute, or an attribute att whose values are asserted to be unique in the target model: TE.allInstances()->isUnique(att).

Solution

In the case of a transformation rule creating an instance t : TE with an identity/unique attribute setting t.id = v, check if a TE instance object with this id value already exists, and if so, use the object to establish the remaining rule effects on t.

Benefits

The pattern avoids the construction of erroneous target models. It can also be used when we wish to share one subordinate object between several referring objects: the subordinate object is created only once, and is subsequently shared by the referrers.

This pattern assists in the decomposition of a transformation, enabling a target object to be created in one phase and referred to and modified by a subsequent phase.

It can be used to ensure the Hippocraticness property of bidirectional transformations: that a target model is not modified if it already satisfies the transformation relation with respect to a modified source model.

Disadvantages

The cost of checking for existing objects can be significant. Look up of objects by key values, using the Object Indexing pattern, can reduce this cost.

Applications and examples

This pattern is applicable to all categories of transformation. The pattern is provided as an inbuilt implementation mechanism in UML-RSDS, and in the `check before enforce' matching semantics for QVT-R rules: target model elements are only created to satisfy a rule if no element already exists that satisfies the rule. The unique lazy rules of ATL are similar in their purpose: such rules use the internal ATL tracing mechanism to look up previously mapped target elements and return these, instead of creating a new target element. Similar mechanisms are the use of equivalent/equivalents in ETL, and resolveIn/resolveoneIn in QVT-O.

Related patterns

Object Indexing can be used to efficiently obtain an existing object with a given primary key value. The pattern facilitates the use of the Entity Merging pattern.

Traces defined by Auxiliary Metamodel or inbuilt to a transformation language can be used to ensure unique instantiation: the trace can be checked for a record of a previous creation of a target object, in order to avoid re-creating it.

The pattern is analogous to the classical Singleton pattern, where creation of more than one instance of the Singleton class is prevented by the pattern.