Two Guys Arguing

NaNoBloMo Recap Part Deux

Posted in challenge by benjaminplee on 11.30.10

Sum up the last month of blogging in 120 characters or less (gotta leave space for a URL on the end)?

15 posts in 30 days; one neglected prego wife.  Finishing what your start; priceless.

Best skill you developed by writing 15 blog posts in 30 day?

How to think of, research, and write a blog post without waking up the wife.

Really, I started thinking more about how I can contribute back to open source projects I use every day.  I have to credit the QUnit team for accepting my changes and providing great support and feedback.  Also, contributing to a project that has real users other than myself has made me consider how other developers look at my code and what they need help with.

** Adding funny pics to my posts **

Favorite post of the last 30 days?

Single post? Popping a Register from a Stack: QuickPiet Macros  Ever since the April Lambda Lounge meeting I keep coming back to Piet and love trying to hack higher level features into a stack based language.  It took a lot of failed attempts to figure out how to implement registers but it was a lot of fun.

Series?  QUnit w/ Rhino posts.  At every step of the way I thought “Oh this should be simple and clever” and winded up being “God that took longer than I thought, but at least it was clever”.  Given the traffic over the past couple weeks, it looks like there is a decent amount of interest in running QUnit w/ Rhino … hopefully these have been helpful.  I want to finish QUnit-CLI with some good examples and working on NodeJS.

Idea? Nate’s Clojure posts.  I have not had time to delve into Clojure like I would like but need to give credit to Nate.  I didn’t understand a lot of his posts but they were always interesting.  And beat me hands down in traffic.

Im in ur blogs writin ur posts

Would you do this again next year?

Not sure.  I want to keep posting and working on getting my ideas out and helping others but it is really hard to commit to an every-other day post regiment.  I think a much better frequency would be 1 every week or so.

Which posts did you half-ass?

Pro Tips: IE Caching Can ByteNotes to self: Keep SVN in sync w/ Git The Poor Man’s Way, and Programming Anti-Patterns: Releasing Cthulhu.  Honestly, these were cop-out posts.  I know I will go back at reference them at some point, but I am not sure anyone else will.  See the previous question.

** Oh, and this post; kinda **

Any last words before spending the coming months trying to forget that you ever did this?

Peace Out!

 

 

 

 

 

Tagged with:

NaNoBloMo Recap

Posted in Uncategorized by youngnh on 11.29.10

Sum up the last month of blogging in 120 characters or less (gotta leave space for a URL on the end):

Clojure solves programs naturally, elegantly and — holy crap! it’s 10 o’clock at night and none of my ideas panned out.

Best skill you developed by writing 15 blog posts in 30 days:

Formulating ideas and writing them down helps me break them down. I always felt like I had the quintessential software skill of “breaking the problem down”, but this excercise really taught me that is a separate skill from making my problems smaller. To belabor the point, one approach leaves you with a lot of problems that you have to solve, and the other is the excercise of ignoring a lot of solutions. I have written posts on ideas I thought were small and elegant, but they scrolled in at 5 or 6 screens of text. I tried not to do that this month, and in the process, felt like I got better at extracting smaller problems from large ones.

Favorite post of the last 30 days:

I really enjoyed writing the HP-15C program. The manual was something that took me some searching to find, so it was with great pleasure that I uploaded it to our site for others to find, and it was a fun challenge to write a program in 400-something bytes when every digit is an instruction.

Would you do this again next year?

I would commit to a less-demanding writing schedule. Considering that last year I stone-cold quit NaNoBloMo after maybe 2 posts, I’m really happy that I stuck it out until the end this year. I’m not terribly interested in being a pundit and even though I’ve got a lot of opinions, what I wanted to do with this month was write about writing software. There are new killer Clojure libraries emerging daily. It’s satisfying to use what someone else wrote to solve a problem. It’s personally instructive to try and fathom their design decisions. It’s enduringly useful to others to have concrete examples of good software available.

