Friday, October 9, 2009

Hack ReSharper for x64 unit tests

If you need to develop 64bit .NET application and run unit tests with R#, you will find that it's not possible, because Visual Studio is 32bit process and R# task runner is loaded as 32bit as well. There is quick and dirty solution to that.

1) Stop Visual Studio
2) Find JetBrains.ReSharper.TaskRunner.exe
3) Drop read-only flag on the file
4) Run
CorFlags.exe JetBrains.ReSharper.TaskRunner.exe /32BIT- /Force


The assembly have now broken signature and VS/R# complains about that. But now you could debug your 64bit unit tests. More info would be probably here

Tuesday, June 23, 2009

Robocode modules as in version 1.7

In version 1.7 we managed to split Robocode into several modules, introduce dependency injection using PicoContainer. We as well started using Maven 2 for builds. Now I will try to explain it a bit.
Robocode

Modules Description

  • robocode.api - This is really robocode.jar as you know it.
    • But now it contains only Robot API, everything what's visible to the robot in package robocode.
    • It also contains Control API, which you can use to automate the game, for example RoboRumble does exactly that.
    • Main entry point of the application. It finds robocode.core and calls it very soon after start.
    • There are few interfaces and base classes to allow robocode.core and other modules implement them.
    • It is very important to not add any compile time dependency of robocode.api on any other .jar, because that would cause all robot authors would need to have that dependency as well.
  • robocode.core - The Hub.
    • It loads all robocode modules. Module is any .jar file in libs directory with name starting with "robocode". It finds class Module from package of the same name as the .jar file. For example net.sf.robocode.repository.Module. The class should initialize itself in static constructor. That's module entry-point. Good way how to do it is to register components (classes and instances) into PicoContainer. Or as a singleton or as a factory (transient). There is only single container in whole application. Components could be also registered under name, which is interesting for extensions, example is JavaHost.
    • Because core is referenced by every other module, it serves as place where all interfaces are defined. But most of the implementations are outside of core. This helps us to prevent spaghetti of dependencies.
    • In the future, we may have more extension points, they will be defined by interface which any extension should implement. The interface will be added into core if they are not specific to one of existing modules.
    • Currently we do NOT consider any interfaces public API, so if you want to create extension to Robocode, you should talk to us on our discussion email group first.
  • robocode.battle - The rules and main battle loop.
    • It runs the battle thread, which is driving battle life-cycle. Battle has rounds, turns, and each turn have several steps. See Battle.runTurn()
    • The battle thread also creates robots, by using robocode.host, pauses them between turns, reads their commands and produces events for robots, like ScannedRobotEvent or HitByBulletEvent. It transfers the event from GUI to interactive robots.
    • It also produces all events for IBattleListener, they are produces each turn and we call them snapshots, about position of robots, bullets, etc. They are (99%) immutable structures. They also could contain serialized form of drawing commands from robots and text written by robots.
    • This module would expose extension points when we will implement custom extensible battle rules and battle fields. Watch this space!
  • robocode.host - The security sandbox for robot.
    • Loads robot into separate ClassLoader, so he can't access anything but robocode.api. There is bit of magic to load from nested .jar files for team robots.
    • Java SecurityManager is enabled, so we prevent it from all cheating or attacks to user computer.
    • Disk operation are limited to certain directory and volume of data. Files are extracted from robot's .jar file on demand.
    • There are *RobotProxy classes, which transform robot's commands like setFire() into message, which is send each turn, on execute() call. It also validates them.
  • robocode.repository - Is the dabase of robots installed on the system.
    • Robot classes are loaded and registered when the game spots them in robots directory. Robot property file is also extracted. The metadata are then stored in database, which is serialized into one file. The trick there is that the database is faster to read than to load all the classes and examine them. We do reload the classes or properties, when we found that file timestamp is later than last timestamp stored in database.
    • There is quite lot of logic to figure out the naming and versioning conflicts of robots.
  • robocode.tests - Our unit tests and integration tests.
    • There are different kinds of robots made just for the purpose of testing some feature of Robocode. Some of them try to break rules, attack the security system. Others behave interesting way on battlefield and we measure them, to learn if the game physics is OK.
    • The tests are build with junit, there is base class RobotTestBed which prepares and runs the battle. It implements IBattleListener and we could read all the events produced by game and watch the game by code. During the game we make notes and in tear down phase we evaluate them with asserts.
    • There are some regular unit tests as well, but just few of them.
  • robocode.ui - Windows and BattleView.
    • Windows are mostly boring stuff, but there are several problems. One of them is UI thread, which keeps window responsible during the game. But we need to make sure that the UI thread is not used to call battle components or even robot's code (as in pre 1.6). To do that BattleManager is really queue of commands for battle thread. They are executed on battle thread in each turn. Well it's not that simple, and we still have some troubles when the game is paused.
    • Another problem is how to paint all the events produced by battle. They are regullary dispatched on battle thread to anyone registered in BattleManager.addListener(), but for UI it;s different. Imagine that we could run the battle at 9K TPS (turn per second), you could easily see that it doesn't make any sense to render it with same frame rate (FPS). So we introduced AwtBattleAdaptor, which receives them all and dispatches only selected ones to UI thread. It's a bit complicated, because we could skip lots of onTurnEnded, but we need to preserve the text output from robots. So here it comes, we cache the texts per robot and inject it into those snapshots which are really dispatched. (That's only place where we broke immutability of snapshots, and we do that transparently to the recipients). Also bear in mind that we need to deliver all the events of other types, and to do that in correct order in relation to onTurnEnded. We do not dispatch onTurnStarted as current optimization. Anyone on GUI thread could register in AwtBattleAdaptor.addListener() and should properly unregister during disposal.
  • robocode.ui.editor - Robot editor for real beginners. Personally I think we need to rather create Robocode as plugin to Eclipse, any volunteer ? There is also problem with Jikes, it's ages old and have bugs. But not all people have JDK installed.
  • robocode.roborumble - Download robots, run the battles, upload results. Robocode@Home distributed computing, to get scores. We had issues several releases now, so we were unable to contribute to the grid with recent versions. Hopefully it's over now.
  • robocode.sound - Just sound. Easy stuff.
  • robocode.installer - Our hand-made installer.
  • robocode.content - All other files to be deployed.
  • robocode.samples - Our sample robots.

How to create new module

  • Read the Developers Guide
  • Unzip robocode.dummy.zip and rename all dummy files and all dummy things inside files.
  • Implement Module class as described above.
  • Add your module to main pom.xml
  • Try if you are smart and brave enough to make at least small progress alone. And then definitely TALK to us about your idea.
  • Then we decide where your extension belongs. If it's to be installed with main installer, keep it in root directory. If it should have separate installer, move it to plugins directory and create installer for it. There are several unfinished examples already. Next time about that.
Over and out.

Wednesday, June 17, 2009

Unity IoC - context aware

Unity will inject it self into constructor or property with IUnityContainer interface. No need to implement any "ApplicationContextAware"
public class Something
{
    //use this
    public Something(IUnityContainer container)
    {
        Container = container;
    }

    //or this
    public IUnityContainer Container { get; set; }
}
Found here

Tuesday, June 16, 2009

How to get non inherited interfaces

Is documented fact that GetInterfaces() method returns all interfaces implemented by given type including inherited ones. I wanted to get only interfaces defined by the class, and was searching for the solution. No luck. But it's really simple.
Just check if base class is implementing the interface, if yes filter it out.
Type type = typeof(Type);
Type[] nonInheritedInterfaces = type.GetInterfaces().Where((ifc)
     => !ifc.IsAssignableFrom(type.BaseType))
  .ToArray();

*Yes, if you are still on 2.0 you could rewrite it to loop.

Sunday, March 8, 2009

URL access to nested .jar files

Tonight I implemented loading of classes from nested jar files. It seems to me that people might be interested in such trick, so here you are.

I created new protocol handler jarjar: which just redirects inner part of URL to .jar handler again. Please note that I use '^/' as jar-in-jar separator to avoid conflict with regular jar handler.

Let's have Outer.jar which contains Inner.jar which contains tested/robots/Ahead.class
Now you can do this
// initialize, just once per JVM please
JarJarURLConnection.register();

new URL(
"jar:jarjar:file:/C:/mylibs/Outer.jar^/Inner.jar!/tested/robots/Ahead.class"
).openStream();
Or create and configure ClassLoader this way
// initialize, just once per JVM please
JarJarURLConnection.register();

new URLClassLoader(
  new URL[]{new URL("jar:jarjar:file:/C:/mylibs/Outer.jar^/Inner.jar!/")}
).loadClass("tested.robots.Ahead");
If you need something more sophisticated or user friendly One-JAR is the solution I think.

Tuesday, January 20, 2009

English peg solitaire

My colleague at work mentioned that there is some puzzle where we have 33 holes in shape of cross and 32 bullets in all the holes but central. To move you should skip one bullet with it's neighbor into empty hole and remove the skipped one. The goal is to clean all but last bullet. He mentioned that it would be interesting to find solution with computer.
And we didn't knew the name of the game. Now it took me almost an hour to find it out for purpose of this article. So now when you know the name of the puzzle, you can easily find the solution on Internet. Actually quite interesting reading.

But I enjoyed brute force search for solution. Below is solution I found after few hours of coding and 6 minutes of intensive computing.

  XXX     XXX     XXX     XXX     XXX     XXX     XX0     XX0   
  XXX     XXX     0XX     0XX     0XX     0XX     0X0     0XX   
XXXXXXX XXXXXXX XX0XXXX 00XXXXX 0X00XXX 0X0X00X 0X0XX0X 0X0X00X 
XXX0XXX X00XXXX X0XXXXX X0XXXXX X0XXXXX X0XXXXX X0XXXXX X0XX0XX 
XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX 
  XXX     XXX     XXX     XXX     XXX     XXX     XXX     XXX   
  XXX     XXX     XXX     XXX     XXX     XXX     XXX     XXX   
  
  XX0     XX0     XX0     XX0     XX0     XX0     XX0     XX0  
  0XX     0XX     0XX     0XX     0XX     0XX     0XX     0XX  
0X0X00X 0X0X00X 0X0X00X 0X0X000 0X0X000 0X0X000 XX0X000 00XX000
X0XXXXX X0XXXXX X0XXXXX X0XXXX0 X0XXXX0 X0XX0X0 00XX0X0 00XX0X0
XXXX0XX XXXXX00 XXX00X0 XXX00XX XXX0X00 XXX0000 0XX0000 0XX0000
  XX0     XX0     XX0     XX0     XX0     XXX     XXX     XXX  
  XXX     XXX     XXX     XXX     XXX     XXX     XXX     XXX  

  XX0     XX0     00X     00X     000     000     000     000  
  0XX     0XX     0XX     0XX     0X0     XX0     XX0     XX0  
00XX000 00XX000 00XX000 00XX000 00XXX00 000XX00 00X0000 00X0000
00XX0X0 00XX0X0 00XX0X0 00XX0X0 00XX0X0 000X0X0 000X0X0 000X0X0
0XX0X00 0XXXX00 0XXXX00 0XX00X0 0XX00X0 0XX00X0 0XX00X0 000X0X0
  XX0     X00     X00     X00     X00     X00     X00     X00  
  XX0     X00     X00     X00     X00     X00     X00     X00  

  000     000     000     000     000     000     000     000  
  XX0     0X0     0X0     0X0     0X0     0X0     0X0     000  
00XX000 000X000 000X000 00XX000 00XX0X0 0000XX0 000X000 0000000
00000X0 00X00X0 00X00X0 00000X0 0000000 0000000 0000000 000X000
00000X0 00000X0 00X00X0 00000X0 0000000 0000000 0000000 0000000
  X00     X00     000     000     000     000     000     000  
  X00     X00     000     000     000     000     000     000  

And there is the code. I renamed it to peg solitaire afterwards. I used C5 library for HashDictionary, but I'm not sure whether it was really necessary. And yes it takes some memory to compute the results as do breadth-first search. I don't even try any clever heuristics or cutting the tree.

Wednesday, July 2, 2008

Plan for Robocode 1.6.2

We just released Robocode 1.6.1 Beta for community testing. While we are waiting for at least one email with feedback (we really miss it), I will present detailed list of tasks for next version 1.6.2.

RobotPeer redesign - messages instead of shared state

Each robot has it's peer which takes care about the real truth about that robot. Things like energy, velocity, direction and also intentions (settings or commands) are stored there. Battle is reading them each turn and modifying according to rules. Robot is accessing that location using RobotAPI. This location is represented with class RobotPeer.
There are two problems currently. First is that both threads battle and robot's are accessing RobotPeer, so data there need to be synchronized. Current implementation does that per call. So subsequent calls could return different values. And as it is, we couldn't do any better, because bigger granularity could lead to deadlocks or at least to slowdown.
Second problem is that there is fixed cost per call for such synchronization from both sides, robot's and battle's.

What we plan is that we split RobotPeer class to two separate parts. First will serve to robot and second will serve to battle. They will be synchronized once per turn. Let's call first part RobotProxy and RobotPeer the second part from now on.
Great moment for such synchronization is when robot calls execute() method. As everyone knows, robot is blocked till next turn there. We could gather all settings and commands set by robot from last turn and make message from them. Then we could send such message to battle's part.
During processing of turn facts about robot are changed, also new events are produced. Such data need to be sent back to robot during resuming after call to execute(). That data will be merged to information set hold by RobotProxy. RobotProxy will serve as local cache for such info.

We may also wish to decompose hosting of the robot from actual battle logic.

Battle recording and replay - listener and producer

  • New version of independent class inside of BattleManager class, next to Battle class. May be called Recorder ?
  • Still controlled thru BattleManager and sending BattleEvents like Battle does
  • Recording should be implemented as IBattleListener
  • Replay will just feed dispatcher of BattleManager
  • Think about IBattleControl interface and where to put method for saving and loading recorded battles
  • Should be possible to go to any turn, and also rewind the battle etc.
  • Should be able to records robot renderings as well as print outs
  • Possibility of saving/loading the Replay to a file, e.g. XML

Introduction of interfaces of future modules

As preparation for modularization in 1.6.3, we could do some of the steps earlier. The difference is that in 1.6.2 we will not move implementations in namespaces and we will not break compatibility. My guess is that full modularization will need such non-compatible changes. I'm not talking about RobotAPI, but ControlAPI. So we might even need to release it under 1.7 version instead of 1.6.3.
It will be big change. So some of the work ahead could help us manage it better.

Low priority tasks - nice to have

  • OptionManager - components subscribe for changes in setup/options/configuration
  • Reduce complexity of TeamPeer, ContestantPeer, ContestantStatistics, TeamStatistics
  • Decouple battle save/load from BattleManager
  • Adding the music from Hans HiScore
  • ATW battle events dispatcher, single point dispatcher on AWT thread
  • Robocode log to window - not only to console but as well to GUI window