Wednesday, September 28, 2011

The Power of Ant

   The development of software on a larger scale, meaning there is some sort of development team, becomes difficult for many reasons. When attempting development with a team you will be dealing with multiple developers, possibly using multiple types of systems, with different development environments and the utilisation of third party libraries. So how in the world can you work together in an effective and efficient way? One option is utilising something called a build system, like Apache Ant:

  "Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications."

so what does that mean...

  It means that Ant uses files called "build files" to execute certain commands or tasks called "targets" whose purpose is to make the software development process better by creating/allowing usability and portability between different systems and developers. Ant provides a "unifying technology and set of principals for building any software system" (Johnson)

Download Ant at:
Apache Ant Home Page

In order to learn the power and utility of a build system like Ant I recently, and successfully, took on the "Ant code Katas". Lets take a look at these simple tasks that demonstrate and teach the Ant basics.  Below in a few of the katas I have added an image of my code to give an example of what a build file looks like.
                                                                             
1. Ant Hello World

Create a script called helloworld.build.xml.  Invoking this script via "ant -f helloworld.build.xml" should print out "Hello World".  The script consists of a single (default) target called helloworld with a single line that invokes the <echo> task to print out Hello World. 

2. Ant Immutable Properties

Create a script called immutable.build.xml.   This script contains two <property> elements, one of which assigns the property "my.property" to the value "1" and the second of which assigns the (same) property "my.property" to the value "2".  Now create a target called "printproperty" that uses the <echo> task to print out the value of the property "my.property".  

If done correctly, this script demonstrates that build properties are immutable: once they are set, their value cannot be changed. 

3. Ant Dependencies

Create a script called dependencies.build.xml.   It should contain targets called "foo", "bar", "baz", "qux", and "elmo".  Each target should consist of a single <echo> task that prints out the name of its target. The default target is foo.

The targets should have the following dependencies:
  • foo should depend upon bar.
  • bar should depend upon baz and elmo in that order.
  • baz depends upon qux.
  • qux depends upon elmo. 
  • elmo has no dependencies.
Now invoke the build script using "ant -f dependencies.build.xml".   What is the order of targets. In particular, where is elmo called and why

Try invoking "ant -verbose -f dependencies.build.xml".   Note that Ant now tells you how it seriallzed the dependency structure.

Now make elmo depend upon bar and reinvoke the script.  Make sure you understand what happens.                                                                                        
4. Hello Ant Compilation

Create a file called HelloAnt.java, which contains a Java class called "HelloAnt" and a main method that prints out "Hello Ant" to the console.  This file should be in a src/ subdirectory.

Create a second file called compile.helloant.build.xml that contains a single target called "compile".  This target contains a single <javac> task that compiles the HelloAnt program.  You do not need Ivy.   You should be able to compile the program with "ant -f compile.helloant.build.xml".   These class files should be written to a build/classes subdirectory.

You may want to create src.dir and build.dir properties. 



















5. Hello Ant Execution

Create a file called run.helloant.build.xml that compiles and runs the HelloAnt program.  It should use the <import> task to import the contents of compile.helloant.build.xml so you have access to those targets. 

Again, make this file as simple as possible. Provide a single target called "run", and make this "run" target depend upon the "compile" imported target so that the code is always compiled before being run. You should be able to compile and run the program with "ant -f run.helloant.build.xml".  

6. Hello Ant Documentation

Create a file called javadoc.helloant.build.xml that generates the JavaDocs for the HelloAnt program.  Once again, make this file as simple as possible; it can contain a single target called "javadoc" that invokes the <javadoc> task.  

The javadoc target should ensure that the HelloAnt program compiles, so you will want to import compile.helloant.build.xml   The javadoc html files should be placed in the build/javadoc subdirectory.  You should be able to generate the JavaDocs with "ant -f javadoc.helloant.build.xml". 

You can create a few properties to support the documentation process if you like.  You must ensure that the javadoc command completes without any warnings or errors.  This means you might have to improve your HelloAnt program!

7.  Cleaning Hello Ant

Add a target called "clean" to your compile.helloant.build.xml file. It should delete the build/ directory.

8. Packaging Hello Ant

