Page History
On this page
| Table of Contents | ||
|---|---|---|
|
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 variableretrieve, modify, add, or remove structural feature values using the Action Language Helper (ALH) API and action scripts in the modeling tool. ALH allows you to dynamically control object properties, and update data during a simulation. You can also retrieve tag values, which store additional metadata.
Getting a structural feature value
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 falseget a structural value. You can use the fUML object syntax, with the object.get("featureName"). featureName can use dot notation for nested features. For example:
- value = self.get("car.speed");
| Note |
|---|
Using Groovy scripting language, you can get nested structural features using just dot notation. For example, value=car.speed |
Alternatively, structural value can also be acquired using ALH API.
| Code Block | ||
|---|---|---|
| ||
ALH.isGlobalVariablegetValue(object, "variableNamefeatureName") |
The The following code fragment example shows how to check if a global variable, e.g., GLOBAL_COUNT, is defined through get the value of the structural feature indicated above in ALH API.
| Code Block | ||
|---|---|---|
| ||
value = ALH.isGlobalVariablegetValue("GLOBAL_COUNT"); // Returns true or false. |
Getting a value from a global variable
self, "car.speed"); |
| Info | ||
|---|---|---|
| ||
While using Rhino JavaScript, the return value of ALH.getValue() is not automatically UnBoxed to a primitive type. Users need to handle such UnBoxing by themselves, by using ALH.getValue(object, "speed").intValue() to get the value of primitive integer type. |
Specifying a structural feature value
You can use script to specify a structural value. You can use the fUML object syntax, with the object.set("featureName", value). featureName can use dot notation for nested features. For example:
- value = self.set("speed", 10);
| Note |
|---|
Using Groovy scripting language, you can set nested structural features using just dot notation. For example, car.speed=100 |
Alternatively, structural value can also be specified using ALH APIDuring 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 | ||
|---|---|---|
| ||
ALH.getGlobalVariablesetValue(object, "variableNamefeatureName", value) |
The example code is as followsfollowing example shows how to specify the value of the structural feature in ALH API:
| Code Block | ||
|---|---|---|
| ||
var avalue = ALH.getGlobalVariablesetValue("GLOBAL_COUNT") + 1self, "speed", 10); |
In addition, the RuntimeObject can be omitted. The following example shows how to set a structural feature value without specifying a RuntimeObject in ALH API:
| Code Block | ||
|---|---|---|
| ||
ALH.setValue("featureName", value) | ||
| 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 variablean object
You can use script to add a value to an object. You can use the fUML object syntax, with object.addValue("featureName", value) and object.addValueAt("featureName", value, insertAt). For example:
- self.addValue("p1",10); // System.p1 = [10]
- self.addValue("p1",30); // System.p1 = [10, 30]
- self.addValueAt("p1",20,2); // System.p1 = [10, 20, 30]
Alternatively, structural value can also be added using ALH API.
| Code Block | ||
|---|---|---|
| ||
ALH.addValue(object, "featureName", value)
|
If you have more than one value in an object and if you want to add a value at a specific position, you can use insertAt argumentA 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 | ||
|---|---|---|
| ||
ALH.setGlobalVariableaddValueAt(object, "variableNamefeatureName", value, insertAt) |
The code example is as followsfollowing code fragment shows how to add values to an object, e.g., System.p1, through ALH API.
| Code Block | ||
|---|---|---|
| ||
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.
addValue(self,"p1",10); // System.p1 = [10]
ALH.addValue(self,"p1",30); // System.p1 = [10, 30]
ALH.addValueAt(self,"p1",20,2); // System.p1 = [10, 20, 30] |
Removing a value of an object
You can use script to remove a value from an object. You can use the fUML object syntax, with object.removeAt("featureName", removeAt). For example:
- print(self); // System.p1 = [10, 20, 30]
- self.removeAt("p1",3); // System.p1 = [10, 20]
| Note |
|---|
If feature has only one value, removeAt index 1 should be used. |
Alternatively, structural value can also be removed using ALH API.
| Code Block | ||
|---|---|---|
| ||
ALH.removeValueAt(object, "featureName", removeAt) |
You can also use the fUML object syntax, withobject.remove("featureName", objectToRemove) to remove an exact fUML object from a feature. For example:
- self.remove("myPart", myPart)
object.remove("featureName", object) is useful when you have direct reference to fUML object you want to remove. Remove can be used in conjunction with get method to get a direct reference to the fUML object.
Alternatively, structural value can also be removed using ALH API.
| Code Block | ||
|---|---|---|
| ||
ALH.removeValue(object, "featureName", objectToRemove) |
Getting a tag value
You can use script to get a tag value. You can use the fUML object syntax, with object.getTagValue("tagName"). For example:
- self.getTagValue("uniform.max"); // 10
Alternatively, tag value can also be acquired using ALH API.
| Code Block | ||
|---|---|---|
| ||
ALH.getTagValue(object, "tagName") |
If the RuntimeObject is omitted, $context$ or self will be used as an object. The context object in the parametric is the constraint block object that owns the constraint, not the actual context of the diagram. The following example shows how to get a tag value without specifying a RuntimeObject in ALH API:
| Code Block | ||
|---|---|---|
| ||
ALH.getTagValue("tagName | ||
| Code Block | ||
ALH.removeGlobalVariable("variableName") |
The following code fragment shows how to remove a global variableget tag values, e.g., GLOBAL_COUNT, that was previously defined max, through ALH API. Also refer to Value access and references by tags.
| Code Block | ||
|---|---|---|
| ||
ALH.removeGlobalVariablegetTagValue("GLOBAL_COUNTself, "uniform.max"); // 10 |