2009-03-29

Unit testing with Eclipse and converting a Map to String

When testing with JUnit, one often has to compare the expected result of a computation with the actual result. A convenient way of doing this is to turn the actual result into a string and then compare two strings. On one hand, the convenience is because converting the actual result to string is often simpler than constructing an object for the expected result (the final decision between these two options depends on which one of the two has applications beyond unit testing). On the other hand, Eclipse helps with string comparisons in two ways:
  • Preference “Java → Editor → Typing → Escape text when pasting into a string literal”: Allows one to quickly turn blocks of text into Java string literals for the expected result.
  • Built-in diff for assertEquals(): If assertEquals() fails for two strings, Eclipse shows a nice diff of the results. This makes it easy to figure out what went wrong.
Obviously, the output has to be deterministic which is a problem for, say, HashMaps. With this motivation in mind, now a question: What is the quickest way of turning a Map into a string where the keys are sorted?
    public static <K extends Comparable<? super K>, V> String toStringSorted(
            Map<K, V> map) {
        return new TreeMap<K, V>(map).toString();
    }
Select the text between the braces to make it visible.

2009-03-21

Proposed features for SPARQL

The W3C has published a wiki page with proposed features for the RDF query language SPARQL. My favorite is “Parameterized Inference”: I've always found how inference is done in RDFS and OWL very cumbersome (i.e. by creating statements). My needs for inference have always been very basic, but just having transitivity available directly in SPARQL would be great. Other interesting proposals: I am missing two things:
  • A way to get matches that are only in the default graph. One can do graph-based restriction, but only for named graphs. That is, the variable involved in the GRAPH construct always has to be bound. A work-around that I use in Hyena is to look for matches in all graphs and then exclude a match if it also appears in a named graph.
  • A more object-oriented way of returning query results. That is, one should be able to return one resource per row, where some columns (“all values of rdf:type”) contain multiple values (are in non-first normal form). LIMIT per resource mentions this feature, but does not go into detail.
Update: To clarify the work-around. Query: all subjects that *only* exist in the default graph.
SELECT DISTINCT ?subj
WHERE {
    ?subj ?pred ?obj .
    OPTIONAL {
        GRAPH ?__graph__ {
            ?subj ?pred ?obj .
        }
    }
    FILTER( !bound(?__graph__) )
}
A simpler version would be (where one does not have to write the same pattern twice which quickly becomes cumbersome with larger patterns):
SELECT DISTINCT ?subj
WHERE {
    GRAPH ?__graph__ { ?subj ?pred ?obj . }
    FILTER( !bound(?__graph__) )
}

2009-03-17

Thunderbird, Mac OS X, and vCards

IMAP problems forced me to temporarily switch from Apple's Mail.app to Mozilla Thunderbird (at work, I use it all the time under Linux). Trying to get my Mac OS X address book contacts into it, I did some research:
  • Thunderbird 3.1 Beta supports the Mac OS X address book.
  • The extension MoreFunctionsForAddressBook can import vCards into the Thunderbird address book (and you can select multiple Address Book entries and export them as a .vcard file).
Alas, Mail.app still does not support tagging. It would be nice to have an interoperable standard here (GMail's labels are implemented differently from Thunderbird's tags).

2009-03-15

Python for shell scripting

Shell scripting is the ultimate experience of unorthodoxy: Everything that one has learned with “proper” programming languages is done differently (well, unless you are into Perl, you naughty little hacker). If you are looking for pain relief, the following article can help:
Python for Bash scripters: A well-kept secret

2009-03-11

A projected user interface for portable devices

Pattie Maes & Pranav Mistry: Unveiling the “Sixth Sense,” game-changing wearable tech
A nice demo of a device that uses a projector and image recognition to project its user interface onto external objects. This is an interesting contribution to exploring the extremes of immersive user interface on one hand and external user interfaces on the other hand.
Update: More user interface ideas:
David Merrill: Siftables, the toy blocks that think

2009-03-08

Remove white space around PDF graphics with pdfcrop

Matthias Hölzl pointed out pdfcrop to me. It is very useful in the following scenario: You want to include a PDF graphic in your LaTeX document. But the application at hand only exports complete pages as PDF (or you print to PostScript and convert the PostScript to PDF). What pdfcrop does is that it removes any whitespace around a graphic, reducing the bounding box to the actual content. If you are using TeX Live then pdfcrop is already installed on your computer, because it comes standard with that LaTeX distribution.

Flattr