Process Interfaces and Data Passing
Why Use an Interface?
- When modeling processes, the appropriate use of data interfaces can achieve process decoupling, making logic clearer. This is akin to defining functions or methods with parameters during programming, improving the efficiency of building and reading models.
- The callee only needs to define the format of the input data. The caller needs to pass data in the correct format as required. The callee and the caller need not concern themselves with each other's logic.
How to Set an Interface for a Process?
-
Creating an interface
You can place a data component in the process, and this data component becomes a local object. Set its type to interface. For more details, refer to the chapter on Data Components.
-
Appearance
After setting the interface, the corresponding port will appear on the outline of the process, the Call component or the Ask component that calls this process. These are utilized for data transmission.
If you want to distinguish between different port styles clearly, you can set the color of the interface data object (in the style control bar of the data object). The corresponding port will then maintain the same color.
-
Data Type
The interface determines the type of data to be transmitted. The data type passed in from the port must be consistent.
Understanding the relationship and differences between the interface and port:
- The interface is located within the process. It's a form of local data object used for reading and writing data within the process.
- The port is displayed on the outline of the process and the Ask or Call components that call this process. Each port corresponds to an interface within the process, facilitating external connections and data passing.
How Many Types of Interfaces Are There?
-
There are two types of interfaces:
Variable interfaces can only pass variables, and Container interfaces can only pass containers.
Since interfaces are created by dragging and dropping data components into the processes, the interface type clearly corresponds to the data component type: variables or containers.
-
Variable ports are indicated by a "V" symbol, while container ports are indicated by an "R" symbol.
How to Use Interfaces within Processes
-
Reading data from an interface
You can read data directly from the interface by using the interface name in any expression within the process, such as
some_variable = interface_name * 2
. -
Output data to interface
In any formula within the process (on the left side of the equation), you can assign the result of a computation to an interface object. For instance, "interface_name = some_expression".
-
Resource operation on the interface
You can link container interfaces and resource operation objects using Container Operate Connector.
The Go
process calls the to Trade
passive process, enabling an exchange of USDC and ETH tokens between WalletA
and Pool
containers at the ExchangeRate
.
- The
to Trade
process is set with anExchangeRate
variable interface and aWalletIn
container interface. Therefore, in theGo
process, these two interfaces ask the target object to pass the corresponding variables and containers.
How to Pass Data to the Port
There are two ways to pass data to the port.
Pass Data Objects via State Connectors
Connect the data object or its quotes of the same data format to the port with the State Connector, which means passing the data object itself.
- The gray passive process
Plus10
is responsible for assigning theOutNum
interface to theInNum
interface +10. - The green process passes the variable
Number
with an initial value of 0 to theInNum
port and the variableOutput
to theOutNum
port. - The final run result is that the variable Output equals 10.
The data objects passed through the State Connector are independent of being read or written; this depends on their use within the process.
In the model above, the Number
variable is passed into the Plus10 process, where the Compute component reads its value, which is "reading." When the OutPut
variable is passed in, the Compute component assigns a result to it, which is "writing." Thus, the final Output
variable reflects the computed result after the process call.
Pass Values
-
Pass the value of the expression to the port.
-
Enter an expression into the InNum input field on the properties panel of the port's object.
-
The input field accepts any expression that can return a value of the format required by the interface definition.
- The gray passive process
Plus10
is responsible for assigning theOutNum
interface to theInNum
interface +10. - The blue process passes the computed value of
Number
to theInNum
port. After the Call component executes its action, the formulaOutPut = This.OutNum
can be used to assign the value of theOutNum
port to the variableOutPut
. - The final result of the execution is that the variable
Output
has a value of 10.
Differences between the Two Methods
- The nature of the passed object differs, resulting in varying results:
- Pass the data objects via State Connectors: The data object is passed. In the called process, any computation or modification of the interface is performed directly on the object.
- Pass values: The value of the expression is passed. Specifically, it's the result of the expression when the process is triggered. This value is then assigned to the interface object. This is similar to passing a value to a function parameter in Python. In the called process, any computations or modifications to the interface are only applied to the temporary interface element, not the object itself.
- Suppose the variable to be passed is V. Using the above State Connector and value input methods to pass data would yield the following:
- If V is a simple type, then in the called process, multiple readings of the interface would give the real-time latest value of V for the former method, and the value of V at the time the process was called for the latter. Writing to the interface would write to V for the former method, but not for the latter.
- If V is a complex variable type like Dict or Set, then in the called process, multiple readings of the interface would give the latest value for both methods, and modifying the interface would rewrite V for both. However, overwriting the interface would overwrite V for the former method, but not for the latter.
- This is due to the inherent differences between complex and simple data types.
- The system prioritizes the method of passing data objects via State Connectors.
- The only method available for containers is passing data objects via State Connectors.
In this model, there are four processes, the grey one named To Be Called
, the purple one Modify InputNumber
, the yellow one Data Element pass in
and the blue one Value passed in
.
A variable named InputNumber
with an initial value of 0 will be changed to 10 by the purple process at the 2nd Tick.
When the grey process is called, it will delay 2 Ticks before adding NumberIn + 10
and passing the result to the NumberOut
port.
In Tick 1, both the yellow and blue processes below will call the grey process.
The yellow process passes the InputNumber
variable itself, so after Tick 2, the grey process will get its latest value of 10. Thus, the result after adding 10 will be Output1 = 20
.
The blue process passes the value of InputNumber
at Tick 1, which is 0. Therefore, after the call ends, the result will be Output2 = 10
.
Note: The grey processTo Be Called
has no concurrency limit, so the model will complete at the end of Tick 3.
How to Access Ports
Access Port Values in Expressions
- Port values can be accessed using the format
component_name.port_name
. - The component containing the port can access its port values using the format
This.port_name
.
View Interface Status
-
The Data Element Monitor can be used in conjunction with debug breakpoints to view the status of interfaces.
-
When used with debug breakpoints, interface status can also be viewed in the "Local" section under the "Call Stack" tab in the Console.