Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Model-to-Model Transformation » ATL with Xtext Resource(M2M Transformation of Xtext Resource)
ATL with Xtext Resource [message #952382] Sun, 21 October 2012 12:06 Go to next message
Artem G. is currently offline Artem G.
Messages: 10
Registered: October 2012
Junior Member
Hi Folks,

my brains are burning already -- I work last 2 days with some odd EMFInjector behaviour. I'd like to transform Xtext resource after user input it in DSL editor to another Model. A solution from Guillaume from this thread isn't working any more. So I inject a LazyLinkingResource coming from doGenerate() and get empty file after transformation:
class GenDSLGenerator implements IGenerator {
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		val test = new Transform()
		test.transform(resource)
	}
}

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
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.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
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.EMFModel;
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 org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.genetty.DSLStandaloneSetup;

import com.google.inject.Injector;

public class Transform {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public void transform(Resource res) throws Exception{
		ResourceSet resSet = new ResourceSetImpl();
		// Create factory and injector
		EMFModelFactory factory = new EMFModelFactory();
		EMFInjector emfinjector = new EMFInjector();
		// Load the DSL metamodel
		Resource data = 
			resSet.getResource(URI.createFileURI("Path to Data.ecore"), true);
	
		Resource DSL = 
			resSet.getResource(URI.createFileURI("Path to DSL.ecore"), true);

		IReferenceModel DSLMetamodel = factory.newReferenceModel();
		emfinjector.inject(DSLMetamodel, DSL);
	
		IReferenceModel dataMetamodel = factory.newReferenceModel();
		emfinjector.inject(dataMetamodel, data);

		// Load the XtextResource as an EMFModel
		EMFModel DSLModel = (EMFModel) factory.newModel(DSLMetamodel);
		//Inject LazyLinkingResource coming from doGenerate()
	       emfinjector.inject(DSLModel, res);
  
		IModel dataModel = factory.newModel(dataMetamodel);
		
		ILauncher launcher = (ILauncher) new EMFVMLauncher();
		launcher.initialize(null);
		launcher.addInModel(DSLModel, "IN", "DSL");
		launcher.addOutModel(dataModel, "OUT", "Data");
	
		InputStream asmIS[] = new InputStream[1];
		asmIS[0] = new FileInputStream("Path to DSL2Data.asm");
		Map<String, Object> launcherOptions = new HashMap<String, Object>();
		launcher.launch("run", null, launcherOptions,(Object[]) asmIS);
		
		File file = new File(" path to dataModel.xmi");
		FileOutputStream fop = new FileOutputStream(file);
	
		// if file doesnt exists, then create it
		if (!file.exists()) {
			file.createNewFile();
		}
		extractor.extract(dataModel, fop, launcherOptions);
	}


I get this exception:
ERROR org.eclipse.xtext.builder.impl.XtextBuilder  - The Xtext resource must contain at least one element.
java.lang.IllegalStateException: The Xtext resource must contain at least one element.
	at org.eclipse.xtext.resource.XtextResource.doSave(XtextResource.java:313)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1417)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:986)
	at org.eclipse.m2m.atl.core.emf.EMFExtractor.extract(EMFExtractor.java:65)
	at org.eclipse.m2m.atl.core.emf.EMFExtractor.extract(EMFExtractor.java:80)
	at org.genetty.transformer.Trans.trans(Trans.java:87)
	at org.genetty.generator.DSLGenerator.doGenerate(DSLGenerator.java:15)
	at org.eclipse.xtext.builder.BuilderParticipant.handleChangedContents(BuilderParticipant.java:259)
	at org.eclipse.xtext.builder.BuilderParticipant.build(BuilderParticipant.java:194)
	at org.eclipse.xtext.builder.impl.RegistryBuilderParticipant.build(RegistryBuilderParticipant.java:60)
	at org.eclipse.xtext.builder.impl.XtextBuilder.doBuild(XtextBuilder.java:161)
	at org.eclipse.xtext.builder.impl.XtextBuilder.incrementalBuild(XtextBuilder.java:141)
	at org.eclipse.xtext.builder.impl.XtextBuilder.build(XtextBuilder.java:91)
	at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)
	at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)
	at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)
	at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)
	at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
	at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)


but in debug I see that this LazyLinkingResource has all EObject I need.

So if I persist this LazyLinkingResource as XMIResource and load it as InputStream all this stuff working. This is very-very strange.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
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.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
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.EMFModel;
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 org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.genetty.DSLStandaloneSetup;

import com.google.inject.Injector;

