Jump to: navigation, search

Difference between revisions of "QVTOML/Examples/InvokeInJava"

 
Line 12: Line 12:
 
// a list of arbitrary in-memory EObjects may be passed
 
// a list of arbitrary in-memory EObjects may be passed
 
ExecutionContextImpl context = new ExecutionContextImpl();
 
ExecutionContextImpl context = new ExecutionContextImpl();
 +
ResourceSet resourceSet = new ResourceSetImpl();
 
Resource inResource = resourceSet.getResource(
 
Resource inResource = resourceSet.getResource(
 
URI.createURI("platform:/resource/myqvtprj/today.world"), true);
 
URI.createURI("platform:/resource/myqvtprj/today.world"), true);
Line 36: Line 37:
 
List<EObject> outObjects = output.getContents();
 
List<EObject> outObjects = output.getContents();
 
// let's persist them using a resource  
 
// let's persist them using a resource  
Resource outResource = resourceSet.getResource(
+
        ResourceSet resourceSet2 = new ResourceSetImpl();
 +
Resource outResource = resourceSet2.getResource(
 
URI.createURI("platform:/resource/myqvtprj/tomorrow.betterWorld"), true);
 
URI.createURI("platform:/resource/myqvtprj/tomorrow.betterWorld"), true);
 
outResource.getContents().addAll(outObjects);
 
outResource.getContents().addAll(outObjects);

Latest revision as of 09:13, 4 January 2012

An example of launching QVT Operational transformations programatically in Java.
See the org.eclipse.m2m.qvt.oml.TransformationExecutor class for details.

// Refer to an existing transformation via URI
URI transformationURI = URI.createURI("platform:/resource/myqvtprj/ChangeTheWorld.qvto");
// create executor for the given transformation
TransformationExecutor executor = new TransformationExecutor(transformationURI);

// define the transformation input
// Remark: we take the objects from a resource, however
// a list of arbitrary in-memory EObjects may be passed
ExecutionContextImpl context = new ExecutionContextImpl();
ResourceSet resourceSet = new ResourceSetImpl();
Resource inResource = resourceSet.getResource(
		URI.createURI("platform:/resource/myqvtprj/today.world"), true);		
EList<EObject> inObjects = inResource.getContents();

// create the input extent with its initial contents
ModelExtent input = new BasicModelExtent(inObjects);		
// create an empty extent to catch the output
ModelExtent output = new BasicModelExtent();

// setup the execution environment details -> 
// configuration properties, logger, monitor object etc.
ExecutionContextImpl context = new ExecutionContextImpl();
context.setConfigProperty("keepModeling", true);

// run the transformation assigned to the executor with the given 
// input and output and execution context -> ChangeTheWorld(in, out)
// Remark: variable arguments count is supported
ExecutionDiagnostic result = executor.execute(context, input, output);

// check the result for success
if(result.getSeverity() == Diagnostic.OK) {
	// the output objects got captured in the output extent
	List<EObject> outObjects = output.getContents();
	// let's persist them using a resource 
        ResourceSet resourceSet2 = new ResourceSetImpl();
	Resource outResource = resourceSet2.getResource(
			URI.createURI("platform:/resource/myqvtprj/tomorrow.betterWorld"), true);
	outResource.getContents().addAll(outObjects);
	outResource.save(Collections.emptyMap());
} else {
	// turn the result diagnostic into status and send it to error log			
	IStatus status = BasicDiagnostic.toIStatus(result);
	Activator.getDefault().getLog().log(status);
}