Deepclone
No edit summary
(Automatically adding template at the end of the page.)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Pretty often you have user scenarios that require you to copy an existing object structure. Like when handling revisions of things and the user wants a new revision but want to start with the old and just tweak a few values.
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 with a clone mechanism of your modeled objects. And once you have a clone mechanism you pretty soon will discover that cloning just 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.
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 viewmodel act as a definition for what the clone should do,, like this for example;
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 you do;
Then, in OCL, do:
  self.deepclone('ViewModel_Person_DeepClone')
  self.deepclone('ViewModel_Person_DeepClone')
The deepclone operator returns the new Person object, complete with copies of OwnedBuildings and the Home links.
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.
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.


Also see the [[OCLOperators_transform]] operator that is a a DeepClone that can change type.
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:

Simpleclassstructure.png

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

Deepclonedefinition.png

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.

This page was edited 96 days ago on 02/10/2024. What links here