MathGroup Archive 2008

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

Search the Archive

Dynamic and J/Link

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87019] Dynamic and J/Link
  • From: "J. McKenzie Alexander" <jalex at lse.ac.uk>
  • Date: Sat, 29 Mar 2008 04:23:05 -0500 (EST)

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


  • Prev by Date: Re: The FinancialData Function
  • Next by Date: Re: Find Upper Neighbor in a list
  • Previous by thread: Re: Collect coefficients of array variables during
  • Next by thread: Re: Dynamic and J/Link