Aug 28, 2009

Interfacing between JRuby and Scala

One of the major features of JVM languages is their interoperability. It's really easy to call Java code from a JRuby script, and (while slightly less easy) you can run a Ruby script from within a Java program.

However, how difficult is it to interface between two non-Java JVM languages? It turns out that it isn't really that hard at all! For this post, we'll talk about how to interface between JRuby and Scala, it's really rather simple.

So here's our scenario. We are writing our main app in Scala, because Scala is faster than Ruby. However there are some situations where we don't want to have to recompile the whole thing to make changes, and we want the code to be nice and easy to understand for people who don't know Scala (admittedly, Scala is not a good first language as it is rather complex). We're going to embed some Ruby scripts within our app.

Here's a basic Scala outline in ScalaTest.scala:
import javax.script._   // import Java's scripting API

object ScalaTest extends Application {
var engine = (new ScriptEngineManager).getEngineByName("jruby")

engine.eval(new FileReader("test.rb"))
And test.rb:
puts "Hello from Ruby!"
Before we attempt to run this though, we'll need to get both JRuby, and the JRuby engine. You can grab JRuby from their website (as of this writing the latest version is 1.3.1), just extract that and grab jruby.jar from the lib folder. As for the JRuby Engine, you'll have to grab the big engine tarball from the Java site and extract jruby-engine.jar from the jruby/build folder. Once you have those put them in the same folder as your code, then compile and run:
scalac ScalaTest.scala
scala -cp .:jruby-engine.jar:jruby.jar ScalaTest
What you should see is "Hello from Ruby!" pop up on the screen.

So this is pretty cool, but how do we get information between Scala and Ruby? That part can be a little bit tricky since the type systems in Scala and Ruby are very different. However for our example here, we will stick to simple things. Here is our new Scala code:
import javax.script._

object ScalaTest extends Application {
val engine = (new ScriptEngineManager).getEngineByName("jruby")

val name = Console.readLine("What is your name?\n")

// assign a variable in the engine
engine.put("name", name)

// cast the result of the execution into a string
val colour = engine.eval(new FileReader("test.rb")).asInstanceOf[String]

Console.print(name + ", your favourite colour is ")

// this part is unnecessary, but let's have some fun with colours :)
if (colour.toLowerCase == "blue")
else if (colour.toLowerCase == "red")
else if (colour.toLowerCase == "green")

Console.println(colour + Console.RESET + ".")
And the Ruby code:
# the name variable is passed in as $name, which is a String
puts $name + ", what is your favourite colour?"
While this is a trivial example, we could use this to embed all sorts of functionality within our application. In fact, this isn't even limited to JRuby. By changing the getEngineByName call to say, "Javascript", we can execute Javascript code instead of Ruby code. Or any other JVM language included in that big engine tarball, including Scheme, Jython, and Groovy to name a few. You can even use Java as a scripting language, although you have to jump through a few hoops since Java doesn't support global variables.

Aug 16, 2009

Lisp and Code Mutation

So exams for summer courses are done, and I have no job. I do have a small contract, but other than that I have plenty of time on my hands. So I'm working on a tiny little idea I had (if you've been following this blog for any amount of time I tend to get a fair few of those).

One thing I've always found interesting about Lisp (for this article we can assume I'm talking about Common Lisp) is the homoiconicity. Well I don't know enough of the language to comment on its practical nature - I've been having a nightmare of a time getting into it, documentation online is not great and there seems to be a lot of simple things (like string padding) that aren't there, or at least not that I can find - some of the things you can do with the language are downright amazing. Ruby likes to boast that it can do metaprogramming, but it is nowhere near as powerful as what Lisp can do.

