Java 5 ; object persistence
May. 7th, 2007 05:03 pmI'm pleased about the Tetrad code being in Java 5, which really is a slightly different language. Java 5 has two nice features:
* nicer 'for' loops (++), such as:
* Parametric types, which avoids all that unnecessary casting that Java is notorious for (+):
Combined, we can now do:
instead of
(although this translation would probably break if you remove an element from the list inside this loop without decrementing i)
-----------------
Sometimes, after your program has finished execution, you notice that some object (e.g. a graph) had a really interesting property... or, if the program is non-deterministic, and you notice a bug, you may want to run the same example again for debugging purposes (or the entire state of the program, if that were possible).
Unfortunately, the object is now gone, and all you have is its footprints. By the time you think of putting Object.serialize() in the code, it's too late: you have to pray that you'll run into that situation again.
What do you do?
* nicer 'for' loops (++), such as:
for (int sampleSize : new int[]{1000, 10000}){ ... }* Parametric types, which avoids all that unnecessary casting that Java is notorious for (+):
Listnodes = dag.getNodes(); Node node0 = nodes.get(0);
Combined, we can now do:
for (Node node : dag.getNodes()) { ... }instead of
for (int i=0; i < dag.getnodes().size(); i++) {
Node node = dag.getnodes().get(i);
...
}
(although this translation would probably break if you remove an element from the list inside this loop without decrementing i)
-----------------
Sometimes, after your program has finished execution, you notice that some object (e.g. a graph) had a really interesting property... or, if the program is non-deterministic, and you notice a bug, you may want to run the same example again for debugging purposes (or the entire state of the program, if that were possible).
Unfortunately, the object is now gone, and all you have is its footprints. By the time you think of putting Object.serialize() in the code, it's too late: you have to pray that you'll run into that situation again.
What do you do?
(no subject)
Date: 2007-05-07 11:39 pm (UTC)(no subject)
Date: 2007-05-08 05:04 am (UTC)I guess this is expensive memory-wise, but maybe not if we only save the places where non-deterministic branching occurred (i.e. all places where we have "degrees of freedom"), and querying the run involves running the program again with using these fake-random values.
(no subject)
Date: 2007-05-08 05:42 am (UTC)You mean use a profiler? There are a few good free full-heap-coverage profilers out there, and quite a few more expensive ones, which let you keep a running heap trace you can run statistics on, toss around into debuggers, etc... they're kindof a pain to use sometimes (and yes, very memory hoggy by nature), but really rather necessary when dealing with multithreaded server code with nondeterministic bugs.
(no subject)
Date: 2007-05-08 06:20 pm (UTC)(no subject)
Date: 2007-05-08 07:47 pm (UTC)http://code.google.com/p/chronicle-recorder/
(no subject)
Date: 2007-05-08 01:07 am (UTC)(no subject)
Date: 2007-05-08 02:00 am (UTC)(no subject)
Date: 2007-05-08 06:09 am (UTC)public abstract class Filter<S,T> { public abstract T apply(S input); public <U> Filter<S,U> postCompose(Filter<T,U> next) { final Filter<S,T> first = this; return new Filter<S,U>() { public U apply(S input) { return next.apply(first.apply(input)); }; }; } } public class SpeechToTextFilter extends Filter<AudioFile, TextFile> { public TextFile apply(AudioFile input) { /* do your transcription */ } } public class TranscriptPersistenceFilter extends Filter<TextFile, Integer> { public Integer apply(TextFile input) { /* persist, return DB id */ } } public class UnmarshallingFilter extends Filter<Integer, TranscriptObject> { public TranscriptObject apply(Integer dbId) { /* ORM translation to custom object */ } } ... Filter<AudioFile, TranscriptObject> fancyFilter = new SpeechToTextFilter().postCompose(new TranscriptPersistenceFilter()).postCompose(new UnMarshallingFilter()); for(AudioFile audioFile : audioFiles) { TranscriptObject alreadyPersistedTranscript = fancyFilter.apply(audioFile); // do stuff with it if you need to }(no subject)
Date: 2007-05-08 06:22 pm (UTC)(no subject)
Date: 2007-05-08 07:03 pm (UTC)(no subject)
Date: 2007-05-08 07:15 pm (UTC)IIRC, Java at least does have closures. C++ does not (gcc will at least tell you as much), so you have to implement them by hand.
(no subject)
Date: 2007-05-08 07:23 pm (UTC)I catch myself doing Wrong Things when I make the mistake of pretending the closures in Java are as "real" as they are in, say, Javascript.
(no subject)
Date: 2007-05-08 07:33 pm (UTC)Eh, we should all just go home and write FORTRAN.