Print

Print


[I'm cc:ing this to the ECJ mailing list as well, though it's only
tangentally of interest to 'em]

On Nov 24, 2004, at 11:30 AM, Steve Butcher wrote:

> Although I have been able to get MASON and ECJ to integrate well in
> "headless" mode, I'm having a great deal of difficulty getting them to
> work in "head" mode. I'm not certain if there are Swing threading
> issues involved or if I simply don't understand the lifecycle
> sufficiently. Basically, I'd like the option of the GP running with or
> without graphics.

This is nontrivial Steve, and I have not attempted something along
these lines yet so I cannot do more than make broad guesses as to
what's in store for you (and dire warnings :-).  The nontriviality
doesn't stem from MASON so much as from Java's dual-threaded GUI event
management nature.  A lot of the code you provided tries to call MASON
mechanics from the inside, and that's probably dangerous.  Instead it's
wiser to fire up MASON as if a user were pressing the play button etc.

Here's the situation.  MASON's core is very simple.  It's usually
single-threaded, and all you do is create a schedule, load it with
stuff, pump it a few times, and stop.  MASON's designed so that this
can be done just by you on the command line, or to run it as a
subsidiary thread underneath [hmmm, I wonder what would happen if we
rejigger MASON not to run in a separate thread but just to constantly
issue invokeLaters in the event queue.  Probably would create too long
an event wait...I gotta think about that].  Trouble is, drawing and
event management (button-pushing etc.) has to be done not from this
separate thread but from the Swing Event thread.  That's largely what
the function of the Console is.

So how does this work?  When you press 'play', we first call start() on
the MASON model.  Then we create a thread which has the following loop
more or less:

        0. Acquire a lock on the schedule
        1. Quit if I've been asked to.
        2. Else pulse the GUIState's scheduled stuff, which:
                - pulses the pre-schedule items in the GUIState
                - pulses the schedule once
                - pulses the post-schedule items in the GUIState
                        (this last one is where displays are usually redrawn)
        3. Release the lock on the schedule
        4. Do an invokeAndWait to allow the Swing Event thread to have some
free time
        5. Go to 0

Meanwhile, all those requested redraws from the post-schedule items
have piled up in the Swing Event thread, blocking on the schedule.
When the schedule is released they then all draw themselves.  The
invokeAndWait makes the underlying MASON thread wait until they've
finished drawing.  That way the two threads (MASON thread and Swing
thread) don't both try to read/write to the model at the same time.

So the problem here is that ECJ doesn't run in the Swing event thread.
It runs in its own thread (the 'main' thread) and typically the 'main'
thread's sole purpose in a Swing GUI model is to set up the Swing
thread and then die.  That won't be happening.  What you'll have to do
is first disable the stop, play, and pause buttons (or just resist the
urge to press 'em).  Then from ECJ's main thread you have to create the
MASON console and set it up, like this:

         HeatBugsWithUI heatbugs = new HeatBugsWithUI(); // or your
app...
         Console c = new Console(heatbugs);
         c.setVisible(true);

You do this once only -- I'd do it at setup() time in ECJ.  To
evaluate, all you have to do is set up your code and pressPlay().  But
then you have to figure out how to block until the simulation has
stopped.  I think it's probably easiest to poll.  Inside your
evaluate() method, you set up the model so that start() gets it going,
and then you do:

     Runnable blocker = new Runnable()
         {
         public void run()
             {
             // intentionally do nothing
             }
         };

        console.pressPlay();
        while(console.getPlayState()!=PS_STOPPED)
                {
                Thread.currentThread().sleep(100);  // needful?  I dunno.
                SwingUtilities.invokeAndWait(blocker);
                }

At this point the model has stopped and you can examine it to see what
the fitness was.  In your Finisher you could do something like this:

                SimApplet.isApplet = true;
                console.doQuit();

doQuit queries isApplet, and if it's true, it doesn't call
System.exit(0).  That way ECJ can call System.exit(0) in its own due
time.

Sean