public class Transform {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public void transform(Resource res) throws Exception{
		ResourceSet resSet = new ResourceSetImpl();
		// Create factory and injector
		EMFModelFactory factory = new EMFModelFactory();
		EMFInjector emfinjector = new EMFInjector();
		// Load the DSL metamodel
		Resource data = 
			resSet.getResource(URI.createFileURI("Path to Data.ecore"), true);
	
		Resource DSL = 
			resSet.getResource(URI.createFileURI("Path to DSL.ecore"), true);

		IReferenceModel DSLMetamodel = factory.newReferenceModel();
		emfinjector.inject(DSLMetamodel, DSL);
	
		IReferenceModel dataMetamodel = factory.newReferenceModel();
		emfinjector.inject(dataMetamodel, data);

		// Load the XtextResource as an EMFModel
		EMFModel DSLModel = (EMFModel) factory.newModel(DSLMetamodel);
               //Persist LazyLinkingResource coming from doGenerate()
		XMIResourceFactoryImpl resFactory = new XMIResourceFactoryImpl();
		URI xmiuri = URI.createFileURI("path to outputGenDSL.xmi");
		XMIResource xmiresource = (XMIResource) resFactory.createResource(xmiuri);

		EObject xtextModel =  res.getAllContents().next();

		xmiresource.getContents().add(xtextModel);
		xmiresource.save(new HashMap());
	    //Load persisted resource as InputStream
	    InputStream is[] = new InputStream[1];
	    is[0] = new FileInputStream("path to outputGenDSL.xmi");
	    Map<String, Object> options = new HashMap<String, Object>();
           //Inject loaded stuff
            emfinjector.inject(DSLModel,is[0], options);
  
		IModel dataModel = factory.newModel(dataMetamodel);
		
		ILauncher launcher = (ILauncher) new EMFVMLauncher();
		launcher.initialize(null);
		launcher.addInModel(DSLModel, "IN", "DSL");
		launcher.addOutModel(dataModel, "OUT", "Data");
	
		InputStream asmIS[] = new InputStream[1];
		asmIS[0] = new FileInputStream("Path to DSL2Data.asm");
		Map<String, Object> launcherOptions = new HashMap<String, Object>();
		launcher.launch("run", null, launcherOptions,(Object[]) asmIS);
		
		File file = new File(" path to dataModel.xmi");
		FileOutputStream fop = new FileOutputStream(file);
	
		// if file doesnt exists, then create it
		if (!file.exists()) {
			file.createNewFile();
		}
		extractor.extract(dataModel, fop, launcherOptions);
	}


If I use Guillaumes solution I get empty file at the end:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
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.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
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.EMFModel;
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 org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.genetty.DSLStandaloneSetup;

import com.google.inject.Injector;

public class Transform {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public void transform(Resource res) throws Exception{
		ResourceSet resSet = new ResourceSetImpl();
		// Create factory and injector
		EMFModelFactory factory = new EMFModelFactory();
		EMFInjector emfinjector = new EMFInjector();
		// Load the DSL metamodel
		Resource data = 
			resSet.getResource(URI.createFileURI("Path to Data.ecore"), true);
	
		Resource DSL = 
			resSet.getResource(URI.createFileURI("Path to DSL.ecore"), true);

		IReferenceModel DSLMetamodel = factory.newReferenceModel();
		emfinjector.inject(DSLMetamodel, DSL);
	
		IReferenceModel dataMetamodel = factory.newReferenceModel();
		emfinjector.inject(dataMetamodel, data);

		// Load the XtextResource as an EMFModel
		EMFModel DSLModel = (EMFModel) factory.newModel(DSLMetamodel);
        
        DSLStandaloneSetup dsl = new DSLStandaloneSetup();             
        Injector injector = dsl.createInjectorAndDoEMFRegistration();
        XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
        resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
        Resource resource = resourceSet.getResource(URI.createURI("platform:/resource/genTest/test.dsl"), true);

            emfinjector.inject(DSLModel, resource);
  
		IModel dataModel = factory.newModel(dataMetamodel);
		
		ILauncher launcher = (ILauncher) new EMFVMLauncher();
		launcher.initialize(null);
		launcher.addInModel(DSLModel, "IN", "DSL");
		launcher.addOutModel(dataModel, "OUT", "Data");
	
		InputStream asmIS[] = new InputStream[1];
		asmIS[0] = new FileInputStream("Path to DSL2Data.asm");
		Map<String, Object> launcherOptions = new HashMap<String, Object>();
		launcher.launch("run", null, launcherOptions,(Object[]) asmIS);
		
