Re: Dynamic and J/Link

*To*: mathgroup at smc.vnet.net*Subject*: [mg87040] Re: Dynamic and J/Link*From*: Szabolcs Horvát <szhorvat at gmail.com>*Date*: Sun, 30 Mar 2008 01:14:44 -0500 (EST)*Organization*: University of Bergen*References*: <fsl23s$g8o$1@smc.vnet.net>

J. McKenzie Alexander wrote: > 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[]; Note that I do not know Java, so I never used JLink. However, I suspect that 'model' only contains a reference to a Java object, so technically its value does not change within Mathematica. > Update[ model ] Also, Update[] does not seem to affect the display of Dynamic expressions at all. Not even in the example on Update's doc page (try Dynamic[t] there). I wonder why Update[] is listed in the "see also" section of so many dynamic-related functions. > > 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. You should take a look at Refresh[], but I think that your solution is a better one. Explicitly chaging a variable's value seems to be the way to let the system know that certain Dynamic expressions must be updated. > > 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 > any other suggestions?