AnyLogic supports functions that parse code blocks from database cells and execute them during model runtime. This is particularly helpful when your database contains some code imported from external data sources (for example, Excel tables). There are cases when it may be more convenient to modify the code within the data source rather than editing the model each time you need to make changes in code.
To assign your variables the values you retrieved from the database, consider using the Query Builder, or take a look at the use cases of reading data from AnyLogic database.
Function | Description |
---|---|
void executeAction(code, argDescriptors) |
Executes the action. Returns nothing. code — a text string containing the code you want to execute. argDescriptors — (optional) an array of parameters and their values, presented in the following format: "parameter 1 name", value, "parameter 2 name", value. Example: executeAction("myEvent.restart(period);", "period", 20); — Restarts the myEvent event after the specified timeout period. |
T executeExpression(code, argDescriptors) |
Executes or evaluates the specified expression to some result. Returns the result.
The result’s type can be specified explicitly — see the next notation of the function. code — a text string containing the code you want to execute. argDescriptors — (optional) an array of parameters and their values, presented in the following format: "parameter 1 name", value, "parameter 2 name", value. Example: Color myColor = executeExpression("new Color(red, green, blue);", "red", 105, "green", 105, "blue", 105); — creates the myColor color with the specified RGB values. |
T executeExpression(returnType, code, argDescriptors) |
Execute or evaluates the specified expression to some result. Returns the result of the specified type. returnType — the type of the value the expression returns. May be void.class, if the evaluated expression is an action. code — a text string containing the code you want to execute. argDescriptors — (optional) an array of parameters and their values, presented in the following format: "parameter 1 name", value, "parameter 2 name", value. Example: Color myColor = executeExpression(Color, "new Color(red, green, blue);", "red", 105, "green", 105, "blue", 105); — creates the myColor color with the specified RGB values. |
Consider a model that has a database. This database includes a table named colors which looks as follows:
The color column lists text strings that contain the names of colors.
Assume your model contains a rectangle named rectangle which you want to be colored randomly, using one of the colors listed in the database.
To command AnyLogic to read values from the database and use these colors, add the following code action:
String colorName = randomFrom(selectFrom(colors).list(colors.color));
rectangle.setFillColor(executeExpression(colorName));
Here, we retrieve the colorName text string as a random record from the database cell. Then we command AnyLogic to color the rectangle, interpreting the text string we retrieved from the database as a proper Java expression.
You can use this anywhere where it is convenient for you: as the button action, or agent action.
-
How can we improve this article?
-