On this page
You can use global variables in ALH to share data across scripts during simulation runtime. Unlike standard variables, they are not limited to a single script and retain their values. You can check if a global variable exists, get its value, add new data, or remove it when no longer needed.
Checking an existing global variable
You can use script to check if a global variable is defined in an environment by using isGlobalVariable("variableName") argument.
If the global variable is specified by the given variable name in the scripting environment, then the argument returns true. Otherwise, it returns false.
ALH.isGlobalVariable("variableName")
The following code fragment shows how to check if a global variable, e.g., GLOBAL_COUNT, is defined through ALH API.
ALH.isGlobalVariable("GLOBAL_COUNT"); // Returns true or false.
Getting a value from a global variable
During the execution of a model, you can obtain a value of a global variable which is already defined by calling getGlobalVariable("variableName") of the ALH API. This argument returns a Java object, which is the value of the global variable specified by the given variable name.
ALH.getGlobalVariable("variableName")
The example code is as follows:
var a = ALH.getGlobalVariable("GLOBAL_COUNT") + 1
Note
You can obtain the value of a global variable directly by using its name.
a = "GLOBAL_COUNT" + 1;
Adding a value to a global variable
A global variable is a variable that is accessible from any session in the same execution. You can use ALH API to create a global variable and specify its value by calling ALH.setGlobalVariable("variableName", value).
ALH.setGlobalVariable("variableName", value)
The code example is as follows
ALH.setGlobalVariable("GLOBAL_COUNT", 1);
The example code assigns 1 to a global variable named GLOBAL_COUNT. If the global variable with the given name does not exist, this method will create a new global variable with the same name and add the value to it.
Removing a defined global variable
You can remove a global variable that is previously defined by calling a removeGlobalVariable("variableName") of the ALH API.
ALH.removeGlobalVariable("variableName")
The following code fragment shows how to remove a global variable, e.g., GLOBAL_COUNT, that was previously defined through ALH API.
ALH.removeGlobalVariable("GLOBAL_COUNT");