I am creating a networking application in Mathematica that runs a non-stop simulation, and periodically receives new information from the remote server that affects the simulation.
Whenever values of certain variables inside the simulation change, Mathematica needs to send an update with the values to the remote server.
Is there a way to catch an event in Mathematica that corresponds to the change in the value of a specific variable?
For example, assume that initially I have two variables: a
and b
, such that a=1
, and b=1
. Then, throughout the simulation one of the variables changes, while the second one remains the same: a=2
, b=1
. Is it possible to write an event handler that knows which variable has changed and what the new value is?
I've seen this discussion: Triggering actions when a variable is set, that recommends using upvalues for Set
and SetDelayed
:
$var /: Set[$var, value_] :=
Block[{$in = True}, Print["$var set to ", value]; $var = value] /; Not@TrueQ[$in]
But this seems to not cover all possible cases when a variable might change, and requires defining UpValues for every variable.
Is there a better way?
ImplementJavaInterface[]
. This event handler basically assigns new values to variables that are used inside the simulation, and thus affects the simulation. I pasted some of the code in my previous question here: mathematica.stackexchange.com/questions/27330/… $\endgroup$Internal`ValueChangeVeto
andInternal`AddHandler["VetoableValueChange", myVetoFunction]
as mentioned by Leonid in the other thread. Unfortunately, the operation of these is not fully understood, and it is not clear if these can be used with ownvalues. Regardless of that, this question is still a duplicate of the other one. $\endgroup$