Thursday, September 17, 2009

Strange XMLParser behaviour - Groovy 1.6.4

ell right now I'm stumbling over a small thing in groovy and don't understand why this is happening.

So this is my code:


new StreamingMarkupBuilder().bind({

sop(desc: "generated for quantification") {
transform(sizedown: 0, attributes: "height", combine: true) {
header{
param(value:"retention_index")
param(value:"quantmass")
param(value:"id")
param(value:"spectra")
}
}
}


simple enough.

And that's the code to test it


def parser = new XmlParser().parse(source.getStream())

assertTrue parser.@desc != null

assertTrue parser.transform != null

assertTrue Integer.parseInt(parser.transform.@sizedown[0]) == 0
assertTrue parser.transform.@attributes[0] == "height"
assertTrue parser.transform.@combine[0] == "true"

assertTrue parser.transform.header.param[0].@value == "retention_index"
assertTrue parser.transform.header.param[1].@value == "quantmass"
assertTrue parser.transform.header.param[2].@value == "id"
assertTrue parser.transform.header.param[3].@value == "spectra"

assertTrue parser.transform.header.param.size() == 4


also pretty straight forward.

Now why is this part different...


transform(sizedown: 0, attributes: "height", combine: true)

assertTrue Integer.parseInt(parser.transform.@sizedown[0]) == 0
assertTrue parser.transform.@attributes[0] == "height"
assertTrue parser.transform.@combine[0] == "true"


and my attributes are returned as an array list compared to


header{
param(value:"retention_index")
param(value:"quantmass")
param(value:"id")
param(value:"spectra")
}

assertTrue parser.transform.header.param[0].@value == "retention_index"
assertTrue parser.transform.header.param[1].@value == "quantmass"
assertTrue parser.transform.header.param[2].@value == "id"
assertTrue parser.transform.header.param[3].@value == "spectra"


where the attributes are returned as simple string. I has to be something obvious, but right now I'm not getting it.

Wednesday, September 16, 2009

XMLMarkupBuilder in groovy.

Since a while i wanted to find a reason to play with the xml markup builders in groovy, since I think they are an awesome idea.
So I gave it a shot


def document = new StreamingMarkupBuilder().bind({

config {
parameter {
param(name: "java.naming.provider.url", value: "127.0.0.1", public:true)
param(name: "java.naming.factory.initial", value: "org.jnp.interfaces.NamingContextFactory", public:true)
param(name: "java.naming.factory.url.pkgs", value: "org.jboss.naming:org.jnp.interfaces", public:true)
param(name: "edu.ucdavis.genomics.metabolomics.util.status.ReportFactory", value: "edu.ucdavis.genomics.metabolomics.binbase.cluster.status.EJBReportFactory")
param(name: "edu.ucdavis.genomics.metabolomics.util.thread.locking.LockableFactory", value: "edu.ucdavis.genomics.metabolomics.binbase.cluster.locking.EJBLockingFactory")

}
}
})

println document


and the result is


<config><parameter><param name='java.naming.provider.url' value='127.0.0.1' public='true'/><param name='java.naming.factory.initial' value='org.jnp.interfaces.NamingContextFactory'/><param name='java.naming.factory.url.pkgs' value='org.jboss.naming:org.jnp.interfaces'/><param name='edu.ucdavis.genomics.metabolomics.util.status.ReportFactory' value='edu.ucdavis.genomics.metabolomics.binbase.cluster.status.EJBReportFactory'/><param name='edu.ucdavis.genomics.metabolomics.util.thread.locking.LockableFactory' value='edu.ucdavis.genomics.metabolomics.binbase.cluster.locking.EJBLockingFactory'/></parameter></config>


Neat! This was very simple and the validation of the code happens at runtime. The only uggly part is that idea shows this part in red brackets


public:true


since public is a keyword of java/groovy. But it does compile...

Guess I have to change my api a bit to avoid this uggly keyword or at least make it optional.

Tuesday, September 15, 2009

Groovy 1.6.4 + IntelliJ8 + Groovy Plugin - nullpointer

For soem reason I keep get nullpointer exception with IntelliJ, which have nothing todo with my code.
They only happen if I execute the project from intelliJ out.

example for this would be:


src/example/dsl/quant.dsl
Caught: java.lang.NullPointerException
java.lang.NullPointerException
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at edu.ucdavis.genomics.metabolomics.binbase.quantificator.dsl.QuantificatorDSL.run(QuantificatorDSL.groovy:45)
at edu.ucdavis.genomics.metabolomics.binbase.quantificator.dsl.QuantificatorDSL$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at edu.ucdavis.genomics.metabolomics.binbase.quantificator.dsl.QuantificatorDSL.main(QuantificatorDSL.groovy:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1296)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:719)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:262)
at groovy.lang.GroovyShell.run(GroovyShell.java:218)
at groovy.lang.GroovyShell.run(GroovyShell.java:147)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:493)
at groovy.ui.GroovyMain.run(GroovyMain.java:308)
at groovy.ui.GroovyMain.process(GroovyMain.java:294)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:111)
at groovy.ui.GroovyMain.main(GroovyMain.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)


