|
|
|
|
Re: Libraries necessary to execute transformation programmatically in Juno [message #1063695 is a reply to message #1063691] |
Thu, 13 June 2013 18:41 |
juan velez Messages: 10 Registered: June 2013 |
Junior Member |
|
|
This is the code, it is taken from the eclipse example http://wiki.eclipse.org/QVTOML/Examples/InvokeInJava
package com.emt.tutorial2.test;
import java.net.URL;
import java.util.List;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
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.util.BasicExtendedMetaData;
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.m2m.qvt.oml.BasicModelExtent;
import org.eclipse.m2m.qvt.oml.ExecutionContext;
import org.eclipse.m2m.qvt.oml.ExecutionContextImpl;
import org.eclipse.m2m.qvt.oml.ExecutionDiagnostic;
import org.eclipse.m2m.qvt.oml.ModelExtent;
import org.eclipse.m2m.qvt.oml.TransformationExecutor;
public class TestEcore {
public TestEcore() {
}
private URI getUri(final String filename) {
final URL url = this.getClass().getResource(filename);
return URI.createURI(url.toString());
}
public EPackage readEPackage(final String ecoreFilename) {
final ResourceSet resourceSet = new ResourceSetImpl();
final ExtendedMetaData extendedMetaData =
new BasicExtendedMetaData(resourceSet.getPackageRegistry());
resourceSet.getLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, extendedMetaData);
final Resource resource = resourceSet.getResource(getUri(ecoreFilename), true);
final EObject eObject = resource.getContents().get(0);
return (EPackage) eObject;
}
public void registerEPackage(EPackage ePackage) {
EPackage.Registry.INSTANCE.put(ePackage.getNsURI(), ePackage);
}
public void transform(final String transformFilename, final String fileToTransform) {
final URI transformUri = getUri(transformFilename);
final TransformationExecutor executor =
new TransformationExecutor(transformUri, EPackage.Registry.INSTANCE);
final ExecutionContextImpl executionContext = new ExecutionContextImpl();
final URI mmaInstance = getUri(fileToTransform);
final ResourceSet resourceSet = new ResourceSetImpl();
final Resource resource = resourceSet.getResource(mmaInstance, true);
final EList<EObject> inputEObjectList = resource.getContents();
final ModelExtent input = new BasicModelExtent(inputEObjectList);
final ModelExtent output = new BasicModelExtent();
executionContext.setConfigProperty("keepModeling", true);
final ExecutionDiagnostic result = executor.execute(executionContext, input, output);
if (result.getSeverity()==Diagnostic.OK) {
List<EObject> outputEObjectList = output.getContents();
for (EObject eObject : outputEObjectList) {
eObject.getClass();
}
}
}
public static void initTestEcore() {
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"ecore", new EcoreResourceFactoryImpl());
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"mma", new XMIResourceFactoryImpl());
}
public static void main(String[] args) {
initTestEcore();
TestEcore testEcore = new TestEcore();
EPackage fromEPackage = testEcore.readEPackage("/MMA.ecore");
System.out.println("NSURI is " + fromEPackage.getNsURI());
testEcore.registerEPackage(fromEPackage);
EPackage toEPackage = testEcore.readEPackage("/MMB.ecore");
System.out.println("NSURI is " + toEPackage.getNsURI());
testEcore.registerEPackage(toEPackage);
testEcore.transform("/MMATransformation.qvto", "/instance.mma");
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03123 seconds