Home » Modeling » ATL » How to create java objects than creating xmi output in ATL transformation?
How to create java objects than creating xmi output in ATL transformation? [message #880035] |
Fri, 01 June 2012 06:12  |
Eclipse User |
|
|
|
Hi there,
The following java program creates an xmi output file called tpfOut.xmi. I am just wondering, if someone can give me a clue how to create/capture java objects instead of xmi output?
The program is:
package org.eclipse.m2m.atl.launch.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 'Tpf3' transformation module.
*/
public class Tpf3 {
/**
* 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 {
Tpf3 runner = new Tpf3();
//runner.loadModels(args[0]);
runner.loadModels("tpfIN.xmi");
runner.doTpf3(new NullProgressMonitor());
//runner.saveModels(args[1]);
runner.saveModels("tpfOut.xmi");
//}
} catch (ATLCoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ATLExecutionException e) {
e.printStackTrace();
}
}
/**
* Constructor.
*
* @generated
*/
public Tpf3() throws IOException {
properties = new Properties();
properties.load(getFileURL("Tpf3.properties").openStream());
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
//System.out.println(properties);
}
/**
* 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 sourceMetamodel = factory.newReferenceModel();
injector.inject(sourceMetamodel, getMetamodelUri("source"));
IReferenceModel targetMetamodel = factory.newReferenceModel();
injector.inject(targetMetamodel, getMetamodelUri("target"));
this.inModel = factory.newModel(sourceMetamodel);
injector.inject(inModel, inModelPath);
this.outModel = factory.newModel(targetMetamodel);
}
/**
* 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 doTpf3(IProgressMonitor monitor) throws ATLCoreException, IOException, ATLExecutionException {
ILauncher launcher = new EMFVMLauncher();
Map<String, Object> launcherOptions = getOptions();
launcher.initialize(launcherOptions);
launcher.addInModel(inModel, "IN", "source");
launcher.addOutModel(outModel, "OUT", "target");
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("Tpf3.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("Tpf3.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("Tpf3.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("Tpf3.options.")) {
options.put(entry.getKey().toString().replaceFirst("Tpf3.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 = Tpf3.class.getResource(fileName);
if (resourceURL != null) {
fileURL = FileLocator.toFileURL(resourceURL);
} else {
fileURL = null;
}
} else {
fileURL = Tpf3.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;
}
}
Thanks in advance.
Kosala
|
|
|
Re: How to create java objects than creating xmi output in ATL transformation? [message #880044 is a reply to message #880035] |
Fri, 01 June 2012 06:26   |
Eclipse User |
|
|
|
Hi
Look at saveModels
Regards
Ed Willink
On 01/06/2012 11:12, Kosala Yapa wrote:
> Hi there,
>
> The following java program creates an xmi output file called
> tpfOut.xmi. I am just wondering, if someone can give me a clue how to
> create/capture java objects instead of xmi output?
>
> The program is:
> package org.eclipse.m2m.atl.launch.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 'Tpf3' transformation module.
> */
> public class Tpf3 {
>
> /**
> * 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 {
> Tpf3 runner = new Tpf3();
> //runner.loadModels(args[0]);
> runner.loadModels("tpfIN.xmi");
> runner.doTpf3(new NullProgressMonitor());
> //runner.saveModels(args[1]);
> runner.saveModels("tpfOut.xmi");
> //}
> } catch (ATLCoreException e) {
> e.printStackTrace();
> } catch (IOException e) {
> e.printStackTrace();
> } catch (ATLExecutionException e) {
> e.printStackTrace();
> }
> }
>
> /**
> * Constructor.
> *
> * @generated
> */
> public Tpf3() throws IOException {
> properties = new Properties();
> properties.load(getFileURL("Tpf3.properties").openStream());
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore",
> new EcoreResourceFactoryImpl());
> //System.out.println(properties);
> }
>
> /**
> * 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 sourceMetamodel = factory.newReferenceModel();
> injector.inject(sourceMetamodel, getMetamodelUri("source"));
> IReferenceModel targetMetamodel = factory.newReferenceModel();
> injector.inject(targetMetamodel, getMetamodelUri("target"));
> this.inModel = factory.newModel(sourceMetamodel);
> injector.inject(inModel, inModelPath);
> this.outModel = factory.newModel(targetMetamodel);
> }
>
> /**
> * 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 doTpf3(IProgressMonitor monitor) throws
> ATLCoreException, IOException, ATLExecutionException {
> ILauncher launcher = new EMFVMLauncher();
> Map<String, Object> launcherOptions = getOptions();
> launcher.initialize(launcherOptions);
> launcher.addInModel(inModel, "IN", "source");
> launcher.addOutModel(outModel, "OUT", "target");
> 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("Tpf3.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("Tpf3.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("Tpf3.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("Tpf3.options.")) {
>
> options.put(entry.getKey().toString().replaceFirst("Tpf3.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 = Tpf3.class.getResource(fileName);
> if (resourceURL != null) {
> fileURL = FileLocator.toFileURL(resourceURL);
> } else {
> fileURL = null;
> }
> } else {
> fileURL = Tpf3.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;
> }
> }
>
>
> Thanks in advance.
> Kosala
|
|
|
Re: How to create java objects than creating xmi output in ATL transformation? [message #881839 is a reply to message #880044] |
Tue, 05 June 2012 06:46  |
Eclipse User |
|
|
|
Hi there,
public void saveModels(String outModelPath) throws ATLCoreException {
IExtractor extractor = new EMFExtractor();
extractor.extract(outModel, outModelPath);
}
//outModelPath location of xmi output
org.eclipse.m2m.atl.core.IExtractor
class EMFExtractor: The EMF implementation of the IExtractor interface.
Problem:
It looks to me that extract() method in EMFExtractor can be used to read xmi input into xmi output. Is there any way to create java objects in memory instead of xmi output?
I really appriciate any help.
Thanks in advance !
|
|
|
Goto Forum:
Current Time: Wed Jul 23 07:14:37 EDT 2025
Powered by FUDForum. Page generated in 0.05906 seconds
|