Getting started
What you need:
- Some existing Eclipse installation with Moclodash plugins.
- An eclipse workspace with a plug-in or EMF project containing an ecore model serialized in one or multiple xmi files. Note that:
- These ecore files must all be in the same folder.
- For our approach to be interesting, the mutable properties of the metamodel must be tagged by adding a suffix
_m
to their names.
In this guide, we consider an empty EMF project called “MyProject” containing a folder “model”, in which the ecore files are stored. More preceisely, our starting point is the following:
Generating cloning material
In this first part we generate all the java code required to created runtime representations of models of a given metamodel, including those of memory efficient clones.
- From a Java perspective, go to Run → Run configurations. Right click on CloningMaterialGeneration, and select New.
- There are three fields to fill and a checkbox:
- the folder that contains all the ecore files,
- the project in which the Java code must be generated,
- the name of the metamodel (which is mostly useful when the metamodel contains multiple packages or files).
- if you also want to make a call to the EMF model code generator without handling a genmodel file yourself, tick the checkbox
- Press “Run”. If you enabled the console view, then you can observe the progress. In the end, four packages are created (one per cloning strategy) each containing:
- A
Copier
class, used by the cloner to instantiate java proxy classes when a class is partially shareable, - A
Tags
class, which can tell if a class is shareable or not for this strategy, - Proxy java classes for all classes of the metamodel that are partially shareable.
- A
Using the cloning strategies
In this second part we use the provided API to clone models using the different strategies. Here is a sample of code that creates a model using the same metamodel as previsously, and then clones it using the four cloning strategies:
/* Creating a model */
ResourceSet mymodel = new ResourceSetImpl();
Resource resource = mymodel.createResource(URI.createFileURI("/tmp/mymodel.xmi"));
MymetamodelFactory factory = MymetamodelFactory.eINSTANCE;
ReadOnlyLeaf ro = factory.createReadOnlyLeaf();
ro.setD(42);
ro.setE(false);
MutableLeaf mut = factory.createMutableLeaf();
mut.setF_m(12);
mut.setG_m(true);
Root r = factory.createRoot();
r.setA_m(1337);
r.setB(9000);
r.setC(true);
r.setMutleaf(mut);
r.setRoleaf(ro);
resource.getContents().add(r);
/* Cloning the model with the four strategies */
Cloner deepCloningCloner = new ClonerImpl(MyMMDeepCloningCloningMaterial.getInstance());
ResourceSet clone1 = deepCloningCloner.clone(mymodel, "deepClones");
Cloner shareFieldsOnlyCloner = new ClonerImpl(
MyMMShareFieldsOnlyCloningMaterial.getInstance());
ResourceSet clone2 = shareFieldsOnlyCloner.clone(mymodel, "shareFieldsOnlyClones");
Cloner shareObjOnlyCloner = new ClonerImpl(MyMMShareObjOnlyCloningMaterial.getInstance());
ResourceSet clone3 = shareObjOnlyCloner.clone(mymodel, "shareObjOnlyClones");
Cloner shareAllCloner = new ClonerImpl(MyMMShareAllCloningMaterial.getInstance());
ResourceSet clone4 = shareAllCloner.clone(mymodel, "shareAlldeepClones");