Hello all,
how can I share a boolean flag between two functions?
Something like this:
boolean newFlag := false;
monitor xxx(){
action onload(){
monitor.subscribe(Measurement.SUBSCRIBE_CHANNEL);
on all Measurement(type=MEASUREMENT_TYPE_A) as measurement
{
newFlag := true;
}
monitor.subscribe(Measurement.SUBSCRIBE_CHANNEL);
on all Measurement(type=MEASUREMENT_TYPE_B) as measurement
{
// here new flag needs to be true;
}
}
}
there are multiple ways to achieve this, depending on what you want to do:
Dynamic Listeners
You can start listeners inside of other listener
on all Measurement(type=MEASUREMENT_TYPE_A) as measurement
{
on Measurement(type=MEASUREMENT_TYPE_B) as measurement
{
}
}
Note that you should have logic in place to avoid starting the same listener multiple times (that’s why I removed the “all”) or to stop the listener when no longer needed using a listener condition including “and not ” or by explicitly calling quit() on the listener.
Sending events
You can define your own events and send them between listeners:
event FlagEvent {}
monitor.subscribe("FlagChannel");
on all Measurement(type=MEASUREMENT_TYPE_A) as measurement
{
send FlagEvent() to "FlagChannel";
}
on all Measurement(type=MEASUREMENT_TYPE_B) as measurement
{
on FlagEvent() {
}
}
Global Variables
You can access variables defined outside of the listener. You have to be aware that listeners will get the value of the variable when they were created, so you cannot just have a Boolean variable. You can either define your own event or e.g. use a dictionary to keep track of the state (you can change the content of the object but not the object itself):
dictionary<string,boolean> flags := {"flagA":false};
on all Measurement(type=MEASUREMENT_TYPE_A) as measurement
{
flags["flagA"]:= true;
}
on all Measurement(type=MEASUREMENT_TYPE_B) as measurement
{
// access flags["flagA"]
}
By using a listener inside another listener they will always get executed one after the other right?
While, if they are separated their execution is independent correct?