Home » Modeling » ATL » Ecore 2 Ecore using ATL ->output java objects
| |
Re: Ecore 2 Ecore using ATL ->output java objects [message #881939 is a reply to message #879059] |
Tue, 05 June 2012 14:21 |
Kosala Yapa Messages: 159 Registered: September 2010 |
Senior Member |
|
|
Hi Willink,
Here is the java launch, which creates output.xmi. I am trying to find a way to capture java objects in memory.
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.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
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;
import target.Device;
import target.TargetPackage;
public class Tpf3 {
private Properties properties;
protected IModel inModel;
protected IModel outModel;
public static void main(String[] args) {
try {
Tpf3 runner = new Tpf3();
runner.loadModels("tpfIN.xmi");
runner.doTpf3(new NullProgressMonitor());
runner.saveModels("tpfOut.xmi");
} catch (ATLCoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ATLExecutionException e) {
e.printStackTrace();
}
}//end main()
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);
}
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);
}
public void saveModels(String outModelPath) throws ATLCoreException {
IExtractor extractor = new EMFExtractor();
extractor.extract(outModel, outModelPath);
}
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());
}
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;
}
protected String getMetamodelUri(String metamodelName) {
return properties.getProperty("Tpf3.metamodels." + metamodelName);
}
protected InputStream getLibraryAsStream(String libraryName) throws IOException {
return getFileURL(properties.getProperty("Tpf3.libraries." + libraryName)).openStream();
}
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;
}
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;
}
}
public static boolean isEclipseRunning() {
try {
return Platform.isRunning();
} catch (Throwable exception) {
// Assume that we aren't running.
}
return false;
}
}
Thanks
|
|
|
Re: Ecore 2 Ecore using ATL -&gt;output java objects [message #882008 is a reply to message #881939] |
Tue, 05 June 2012 16:05 |
Ed Willink Messages: 7655 Registered: July 2009 |
Senior Member |
|
|
Hi
After 5 minutes downloading and installing ATL I can see that
IModel has an EMFModel derived class for which the getResource() method
returns a Resource which is usually full of Java model objects.
Try using the Eclipse tooling to help discover what your coding tools do.
Regards
Ed Willink
On 05/06/2012 15:21, Kosala Yapa wrote:
> Hi Willink,
>
> Here is the java launch, which creates output.xmi. I am trying to find
> a way to capture java objects in memory.
>
>
> 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.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
> import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
> 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;
>
> import target.Device;
> import target.TargetPackage;
>
>
>
> public class Tpf3 {
>
> private Properties properties;
> protected IModel inModel;
> protected IModel outModel;
>
>
> public static void main(String[] args) {
> try {
> Tpf3 runner = new Tpf3();
> runner.loadModels("tpfIN.xmi");
> runner.doTpf3(new NullProgressMonitor());
> runner.saveModels("tpfOut.xmi");
> } catch (ATLCoreException e) {
> e.printStackTrace();
> } catch (IOException e) {
> e.printStackTrace();
> } catch (ATLExecutionException e) {
> e.printStackTrace();
> }
> }//end main()
>
> 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);
> }
>
>
> 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);
> }
>
>
> public void saveModels(String outModelPath) throws ATLCoreException {
> IExtractor extractor = new EMFExtractor();
> extractor.extract(outModel, outModelPath);
> }
>
>
> 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());
> }
>
>
> 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;
> }
>
>
> protected String getMetamodelUri(String metamodelName) {
> return properties.getProperty("Tpf3.metamodels." +
> metamodelName);
> }
>
>
> protected InputStream getLibraryAsStream(String libraryName)
> throws IOException {
> return getFileURL(properties.getProperty("Tpf3.libraries." +
> libraryName)).openStream();
> }
>
>
> 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;
> }
>
>
> 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;
> }
> }
>
>
> public static boolean isEclipseRunning() {
> try {
> return Platform.isRunning();
> } catch (Throwable exception) {
> // Assume that we aren't running.
> }
> return false;
> }
> }
>
>
> Thanks
>
>
>
>
>
>
|
|
|
Goto Forum:
Current Time: Thu Mar 28 10:55:48 GMT 2024
Powered by FUDForum. Page generated in 0.12027 seconds
|