Monday, January 21, 2008

HTML as a programming language

I read an article a couple days ago about robots that were programmed by genetic algorithms where one of the patterns that evolved was that of a liar. I've been looking for something to try out with genetic algorithms and thought it would be interesting to see if I could create a liar bot as well.
Since I've been reading Programming Collective Intellegence (along with everyone else!) and Dave always telling me this stuff needs to be done as a tree ("LISP, NICK! LISP!") I started thinking down that path.
Then I realized that HTML is a tree, and quickly coded up this useless calculating proof of concept: HTML programming!

<body>
<div class="add">
<div class="power">
<div>x</div>
<div>2</div>
</div>
<div class="multiply">
<div>5</div>
<<div>x</div>
</div>
<div>4</div>
</div>
</body>

This is the "Code". It's a polynomial, x^2+5x+4. And then the "interpreter":

function writeExpression(action, actors) {
//console.debug(action,actors);
if (action == 'add') action = '+';
if (action == 'subtract') action = '-';
if (action == 'multiply') action = '*';
if (action == 'power') action = 'Math.pow(';

var openParen = action.indexOf("(");
if (openParen > -1) {
action += actors.join(",")+")";
return action;
} else {
return actors.join(action);
}
}

function depthFirst(el) {
if (el.childElements().length == 0) return el.innerHTML;
var children = [];
for(var i = 0; i < el.childElements().length; i++) {
children.push(depthFirst(el.childElements()[i]));
}

var expression = writeExpression(el.className, children);
//console.log(expression);
return expression;
}

function parse() {
var code = depthFirst($(document.body));
$(document.body).insert("<br/>"+code);
x = prompt('X = ?');
eval("fun = function(x) {return "+code+";}; alert('f(x) = "+code+"\\nf("+x+") = '+fun(x));");
}

It's just a depth first search that then decodes the class name into an operator and returns the equation for that branch.

Sunday, January 6, 2008

Reflecting off a cd case 2


Reflecting off a cd case 2
Originally uploaded by joecagit

Progress has been made!
Using GPSD's python wrapper I'm connecting to GPSD running on my desktop machine. I also fixed the spacing of the lines on the speed chart and the number of numbered lines on the compass. I also dropped in the current speed value.

The first test revealed how tiny the font at 25 (pixels? points?) is. So I bumped it up to 50 and doubled the draw size of the two gauges. Either by luck or some math that I some how got right, the fonts fix perfectly.

When trying to take this picture I noticed that the display goes away completely when the any part of the light from the GPS is behind it. That means that the HUD will disappear in other car's lights or street lights, etc. I hope that won't be too much of an issue.

Friday, January 4, 2008

Running on N800!

IMG_2250.JPG
IMG_2250.JPG, originally uploaded by joecagit.

Thanks to pygame and anyone who worked on porting it and python to Maemo I have the first steps running on the N800!
Fullscreen - check
Flip Horizontal - check

I also found wiimote.py on the internet and will be trying to integrate a wiimote as an "angle of attack" and G-meter.

It's taking a little bit of time since I have to now learn python to get this all to work. But, hey, one more language to add to my collection.

Tuesday, January 1, 2008

First Prototype HUD GPS Hookup

IMG_2246.JPG
IMG_2246.JPG, originally uploaded by joecagit.

This is the first test of my prototype for a car HUD. What I'm going for is a fighter jet feel in my car. I'm prototyping using Shoes for now to just work out the details quickly. I'm using gpsd with Net::Telnet in Ruby to access the heading (across the top) and speed (down the side).
The image doesn't really make sense since the heading coming over the wire isn't what's showing on the compass. I think I took the picture as one of the bad sentences came across. (Every once in a while it would change to 358ish degrees when it was supposed to be about 2 degrees)
Also the speed coming across is either in knots or meters/second so that's what's showing on the HUD not the 25 mph on the GPS.
I have to still figure out how to get the waypoint information from the GPS to update the hud. That's what the little circle is. Hardcoded to 285 in the picture.

Thursday, November 29, 2007

Prototype/Javascript rules

I was doing some work with the new prototype this week to display some tables that represent a part of the file system. Since each one of them was a folder and could have numerous files I needed each one to collapse, but I wanted to keep all the info in the same table so divs where out.
This isn't ground breaking, but it shows the power of the prototype.js 1.6. This code closed each row after the header row and I didn't need to provide unique id's for each row on each table! All I did as you'll see below is pass the link that was toggling the collapse and expansion.

function _toggle(a) {
a = $(a);
var trs = a.up('tbody').childElements();
trs.without(trs.first()).invoke('toggle');
a.update(a.innerHTML == "-"?"+":"-");
return false;
}
Event.observe(window, 'load', function() {
$$('a[rel="toggle"]').each(function(a) { _toggle(a);});
});