Anyway, enough on the impressions of the language (I'm a language snob, I know). Let's get to this idea. A friend of mine who is doing her master's at University of Calgary in swarm theory was out visiting about a month ago for a conference on genetic algorithms and evolutionary computing called GECCO. So of course, we ended up talking about the subject. She said that with much of the current research, what they do with genetic algorithms is they put a set of inputs into a program, and then let the program find an "optimal" version of those variables. I thought this was interesting, but limited. See, the variables will change, but the code? Nope, it will stay fixed. The algorithm doesn't change, just the variables within it. Now when working with large neural networks this isn't bad at all, but sometimes maybe the program is the thing that should be changed and not the inputs.

So it got me thinking. In Lisp, because of homoiconicity, code is data and therefore can be manipulated as data. So what if you wrote a program that took as an input another program and a specification, and would alter the program in random ways and check if the program still matched the specification? It would be very interesting to see what would come out.

An even more interesting thing is this: what if the specification was incomplete? If it is too incomplete, the program might not even work anymore. However if the specification just checks the important bits, it's possible that a program outputted by the program re-writer could do some interesting extra things outside of the specification. Most likely not, but it could happen.

Finally as a closing note, there will be programmers who will point out that you could write a program re-writer in any language. It's just the nice thing about Lisp is that you can read in and parse the program by going (read input), which returns an s-expression representing the code that was just read in. It is trivially simple. Then on top of that you can manipulate this s-expression all you like, and then output it to a new file by going (pprint sexp out) and you have an output which is still Lisp. So the whole time, you're working with a single language and there is no real need for any intermediate forms.

Aug 14, 2009

Actor Simulation Synchronization Issues

A while back I did up a post about using Scala actors in a synchronized simulation. I'm hoping to use this to model a simple economy, however I've run into a major problem and I'm trying to find a solution to it - which is why it took so long for me to write a follow-up to that last post.

The way the other simulation worked is by using synchronized time steps. Normally this is good because it means that actors which take varying amounts of time to process can all finish their job before the next time step starts. This also helps when you decide to scale and use a distributed system, you will end up having slower machines and network delay, but can still maintain synchronization because the faster machines will either do more work, or will wait for the slower machines.

Synchronization in my simulation was done using the Act and DoneAct messages. When an agent receives an Act message, it means that the next time step is starting and that they should begin processing. Then when they are done, they send a DoneAct message to the simulation driver to say that they have finished processing. The problem with this is that some agents may send messages to other agents that are finished. There is the scenario that an agent, A, sends a message to another agent, B, and then A sends the DoneAct message to the simulator. However B may well still be processing away the message, which may in turn affect agent C, which may then wrap around and affect agent A. Effectively this could turn into an infinite loop, and due to the nasty halting problem there is not really any general way of detecting this.

I have a solution, however I am not sure if it will work. Basically agents cannot communicate with one another if they are using the ! approach (asynchronous messaging). They can use the ?! method (synchronous method) where when agent A sends a message to agent B, agent A will wait for agent B to return before continuing execution (basically the same as a method call in traditional OO languages like C++ or Java). However in the case where agent A wants to send an asynchronous message to agent B, it can do so by sending a Delayed() object to the simulator, which will then send the message to B at the next time step.

Basically I'm thinking out loud here, hoping that by explaining things here it might give me some insights on how to go about building this thing. If anybody is interested in this type of thing and wants to give their two cents, feel free to comment :)

Aug 13, 2009

Confessions on Retirement

Back in June I announced that I would no longer be working as a programmer, at least temporarily.

That is not entirely true, and over the last little while I've come up with a few scenarios where I will be writing code:
  • Open-source software - I still have no problem with submitting patches to open-source software, or creating my own open-source works. While I don't believe that users of open-source software should be required to give back to the community, I do think that if I'm using the fruits of all their hard work, it's only fair that I give something back now and then. But that's a personal philosophy, and I will not tell others to do the same.
  • Coding for myself - this has a bit of overlap with the previous one, since contributing to open-source helps fulfill some personal obligation or whatever you want to call it, or it helps improve the tools that I use every day, so you can say that any open-source contribution I make is basically coding for myself anyway. However this point is more for non-open-source projects I may undertake. For example, if I should decide to start my own business that uses some kind of software, it's likely that I will be writing some code. Should the healthiness of the business depend on others not having the software, then I will likely keep it proprietary. However patches and tweaks I make to open-source software that I use would be contributed back (well, they'd have to be if it were a GPL'd library).
  • Small Contracts - this is the main confession I have to make. I don't like coding sites for other people's business, mainly because they usually aren't a business I'm really all that interested in, nor do I have much stake in the gains of the business. However at the moment I am a student. Other people have money; I do not. So small contracts are a good way to get a few bucks on the side to help pay for food and rent. Since people giving out small contracts usually don't care too much whether you like their work or not provided you do a good job, there isn't really any issues with my actual sentiments towards the work.
So those are my confessions, sorry for the misleading post saying that I was retiring from coding - a few people who know me expressed shock at that announcement. I still am sorta retired since I won't be doing any full-time jobs in software (unless it is for myself), however I'm not totally out of the biz.

Aug 1, 2009

Ubuntu's Hibernate Won't Wake Up

UPDATE (Feb. 14, 2010): This is not a way to get hibernate working on Ubuntu. This is a way to make your computer able to boot after you've accidentally put it into hibernate and it won't wake up anymore.

So I made the mistake today of clicking "Hibernate" from the shut down menu. It went into hibernate just fine, just a few hours later when I wanted to reboot it didn't work. I just got to a black screen with a flashing cursor in the top left. I figured it was just being slow, so I went to make breakfast. Came back, still in the same state.

After digging through forums under Windows and trying to find a good way to fix my problem, I ended up with the following solution, if you ever find yourself in this situation:

If you're using GRUB1 (if you haven't installed Ubuntu from scratch since Oct. 09)
1) Hit Esc if you need to access the boot menu, otherwise it should pop up normally.
2) Go to the normal Ubuntu item (usually the one that is selected by default) and press "e".
3) Go to the line that starts with "kernel" using the arrow keys and press "e".
4) Remove the "quiet" and "splash" from the end, and add "no_console_suspend".
5) Press Enter, and then "b".

With GRUB2:
1) Hit Esc if you need to access the boot menu, otherwise it should pop up normally.
2) Go to the normal Ubuntu item (usually the one that is selected by default) and press "e".
3) Go to the line that starts with "kernel" using the arrow keys and press "End".
4) Remove the "quiet" and "splash" from the end, and add "no_console_suspend".
5) Press Ctrl+x to boot.

This should get you back into Ubuntu, however it will not be resumed from the previous state.

Do not use hibernate with Linux unless you're either certain it will work, or you know how to fix it if it doesn't work. I personally have never seen it work on any distribution in the 5-6 years I've been using Linux. You'll end up with far more headaches than it's worth.

UPDATE (Sept. 5, 2009): I have used suspend, it works fine.