All posts by ajohnson

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

java properties files

I mentioned last night that I was attempting to use property files. So let’s say you have a class in a jar file that reads a property file and the property file is in the jar. Is there any way to load the property file without knowing exactly on the file system where the property file lives? Should I be using a ResourceBundle instead?

HTTP Spider & Lucene

Spent the majority of my day today refactoring the HTTP spider & Lucene indexing application I’ve been writing on and off for the last couple months as a learning exercise. One of the first things I did was modify the 3 modules to implement the Runnable interface rather than extending the Thread object. Big thanks for Joe for his detailed thoughts on the subject. Probably the biggest reason for doing so is that implementing the Runnable interface means that the classes (a class that handles retrieving web pages, a class that indexes the web pages using Lucene and a class that saves the resulting web pages to a database) could possibly extend some type of task/thread class that I’d want to implement in the future (again, a Joe suggestion).

After completing that, I explored the various ways in which one might interface with the software… the only way (right now) being via the command line with multiple arguments. Since remembering command line arguments can be tedious, I looked at the Properties class, whose methods give you the ability to load a text file with key/element pairs and then get() and set() properties within the file. Java.sun.com has an introduction to the System and Properties class.

Finally, I rewrote each module (mentioned above) so that while still running inside of a while(boolean) loop, they sleep for .5 seconds before iterating through the loop. Hopefully (and it appears this is true) this means that the CPU isn’t stressed out too much.

I uploaded the source here (it also requires the commons http client jar, the commons logging jar, and the lucene jar. If you’re a Java programmer, I’d love your feedback on the code, not from a feature standpoint but from a syntax and architectural standpoint (ie: I care less about whether or not you think you’d actually use this and more about what you think of the code.) How would you change it? What did I do wrong? What did I do right?