Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » ATL » Troubles with ATL API
Troubles with ATL API [message #909183] Thu, 06 September 2012 13:32 Go to next message
El Arbi Aboussoror is currently offline El Arbi Aboussoror
Messages: 88
Registered: June 2010
Member
Hello,

I'm trying to use the ATL API to launch my transformations, but the result is a null resource !

import java.io.InputStream;
import java.util.Collections;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.m2m.atl.core.emf.EMFInjector;
import org.eclipse.m2m.atl.core.emf.EMFModel;
import org.eclipse.m2m.atl.core.emf.EMFModelFactory;
import org.eclipse.m2m.atl.core.emf.EMFReferenceModel;
import org.eclipse.m2m.atl.core.launch.ILauncher;
import org.eclipse.m2m.atl.engine.emfvm.launch.EMFVMLauncher;


public class AtlFacade {

	private AtlExecutionContext context;

	public AtlFacade(AtlExecutionContext context) {
		this.context = context;
	}

	public Resource evaluate(InputStream ASMInputStream, Resource sourceM) {
		EMFInjector injector = new EMFInjector();
		EMFVMLauncher emfVmLauncher = new EMFVMLauncher();
		emfVmLauncher.initialize(Collections.<String, Object> emptyMap());
		EMFModelFactory emfModelFactory = new EMFModelFactory();
		// inject source MM
		EMFReferenceModel sourceEmfMM = (EMFReferenceModel) emfModelFactory
				.newReferenceModel();
		injector.inject(sourceEmfMM, context.sourceMM);

		// inject target MM
		EMFReferenceModel targetEmfMM = (EMFReferenceModel) emfModelFactory
				.newReferenceModel();
		injector.inject(targetEmfMM, context.targetMM);

		// inject source M
		EMFModel sourceEmfM = (EMFModel) emfModelFactory.newModel(sourceEmfMM);
		injector.inject(sourceEmfM, sourceM);

		// load source M
		emfVmLauncher.addInModel(sourceEmfM, "IN", context.sourceMetamodelName);

		// create & load target M
		EMFModel targetM = (EMFModel) emfModelFactory.newModel(targetEmfMM);
		emfVmLauncher.addOutModel(targetM, "OUT", context.targetMetamodelName);

		Object asm = emfVmLauncher.loadModule(ASMInputStream);
		emfVmLauncher.launch(ILauncher.RUN_MODE, new NullProgressMonitor(),
				Collections.<String, Object> emptyMap(), asm);

		return targetM.getResource();
	}
}


This method is used as following

	private Resource xml2Scenario2(final Resource sourceXMLRes) {
		Resource result = null;
		ResourceSet resourceSet = new ResourceSetImpl();
		
	
		EPackage.Registry.INSTANCE.put(ScenarioPackage.eNS_URI, ScenarioPackage.eINSTANCE);
		
		Resource sourceMM = resourceSet.getResource(XML_NS_URI, true);
		Resource targetMM = resourceSet.getResource(scenario_NS_URI, true);
		AtlExecutionContext context = new AtlExecutionContext(sourceMM,
				targetMM, "XML", "Scenario");
		AtlFacade atlFacade = new AtlFacade(context);
		try {
			result = atlFacade.evaluate(XML2Scenario_PATH.openStream(), sourceXMLRes);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}


And the sourceXMLRes is an XML model loaded from a file.


I've launched my test in debugging mode and I found that in the run method of the ASM class (from org.eclipse.m2m.atl.engine.emfvm) in the line :

ret = mainOperation.exec(frame.enter(), monitor);


ret is null !?

Is something missing ?

[Updated on: Thu, 06 September 2012 13:42]

Report message to a moderator

Re: Troubles with ATL API [message #909192 is a reply to message #909183] Thu, 06 September 2012 14:01 Go to previous messageGo to next message
El Arbi Aboussoror is currently offline El Arbi Aboussoror
Messages: 88
Registered: June 2010
Member
It seems that the inject(IModel targetModel, Resource mainResource) method is not working as expected. In fact if I save my resource in a file and use the inject(IModel targetModel, String source, Map<String, Object> options) method it works perfectly !
Is it a bug ?
Any workaround without saving the resource ?

Re: Troubles with ATL API [message #909472 is a reply to message #909183] Fri, 07 September 2012 03:47 Go to previous messageGo to next message
William Piers is currently offline William Piers
Messages: 297
Registered: July 2009
Senior Member
Hello,

The injector api role is to inject real-world models (e.g. EMF resource)
into an IModel, usable by ATL.
The extractor has the inverse behaviour, as it allows to persist the
model managed by the transformation (targetM) into the EMF resource.

In your transformation you are missing the last step, which consists on
extracting the result model into the resource, using the EMFExtractor.

A complete example of use of this API is available as an example, in the
public 2 private example.
Also generating an atl plugin (new ATL plugin, the follow the wizard)
from your transformation will create a standalone java app which
launches your transformation.

Some useful links:
http://wiki.eclipse.org/ATL_Developer_Doc#Core_API
http://wiki.eclipse.org/ATL/User_Guide_-_The_ATL_Tools#ATL_Plugins
http://wiki.eclipse.org/ATL_Developer_Doc#Examples_of_use_.28run_ATL_transformations_in_Java.29
http://wiki.eclipse.org/ATL_Developer_Doc#Core_API

Best regards,

William

Le 06/09/2012 19:32, ElArbi Mising name a écrit :
> Hello,
>
> I'm trying to use the ATL API to launch my transformations, but the
> result is a null resource !
>
> import java.io.InputStream;
> import java.util.Collections;
>
> import org.eclipse.core.runtime.NullProgressMonitor;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.m2m.atl.core.emf.EMFInjector;
> import org.eclipse.m2m.atl.core.emf.EMFModel;
> import org.eclipse.m2m.atl.core.emf.EMFModelFactory;
> import org.eclipse.m2m.atl.core.emf.EMFReferenceModel;
> import org.eclipse.m2m.atl.core.launch.ILauncher;
> import org.eclipse.m2m.atl.engine.emfvm.launch.EMFVMLauncher;
>
>
> public class AtlFacade {
>
> private AtlExecutionContext context;
>
> public AtlFacade(AtlExecutionContext context) {
> this.context = context;
> }
>
> public Resource evaluate(InputStream ASMInputStream, Resource
> sourceM) {
> EMFInjector injector = new EMFInjector();
> EMFVMLauncher emfVmLauncher = new EMFVMLauncher();
> emfVmLauncher.initialize(Collections.<String, Object> emptyMap());
> EMFModelFactory emfModelFactory = new EMFModelFactory();
> // inject source MM
> EMFReferenceModel sourceEmfMM = (EMFReferenceModel)
> emfModelFactory
> .newReferenceModel();
> injector.inject(sourceEmfMM, context.sourceMM);
>
> // inject target MM
> EMFReferenceModel targetEmfMM = (EMFReferenceModel)
> emfModelFactory
> .newReferenceModel();
> injector.inject(targetEmfMM, context.targetMM);
>
> // inject source M
> EMFModel sourceEmfM = (EMFModel)
> emfModelFactory.newModel(sourceEmfMM);
> injector.inject(sourceEmfM, sourceM);
>
> // load source M
> emfVmLauncher.addInModel(sourceEmfM, "IN",
> context.sourceMetamodelName);
>
> // create & load target M
> EMFModel targetM = (EMFModel)
> emfModelFactory.newModel(targetEmfMM);
> emfVmLauncher.addOutModel(targetM, "OUT",
> context.targetMetamodelName);
>
> Object asm = emfVmLauncher.loadModule(ASMInputStream);
> emfVmLauncher.launch(ILauncher.RUN_MODE, new
> NullProgressMonitor(),
> Collections.<String, Object> emptyMap(), asm);
>
> return targetM.getResource();
> }
> }
>
> This method is used as following
>
> private Resource xml2Scenario2(final Resource sourceXMLRes) {
> Resource result = null;
> ResourceSet resourceSet = new ResourceSetImpl();
>
>
> EPackage.Registry.INSTANCE.put(ScenarioPackage.eNS_URI,
> ScenarioPackage.eINSTANCE);
>
> Resource sourceMM = resourceSet.getResource(XML_NS_URI, true);
> Resource targetMM = resourceSet.getResource(scenario_NS_URI,
> true);
> AtlExecutionContext context = new AtlExecutionContext(sourceMM,
> targetMM, "XML", "Scenario");
> AtlFacade atlFacade = new AtlFacade(context);
> try {
> result = atlFacade.evaluate(XML2Scenario_PATH.openStream(),
> sourceXMLRes);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> return result;
>
> }
>
> And the sourceXMLRes is an XML model loaded from a file.
> Is something missing ?
>
>
Re: Troubles with ATL API [message #911281 is a reply to message #909472] Tue, 11 September 2012 05:40 Go to previous message
El Arbi Aboussoror is currently offline El Arbi Aboussoror
Messages: 88
Registered: June 2010
Member
Thanks for your answer.
I have also done another error which is the use the old vm engine instead of the EMF specific. So I find my self manipulating ASMEMFModel instead of IModel implementations.
Previous Topic:ATL transformation from EObject
Next Topic:Problems with targetNamespace
Goto Forum:
  


Current Time: Fri Apr 18 11:46:52 EDT 2014

Powered by FUDForum. Page generated in 0.01978 seconds