It’s groovy baby …

Wouldn’t it be nice to access the API of an own RCP by using a script? Hence, adding script support to an own RCP application, that’s groovy. Indeed, Groovy is a good choice for such a purpose. Hence, I thought it’s easy to install the Groovy-Eclipse 3.7 plug-in into my own RCP OpenChrom from the marketplace … and boooom, that’s it. It wasn’t. After 3 days of handling with missing JDT and PDE dependencies, I surrendered. Despite of that, the Groovy-Eclipse plug-in is a great choice for the Eclipse IDE, but it didn’t worked for me. Anyhow, I got it working with a simple RCP test case, but the next issue was to access the API of the enclosing RCP. Again, there were problems accessing the OSGi bundles. Why on earth, there must be a solution. It was really getting ungroovy now. But after staying off the project some days, I thought about testing an embedded version of the Groovy Shell. That was the solution.

First of all, the latest Groovy binary release is needed:
http://dist.groovy.codehaus.org/distributions/groovy-binary-2.0.2.zip

It includes a folder called “embeddable” which contains the groovy redistributable “groovy-all-2.0.2.jar”. Please consider, Groovy is licensed under the Apache License, Version 2.0. Please also take care of the ANTLR, ASM, CLI, JSR223 licenses. They are stored in the downloaded zip file. The jar file needs to be included in the classpath of the plug-in. Use the “Runtime” tab of the MANIFEST.MF file.

Groovy-Jar-Classpath

A handler can be used to load groovy scripts inside the RCP application.

try {
	GroovyShell groovyShell = new GroovyShell();
	File file = new File("parse-chromatogram.groovy");
	Script script = groovyShell.parse(file);
	script.run();
} catch(CompilationFailedException e) {
	System.out.println(e);
} catch(IOException e) {
	System.out.println(e);
}

The Groovy Shell can be used now to execute Groovy script files, for example a file to parse a chromatogram: parse-chromatogram.groovy.

/*
 * Select a chromatogram file.
 * Print the TIC value of each scan.
 */
File file = new File("Chromatogram1.CDF")
IProgressMonitor monitor = new NullProgressMonitor()
IChromatogramImportConverterProcessingInfo processingInfo
try {
	/*
	 * Try to process the chromatogram.
	 */
	processingInfo = ChromatogramConverter.convert(file, monitor)
	IChromatogram chromatogram = processingInfo.getChromatogram()
	int numberOfScans = chromatogram.getNumberOfScans()
	for(int i = 1; i <= numberOfScans; i++) {
		ISupplierMassSpectrum scan = chromatogram.getScan(i)
		println("SCAN [" + i + "] - TIC: " + scan.getTotalSignal())
	}
} catch(TypeCastException e) {
	/*
	 * Print error messages.
	 */
	List<IProcessingMessage> messages = processingInfo.getMessages()
	for(int i = 0; i < messages.size(); i++) {
		IProcessingMessage message = messages.get(i)
		println(message.getDescription() + "\t" + message.getMessage())
	}
}

But which classes and bundles are accessible by the the Groovy script? It depends on the referenced packages by the executing plug-in. In this case, I’ve added all necessary bundles of Eclipse and OpenChrom to use the scripts in a variety of situations. Use the “Dependencies” tab of the MANIFEST.MF file to add plug-ins whose classes shall be accessible by the Groovy script.

Plugin-Dependencies

That’s it! This feature will be available in the next release of OpenChrom end of October 2012, version 0.7.0 “Nernst”. The next step will be to offer a support for editing groovy files within OpenChrom. But so far, it’s a great step forward. The users of OpenChrom have the chance to create their own evaluation scripts now.

That’s groovy baby …

Advertisements

About Philip Wenig

Founder of OpenChrom
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s