Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » ATL » ATL
ATL [message #1059344] Fri, 17 May 2013 15:15 Go to next message
ransou RMG is currently offline ransou RMG
Messages: 15
Registered: February 2013
Junior Member
Hello,
I would like to generate a model transformation in a Java code.I would like to know all the steps necessary to do it.
Thank you.
Re: ATL [message #1059347 is a reply to message #1059344] Fri, 17 May 2013 15:21 Go to previous messageGo to next message
Federico Toledo is currently offline Federico Toledo
Messages: 97
Registered: April 2012
Location: Ciudad Real, Spain
Member
Here you have info

http://wiki.eclipse.org/ATL/User_Guide_-_The_ATL_Tools#ATL_Plugins.

I suggest to look for what you need in the forum before asking, and try to improve subjects Smile

good luck
Re: ATL [message #1059352 is a reply to message #1059347] Fri, 17 May 2013 15:35 Go to previous messageGo to next message
ransou RMG is currently offline ransou RMG
Messages: 15
Registered: February 2013
Junior Member
Thanks but I try with this code:

import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.m2m.atl.drivers.emf4atl.AtlEMFModelHandler;
import org.eclipse.m2m.atl.engine.vm.Atllauncher;
import org.eclipse.m2m.atl.engine.vm.AtlModelHandler;
import org.eclipse.m2m.atl.engine.vm.ModelLoader;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMModel;

public class Families2PersonsProgrammaticlaunch {
public static void main(String[] args) {
try {
//AtlModelHandler and ModelLoader init
AtlModelHandler.registerDefaultHandler("EMF", new AtlEMFModelHandler());
AtlModelHandler amh = AtlModelHandler.getDefault("EMF");
ModelLoader ml = amh.createModelLoader();

//stores metamodels and models
Map models = new HashMap();
//used to locate metamodels,models and trasformation file
URL url;

//URL where Families.ecore (input metamodel) is located
url = new URL("chemin d'emplacement du premier métamodèle");
//load Families metamodel (the name is the same in the ATL file [ex.: ATL CODE-> create OUT : Persons from IN : *Families*;])
ASMModel metamodel_families = ml.loadModel("Families", ml.getMOF(), url.openStream());
//store
models.put("Families", metamodel_families);

//URL where Persons.ecore (output metamodel) is located
url = new URL("chemin d'emplacement du deuxième métamodèle");
//load Persons metamodel (the name is the same in the ATL file [ex.: ATL CODE-> create OUT : *Persons* from IN : Families;])
ASMModel metamodel_persons = ml.loadModel("Persons", ml.getMOF(), url.openStream());
//store
models.put("Persons", metamodel_persons);

//URL where sample-Families.xmi (input model) is located
url = new URL("chemin d'emplacement du modèle de Families sample-Families.xmi");
//load Families model (the name is the same in the ATL file [ex.: ATL CODE-> create OUT : Persons from *IN* : Families;])
ASMModel model_families = ml.loadModel("IN", metamodel_families, url.openStream());
//store
models.put("IN", model_families);

//URL where sample-Persons.xmi (output model) is located
url = new URL("chemin d'emplacement du modèle sample-Persons.xmi( que doit le programme retourner)");
//create a new output model (the name is the same in the ATL file [ex.: ATL CODE-> create *OUT* : Persons from IN : Families;])
ASMModel outputModel = ml.newModel("OUT", "chemin d'emplacement du modèle sample-Persons.xmi (que doit le programme retourner)", metamodel_persons);
//store
models.put("OUT", outputModel);

//URL where Families2Persons.asm (transformation .asm) is located
url = new URL("chemin d'emplacement du fichier Families2Persons.asm");
//get an Atllauncher
Atllauncher mylauncher = Atllauncher.getDefault();
//execute the transformation (in this case we pass just the map "models")
mylauncher.launch(url, Collections.EMPTY_MAP, models, Collections.EMPTY_MAP, Collections.EMPTY_LIST, Collections.EMPTY_MAP);

//save the outputModel in "file://C:/Documents and Settings/Administrator/workspace2/Families2Persons/sample-Persons.xmi"
outputModel.getModelLoader().save(outputModel, "chemin d'emplacement du modèle sample-Persons.xmi (que doit le programme retourner)");

//once executed this file make a refresh on the destination folder
}
catch (Exception e) {
e.printStackTrace();
}


}

}
but I don't know is it a class in a java project or in other type of project and if it is a class in a java project unfortunately it doesn't work when I run it as a java application.
Re: ATL [message #1059353 is a reply to message #1059352] Fri, 17 May 2013 15:40 Go to previous messageGo to next message
Federico Toledo is currently offline Federico Toledo
Messages: 97
Registered: April 2012
Location: Ciudad Real, Spain
Member
Any Java project ....
If you try to follow the example in the link I copied, you could start using the generated java code for the plugin in your java project, copying the corresponding references.
Re: ATL [message #1059468 is a reply to message #1059353] Sun, 19 May 2013 09:58 Go to previous message
ransou RMG is currently offline ransou RMG
Messages: 15
Registered: February 2013
Junior Member
Hi,
Ok Federico I saw the link and what I need to do is the part of "ATL Plugins".I create a new ATL Plugin based on an ATL project Families2Persons.I had the same hiérarchie as the figure http://wiki.eclipse.org/Image:ATL_plugin_wizard_result.png.The java class generated by the plugin is like this:

package org.eclipse.m2m.atl.Families2Persons.files;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.m2m.atl.common.ATLExecutionException;
import org.eclipse.m2m.atl.core.ATLCoreException;
import org.eclipse.m2m.atl.core.IExtractor;
import org.eclipse.m2m.atl.core.IInjector;
import org.eclipse.m2m.atl.core.IModel;
import org.eclipse.m2m.atl.core.IReferenceModel;
import org.eclipse.m2m.atl.core.ModelFactory;
import org.eclipse.m2m.atl.core.emf.EMFExtractor;
import org.eclipse.m2m.atl.core.emf.EMFInjector;
import org.eclipse.m2m.atl.core.emf.EMFModelFactory;
import org.eclipse.m2m.atl.core.launch.ILauncher;
import org.eclipse.m2m.atl.engine.emfvm.launch.EMFVMLauncher;

/**
* Entry point of the 'Families2Persons' transformation module.
*/
public class Families2Persons {

/**
* The property file. Stores module list, the metamodel and library locations.
* @generated
*/
private Properties properties;

/**
* The IN model.
* @generated
*/
protected IModel inModel;

/**
* The OUT model.
* @generated
*/
protected IModel outModel;

/**
* The main method.
*
* @param args
* are the arguments
* @generated
*/
public static void main(String[] args) {
try {
if (args.length < 2) {
System.out.println("Arguments not valid : {IN_model_path, OUT_model_path}.");
} else {
Families2Persons runner = new Families2Persons();
runner.loadModels(args[0]);
runner.doFamilies2Persons(new NullProgressMonitor());
runner.saveModels(args[1]);
}
} catch (ATLCoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ATLExecutionException e) {
e.printStackTrace();
}
}

/**
* Constructor.
*
* @generated
*/
public Families2Persons() throws IOException {
properties = new Properties();
properties.load(getFileURL("Families2Persons.properties").openStream());
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
}

/**
* Load the input and input/output models, initialize output models.
*
* @param inModelPath
* the IN model path
* @throws ATLCoreException
* if a problem occurs while loading models
*
* @generated
*/
public void loadModels(String inModelPath) throws ATLCoreException {
ModelFactory factory = new EMFModelFactory();
IInjector injector = new EMFInjector();
IReferenceModel familiesMetamodel = factory.newReferenceModel();
injector.inject(familiesMetamodel, getMetamodelUri("Families"));
IReferenceModel personsMetamodel = factory.newReferenceModel();
injector.inject(personsMetamodel, getMetamodelUri("Persons"));
this.inModel = factory.newModel(familiesMetamodel);
injector.inject(inModel, inModelPath);
this.outModel = factory.newModel(personsMetamodel);
}

/**
* Save the output and input/output models.
*
* @param outModelPath
* the OUT model path
* @throws ATLCoreException
* if a problem occurs while saving models
*
* @generated
*/
public void saveModels(String outModelPath) throws ATLCoreException {
IExtractor extractor = new EMFExtractor();
extractor.extract(outModel, outModelPath);
}

/**
* Transform the models.
*
* @param monitor
* the progress monitor
* @throws ATLCoreException
* if an error occurs during models handling
* @throws IOException
* if a module cannot be read
* @throws ATLExecutionException
* if an error occurs during the execution
*
* @generated
*/
public Object doFamilies2Persons(IProgressMonitor monitor) throws ATLCoreException, IOException, ATLExecutionException {
ILauncher launcher = new EMFVMLauncher();
Map<String, Object> launcherOptions = getOptions();
launcher.initialize(launcherOptions);
launcher.addInModel(inModel, "IN", "Families");
launcher.addOutModel(outModel, "OUT", "Persons");
return launcher.launch("run", monitor, launcherOptions, (Object[]) getModulesList());
}

/**
* Returns an Array of the module input streams, parameterized by the
* property file.
*
* @return an Array of the module input streams
* @throws IOException
* if a module cannot be read
*
* @generated
*/
protected InputStream[] getModulesList() throws IOException {
InputStream[] modules = null;
String modulesList = properties.getProperty("Families2Persons.modules");
if (modulesList != null) {
String[] moduleNames = modulesList.split(",");
modules = new InputStream[moduleNames.length];
for (int i = 0; i < moduleNames.length; i++) {
String asmModulePath = new Path(moduleNames[i].trim()).removeFileExtension().addFileExtension("asm").toString();
modules[i] = getFileURL(asmModulePath).openStream();
}
}
return modules;
}

/**
* Returns the URI of the given metamodel, parameterized from the property file.
*
* @param metamodelName
* the metamodel name
* @return the metamodel URI
*
* @generated
*/
protected String getMetamodelUri(String metamodelName) {
return properties.getProperty("Families2Persons.metamodels." + metamodelName);
}

/**
* Returns the file name of the given library, parameterized from the property file.
*
* @param libraryName
* the library name
* @return the library file name
*
* @generated
*/
protected InputStream getLibraryAsStream(String libraryName) throws IOException {
return getFileURL(properties.getProperty("Families2Persons.libraries." + libraryName)).openStream();
}

/**
* Returns the options map, parameterized from the property file.
*
* @return the options map
*
* @generated
*/
protected Map<String, Object> getOptions() {
Map<String, Object> options = new HashMap<String, Object>();
for (Entry<Object, Object> entry : properties.entrySet()) {
if (entry.getKey().toString().startsWith("Families2Persons.options.")) {
options.put(entry.getKey().toString().replaceFirst("Families2Persons.options.", ""),
entry.getValue().toString());
}
}
return options;
}

/**
* Finds the file in the plug-in. Returns the file URL.
*
* @param fileName
* the file name
* @return the file URL
* @throws IOException
* if the file doesn't exist
*
* @generated
*/
protected static URL getFileURL(String fileName) throws IOException {
final URL fileURL;
if (isEclipseRunning()) {
URL resourceURL = Families2Persons.class.getResource(fileName);
if (resourceURL != null) {
fileURL = FileLocator.toFileURL(resourceURL);
} else {
fileURL = null;
}
} else {
fileURL = Families2Persons.class.getResource(fileName);
}
if (fileURL == null) {
throw new IOException("'" + fileName + "' not found");
} else {
return fileURL;
}
}

/**
* Tests if eclipse is running.
*
* @return <code>true</code> if eclipse is running
*
* @generated
*/
public static boolean isEclipseRunning() {
try {
return Platform.isRunning();
} catch (Throwable exception) {
// Assume that we aren't running.
}
return false;
}
}


Now I would like to Know what are the modifications which I should do to run this plugin and how I can run it.

Thank you for your interest.
Previous Topic:injection of an XMI into a database
Next Topic:How to execute ATL Transformation without using eclipse
Goto Forum:
  


Current Time: Thu Oct 23 12:44:31 GMT 2014

Powered by FUDForum. Page generated in 0.02500 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software