The Groovy Task
Basics
The GroovyTask can run some Groovy code. It renders possible to execute small pieces of script code and also to call any code running of the JVM such as Java, Scala,… by providing a jar archive as resource and by calling it with from the script.
Creation
To create a Groovy task:
> select Task > Groovy in the top menu
> give it a name
> type the groovy code you want to execute
> you can embbed an external java program in the task by adding *.jar files in the Lib section
> you can configure the input/output Prototypes (use for this the arrow button
), see also the general documentation about tasks.
> click on create
Your Groovy Task is ready to use in a Mole.
Built-in functions
These built-in functions are provided by the Groovy Task:
newRNG(seed: Long)to use with the variable oMSeed for initializing a new pseudo-random numbers generatornewFile()to create a new file in the current working directory of the tasknewDir()to create a new directory in the current working directory of the task
Example
Hello world in Groovy
> Create the groovy task through the menu Task > Groovy
> Give a name to this task, for example hello
> In the code area, type println "Hello World!"
> You should have a task like that:

> Finally, click on create. You can now right-click in the scene for instanciating your task, and run your mole.
Adding a jar
If you have a java project that still do your process, you can add a JAR of your project as a lib.
> Consider that your project contains this class:
package org.mycompany.myproject;
public class MyClass {
public static void hello() {
System.out.println("Hello World!");
}
}
> Build a jar of your project, double-click in the Lib input field of your task and select your jar file.

> Now, you can modify the code of your groovy task with this one:
import org.mycompany.myproject.MyClass MyClass.hello()
Considering OSGi bundles
If your project deals with specific class-loaders of if you want to prevent clashes in libraries loading, you may consider to use OSGi (on which rely OpenMOLE). For example, OpenMOLE is still using the java library XStream for serialization purpose, so if you want to embed a project that use it too, you will encounter class-loading difficulties.
> Consider that your project contains this class:
package org.mycompany.myproject;
import com.thoughtworks.xstream.XStream;
public class MyClass {
String s;
public static void hello(String xml) {
MyClass mc = (MyClass) new XStream().fromXML(xml);
System.out.println(mc.s);
}
}
> You can build your maven project as a bundle by adding this plugin configuration into your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*;resolution:=optional</Import-Package>
<Export-Package>org.mycompany.myproject.*</Export-Package>
<Embed-Dependency>*;scope=!provided;inline=true</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
> Build your maven project with the goals package bundle:bundle
> Now, you can add your jar file as a libraries, by clicking on the mole settings button
.
> Modify slightly the code of your groovy task:
import org.mycompany.myproject.MyClass
MyClass.hello("<org.mycompany.myproject.MyClass><s>hello world</s></org.mycompany.myproject.MyClass>")
