MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Dynamic and J/Link

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87091] Re: Dynamic and J/Link
  • From: Albert Retey <awnl at arcor.net>
  • Date: Mon, 31 Mar 2008 02:06:41 -0500 (EST)
  • References: <fsl23s$g8o$1@smc.vnet.net>

Hi,

> I'm using Java to do some agent-based modeling and am wondering what  
> the best way is to create a dynamic graphic that will update  
> automatically whenever the state of the simulation changes.
> 
> Suppose - for sake of argument -  that the simulation is a single Java  
> object saved in a Mathematica variable called "model".  Suppose that  
> calling the Java method step() advances the simulation one iteration  
> and that calling the Java method getState() returns a rectangular 2D  
> array of 0s and 1s.  Now, one easy way of representing the state of  
> the model is by using Raster, so the following gives a basic graphic  
> representation:
> 
> 	Graphics[ Raster[model@getState[]] ]
> 
> Suppose, now, that we wrap that statement with Dynamic as follows:
> 
> 	Dynamic[
> 		Graphics[ Raster[ model@getState[] ] ]
> 	]
> 
> If I then evaluate the following, the displayed graphic doesn't change  
> at all, even though (intuitively, at least) it should:
> 
> 	model@step[];
> 	Update[ model ]
> 
> For some reason, calling Update on the variable model doesn't cause  
> the Dynamic object in the notebook to recognize that it needs to  
> refresh the displayed graphic.  Why is that?
> 
> Now, the only way I've found to solve this problem is the draw the  
> display using a second variable which contains a local copy of the  
> state of the Java object. First, set up the initial display in the  
> notebook as follows:
> 
> 	array = model@getState[];
> 	Dynamic[
> 		Graphics[ Raster[ array ] ]
> 	]
> 
> then manually update the variable array each time the simulation  
> changes:
> 
> 	model@step[];
> 	array = model@getState[]; (* This triggers a redisplay *)
> 
> The two irritations about this are that it (a) it requires polluting  
> the current Mathematica's session namespace with another variable that  
> doesn't do anything except hold a value which could be easily obtained  
> by calling model@getState[], and (b) it requires some (minor)  
> additional code to configure each display.  Those aren't real worries,  
> but it does suggest that I'm not doing this the most efficient way.
> 
> Is the above solution the best (or, indeed, only) way to get Dynamic  
> to recognize state changes of J/Link objects?  Does anyone else have  

My experience is also that Update is, unlike stated in the 
documentation, not really causing Dynamic to realize changes, even in 
other circumstances. Others have given hints about workarounds, another 
thing that probably helps is the UpdateInterval option of Dynamic. Of 
course it doesn't do the same thing, but in many cases where external 
processes are involved in my opinion it is an even better approach. 
Other than that I have also used global variables that just indicate the 
changing of something that otherwise wouldn't be recognized by Dynamic. 
To make your code more clean and robust, it is probably a good idea to 
encapsulate this in an extra function as David Bailey has suggested.

hth,

albert


  • Prev by Date: Re: Re: Re: Mathematica notebooks
  • Next by Date: RE: Re: Operate on Heads
  • Previous by thread: Re: Dynamic and J/Link
  • Next by thread: Re: Find Upper Neighbor in a list