Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

id766377124

...

id766377145

...

id766377123

On this page

Table of Contents
maxLevel4

...

id766377134

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.

Code Block
languagetext

...

ALH.isGlobalVariable(

...

"variableName")

The following code fragment shows how to check if a global variable, e.g., GLOBAL_COUNT, is defined through ALH API.

Code Block
languagetext
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.

...

Code Block
languagetext

...

ALH.getGlobalVariable(

...

"variableName")

The example code is as follows:

Code Block
languagetext
var a = ALH.getGlobalVariable("GLOBAL_COUNT") + 1
Note
titleNote

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).

...

Code Block
languagetext

...

ALH.setGlobalVariable(

...

"variableName",

...

 value)

The code example is as follows

Code Block
languagetext
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.

Code Block

...

languageperl
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.

Code Block
languagetext
ALH.removeGlobalVariable("GLOBAL_COUNT");