Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "QVTOML/Examples/InvokeInJava"
(New page: An example of launching QVT Operational transformations programatically in Java.<br/> See the <code>org.eclipse.m2m.qvt.oml.TransformationExecutor</code> class for details. <code><pre> //...) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
<code><pre> | <code><pre> | ||
// Refer to an existing transformation via URI | // Refer to an existing transformation via URI | ||
− | URI transformationURI = URI.createURI("platform:/ | + | URI transformationURI = URI.createURI("platform:/resource/myqvtprj/ChangeTheWorld.qvto"); |
// create executor for the given transformation | // create executor for the given transformation | ||
TransformationExecutor executor = new TransformationExecutor(transformationURI); | TransformationExecutor executor = new TransformationExecutor(transformationURI); | ||
Line 11: | Line 11: | ||
// Remark: we take the objects from a resource, however | // Remark: we take the objects from a resource, however | ||
// a list of arbitrary in-memory EObjects may be passed | // a list of arbitrary in-memory EObjects may be passed | ||
+ | ExecutionContextImpl context = new ExecutionContextImpl(); | ||
+ | ResourceSet resourceSet = new ResourceSetImpl(); | ||
Resource inResource = resourceSet.getResource( | Resource inResource = resourceSet.getResource( | ||
− | URI.createURI("platform:/ | + | URI.createURI("platform:/resource/myqvtprj/today.world"), true); |
EList<EObject> inObjects = inResource.getContents(); | EList<EObject> inObjects = inResource.getContents(); | ||
Line 35: | 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 resourceSet2 = new ResourceSetImpl(); |
− | URI.createURI("platform:/ | + | Resource outResource = resourceSet2.getResource( |
+ | URI.createURI("platform:/resource/myqvtprj/tomorrow.betterWorld"), true); | ||
outResource.getContents().addAll(outObjects); | outResource.getContents().addAll(outObjects); | ||
outResource.save(Collections.emptyMap()); | outResource.save(Collections.emptyMap()); |
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);
}