Variables
Icons
The icon for variables is shown below.
The different icons for variables and their quotes on the canvas in different scenarios can be found in the Data Components chapter.
Function
Variables store various types of data.
Key Points
Data Types
The Detailed Specifications chapter provides information about the various data types supported by variables and the methods available for each type.
Simple Variables
When a variable is of a simple (Value) type, it is dynamic, meaning it can store various subtypes such as Number, String, Entity, etc. The type can change during the simulation due to overwriting.
Complex Variables
These include Dict, List, and Set, which are static. Once initialized, they cannot be overwritten by variables of other types.
When assigning B to variable A (i.e., A = B
):
- If B is a complex type (such as List), modifications to A (such as
A.pop()
) will also modify the original object B. - Conversely, if B is a simple type, changes to A (like
A += 1
) will not affect B's value. - If you want to avoid the above situation, use the
copy()
method, such asA = B.copy()
.
Setup Methods
- Variables can be set up by defining their type and initial value.
- The methods for setting global variables, entity properties, and local variables vary based on the location of the variable.
As a Global Variable (General settings)
Type
Choose from Value, List, Dict, or Set.
Initial Value for Value Type
The input box accepts an expression that returns a simple type value. The expression cannot use other variables.
Initial Value for List Type
It supports any data type as an element in the List.
-
Choose Input Method
Overall expression or individual element definition expression
-
Overall Expression Assignment
The format is
[element0, element1, ...]
-
Individual Element Definition Expression Assignment
- Add a new list element: Click the button to add an input box.
- Define the current position's list element: Input an expression.
- Repeat the previous steps, until all elements of the list have been defined.
-
Initial Value for Dict Type
-
Dict's Key supports data types other than a dictionary, while its value supports any data type.
-
Choose Input Method
Overall expression, key-value pairs expression, or batch keys construction.
-
Overall Expression Assignment
Format as
{key1: value1, key2: value2, ...}
. -
Key-Value Pair Expression Assignment
- Create a new key-value pair: Press the add button to display a row with two input boxes for a key-value pair.
- Input a key-value pair: Type the key expression in the left input box and the value expression in the right one.
- Repeat the previous steps: Continue this process until all key-value pairs for the Dict are defined.
-
Batch Keys Construction
This method enables the construction of keys in batch mode and the specification of a default value.
-
Constructing Batch Keys
-
You can use construction expressions to represent possible keys in batch mode without actually creating them. Click on the formula bar, and a dropdown menu will suggest commonly used expressions. Its usage is similar to the container.
-
You can preview the effect of batch keys construction by clicking the preview button, as illustrated in the left image below.
-
Once the keys are constructed, when you use the related Dict object in other corresponding locations, the formula bar will provide suggestions based on the construction to aid in modeling, as illustrated in the right image below.
-
-
Set Default Value
The input box accepts an expression. If any key is not assigned a value, it defaults to this value.
-
-
- The mechanism of batch key construction in Dict is similar to that of containers. It's beneficial when dealing with a large number of keys that have a regular pattern. For instance, in the DeFi field, you can use
ProductJoin
to construct all ERC20 tokens and their pairwise LP tokens as container keys, thereby supporting the storage of balances for all these tokens. - However, this doesn't mean the system will initialize all key-value pairs at runtime. Instead, it will instantiate them based on actual requirements.
- Unlike containers, the default value of Dict's Value may be modified during modeling if
DictA = DictB
is used.
As an Entity Property
- Variables are placed inside entities, they serve as entity properties.
- The data type setting and initial assignment are the same as for global variables.
- Belong to (as shown above) Setting: Choose whether the variable belongs to the individual or the observer.
- Support for personalized configuration of each individual's variable initial assignment in the properties panel of the Agent.
As a Process Local Variable
-
When variables are inside a process, they serve as Process Local Variables.
-
The data type setting and initial assignment are the same as for global variables.
-
Three Types of Local Variables: Temporary variables, variable interfaces, and regular variables.
Temporary Variables
- Lifespan: Created when the process start event Trigger is generated and destroyed when the Trigger is destroyed.
- Register: Data is registered based on the Trigger (different Triggers correspond to different values).
- Please refer to the Data Components chapter for a detailed explanation of the working mechanism of temporary variables and model examples.
Variable Interfaces
- When values are passed into the corresponding port in parameter form, the variable interface is created at the start event of the process when the trigger is generated and destroyed at the end event when the trigger reaches the end. Data is stored according to the trigger, with different triggers corresponding to different values.
- When data objects are passed into the corresponding port via State Connectors, the variable interface has no register, and its lifespan is independent of the local trigger within the process.
- After setting the process data interface, the corresponding port will be displayed in the outline of the process and in the outline of the components that call or ask to call this process. The port is denoted by "V" and is used for external data input. Refer to the Process Interface and Data Passing chapter for detailed usage and model examples.
Regular variables
- Similar to global variables or entity properties, the setting methods remain the same. However, regular variables exist permanently and do not have a register mechanism.
Example
This is a process modeling of the GBM price generator.