Now this exception does not occur, if I make a complete rebuild.

For some reason it is not able to initialize a class if it defined in another source file. More about this is written here

Thursday, September 10, 2009

groovy 1.5.6 and delegates

while trying to write a DSL I discovered an annoying bug in groovy 1.5.6, I guess. Basically if I define a delegate for a closure it has to be in the same file as the closure. If i define it in another file I always get a nullpointer exception...


example:



class MyDelegate {
def test() {
println "tada"
}
}

static ExpandoMetaClass createEMC(Class clazz, Closure cl) {
ExpandoMetaClass emc = new ExpandoMetaClass(clazz, false)

cl(emc)

emc.initialize()
return emc
}

Script dslScript = new GroovyShell().parse("test{\ntest()\n}")

dslScript.metaClass = createEMC(dslScript.class, {
ExpandoMetaClass emc ->

emc.test = {

Closure closure ->
closure.delegate = new MyDelegate()
closure.resolveStrategy = Closure.DELEGATE_FIRST

try {
closure()
}
catch (Exception e) {
e.printStackTrace()
}
}
})
dslScript.run()



this works fine. Now if I move the class 'MyDelegate' to it's own file I end up with the following exception.


java.lang.NullPointerException
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.getDelegateMethod(ClosureMetaClass.java:207)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:280)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:78)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrent0(ScriptBytecodeAdapter.java:112)
at Script1$_run_closure1.doCall(Script1.groovy:2)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:248)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:78)
at Script1$_run_closure1.doCall(Script1.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:248)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:778)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeClosure(ScriptBytecodeAdapter.java:605)
at edu.ucdavis.genomics.metabolomics.binbase.quantificator.dsl.Tester$_run_closure1_closure2.doCall(Tester.groovy:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:248)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at groovy.lang.Closure.call(Closure.java:292)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke(ClosureMetaMethod.java:72)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:78)
at Script1.run(Script1.groovy:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:778)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0(ScriptBytecodeAdapter.java:198)
at edu.ucdavis.genomics.metabolomics.binbase.quantificator.dsl.Tester.run(Tester.groovy:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:778)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:401)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1105)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:749)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at edu.ucdavis.genomics.metabolomics.binbase.quantificator.dsl.Tester.main(Tester.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1105)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:749)
at groovy.lang.GroovyShell.runMainOrTestOrRunnable(GroovyShell.java:244)
at groovy.lang.GroovyShell.run(GroovyShell.java:218)
at groovy.lang.GroovyShell.run(GroovyShell.java:147)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:493)
at groovy.ui.GroovyMain.run(GroovyMain.java:308)
at groovy.ui.GroovyMain.process(GroovyMain.java:294)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:111)
at groovy.ui.GroovyMain.main(GroovyMain.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:101)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)


time to give groovy 1.6.4 a try and maybe migrate my project over to it.

Wednesday, September 9, 2009

DSL - love or hate?

recently I started to work more with DSL's (domain specific language) since I wanted to go away from doing everything in XML.

The approach of XML is great, but I'm not a big fan of the overhead with it.

So the first approach was to create a DSL for the BinBase system, precisely to provide the user with a really simple way of calculating an experiment.

It should look something like this:


/**
* our quantification dsl
*/
quantify {

//define the name of the setup
name "quantification for ${new Date()}"

//define the samples for the calculations
sample "sample 1"
sample "sample 2"
sample "sample 3"
sample "sample 4"
sample "sample 5"
sample "sample 6"
sample "sample 7"
sample "sample 8"
sample "sample 9"
sample "sample 10"

//defines the bins in the table
bin 321, 22.0
bin 322
bin 323
bin 324
bin 325, 22.0

//report definition
report {

sizeDown 50

format "xls"

replace true
}
}


And the result should calculate the data, send them to the cluster and generates a quantification report. If this all works out the way we want it. We will than adapt it to schedule BinBase experiment's this way.

My current fazit is that I love the idea of DSL's, but hate the implementations of it.

jboss - binding to all registered network cards

The JBoss Application server binds only to the local interface '127.0.0.1' since the version 4.0

Since this is not desired in a production system you can override this feature with the '-b' command.

An example to start jboss in the all configuration and bound to all registered interfaces would be


sh run.sh -b 0.0.0.0 -c all

Thursday, September 3, 2009

creating a project with maven2

it is kinda pathetic I'm using maven since several years now and just can't remember this one command.


mvn archetype:create -DgroupId=myGroup -DartifactId=myArtifact


so I write it down here to make it easier for me to lookup.

Wednesday, September 2, 2009

osx - set disk as startup disk via terminal

to set a disk as startup disk is quite simple just execute:


sudo bless -mount /Volumes/"name of your startup disk" -setBoot

Tuesday, September 1, 2009

run-app vs run-war

the main differences between these two commands is that:

run-app runs a grails applications and allows the reloading from resources
run-war generates a war file and runs the war file, so no reloading of resources.

Short during development you want to use run-app.