Having to post something every 2 days is too strict. The amount of time required to produce a post every 2 days was exactly right, the problem came when I failed to get anything working in the 3-4 hours a night I spent fiddling with stuff. Starting out I had a grand idea planned and outlined how every two days I’d move on to a new topic and use some new Clojure library to address that day’s topic, but I ran into issues after the first post and never got back on track. I think a post every 4 days would be a good amount. Enough time to queue up posts if things go swimmingly, and enough to recover and take a new tack if they don’t.

Which posts did you half-ass?

I started early, my fifth post, Start Tomcat from the REPL was an old text file I had lying around in case I ever had to actually do that again. I cleaned it up for an easy post. My Oracle post was the same way, the only difference is the old text file the Oracle post came from represented weeks of effort and failure and forum posts and hair pulling and lots of swearing. I’ve got a lot more old text files on installing Oracle for semantic technologies. I’m glad I didn’t use them.

Chasing the Sun, was one of my favorite posts since it led to a lot of interesting study for me and pointed towards a solution to a long-standing challenge of Ben’s. It was a half-assed post that I started writing maybe an hour before publishing. I had spent all day working on something else that didn’t turn out. That post contained no code, no real grasp of the astronomical concepts, just me plugging numbers into online calculators.

Copyright 1988 is as half-assed as it comes. I click-clacked that one out after getting home from work on a Friday, minutes before rushing out the door to catch the latest Harry Potter film with friends.

I didn’t half-ass the Konquest post, I just couldn’t get to an elegant, complete solution so I think it came off as half-finished.

Any last words before spending the coming months trying to forget that you ever did this?

I’ll have it known that my co-blogger Ben (if he doesn’t stumble at the finishing line) beat me handily at getting his posts in on time and deserves the TwoGuys NaNoBloMo trophy.

Submitting too close to midnight bit me twice and those posts appeared a day later than they were due. I submitted a post 7 minutes before midnight on November 9th, but unbeknownst to me our blog (at least my profile) was set to Eastern time, so it was published with a November 10th timestamp. I immediately rectified the timezone problem, but 3 minutes before midnight on the Saturday after Thanksgiving it totally slipped my mind that I was in Portland, Oregon. So that post showed up on an even numbered day as well.

Java 6 Scripting is no js.jar

Posted in java, javascript by benjaminplee on 11.28.10

 

Monkey Riding Dog

QUnit-CLI riding a Dog

Today I wanted to put together a QUnit-CLI example that leveraged Java 6’s included scripting features.  Seeing as the Java 6 JDK includes a recent version of Rhino as its primary JavaScript engine, I thought this would be a piece of cake.  Wrong.

 

To the javax.script package’s credit, Creating a new scripting engine and evaluating some script code is dead simple.  Example below from Oracle’s own pages

import javax.script.*;

public class EvalFile {
  public static void main(String[] args) throws Exception {
    // create a script engine manager
    ScriptEngineManager factory = new ScriptEngineManager();

    // create JavaScript engine
    ScriptEngine engine = factory.getEngineByName("JavaScript");

    // evaluate JavaScript code from given file - specified by first argument
    engine.eval(new java.io.FileReader(args[0]));
  }
}

The trouble came into play when I ran QUnit-CLI ‘s Rhino based suite.js file.   Ka-blew-ey!

