Monday, April 21, 2008

Accepted to google summer of code!

I was accepted to participate in Google Summer of Code. I will be blogging regularly about my progress. The project I will be working is a Groovy Forms Module, which will enable administrators of OpenMRS to quickly create forms along with HTML and controllers. I think it will be a fun experience.

Wednesday, April 9, 2008

The new groovy mixins syntax

Today, I read this post to the groovy dev list and felt the strong urge to play with it. Now to use this, you'll need to compile the groovy code from the trunk in their subversion repository. Read the tutorial about how to build from source. To avoid the dreaded OutOfMemoryException, be sure to increase the amount of heap available to the JVM by doing: ANT_OPTS=-Xmx512m. Now the code.
 
class DeceptiveIntegerExt {
static def minus(int self, int other) {
self + other;
}
}

Integer.mixin DeceptiveIntegerExt

assert 5 - 5 == 10


Let's explain this code: First, I create my mixin class which I named DeceptiveIntegerExt for the sheer reason that the only purpose was to overload the subtraction operator and make it add the numbers. Then I add the mixin class to the Integer class; finally I assert that my mixin works as it should, and indeed it does. Albeit that this is a VERY simple example, but I wanted to play with it.

That's all for now.

Sunday, April 6, 2008

a CICE Bot

After writing a bot using both BGGA and First-Class Methods (FCM), I wanted to do Concise Instance Creation Expressions (CICE) as well. To compile you will need the cice prototype compiler and the jerklib jar. Now here we go:


import jerklib.ConnectionManager;
import jerklib.ProfileImpl;
import jerklib.Session;
import jerklib.events.listeners.IRCEventListener;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;

/**
* Created: Apr 6, 2008 10:55:08 PM
*
* @author Robert O'Connor
*/
public class CICEBot {
ConnectionManager manager = new ConnectionManager(new ProfileImpl("cicebot", "cicebot", "cicebot2", "cicebot3"));
Session s = manager.requestConnection("irc.freenode.org");

public CICEBot() {
s.addIRCEventListener(IRCEventListener(IRCEvent e) {
if(e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().join("#jerklib");
} else if(e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent)e;
jce.getChannel().say("Hello from CICE!!!");
}
});
}

public static void main(String[] args) {
new CICEBot();
}
}


The regular java version looks like this:


import jerklib.ConnectionManager;
import jerklib.ProfileImpl;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;
import jerklib.events.listeners.IRCEventListener;

/**
* Created: Apr 6, 2008 10:05:09 PM
*
* @author Robert O'Connor
*/
public class JavaBot {
ConnectionManager manager = new ConnectionManager(new ProfileImpl("javabot", "javabot", "javabot2", "javabot3"));
Session s = manager.requestConnection("irc.freenode.org");

public JavaBot() {
s.addIRCEventListener(new IRCEventListener() {
public void receiveEvent(IRCEvent e) {
if (e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().join("#jerklib");
} else if (e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent) e;
jce.getSession().sayChannel(jce.getChannel(), "Hi from Java!!!");
}

}


});
}

public static void main(String[] args) {
new JavaBot();
}

}

a first class bot (w/ FCM)

After writing a bot using BGGA, I decided to write one using First-Class Methods. Now to compile this, again you will need the FCM prototype compiler and the jerklib jar.

import jerklib.ConnectionManager;
import jerklib.ProfileImpl;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;

/**
* Created: Apr 6, 2008 10:23:06 PM
*
* @author Robert O'Connor
*/
public class FCMBot {
ConnectionManager manager = new ConnectionManager(new ProfileImpl("fcmbot","fcmbot","fcmbot2","fcmbot3"));
Session s = manager.requestConnection("irc.freenode.org");

public FCMBot() {
s.addIRCEventListener(#(IRCEvent e) {
if(e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().join("#jerklib");
} else if(e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent)e;
jce.getChannel().say("Hello from FCM!!!");
}
});
}

public static void main(String[] args) {
new FCMBot();
}
}

It is much like the BGGA bot; in fact i changed nothing and followed the same design:


import jerklib.ConnectionManager;
import jerklib.ProfileImpl;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;
import jerklib.events.listeners.IRCEventListener;

/**
* Created: Apr 6, 2008 10:05:09 PM
*
* @author Robert O'Connor
*/
public class JavaBot {
ConnectionManager manager = new ConnectionManager(new ProfileImpl("javabot", "javabot", "javabot2", "javabot3"));
Session s = manager.requestConnection("irc.freenode.org");

public JavaBot() {
s.addIRCEventListener(new IRCEventListener() {
public void receiveEvent(IRCEvent e) {
if (e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().join("#jerklib");
} else if (e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent) e;
jce.getSession().sayChannel(jce.getChannel(), "Hi from Java!!!");
}

}


});
}

public static void main(String[] args) {
new JavaBot();
}

}

an IRC Bot written using BGGA

I was curious how hard it'd be to write an IRC Bot using BGGA closures; now to compile this you'll need to grab the prototype and the jerklib jar. So here's the code:

import jerklib.ConnectionManager;
import jerklib.ProfileImpl;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;

/**
* Created: Apr 6, 2008 9:19:13 PM
*
* @author Robert O'Connor
*/
public class BGGAIRCBot {
ConnectionManager manager = new ConnectionManager(new ProfileImpl("bggabot","bggabot","bggabot2","bggabot3"));
Session s = manager.requestConnection("irc.freenode.org");

public BGGAIRCBot() {
s.addIRCEventListener({IRCEvent e =>
if(e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().join("#jerklib");
} else if(e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent)e;
jce.getSession().sayChannel(jce.getChannel(),"Hi from BGGA!!!");
}
});



}

public static void main(String[] args) {
new BGGAIRCBot();

}


}


Now, what would it look like without BGGA? It would like something like this:


import jerklib.ConnectionManager;
import jerklib.ProfileImpl;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;
import jerklib.events.listeners.IRCEventListener;

/**
* Created: Apr 6, 2008 10:05:09 PM
*
* @author Robert O'Connor
*/
public class JavaBot {
ConnectionManager manager = new ConnectionManager(new ProfileImpl("javabot", "javabot", "javabot2", "javabot3"));
Session s = manager.requestConnection("irc.freenode.org");

public JavaBot() {
s.addIRCEventListener(new IRCEventListener() {
public void receiveEvent(IRCEvent e) {
if (e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().join("#jerklib");
} else if (e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent) e;
jce.getSession().sayChannel(jce.getChannel(), "Hi from Java!!!");
}

}


});
}

public static void main(String[] args) {
new JavaBot();
}

}

Thursday, April 3, 2008

a groovy way to count number of lines

Groovy is nice to solve small problems (which usually will never pop up; but may!)

Note: The second example using eachLine() on a text file is the same file i used for my Automatic Resource Management (ARM) blocks example.


/*
this
is
a
string
*/
def str = """this
is
a
string
"""
int num = 0;
str.each { if(it == '\n') num++; }
println num // 4
/**
Doug Lea
Josh Bloch
Bob Lee
**/
num = 0;
new File("names.txt").eachLine { num++ }
println num; // 3


So that's it! 1 line of code to actually count the lines! the other 2 are just initialization and output!

Tuesday, April 1, 2008

April Fools: Byte code style!

Today, I stumbled upon this. It's amusing to say the least; at first i took it seriously but then it clicked, that this was possibly an April fools joke. My suspicions were proven to be true when i took a hex editor to String's bytecode, just to see for myself. I'd just like to say, an April Fool's joke done well Christian!

I cannot talk April Fool's without talking about Google. They have the most creative minds behind them, so it's not shocking they have some pretty imaginative April fool's jokes!