• Download Gist
Launch_atl_transformation_with_xtext_model_as_input.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
package com.xtextatl.example;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
 
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.m2m.atl.core.ATLCoreException;
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;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import com.dsl.DSLStandaloneSetup;
 
import com.google.inject.Injector;
 
public class Main {
 
/**
* @param args
* @throws ATLCoreException
* @throws IOException
*/
public static void main(String[] args) throws ATLCoreException, IOException {
// Replace DSL by your Language name
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("URI to your dsl file"), true);
// Create factory and injector
EMFModelFactory factory = new EMFModelFactory();
EMFInjector emfinjector = new EMFInjector();
// Load the DSL metamodel
EMFReferenceModel dslMetamodel = (EMFReferenceModel) factory.newReferenceModel();
emfinjector.inject(dslMetamodel, "DSL Metamodel Path");
// Load the XtextResource as an EMFModel
EMFModel model = (EMFModel) factory.newModel(dslMetamodel);
emfinjector.inject(model, resource);
 
EMFReferenceModel outMetamodel = (EMFReferenceModel) factory.newReferenceModel();
emfinjector.inject(outMetamodel, "OUT Metamodel Path");
EMFModel outModel = (EMFModel) factory.newModel(outMetamodel);
EMFVMLauncher launcher = new EMFVMLauncher();
launcher.initialize(Collections.<String, Object> emptyMap());
launcher.addInModel(model, "IN", "DSL");
launcher.addOutModel(outModel, "OUT", "OUTMM");
InputStream asm = Main.class.getResourceAsStream("transformation.asm path");
launcher.launch(
ILauncher.RUN_MODE,
new NullProgressMonitor(),
Collections.<String, Object> emptyMap(),
new Object[] {asm} );
outModel.getResource().setURI(URI.createURI("URI to store out model"));
outModel.getResource().save(Collections.EMPTY_MAP);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.