In QVT, it is possible to execute external code in a transformation. According to the standard, it has some benefits (QVT, 2009, p.10):
- It allows complex algorithms to be coded in any programming language with a MOF binding (or that can be executed from a language with a MOF binding).
- It allows the use of domain specific libraries to calculate model property values. For example, mathematical, engineering, bioscience and many other domains have large libraries that encode domain-specific algorithms which will difficult, if not impossible to express using OCL.
- It allows implementations of some parts of a transformation to be opaque.
The external code is called a “black box implementation”.
Using this concept, in Eclipse QVTo it is possible to execute an external Java code. In this post I will describe how you can easily create a black box implementation to execute a math sine operation.
- You must create a plug-in that contains the black box implementation. The easiest way to do it is to use the existing example. In eclipse, select “File->New->Example” and choose “Black Box Library Definition”. Click Finish. You will see a new Project called “org.eclipse.m2m.qvt.oml.examples.blackbox”.
- Open the project and edit the class “UtilitiesLibrary”. Add a new method:
public static double sin (double value) { return Math.sin(value); }
That’s it. You’ve created your black box operation.
- Now you have to create a new plug-in. There are two options to do this:
- If you want just to test, you can open a new eclipse instance considering the existing code by selecting the file “plugin.xml” and right clicking “Run As->Eclipse Application”. It will open a new instance of eclipse with your plug-in available.
- The other option is to actually create the plug-in. To do so, open “plugin.xml” and select “Export Wizard” in its “Export” section in the “Overview tab”. Select the corresponding plug-in (org.eclipse.m2m.qvt.oml.examples.blackbox (1.0.0)). Copy the jar file (org.eclipse.m2m.qvt.oml.examples.blackbox_1.0.0.jar) to the “plugins” folder of eclipse and restart eclipse.
- Now, the black box implementation is available. Just create a new “Operational QVT Project” (in “New->Project”). Create a new transformation (right click the transforms folder and select “New->Other” and “Operational QVT Transformation”). Write the following code:
import m2m.qvt.oml.ExampleJavaLib; transformation Test(); main() { log(sin(2).toString()) }
An run the transformation. It will present the result of “sin(2)” in the console. The magic is to import the package “m2m.qvt.oml.ExampleJavaLib” (this name is defined in the “plugin.xml” in “” – just change it to have a different name).
I used the Eclipse Helio version, but it should work in the newer version of eclipse.
Obs: depending on your Eclipse configuration, this error appear while generating the plug-in:
D:.metadata.pluginsorg.eclipse.pde.coretemp org.eclipse.pde.container.feature assemble.org.eclipse.pde.container.feature.win32.win32.x86.xml:88: The following error occurred while executing this line: D:.metadata.pluginsorg.eclipse.pde.coretemp org.eclipse.pde.container.feature assemble.org.eclipse.pde.container.feature.win32.win32.x86.xml:103: The following error occurred while executing this line: D:.metadata.pluginsorg.eclipse.pde.coretemp org.eclipse.pde.container.feature assemble.org.eclipse.pde.container.feature.win32.win32.x86.xml:26: The following error occurred while executing this line: D:\org.eclipse.m2m.qvt.oml.examples.blackboxbuild.xml:214: The following error occurred while executing this line: C:org.eclipse.m2m.qvt.oml.examples.blackboxbuild.xml:289: Problem: failed to create task or type apitooling.apigeneration Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any/ declarations have taken place.
This seems to happen because the plugin “org.eclipse.pde.api.tools” is not available… Just go “Help->Install New Software” and download “Eclipse Plug-in Development Environment”.
Thanks this was helpful! 🙂