Hans Karlsen (talk | contribs) No edit summary |
(Automatically adding template at the end of the page.) |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
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 | 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 | Given this model: | ||
[[File:Simpleclassstructure.png|none|thumb|549x549px]] | [[File:Simpleclassstructure.png|none|thumb|549x549px]] | ||
Let a ViewModel act as a definition for what the clone should do - like this, for example: | |||
[[File:Deepclonedefinition.png|none|thumb|815x815px]] | [[File:Deepclonedefinition.png|none|thumb|815x815px]] | ||
Then in OCL | Then, in OCL, do: | ||
self.deepclone('ViewModel_Person_DeepClone') | self.deepclone('ViewModel_Person_DeepClone') | ||
The | 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 | A good place to use this operator is in a method on a class, for example, <code>CreateClone() : Person</code>. That way, you just call <code>self.CreateClone</code> 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. | |||
[[Category:OCL]] | [[Category:OCL]] | ||
[[Category:Operators]] | [[Category:Operators]] | ||
[[Category:EAL]] | |||
{{Edited|July|12|2024}} |
Latest revision as of 15:33, 10 February 2024
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:
self.deepclone('ViewModel_Person_DeepClone')
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.