		File file = new File(" path to dataModel.xmi");
		FileOutputStream fop = new FileOutputStream(file);
	
		// if file doesnt exists, then create it
		if (!file.exists()) {
			file.createNewFile();
		}
		extractor.extract(dataModel, fop, launcherOptions);
	}



Any help would be very appreciated! Thanks, Artem.
Re: ATL with Xtext Resource [message #952596 is a reply to message #952382] Sun, 21 October 2012 16:03 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4045
Registered: July 2009
Senior Member
Hi

You're more likely to get a sensible answer on the ATL newsgroup.

You don't seem to call DSLStandaloneSetup.doSetup().

Regards

Ed Willink



On 21/10/2012 13:07, Artem G. wrote:
> Hi Folks,
>
> my brains are burning already -- I work last 2 days with some odd
> EMFInjector behaviour. I'd like to transform Xtext resource after user
> input it in DSL editor to another Model. A solution from Guillaume
> from
> http://www.eclipse.org/forums/index.php/mv/msg/159523/506695/#msg_506695
> thread isn't working any more. So I inject a LazyLinkingResource
> coming from doGenerate() and get empty file after transformation:
>
> class GenDSLGenerator implements IGenerator {
>
> override void doGenerate(Resource resource, IFileSystemAccess fsa) {
> val test = new Transform()
> test.transform(resource)
> }
> }
>
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.HashMap;
> import java.util.Map;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EObject;
> 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.XMIResource;
> import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
> 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.EMFModel;
> 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 org.eclipse.xtext.resource.XtextResource;
> import org.eclipse.xtext.resource.XtextResourceSet;
> import org.genetty.DSLStandaloneSetup;
>
> import com.google.inject.Injector;
>
> public class Transform {
>
> /**
> * @param args
> * @throws Exception */
> public void transform(Resource res) throws Exception{
> ResourceSet resSet = new ResourceSetImpl();
> // Create factory and injector
> EMFModelFactory factory = new EMFModelFactory();
> EMFInjector emfinjector = new EMFInjector();
> // Load the DSL metamodel
> Resource data =
> resSet.getResource(URI.createFileURI("Path to Data.ecore"), true);
>
> Resource DSL =
> resSet.getResource(URI.createFileURI("Path to DSL.ecore"), true);
>
> IReferenceModel DSLMetamodel = factory.newReferenceModel();
> emfinjector.inject(DSLMetamodel, DSL);
>
> IReferenceModel dataMetamodel = factory.newReferenceModel();
> emfinjector.inject(dataMetamodel, data);
>
> // Load the XtextResource as an EMFModel
> EMFModel DSLModel = (EMFModel) factory.newModel(DSLMetamodel);
> //Inject LazyLinkingResource coming from doGenerate()
> emfinjector.inject(DSLModel, res);
>
> IModel dataModel = factory.newModel(dataMetamodel);
>
> ILauncher launcher = (ILauncher) new EMFVMLauncher();
> launcher.initialize(null);
> launcher.addInModel(DSLModel, "IN", "DSL");
> launcher.addOutModel(dataModel, "OUT", "Data");
>
> InputStream asmIS[] = new InputStream[1];
> asmIS[0] = new FileInputStream("Path to DSL2Data.asm");
> Map<String, Object> launcherOptions = new HashMap<String,
> Object>();
> launcher.launch("run", null, launcherOptions,(Object[]) asmIS);
>
> File file = new File(" path to dataModel.xmi");
> FileOutputStream fop = new FileOutputStream(file);
>
> // if file doesnt exists, then create it
> if (!file.exists()) {
> file.createNewFile();
> }
> extractor.extract(dataModel, fop, launcherOptions);
> }
>
>
> I get this exception:
>
> ERROR org.eclipse.xtext.builder.impl.XtextBuilder - The Xtext
> resource must contain at least one element.
> java.lang.IllegalStateException: The Xtext resource must contain at
> least one element.
> at
> org.eclipse.xtext.resource.XtextResource.doSave(XtextResource.java:313)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1417)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:986)
> at
> org.eclipse.m2m.atl.core.emf.EMFExtractor.extract(EMFExtractor.java:65)
> at
> org.eclipse.m2m.atl.core.emf.EMFExtractor.extract(EMFExtractor.java:80)
> at org.genetty.transformer.Trans.trans(Trans.java:87)
> at
> org.genetty.generator.DSLGenerator.doGenerate(DSLGenerator.java:15)
> at
> org.eclipse.xtext.builder.BuilderParticipant.handleChangedContents(BuilderParticipant.java:259)
> at
> org.eclipse.xtext.builder.BuilderParticipant.build(BuilderParticipant.java:194)
> at
> org.eclipse.xtext.builder.impl.RegistryBuilderParticipant.build(RegistryBuilderParticipant.java:60)
> at
> org.eclipse.xtext.builder.impl.XtextBuilder.doBuild(XtextBuilder.java:161)
> at
> org.eclipse.xtext.builder.impl.XtextBuilder.incrementalBuild(XtextBuilder.java:141)
> at
> org.eclipse.xtext.builder.impl.XtextBuilder.build(XtextBuilder.java:91)
> at
> org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
> at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
> at
> org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
> at
> org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)
> at
> org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)
> at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
> at
> org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)
> at
> org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)
> at
> org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)
> at
> org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
> at
> org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
> at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
>
>
> but in debug I see that this LazyLinkingResource has all EObject I need.
>
> So if I persist this LazyLinkingResource as XMIResource and load it as
> InputStream all this stuff working. This is very-very strange.
>
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.HashMap;
> import java.util.Map;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EObject;
> 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.XMIResource;
> import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
> 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.EMFModel;
> 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 org.eclipse.xtext.resource.XtextResource;
> import org.eclipse.xtext.resource.XtextResourceSet;
> import org.genetty.DSLStandaloneSetup;
>
> import com.google.inject.Injector;
>
> public class Transform {
>
> /**
> * @param args
> * @throws Exception */
> public void transform(Resource res) throws Exception{
> ResourceSet resSet = new ResourceSetImpl();
> // Create factory and injector
> EMFModelFactory factory = new EMFModelFactory();
> EMFInjector emfinjector = new EMFInjector();
> // Load the DSL metamodel
> Resource data =
> resSet.getResource(URI.createFileURI("Path to Data.ecore"), true);
>
> Resource DSL =
> resSet.getResource(URI.createFileURI("Path to DSL.ecore"), true);
>
> IReferenceModel DSLMetamodel = factory.newReferenceModel();
> emfinjector.inject(DSLMetamodel, DSL);
>
> IReferenceModel dataMetamodel = factory.newReferenceModel();
> emfinjector.inject(dataMetamodel, data);
>
> // Load the XtextResource as an EMFModel
> EMFModel DSLModel = (EMFModel) factory.newModel(DSLMetamodel);
> //Persist LazyLinkingResource coming from doGenerate()
> XMIResourceFactoryImpl resFactory = new XMIResourceFactoryImpl();
> URI xmiuri = URI.createFileURI("path to outputGenDSL.xmi");
> XMIResource xmiresource = (XMIResource)
> resFactory.createResource(xmiuri);
>
> EObject xtextModel = res.getAllContents().next();
>
> xmiresource.getContents().add(xtextModel);
> xmiresource.save(new HashMap());
> //Load persisted resource as InputStream
> InputStream is[] = new InputStream[1];
> is[0] = new FileInputStream("path to outputGenDSL.xmi");
> Map<String, Object> options = new HashMap<String, Object>();
> //Inject loaded stuff
> emfinjector.inject(DSLModel,is[0], options);
>
> IModel dataModel = factory.newModel(dataMetamodel);
>
> ILauncher launcher = (ILauncher) new EMFVMLauncher();
> launcher.initialize(null);
> launcher.addInModel(DSLModel, "IN", "DSL");
> launcher.addOutModel(dataModel, "OUT", "Data");
>
> InputStream asmIS[] = new InputStream[1];
> asmIS[0] = new FileInputStream("Path to DSL2Data.asm");
> Map<String, Object> launcherOptions = new HashMap<String,
> Object>();
> launcher.launch("run", null, launcherOptions,(Object[]) asmIS);
>
> File file = new File(" path to dataModel.xmi");
> FileOutputStream fop = new FileOutputStream(file);
>
> // if file doesnt exists, then create it
> if (!file.exists()) {
> file.createNewFile();
> }
> extractor.extract(dataModel, fop, launcherOptions);
> }
>
>
> If I use Guillaumes solution I get empty file at the end:
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.HashMap;
> import java.util.Map;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EObject;
> 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.XMIResource;
> import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
> 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.EMFModel;
> 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 org.eclipse.xtext.resource.XtextResource;
> import org.eclipse.xtext.resource.XtextResourceSet;
> import org.genetty.DSLStandaloneSetup;
>
> import com.google.inject.Injector;
>
> public class Transform {
>
> /**
> * @param args
> * @throws Exception */
> public void transform(Resource res) throws Exception{
> ResourceSet resSet = new ResourceSetImpl();
> // Create factory and injector
> EMFModelFactory factory = new EMFModelFactory();
> EMFInjector emfinjector = new EMFInjector();
> // Load the DSL metamodel
> Resource data =
> resSet.getResource(URI.createFileURI("Path to Data.ecore"), true);
>
> Resource DSL =
> resSet.getResource(URI.createFileURI("Path to DSL.ecore"), true);
>
> IReferenceModel DSLMetamodel = factory.newReferenceModel();
> emfinjector.inject(DSLMetamodel, DSL);
>
> IReferenceModel dataMetamodel = factory.newReferenceModel();
> emfinjector.inject(dataMetamodel, data);
>
> // Load the XtextResource as an EMFModel
> EMFModel DSLModel = (EMFModel) factory.newModel(DSLMetamodel);
> DSLStandaloneSetup dsl = new
> DSLStandaloneSetup(); Injector injector =
> dsl.createInjectorAndDoEMFRegistration();
> XtextResourceSet resourceSet =
> injector.getInstance(XtextResourceSet.class);
> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
> Boolean.TRUE);
> Resource resource =
> resourceSet.getResource(URI.createURI("platform:/resource/genTest/test.dsl"),
> true);
>
> emfinjector.inject(DSLModel, resource);
>
> IModel dataModel = factory.newModel(dataMetamodel);
>
> ILauncher launcher = (ILauncher) new EMFVMLauncher();
> launcher.initialize(null);
> launcher.addInModel(DSLModel, "IN", "DSL");
> launcher.addOutModel(dataModel, "OUT", "Data");
>
> InputStream asmIS[] = new InputStream[1];
> asmIS[0] = new FileInputStream("Path to DSL2Data.asm");
> Map<String, Object> launcherOptions = new HashMap<String,
> Object>();
> launcher.launch("run", null, launcherOptions,(Object[]) asmIS);
>
> File file = new File(" path to dataModel.xmi");
> FileOutputStream fop = new FileOutputStream(file);
>
> // if file doesnt exists, then create it
> if (!file.exists()) {
> file.createNewFile();
> }
> extractor.extract(dataModel, fop, launcherOptions);
> }
>
>
>
> Any help would be very appreciated! Thanks, Artem.
Re: ATL with Xtext Resource [message #952613 is a reply to message #952596] Sun, 21 October 2012 16:23 Go to previous messageGo to next message
Artem G. is currently offline Artem G.
Messages: 10
Registered: October 2012
Junior Member
Hi Ed, thanks for reply!

Adding DSLStandaloneSetup.doSetup(); brings nothing, while:
public class DSLStandaloneSetup extends DSLStandaloneSetupGenerated{

	public static void doSetup() {
		new DSLStandaloneSetup().createInjectorAndDoEMFRegistration();
	}
}


this is the same, what I'm doing at the begin.

Should I cross-post this treat to the ATL newsgroup?

-a

[Updated on: Sun, 21 October 2012 16:23]

Report message to a moderator

Re: ATL with Xtext Resource [message #952631 is a reply to message #952613] Sun, 21 October 2012 16:40 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4045
Registered: July 2009
Senior Member
Hi

You need to take more time composing your messages. They seem to contain
significant ambiguities or errors so I cannot really tell what you're
asking.

doSetup() is the Xtext standalone API. If you are expert enough to fully
understand the hidden DI behaviours, then you are expert enough to solve
your own problems.

Regards

Ed Willink





On 21/10/2012 17:23, Artem G. wrote:
> Hi Ed, thanks for reply!
>
> Adding GenDSLStandaloneSetup.doSetup(); brings nothing, while:
>
> public class DSLStandaloneSetup extends DSLStandaloneSetupGenerated{
>
> public static void doSetup() {
> new DSLStandaloneSetup().createInjectorAndDoEMFRegistration();
> }
> }
>
>
> this is the same, what I'm doing at the begin.
>
> Should I cross-post this treat to the ATL newsgroup?
>
> -a
Re: ATL with Xtext Resource [message #952644 is a reply to message #952631] Sun, 21 October 2012 16:55 Go to previous message
Artem G. is currently offline Artem G.
Messages: 10
Registered: October 2012
Junior Member
Sorry Ed, my last reply was not sarcastic. I really added .doSetup() but that doesn't helps.
Previous Topic:[ATL] referencing to element created from a lazy rule
Next Topic:[ATL] Avoid duplicate entries in target model
Goto Forum:
  


Current Time: Tue Sep 16 11:46:26 GMT 2014

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

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