Print

Print


On Nov 6, 2013, at 11:56 AM, Sadat Chowdhury wrote:

> I have a way of detecting when I hit certain milestones. What I would like to do is, "freeze" the GUI part (viewer part in the MVC framework) until I hit those milestones, let the viewer act on the model at that point, for certain number of steps, and then disconnect the viewer until I hit the next milestone. The purpose is to only wake up the viewer when it is signaled -- otherwise run the main simulation in a detached mode.
> 
> Are there any particular API calls, examples, patterns I should follow?

So this got me thinking how this should be done and I think this is how you should do it.

1. Download the SVN repository version of MASON, where I have added a new method called Display2D.requestUpdate().  This method can be called from your model thread or from the Swing Event Thread to request that MASON redraw the Display2D *once* even if it's been told to never update itself.

2. In your setupPortrayals() method in your GUIState, add code along these lines:

---
Steppable updater = new Steppable()
	{
	public step(SimState state)
		{
		// Though we're in the model thread and repainting is done in
		// the GUI thread, we're fine doing the next thing because requestUpdate()
		// and repaint() are threadsafe.
		
		MyModel myModel = (MyModel) state;
		if (myModel.wantsToGetRedrawn())  // you need to define this in your model
			display.requestUpdate();	
		}
	};
	
// Now we need to schedule the updater before the window is scheduled
scheduleImmediatelyAfter(updater);

// Now we continue resetting and repainting the display as usual.
// Notice that this is AFTER we have scheduled the updater
display.reset();
display.repaint();
---

We are adding to the GUIState MiniSchedule a Steppable which is called AFTER the model is stepped but BEFORE the display is stepped (the display is scheduled in its reset() method).  This method calls a method I just made up called wantsToGetRedrawn(), and if it's true, then the display is asked to update itself.  You need to implement wantsToGetRedrawn(), or whatever, in your model to return true for each and every iteration that your model would like be displayed.

3. Fire up the GUI version of your model.  

4. Click on the Refresh icon (it's the circular arrow, sort of like a recycling arrow second icon from the far left on your Display2D window).  Choose "Never Redraw".  Unfortunately for the moment you'll have to always choose this manually -- I gotta get around to setting that as a default.

Now when you run your model it should never redraw the screen until the model returns true in its wantsToGetRedrawn() method.  Note that I've not tested this, but I suspect it'll work fine.

Sean