The table would be rendered something like this. You can see the toggling link. I added a "rel" attribute to be able to identify and collapse all the tables when the page loaded.

<table>
<tbody>
<tr>
<td class="name" colspan="2">
<div><a href="#" onclick="_toggle(this); return false;" rel="toggle">-</a></div>Template Something
</td>
</tr>
</tbody>
</table>

So little code that does so much. But it really isn't that much. It's just that doing this the old way required you to put in so much code with id parsing to get it done. So really it's just the right about of code for the perceived work. I like it!

Tuesday, November 13, 2007

Spring Transactions

Spring offers the capability to manage DB transactions outside of the application container. We recently came across this need with Hibernate. Since we are using Java 5 we went with annotations. After some fumbling around trying to understand what was happening, we got something working. Then we tried to push it further.

We were trying to solve the situation where you have two transactions in a wrapper method. The two transactions should be independent. An exception in the second should not cause a roll back in the first. (This was a hypothetical situation I think. It was brought up by another programmer). So here's what we had:

class Service {
public wrapperMethod() {
txMethodA();
txMethodB();
}
@Transactional
public txMethodA(){ ... do some db inserts. commit. }
@Transactional
public txMethodB(){ ... do some db inserts. throw exception. commit. }
}
class Action {
public execute() {
service.wrapperMethod();
}
}

As a test we would sometimes throw an exception between the two methods inside the wrapper. We were also using Spring's OpenSessionInView filter.

What we immediately found it this situation was that because of the OSV filter setting readOnly to true that we couldn't use txMethodA to insert with out a transaction on the wrapperMethod. So we put a transaction on the wrapperMethod. This led to the exception rolling all inserts back. So it seemed that the transactional annotation was being ignored.
We then tried changing the propagation on txMethodA and txMethodB to REQUIRES_NEW to try to create a new transaction for each method. This too was ignored. Anything we tried would not result in the commit of one and and the rollback of the other.

Well, after some reading (duh) from the Spring docs, namely chapter 9 it dawned on me.

This maybe obvious to some people, but I run up against this once in a while and it doesn't seem intuitive to me so I forget how it works all the time. Take this example:

class A{
public doSomething() {
doSomethingElse();
}

public doSomethingElse() {
println "Class A";
}
}

class B extends A {
public doSomethingElse() {
println "Class B";
}
}

B b = new B();
b.doSomethingElse(); //-> Class B

Turns out it actually works like I expect, i.e. prints "Class B". I was thinking that this would print "Class A". This was disheartening because I thought I figured out what was happening.

Turns out I was close but not quite there. I read the section on proxies which is what Spring creates for each transactional class, even if the transaction is only on one method. It is the proxy class/object that gets wired in to the action, not the actual session. This is important be cause, as the page says, self references in the session object will not go through the proxy.

I guess the proxy can be though of as using the decorator pattern, but doesn't add anything to it. So what was happening?

Our original call to the wrapper was through the proxy. This would set up transactions if we had them on the wrapper, but we didn't. That is why we got the readOnly error. We were still using the OSIV transaction. Once we added a transaction to the wrapper class and called through the proxy the transaction was started for the wrapper but then the self referenced calls to txMethodA and txMethodB where only in the service object. Not throught the proxy. The pojo service object knows nothing about transactions so that's why nothing happened and it seemed like it was being ignored.

This is just something to keep in mind not only when using spring transactions but when using any Spring AOP advised code. It's also a sign I need to read more...

Sunday, November 4, 2007

Rails "What the...?'"s

I recently stumbled across 2 rails behaviors that have left me scratching my head. They both involve respond_to.
The first is how respond_to handles requests from Javascript changing the browsers location.
window.location='http://some/new/page';

Coming from Safari (and IE 6, I think) the request will be treated as a XHR. So this will cause your respond_to to return the .js action. I don't know if this is rails fault or the browser's but luckily you can fall back to request.xhr?
I had to replace my respond_to section with this:

render :template => 'items/new.rjs' if request.xhr?
render :template => 'items/new' unless request.xhr?

to get it to behave like I would expect across all browsers.

The other issue I had was during testing. I had a simple edit action:

def edit
@item = Item.find(params[:id])
respond_to do |wants|
wants.js
wants.html
end
end

Notice the order of the wants. The XHR is before the regular request. From my test I was calling
get :edit, :id => 1

and asserting
 assert_template 'edit'


This was failing because it was "expecting <"edit"> but rendering <"edit.rjs">". Hitting the action in a browser rendered the right result but the test failed. I had to reverse those two, so that the regular request was before the XHR request and then it worked. There seems to be first-come-first-serve functionality if the mime-type isn't specified.
I'll have to look into why the functional test "get" method doesn't set a mime-type.