You will often have user scenarios that require you to copy an existing object structure, such as handling revisions of things - the user wants a new revision but wants you to start with the old object structure and tweak a few values.

In cases like these, it would be neat to use a cloning mechanism for your modeled objects. Once you have a cloning mechanism, you will soon discover that cloning one object will not suffice. You want to follow associations and clone owned child objects – but only in some cases; in other cases, you don’t want to clone the child objects, but rather reference the same ones from the clone.

Given this model:


Let a ViewModel act as a definition for what the clone should do - like this, for example:


Then, in OCL, do:


The deep clone operator returns the new Person object, complete with copies of OwnedBuildings and the Home links.

A good place to use this operator is in a method on a class, for example, CreateClone() : Person. That way, you just call self.CreateClone from a class action using the returned object as the root of the ViewModel the action opens.

See also the OCLOperators_transform operator which is a DeepClone that can change type.

This page was edited 29 days ago on 06/17/2024. What links here