No edit summary |
No edit summary |
||
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]] | ||
We let a | We 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, 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. | ||
Also see the [[OCLOperators_transform]] operator | Also, see the [[OCLOperators_transform]] operator which is a DeepClone that can change type. | ||
[[Category:OCL]] | [[Category:OCL]] | ||
[[Category:Operators]] | [[Category:Operators]] | ||
[[Category:EAL]] | [[Category:EAL]] |
Revision as of 09:17, 27 February 2023
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:
We 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.
Also, see the OCLOperators_transform operator which is a DeepClone that can change type.