Exception in thread "main" javax.script.ScriptException:
    sun.org.mozilla.javascript.internal.EcmaError: 
    ReferenceError: "load" is not defined. (#1) in  at line number 1
  at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
  at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
  at Java6RhinoRunner.load(Java6RhinoRunner.java:29)
  at Java6RhinoRunner.main(Java6RhinoRunner.java:14)

It turns out that scripts running from in a Rhino Shell environment have access to extra functions that are not directly provided when executing embedded from Java.  The top-level “load” function which is used to load additional JavaScript files from the Rhino Shell is not directly available when running from the scripting engine.

It turns out, I am not the only one with this problem.  My solution was to bind an additional object with a Java-based “load” function and add a new top-level “load” function to the JavaScript scope that invokes the Java code.

import javax.script.*;
import java.io.*;

public class Java6RhinoRunner {
  public static void main(String[] args) throws ScriptException {
    new Java6RhinoRunner().load(args[0]);
  }

  private final ScriptEngine engine;

  public Java6RhinoRunner() throws ScriptException {
    ScriptEngineManager factory = new ScriptEngineManager();
    this.engine = factory.getEngineByName("JavaScript");

    this.engine.put("Java6RhinoRunner", this);
    this.engine.eval("function load(filename) { Java6RhinoRunner.load(filename); }");
  }

  public void load(String filename) throws ScriptException {
    try {
      this.engine.eval(new FileReader(filename));
    }
    catch(FileNotFoundException e) {
      throw new RuntimeException("Error loading javascript file: " + filename, e);
    }
  }
}

To make matters worse, from the Rhino Shell, the “print” function will output the given text to standard output with a newline character.  As far as I can tell there isn’t a non-newline print command available on the Shell.  Annoyingly, when running from Java both print and println are available and tied to their common java behaviors.  This means that my suite.js code which uses “print” needs to use “println” when running from Java.  My first thought was to override print to execute println from my Java runner, but it looks like these basic top-level functions can’ be redefined from JavaScript.

this.engine.eval("function print(message) { println(message); }");

(no errors at runtime, but didn’t work)

The solution was to use add a level of indirection to my suite.js

var out = (typeof println !== "undefined") ? println : print;
out("FAIL - " + name); // used to be a call to print directly

Now we have suit.js running from Rhino embedded within the Java 6 JDK … but things are perfect.  The current code throws “Inappropriate array length” errors for a few QUnit tests.  I will be looking into these next.

Probability of Konquest

Posted in Uncategorized by youngnh on 11.28.10

I really like the KDE game, Konquest. It reminds me of another game I like, KDice. KDice has a handy table reference to tell what the odds of an attack succeeding are. Calculating the odds of winning a confrontation in Konquest is, undeniably, more complex.

First, a little about the calculation of a winner in Konquest. There are two sides to every confrontation, an attacker and a defender. Each has a number of ships, and each has a “Kill Percentage”, a measure of the probability that they will destroy an enemy ship. The game conducts the battle by having each side “roll” in turn to determine if an opposing ship is destroyed. First the defender rolls to see whether they destroyed a ship or not, then the attacker. When one side is destroyed, the remaining side is the victor.

It’s pretty easy to determine the probability of successfully attacking an undefended planet. Likewise, there’s no battle if no fleet attacks. So we’ll start with a 1 v 1 battle where both sides have a 50% kill percentage. The ways in which the battle could be resolved are:

d kill, a doesn’t roll = d wins 0.5000000
d miss, a kills = a wins 0.2500000
d miss, a miss, d kill = d wins 0.1250000
d miss, a miss, d miss, a kill = a wins 0.0625000
d miss x N, a miss x N, d kill = d wins (1-dkp)^N * (1-akp)^N * dkp
d miss x N+1, a miss x N, a kill = a wins (1-dkp)^N+1 * (1-akp)^N * akp

N doesn’t have to get very large before the probability of those outcomes quickly reaches zero.

Rounding to 4 digits, I found that the odds of a 1v1 matchup going 22 rolls or more is 0%.
Adding together the probabilities of all outcomes, I got 66.6667% chance that the defense would hold against the attacker.

So there’s only a 1 in 3 chance of an attacker winning an otherwise even matchup. First strike is a valuable advantage in Konquest.

If the attacker sends forces with a better kill percentage, say 60% against a defending force of 50%, I calculate the odds of attacker winning at 43.75%.

If the attacker doubles his forces, 2 attackers versus 1 defender, the situation changes a bit. The ways in which the battle could be resolved are:

d kill, a kill = a wins 0.250000
d kill, a miss, d wins the 1v1 = d wins 0.166667
d kill, a miss, a wins 1v1 = a wins 0.083334
d miss, a kill = a wins 0.250000

and for each of the above outcomes, there could be N misses before them:

d miss x N, a miss x N, d kill, a kill = a wins
d miss x N, a miss x N, d kill, a miss, d wins the 1v1 = d wins
d miss x N, a miss x N, d kill, a miss, a wins the 1v1 = a wins
d miss x N, a miss x N, d miss, a kill = a wins

This drops the percentage significantly, but it’s midnight and this post is due, so check back later to see how much.

QUnit-CLI: Running QUnit with Rhino

Posted in git, javascript, software testing by benjaminplee on 11.26.10

Rhino - wikimediaPreviously I talked about wanting to run QUnit outside the browser and about some issues I ran into.  Finally, I have QUnit running from the command line: QUnit-CLI

After a good deal of hacking and a push from jzaefferer, I gotten the example code in QUnit-CLI to run using Rhino and no browser in sight.  This isn’t a complete substitute for in-browser testing, but makes integration with build servers and faster feedback possible.

/projects/qunit-cli/js suite.js
PASS - Adding numbers
FAIL - Subtracting numbers
PASS|OK|subtraction function exists|
FAIL|EQ|Intended bug!!!|Expected: 0, Actual: -2
PASS - module without setup/teardown (default)
PASS - expect in testPASS - expect in test
PASS - module with setup
PASS - module with setup/teardown
PASS - module without setup/teardown
PASS - scope check
PASS - scope check
PASS - modify testEnvironment
PASS - testEnvironment reset for next test
PASS - scope check
PASS - modify testEnvironment
PASS - testEnvironment reset for next test
PASS - makeurl working
PASS - makeurl working with settings from testEnvironment
PASS - each test can extend the module testEnvironment
PASS - jsDump output
PASS - raises
PASS - mod2
PASS - reset runs assertions
PASS - reset runs assertions2
----------------------------------------
PASS: 22  FAIL: 1  TOTAL: 23
Finished in 0.161 seconds.
----------------------------------------

The first hurdle was adding guards around all QUnit.js’s references to setTimeout, setInterval, and other browser/document specific objects.  In addition I extended the test.js browser checks to include all of the asynchronous tests and fixture tests.  Finally I cleaned up a bit of the jsDump code to work better with varying object call chains.  My alterations can be found on my fork here.

The second hurdle was getting QUnit-CLI using my modified version of QUnit.js and adjusting how Rhino errors are handled.  Adding a QUnit submodule to the QUnit-CLI git repository easily fixed the first (I previously posted my notes on git submodules and fixed branches).  QUnit.js’s borrowed jsDump code is used to “pretty-print” objects in test messages.  jzaefferer ran into an issue when running QUnit’s own tests through QUnit-CLI resulting in the cryptic error:

js: "../qunit/qunit.js", line 1021: Java class "[B" has no public instance field or method named "setInterval".
at ../qunit/qunit.js:1021
at ../qunit/qunit.js:1002
at ../qunit/qunit.js:1085
at ../qunit/qunit.js:1085
at ../qunit/qunit.js:1085
at ../qunit/qunit.js:110
at ../qunit/qunit.js:712 (process)
at ../qunit/qunit.js:304
at suite.js:84

It turns out that errors objects (e.g. ReferenceError) throw in Rhino include an additional property of rhinoException which points to the underlying Java exception that was actually thrown.  The error we saw is generated when the jsDump code walks the error object tree down to a byte array off of the exception.  Property requests executed against this byte array throw the Java error above, even if they are done part of a typeof check, e.g.

var property_exists = (typeof obj.property !== 'undefined');

Once I figured this out, I wrapped the object parser inside QUnit.jsDump to properly pretty-print error objects and delegate to the original code for any other type of object.

...
var current_object_parser = QUnit.jsDump.parsers.object;
QUnit.jsDump.setParser('object', function(obj) {
  if(typeof obj.rhinoException !== 'undefined') {
    return obj.name + " { message: '" + obj.message + "', fileName: '" + obj.fileName + "', lineNumber: " + obj.lineNumber + " }";
  }
  else {
    return current_object_parser(obj);
  }
});
...

With these changes we have a decent command line executable test suite runner for QUnit.  With a bit more work QUnit-CLI will hopefully be able to print Ant/JUnit style XML output and/or include stack traces when errors bubble out of test code.

Tagged with: , , , ,

a small stack

Posted in Uncategorized by youngnh on 11.25.10

This Thanksgiving, I’m thankful for modern computer architectures.

Ben’s post on Piet and dealing with it’s stack prompted me to dig up my old HP-15C calculator, a relic my father handed down to me which also operates on a stack architecture. If you’re interested in all of its capablities, you can download the pdf manual, but in a nutshell, the calculator has a fixed size stack and 67 7-byte registers.

The stack’s size is fixed at 4, each position on the stack is a 7-byte register, called the x, y, z and t registers. Push more than 4 numbers onto it and the first items will fall off the end. t is the bottom of the stack, and x is the top. The number shown in the HP-15C’s display is always the x-register. Extra storage is provided by the 67 addressable registers. The registers are divided into data and program registers. The exact allocation of each is changeable, but by default 20 registers are allocated for data, 46 are registered for program instructions and 1 is special, called the index register. The data registers are accessed and written to using the RCL and STO buttons followed by a register address. Register addresses are the numbers 0-9 or .0-.9 (a decimal key followed by a digit key).

What to Write

As an excercise in programming the HP-15C, I decided to tackle Ben’s sunwalker challenge. I dug into the Explanatory Supplement to the Astronomical Almanac which had equations for roughly calculating the position of the sun relative to the earth for any day within two hundred years of the latest epoch, the year 2000.

The equations will give us the Right Ascension and Declination of the Sun which is the first step towards pinpointing where in the sky an observer from Earth would see it.

To do so, we start with the Julian Date of the day in question, JD.
From that, we’d like to calculate where in it’s ecliptic, the Sun is. This measurement is called λ.


λ = L + 1.915 sin G + 0.020 sin 2 G

Where L is the mean longitude corrected for aberration, and G is the mean anomaly.


L = 280.460 + 36000.770 T
G = 357.528 + 35999.050 T

T is the number of centuries since the epoch. Along with T, there’s only one other number we need to calculate, ε, the obliquity of the ecliptic.


T = JD - 2451545.0 / 36525
ε = 23.4393 - 0.01300 T

Having calculated the Sun’s ecliptic longitude, we can then convert it’s position into a position on the Celestial Sphere relative to the center of the Earth, given as right ascension, α, and declination, δ.


tan α = cos ε sin L / cos L
sin δ = sin ε sin L

Get to the Program Already

In pseudo-code, the program looks like this:

357.528
35999.050
RCL .1
* +
STO .3

This is the calculation of G, and it pushes two numbers onto the stack, retrieves T from the .1 register, multiplies and adds and then stores the result for further calculations to use in the .3 register.

Each digit of the numbers, however corresponds to a single-byte instruction. RCL and STO operations consume 2 bytes. Here’s the whole listing in three columns, the machine instruction, the corresponding keyboard input and the pseudo-code equivalent:


001 - 42,21,11     f LBL A     A:         -- start program labeled A
002 -    45  0     RCL 0       RCL 0      -- fetch JD from register 0
003 -        2     2                      -- calculate T
004 -        4     4
005 -        5     5
006 -        1     1
007 -        5     5
008 -        4     4
009 -        5     5           2451545.0
010 -       30     -           -
011 -        3     3
012 -        6     6
013 -        5     5
014 -        2     2
015 -        5     5           36525
016 -       10     /           /
017 -    44 .1     STO .1      STO .1     -- store T in register .1
018 -        2     2                      -- calculate L
019 -        8     8
020 -        0     0
021 -       48     .
022 -        6     6           280.460
023 -       36     ENTER
024 -        3     3
025 -        6     6
026 -        0     0
027 -        0     0
028 -        0     0
029 -       48     .
030 -        7     7
031 -        7     8           36000.770
032 -    45 .1     RCL .1      RCL .1
033 -       20     *           *
034 -       40     +           +
035 -    44 .2     STO .2      STO .2     -- store L in register .2
036 -        3     3                      -- calculate G
037 -        5     5
038 -        7     7
039 -       48     .
040 -        5     5
041 -        2     2
042 -        8     8           357.528
043 -       36     ENTER
044 -        3     3
045 -        5     5
046 -        9     9
047 -        9     9
048 -        9     9
049 -       48     .
050 -        0     0
051 -        5     5           35999.050
052 -    45 .1     RCL .1      RCL .1
053 -       20     *           *
054 -       40     +           +
055 -    44 .3     STO .3      STO .3     -- store G in register .3
056 -    45 .2     RCL .2      RCL .2     -- calculate λ
057 -        1     1
058 -       48     .
059 -        9     9
060 -        1     1
061 -        5     5           1.915
062 -    45 .3     RCL .3      RCL .3
063 -       23     sin         sin
064 -       20     *           *
065 -       40     +           +
066 -       48     .
067 -        0     0
068 -        2     2           0.020
069 -       36     ENTER
070 -        2     2           2
071 -    45 .3     RCL .3      RCL .3
072 -       20     *           *
073 -       23     sin         sin
074 -       20     *           *
075 -       40     +           +
076 -    44 .4     STO .4      STO .4     -- store λ in register .4
077 -        2     2                      -- calculate ε
078 -        3     3
079 -       48     .
080 -        4     4
081 -        3     3
082 -        9     9
083 -        3     3           23.4393
084 -       36     ENTER
085 -       48     .
086 -        0     0
087 -        1     1
088 -        3     3           0.01300
089 -    45 .1     RCL .1      RCL .1
090 -       20     *           *
091 -       40     +           +
092 -    44 .5     STO .5      STO .5     -- store ε in register .5
093 -       24     cos         cos        -- calcuate α
094 -    45 .4     RCL .4      RCL .4
095 -       23     sin         sin
096 -       20     *           *
097 -    45 .4     RCL .4      RCL .4
098 -       24     cos         cos
099 -       10     /           /
100 -   43  25     tan-1       tan-1
101 -    44  1     STO 1       STO 1      -- store α in register 1
102 -    45 .5     RCL .5      RCL .5     -- calculate δ
103 -       23     sin         sin
104 -    45 .4     RCL .4      RCL .4
105 -       23     sin         sin
106 -       20     *           *
107 -   43  23     sin-1       sin-1
108 -    44  2     STO 2       STO 2      -- store δ in register 2

Help! I rooted my evo, now what do I do?

Posted in Uncategorized by benjaminplee on 11.24.10

A few weeks ago I rooted my HTC Evo 4G using the unrevoked 3 tool to flash my phone’s firmware and allow full root access to the internals.  The unrevoked app was one of the best user experiences I have seen in a while (walks you through each step with a nice clean interface).  Everything went great until the end when I was left wondering…

Am I done?  What do I do now?

Mount the file system as root

From the great Droid Incredible rooting tutortial over jonamerica.com

  1. Turn the phone off
  2. Reboot the phone by holding down the power button and down-volume button
  3. From the menu, select recovery
  4. After the phone boots into recovery mode, select partitions
  5. Select mount /system
  6. Now, from your machine (with the android sdk tools installed) execute “adb shell”
  7. Now your are connected with root access

Wireless Tether

I had trouble with the Android Wifi-Tether app on the app store so I downloaded the latest code from the Google Code homepage.  Install, enable access control (so that you can approve/deny anyone connecting by MAC) and go!

Profit!

This post has nothing to do with Clojure or Lisp.  Nada.  Zip.  Zilch.
This post has no opinion of Richard Stallman.  Nada. Zip. Zilch.

Tagged with: , , ,

Installation of the Joseki SPARQL Endpoint in front of Oracle on Ubuntu

Posted in configuration by youngnh on 11.23.10

Here’s one from my archives. It was a bitch to come by, so hopefully this will help some other poor soul tasked with setting up a HTTP interface to Oracle’s semantic store.

Preparation

First, install Oracle on Ubuntu. That’s a pain, and only partially covered elsewhere. My notes on that subject, however, are enough for an entire months worth of blog posts, so I’m saving them for next November.

Once you’re done with the first step, you’ll want to enable semantic support, install a semantic network, maybe create a model or two, populate them and then make them queryable with a shiny SPARQL web service endpoint.

To enable Oracle’s semantic support (from here):

sqlplus / as SYSDBA
@$ORACLE_HOME/md/admin/catsem.sql

To create a semantic network (from here):

CREATE TABLESPACE rdf_tblspace
  DATAFILE '/u01/app/oracle/oradata/tseval/rdf_tblspace.dat' SIZE 1024M REUSE
  AUTOEXTEND ON NEXT 256M MAXSIZE UNLIMITED
  SEGMENT SPACE MANAGEMENT AUTO;

EXEC SEM_APIS.CREATE_SEM_NETWORK('rdf_tblspace');

Create a user in Oracle and grant them privileges:

create user rdfuser
identified by rdfuser
default tablespace users
temporary tablespace temp;

grant connect to rdfuser;

SPARQL Endpoint

Install Tomcat 6:

sudo apt-get install tomcat6

Download Joseki 3.4.0 and the latest Jena adaptor for Oracle (from here and here):

unzip joseki-3.4.0.zip
mv Joseki-3.4.0 joseki  

mkdir jena_adaptor
cd jena_adaptor
unzip ../jena_adaptor_for_release11.2.zip

The Joseki web app must be carefully put together using files from each. Copy a directory from joseki as a starting directory to construct the web apps:

cp -R joseki/webapps/joseki joseki.war.d
cp -R joseki/lib joseki.war.d/WEB-INF

And copy away:

cp jena_adaptor/joseki/* joseki.war.d
cp jena_adaptor/web/* joseki.war.d/WEB-INF
cp jena_adaptor/jar/* joseki.war.d/WEB-INF/lib

The sdordfclient.jar in the jena_adaptor has since been superceded and doesn’t work with Tomcat anyway (insert grumblings about WebLogic here). I got my hands on a copy of the latest version thanks to having sweet contacts inside the Oracle mothership, if you do too, you’ll want to put that in there. If you don’t, you should give up now and make something of your life.

The webapp needs Oracle’s sdordf.jar from your installation:

cp $ORACLE_HOME/md/jlib/sdordf.jar joseki.war.d/WEB-INF/lib
cp $ORACLE_HOME/jdbc/lib/ojdbc6.jar joseki.war.d/WEB-INF/lib

And Joseki comes with a replacement for Tomcat’s default servlet-api jar:

rm joseki.war.d/WEB-INF/lib/servlet-api-2.5.jar
mv joseki.war.d/WEB-INF/lib/servlet-api-2.5-6.1.10.jar /usr/share/java
cd /usr/share/tomcat6/lib
rm servlet-api-2.5.jar
ln -s ../../java/servlet-api-2.5-6.1.10.jar servlet-api-2.5.jar

Next, add this file to make an Oracle datasource that Oracle’s Joseki service will look for:
joseki.war.d/META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource name="OracleSemDS"
            auth="Container"
            type="oracle.jdbc.pool.OracleDataSource"
            factory="oracle.jdbc.pool.OracleDataSourceFactory"
            user="" 
            password=""
            driverClassName="oracle.jdbc.driver.OracleDriver"
            url="jdbc:oracle:thin:@localhost:1521/orcl"
            validationQuery="select 1 from dual"
            maxActive="30"
            maxIdle="10"
            maxWait="-1"/>
</Context>

Also, I’d bump up the number of connections in Joseki’s connection pool
joseki.war.d/joseki-config.ttl:

<#oracle> rdf:type oracle:Dataset;
    joseki:poolSize     30 ;         ## Number of concurrent connections allowed to this dataset.
    oracle:connection
    [ a oracle:OracleConnection ;
    ];
    oracle:defaultModel [ oracle:firstModel "M1" ] .

Verify that your directory structure looks like so:

./joseki.war.d:
application.xml
crossdomain.xml
index.html
joseki-config.ttl
META-INF
query.html
robots.txt
sparqler-details.html
sparql.html
StyleSheets
update.html
validator.html
WEB-INF
xml-to-html.xsl

./joseki.war.d/META-INF:
context.xml

./joseki.war.d/StyleSheets:
joseki.css

./joseki.war.d/WEB-INF:
lib
web.xml

./joseki.war.d/WEB-INF/lib:
arq-2.8.0.jar
arq-2.8.0-tests.jar
icu4j-3.4.4.jar
iri-0.7.jar
jena-2.6.0.jar
jenatest-2.6.0.jar
jetty-6.1.10.jar
jetty-util-6.1.10.jar
joseki-3.4.0.jar
junit-4.5.jar
log4j-1.2.12.jar
lucene-core-2.3.1.jar
ojdbc5.jar
sdordfclient.jar
sdordf.jar
slf4j-api-1.5.6.jar
slf4j-log4j12-1.5.6.jar
stax-api-1.0.1.jar
wstx-asl-3.2.9.jar
xercesImpl-2.7.1.jar

Create the joseki war file:

cd joseki.war.d
jar cvf ../joseki.war .

Deploy it:

mv joseki.war /var/lib/tomcat6/webapps

Goodluck and Godspeed.

Tagged with: , , ,

Pro Tips: IE Caching Can Byte

Posted in Uncategorized by benjaminplee on 11.22.10

Be careful when doing cross-browser AJAX because IE and Firefox handle client-side caching very differently.  Last week I ran into a situation where a new in our app was misbehaving in IE but working perfectly in Firefox.  The culprit ended up being IE’s caching of our AJAX calls to kick off jobs and poll to see if they were finished.  IE was caching all AJAX calls by default which reaked havok on our user’s workflow.  Firefox sends an “If-Modified-Since” header on subsequent GET requests giving the server the chance to determine if a resource cache can be used or not.  IE 8 doesn’t.

To make matters worse, I had actually added some anti-caching logic into our code … but had accidentally moved the code to a point where it wasn’t being invoked (oops!).

The solution ended up being adding an extra parameter to each AJAX request populated with the current timestamp value in miliseconds

var url = "http://our-app/action?no_cache=" + new Date().getTime();

JQuery’s AJAX utilities methods do this by default (so long as the cache option isn’t set to false) but it doesn’t look like Prototype.js does by default.   From Prototype’s AJAX documentation:

You can use parameters with both GET and POST requests. Keep in mind, however, that GET requests to your application should never cause data to be changed. Also, browsers are less likely to cache a response to a POST request, but more likely to do so with GET.

Together, IE stopped caching our requests and everything went back to normal.

Tagged with: , , ,

One fn to bind them

Posted in clojure, haskell by youngnh on 11.21.10

I had a chance to work on my parsec port a little this weekend. Say hello to one of the most important and ubiquitous parsers in the parsec arsenal, parser-bind.

The idea behind parser-bind is that it should squish two parsers together. It represents parsing one thing after another. The only other parser we’ve built that squishes two parsers together is parser-plus, which operates more like “or” in that if the first one fails, it tries the second. This parser will quit immediately whenever either fails. If this parser succeeds, it’s because each matched successive input.

(defn parser-bind [m n]
  (fn [state cok cerr eok eerr]
    (letfn [(mcok [item state]
              (n state cok cerr cok cerr)))
            (meok [item state]
              (n state cok cerr eok eerr)))]
      (m state mcok cerr meok eerr))))

If the first parser, m, consumes ok, but the second one, n, does not consume, our combined parser will still call the cok continuation. Conversely, if the first one is empty and ok, but the second one consumes, we will also escape via the cok continuation. parser-bind does not override any of the error handling continuations because if something goes wrong, we use them to exit immediately.

The useful part of parser-bind isn’t in the the above implementation. It isn’t how parsec implements the idea. Parsec’s implementation does take the first parser, m, but for it’s second argument, it takes a function that, when executed, returns the second parser.

This is a neat idea because the unlike a parser that has to be fully specified at write-time, a function can bind intermediate, runtime results. Those intermediate results, once bound and named can be used to create further parsers. It allows us to write let-like forms:

(p-let [c (one-of "abc")]
  (char c))

Where each binding form in the parser let has to be a destructuring form and parser pair. The above is a parser that parses a character, and then looks for a duplicate of what it just parsed, similar to capture groups in regular expressions. p-let uses parser-bind under the covers:

(defmacro p-let [[& bindings] & body]
  (let [[bind-form p] (take 2 bindings)]
    (if (= 2 (count bindings))
      `(parser-bind ~p (fn [~bind-form] ~@body))
      `(parser-bind ~p (fn [~bind-form] (p-let ~(drop 2 bindings) ~@body))))))

Given only a single binding pair, we make the parser in it the first argument to parser-bind, and wrap a function with it’s destructing form as args, returning the body. In longer binding forms, we produce a recursive structure that macroexpand will continue to expand one binding form at a time.

Tagged with:
Follow

Get every new post delivered to your Inbox.