Is it possible to detect (and create a trigger for) an event when any of the variables defined in the notebook changes its value (for any reason)?
I need to know which variable got changed and what the new value is.
Here are some details:
I have a client-server application, where the client is my Mathematica notebook. I also have a Java layer in between. The server runs MongoDB database and when any client modifies it, the server sends updates to all other clients. Specifically, to the Java layer. The job of the Java layer is to keep an up-to-date local copy of the database, and to relay all updates to the Mathematica client.
Needs["JLink`"]
InstallJava[];
ReinstallJava[ClassPath -> "/Users/verse/Desktop/java-ddp-client.jar"];
LoadJavaClass["java.lang.System"];
LoadJavaClass["java.lang.Object"];
LoadJavaClass["java.lang.reflect.Array"];
LoadJavaClass["com.wolfram.jlink.JLinkClassLoader"];
LoadJavaClass["me.kutrumbos.examples.MessageHandler"];
url = "localhost"; port = 3000;
client = JavaNew["me.kutrumbos.DdpClient", url,
MakeJavaObject[port]];
observer =
JavaNew["me.kutrumbos.examples.ObservableDdpClientObserver"];
collections = {};
onCollectionUpdate[msg_] :=
Module[{}, collections = observer@getJSONObjectsList["test"];];
observer@addMessageHandler[
ImplementJavaInterface["me.kutrumbos.examples.MessageHandler",
"update" -> "onCollectionUpdate"]];
client@addObserver[observer];
(ShowJavaConsole[];)
client@connect[];
In this example, variable collections gets updated by the Java layer every time there is a change in the underlying database. It does not happen through the Set command inside the notebook. I need to know when this does happen and I need to properly update other variables.
Conversely, when any of the relevant variables change inside the notebook, I need to be able to update the database on the server using an API request. To send such a request, I need to know which variable has changed and what the new value is.
Dynamicthere are many examples there. – bill s Feb 07 '14 at 15:59Monitor[]? – Dr. belisarius Feb 07 '14 at 15:59Dynamictutorial, but didn't see a way to detect the changes. My understanding was thatDynamicdoes the updating itself, but does not allow for trigger actions. @belisarius, I thinkMonitorwon't work for my purposes -- it only shows the current value of a variable while a given expression is evaluated, but in my case there is no evaluation. Variables may be changed at any time by an external process (server update). – verse Feb 07 '14 at 16:08Dynamicis implemented it may be useful or entirely useless for your task. There are other methods such asScheduledTaskthat you should look at too. But outlining all of these methods is IMHO too extensive. – Mr.Wizard Feb 07 '14 at 16:31