Prisoner’s Dilemma

Finished Prisoner’s Dilemma: John Von Neumann, Game Theory and the Puzzle of the Bomb today, a rainy miserable Saturday just like every other Saturday the last couple weekends here in Massachusetts. When will the weather get better? As before, I like to post interesting quotes:

“… Jacob Bronowski wrote in 1973, ‘You must see that in a sense all science, all human thought, is a form of play. Abstract thought is the neotony of the intellect, by which man is able to carry out activities which have no immediate goal in order to prepare himself for long-term strategies and plans.'” [pg 39]

“It’s no exaggeration to say that society is founded on cooperation. Whether to litter — leave a tip — shoplift — stop and help someone — lie — conserve electricity — etc., etc. — all are dilemmas of individual gain and the common good. Some commentators have speculated that irrational cooperation is the cornerstone of of society, and without it life would be, as Hobbes put it, ‘solitary, poor, nasty, brutish, and short.'” [pg 227]

On the TIT FOR TAT strategy that Robert Axelrod used in the iterative prisoners dilemma game: “… one of the more surprising findings was that TIT FOR TAT won without ever exploiting another strategy. ‘We tend to compare our scores to other people’s scores,’ he explained. ‘But that’s not the way to get a good score. TIT FOR TAT can’t beat anybody, but it still wins the tournament. That’s a very bizarre idea. You can’t win a chess tournament by never beating anybody.” [pg 241]

Pages 249 & 250 describe real-life examples of iterated prisoners dilemmas in the lives of stickleback fish (read more about that here) and the vampire bat of South America.

Using Sockets in CFMX

I’ve read emails [1] [2] from a couple different people on the various email lists that CFMX can’t use sockets…. which is true in the sense that CFMX doesn’t have a <cfsocket> tag, but not true in the sense that you can’t easily use sockets in ColdFusion. I banged around this morning for a bit and came up with this example, which does an HTTP request of www.mindseye.com using a java.net.Socket object and then loops over the returned headers and prints them out to the screen:

<cfscript>
// get a socket object
socket = CreateObject(“java”, “java.net.Socket”);

// call the Socket constructor
socket.init(“www.mindseye.com”, 80);

// get a PrintWriter object
printWriter = CreateObject(“java”, “java.io.PrintWriter”);

// call the PrintWriter constructor
printWriter.init(socket.getOutputStream(), true);

// get an InputStreamReader object
inputsr = CreateObject(“java”, “java.io.InputStreamReader”);

// call the InputStreamReader constructor
inputsr.init(socket.getInputStream());

// get a BufferedReader object
input = CreateObject(“java”, “java.io.BufferedReader”);

// call the BufferedReader constructor
input.init(inputsr);

// request the homepage
printWriter.println(‘GET /index.cfm’);
      
// loop over result by reading line by line
run = true;
while (run) {
   // read the result line by line
   resultLine = input.readLine();
   if (len(resultLine) GT 0) {
      WriteOutput(“Header: ” & resultLine & “<br />”);
   } else {
      run = false;
   }
}   
</cfscript>

You could easily modify this to write xml to a server socket:

printWriter.println(‘<order orderType=”quote” customerType=”RETAIL” orderNumber=”1234″ orderDate=”2003-03-21″>’);
printWriter.println(‘<shipment ID=”1″ city=”Boston” state=”MA” zip=”02210″>’);
printWriter.println(‘<line_item ID=”1″ SKU=”18JK23″ category=”GSHOE” quantity=”2″ total=”39.90″ />’);
printWriter.println(‘</shipment>’);
printWriter.println(‘</order>’);

or do a variety of other useful things like send SMTP email, do DNS lookups, or send SOAP packets.

Sharing session data between ColdFusion and J2EE components

Informative article on developerWorks titled Together at last: Sharing session data between ColdFusion and J2EE components. After you read that article, read my notes on creating session listeners for CFMX. I show you how to capture CFMX sessionOnStart and sessionOnEnd events using servlet listeners.

I little birdie told me that sessionOnEnd and sessionOnStart events were coming in Red Sky, but I’m not in the beta program so I wouldn’t know…

yosemite pics

You know how some places don’t look as beautiful once you get there? These pictures of Yosemite are amazing, but if you go in person it’s a religious experience, even if you’re not into religion. I grew up camping in Yosemite during the summers and Karen and I camped in the valley and climbed Half Dome on our first anniversary. Bring your camera and watch out for the bears! [via kottke.org]

ResourceBundles

Update: A ResourceBundle object is the way to go. In comparison to the Properties class, the ResourceBundle class is a breeze. To load, read and retrieve the key ‘searchlib.dblibrary’ from a text file called ‘database.properties’ using the Properties class, you’d end up with something like this:

String databaseDriver;
try {
  FileInputStream propFile = new FileInputStream(“c:\\myapp\\sandbox\\database.properties”);
  Properties p = new Properties();
  p.load(propFile);
  databaseDriver = p.getProperty(“searchlib.dblibrary”);
} catch (java.io.FileNotFoundException fe) {}
} catch (java.io.IOException ie) {}

where you have to hardcode the path to the properties file and if the path changes, you have to recompile.

To do the same thing with the ResourceBundle class, you’d end up with something like this:

String databaseDriver;
ResourceBundle res = ResourceBundle.getBundle(“database”);
databaseDriver = res.getString(“searchlib.dblibrary”);

The file this time is located by JVM, located somewhere in the classpath. No try/catch is necessary. Alot easier isn’t it?

If you’re interested in reading more about the ResourceBundle class, check out this article on developer.java.sun.com:

Java Internationalization: Localization with ResourceBundles