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();
}
}
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;
}