Re: Dynamic and J/Link
- To: mathgroup at smc.vnet.net
- Subject: [mg87053] Re: Dynamic and J/Link
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Sun, 30 Mar 2008 01:17:11 -0500 (EST)
- 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[]; > 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 > any other suggestions? > > Many thanks, > > Jason > > -- > Dr. J. McKenzie Alexander > Department of Philosophy, Logic and Scientific Method > London School of Economics and Political Science > Houghton Street, London WC2A 2AE > > > > > > > Please access the attached hyperlink for an important electronic communications disclaimer: http://www.lse.ac.uk/collections/secretariat/legal/disclaimer.htm > Your variable 'model' contains a reference to an instance of a Java class (somewhat analogous to a pointer in C), it does not contain the instance itself, which lies in a completely separate Java process. Thus, as far as Mathematica is concerned, the variable does not change when you call step. I would just wrap your two lines in a function and call that: UpdateModel[]:= ( model@step[]; array=model@getState[]; ); David Bailey http://www.dbaileyconsultancy.co.uk