November 11th, 2008
Copying EMF objects accross namespaces
I am currently preparing a new release for my GenGMF project. Because the new meta model contains important but incompatible changes I have migrated the GenGMF tooling to a new name space and created a migration script for converting the specific editor models. The new meta model is much smaller than the old one which results in smaller models — I’ve eliminated the need for the mapping elements in the templates. To make the migration as easy as possible I’ve created some helper functions to move corresponding attributes and references. Only the “real” changes in the meta model are migrated using a Xtend script.
To demonstrate the facilities of the CrossNamespaceCopier
I’ve created a small meta model…
As you see the Class2
from the “http://source.namespace
” has been eliminated in the “http://target.namespace
” and both attributes have been moved – attribute2
to Class1
and attribute3
to Class3
. In order to migrate a model instance one has to call the CrossNamespaceCopier
which copies the objects while migrating the name spaces according to rules previously set. OtherClass
objects from the “http://other.namespace
” will be copied as they are. All references and attributes which could not be automatically migrated need to copied in a small script manually.
extension CrossNamespaceCopier;
namespace::target::Class1 migrate(namespace::source::Class1 sc1)
: let tc1 =
// initialization for the copier, but the
// let statement needs to be in the beginning
( crossNsInitStaticInstance()
-> crossNsSetupNamespaceMapping(
"http://source.namespace",
"http://target.namespace")
// the content for the tc1 variable
-> ( (namespace::target::Class1)
crossNsCopy(sc1)))
// skipping Class2 in source
: tc1.setClass3Reference(
(namespace::target::Class3)
sc1.class2Reference.class3Reference
.crossNsCopy())
// pull up attribute2
-> tc1.setAttribute2(
sc1.class2Reference.attribute2)
// pull down attribute3
-> tc1.class3Reference.setAttribute3(
sc1.class2Reference.attribute3)
// uninitializing
-> crossNsReleaseStaticInstance()
-> tc1
;
You will find the CrossNamespaceCopier
as well as the demo files contained in a zip file.