Epsilon Model Migration (Flock) - Full Copy

109 Views Asked by At

in the webpage of Epsilon Model Migration Language (https://www.eclipse.org/epsilon/doc/flock/) there is the description of the "Full Copy" primitive. I want to use it to copy a model in the simplest possible way, generating a new model with another name (but essentially is a full copy of the first one). Can you show me some pieces of code? Is my first time with Epsilon. Thanks.

1

There are 1 best solutions below

4
On

Below is a minimal example that demonstrates running Flock from Java to migrate an EMF-based tree model. The complete Maven project is in Epsilon's Git repo.

package org.eclipse.epsilon.examples.standalone.flock;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.epsilon.emc.emf.EmfModel;
import org.eclipse.epsilon.flock.FlockModule;

public class FlockStandaloneExample {
    
    public static void main(String[] args) throws Exception {
        
        Path root = Paths.get(FlockStandaloneExample.class.getResource("").toURI()),
                modelsRoot = root.getParent().resolve("models");
            
        // Set up the original model
        EmfModel original = new EmfModel();
        original.setName("Source");
        original.setReadOnLoad(true);
        original.setStoredOnDisposal(false);
        original.setMetamodelFile(modelsRoot.resolve("Tree.ecore").toAbsolutePath().toString());
        original.setModelFile(modelsRoot.resolve("Tree.xmi").toAbsolutePath().toString());
        original.load();
        
        // Set up the migrated model
        EmfModel migrated = new EmfModel();
        migrated.setName("Migrated");
        migrated.setReadOnLoad(false);
        migrated.setStoredOnDisposal(true);
        migrated.setMetamodelFile(modelsRoot.resolve("Tree.ecore").toAbsolutePath().toString());
        migrated.setModelFile(modelsRoot.resolve("Tree.migrated.xmi").toAbsolutePath().toString().replace("/target/classes/", "/src/"));
        migrated.load();
        
        // Run the migration transformation
        FlockModule module = new FlockModule();
        module.parse(root.resolve("tree2tree.mig"));
        module.getContext().getModelRepository().addModel(original);
        module.getContext().getModelRepository().addModel(migrated);
        module.getContext().setOriginalModel(original);
        module.getContext().setMigratedModel(migrated);
        module.execute();
        
        // Save the migrated model
        module.getContext().getModelRepository().dispose();
    }
    
}