On this page
Checking an existing global variable
You can check if a global variable is defined in an environment by calling the isGlobalVariable(String variableName) method
If the global variable specified by the given variable name is present in the scripting environment, this method will return true. Otherwise, it returns false.
boolean isGlobalVariable(String 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 has already been defined, by calling getGlobalVariable(String variableName) of the ALH API. This method returns a Java object, which is the value of the global variable specified by the given variable name.
object getGlobalVariable(String 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.
var 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(String variableName, Object value).
void setGlobalVariable(String variableName, Object 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 has previously been defined by calling a removeGlobalVariable(String variableName) of the ALH API.
void removeGlobalVariable(String 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");