Create a file called dist.helloant.build.xml.  It should contain a single target called "dist".   The dist target should have dependencies that compile, execute, and create javadocs for the helloant system (thus checking to make sure the system works correctly).   After all of these dependencies should be the clean dependency. 

The contents of the dist target creates a zip file containing the (cleaned) version of the current directory and all its subdirectories.  This zip file should be put into the (newly created) build/dist directory. Its name should be ant-code-katas.zip,  Unzipping this file should create a new directory called "ant-code-katas" which contains your ant kata files. 

To check your work, invoke "ant -f dist.helloant.build.xml".  Now cd to the build/dist directory, invoke "jar -xf ant-code-katas.zip".  This should create a new directory in build/dist called "ant-code-katas.   Now cd into that directory, and make sure that all of your kata files (and the src/ directory) are there, but that the build/ directory is not there.  Finally, invoke "ant -f dist.helloant.build.xml" in this directory. It should create a new build/dist directory containing yet another copy of ant-code-katas.
 Overall I have found that with a little practice Apache Ant is a very easy and powerful tool.  It is easy to imagine how necessary build systems like Ant are for certain kinds of software development.

Tuesday, September 20, 2011

Java Fueled Robots

  So you know how to program, your familiar with object oriented programming and you can write some fairly complex code, now where can you put your awesome skills to the test and have fun doing so?  How about designing the behaviour of a robot?
That sounds fun...
Better yet how about designing a complex, cannon wielding, slaying battle robot !
Now thats better.
   Robocode is an open source educational game started by Matt Nelson of IBM, you can get a good idea of what the game is all about here: ROBOCODE
Robocode allows you to design the look and behaviour of a battle robot, you can create anything from a simple robot that shoots its gun in the direction of an enemy, to one that utilises complex statistical analysis, your skill level is the only limit with these battle bots.  Well then, who do you get to battle and prove your superiority over given that you put your time and effort into making an effective killing machine? Robocode has a good sized online community where you can test your robots might against other programmers robots here: sourceforge 
  Okay so you are convinced and want to take on the quest of creating a robot capable of some mayhem, you need a starting point.  I would suggest starting the way I did, by completing the 13 robocode Katas.

Robocode Code Katas

  1. The minimal robot. Does absolutely nothing at all. 
  2.  Move forward a total of 100 pixels per turn. When you hit a wall, reverse direction.
  3. Each turn, move forward a total of N pixels per turn, then turn right. N is initialized to 15, and increases by 15 per turn.
  4. Move to the center of the playing field, spin around in a circle, and stop.
  5. Move to the upper right corner. Then move to the lower left corner. Then move to the upper left corner. Then move to the lower right corner.
  6.  Move to the center, then move in a circle with a radius of approximately 100 pixels, ending up where you started.
  7. Pick one enemy and follow them.
  8. Pick one enemy and follow them, but stop if your robot gets within 50 pixels of them.
  9. Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
  10. Sit still. Rotate gun. When it is pointing at an enemy, fire.
  11. Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
  12.  Sit still. Rotate gun. When it is pointing at an enemy, use bullet power proportional to the distance of the enemy from you. The farther away the enemy, the less power your bullet should use (since far targets increase the odds that the bullet will miss). 
  13. Sit still. Pick one enemy and attempt to track it with your gun. In other words, try to have your gun always pointing at that enemy. Don't fire (you don't want to kill it).

  These katas are a great starting place to really get familiar with the robocode system (in my opinion katas are a great starting place for any new concept).  Once completed you will have a firm grasp of how to use all of the robots functions and have a good idea about how to start coding a battle ready robot.  I found these katas to be fairly basic only spend significant time on a few of them, why? because I had to remember my trigonometry !  You know... that class you took years ago and never found a use for... well, its back.  I was actually very excited to be able to use things like the distance formula and tangent lines for something practical.  The most difficult aspect of these katas for me was reviewing and figuring out applicable trig. formulas.

So now that you know what Robocode is I am sure you want to see it in action. 
Here are katas 7 and 9 together, no destructiveness but a funny demonstration of their respective behaviours: 

 Go get started with Robocode, I guarantee you will not be disappointed.