Posted by Uncle Bob on 08/19/2008
I have put together a nice little demonstration of the Bauble concept. You may recall that I first wrote about it here. Baubles are a simple component scheme for Ruby, good for when you want a component, but don’t need something as heavy as a gem.
orbit.zip contains all the files for this demonstration. I suggest you download and unpack it.
First you need to install the Bauble gem. Don’t worry, it won’t hurt anything. Just say gem install orbit/bauble/Bauble-0.1.gem (You’ll probably have to do it with sudo.) That should install Bauble. From now on you only need to say require 'bauble' in your ruby scripts that make use of it.
Now you should be able to run the orbital simulator. Just type:
cd orbit/MultipleBodyOrbit/lib
jruby multiple_body_orbit.rb
A swing window should pop up and you should be able to watch an orbital simulation. Every run shows a different random scenario, so you can kill a lot of time by watching worlds in collision.
The thing to note, if you are a ruby programmer, is the use of the term Bauble::use(-some_directory-). If you look in the multiple_body_orbit.rb file you’ll see I use two Baubles, the Physics bauble does the raw calculation for all the gravity, forces, collisions, etc. The cellular_automaton bauble provides a very simple Swing framework for drawing dots on a screen. (Yes, this is jruby).
If you look in either of the two Baubles, you’ll see that the require statements within them do not know (or care) about the directory they live in. There is none of that horrible __FILE__ nonsense that pollutes so many ruby scripts. This is because the Bauble::use function puts the directory path in the LOAD_PATH so that subsquent require statements can simply eliminate the directory spec.
Take a look at the Bauble source code. It’s no great shakes.
Also take a look at the two baubles. They show a pretty nice way to decouple business rules from gui. You might recognize the MVP pattern. The multiple_body_orbit.rb file contains the presenter. Clearly the Physics module is the model. And the cellular_automaton module is the view. (There is no controller, because there is no input.)
Posted in Uncle Bob's Blatherings, Dynamic Languages, Design Principles, Clean Code
Meta 29 comments, permalink, rss, atom
Posted by Uncle Bob on 08/14/2008
“Quintessence” was the name of the keynote I gave at the banquet of Agile 2008. Though the talk covered a lot of ground, the upshot was that we need an addition to the agile manifesto…
Comments
Micah Martin about 4 hours later:
note: The gem has to be installed in your instance of jruby.
& jruby -S gem install orbit/bauble/Bauble-0.1.gem
Very cool simulator! I find myself watching it over and over. However, why are the trajectories wobbly? I don’t recall them being so in the previous version you showed me.
unclebob about 12 hours later:
jruby -S gem install orbit/bauble/Bauble-0.1.gem
Ah yes! Thanks for the correction.
why are the trajectories wobbly?
The view pins the sun at the origin. Massive objects that orbit the sun make it wobble in normal space. That translates to a wobble of the entire background. In essence you are seeing all the orbits from the point of view of the sun.
In our own skies, the trajectories of all the planets wobble because we ride on the Earth. So the motion of the Earth is superimposed onto the motion of the rest of the planets. In this simulation you are riding on the Sun, and large planets make the Sun wobble.
EGHM 2 days later:
Very cool! Took me a while to realize it was working, so I added @frame.setBackground(Color.black) to cellular_automaton.makeFrame and change the point color to white in multiple_body_orbit.draw if I knew a bit more about how configuration was done I would have taken a stab at putting these two things that changed together, together.