AnyLogic enables defining your own functions. Function will return the value of an expression each time the user calls it from the model. Functions are helpful when you need to re-use the same function in multiple places in your model. Since you write functions in Java, you have all the advantages of this language, such as conditional and cyclic operators, branches, and more.
AnyLogic also supports a special type of functions — table functions. A table function is a function defined in the tabular form. You can make it continuous by interpolating and/or extrapolating. You may need table functions to define a complex non-linear relationship which cannot be described as a composition of standard functions, or to bring experimental data defined as a table function to a continuous mode.
To define a function
- Drag the
Function element from the Basic Elements palette onto the graphical diagram of agent (or experiment).
- In the Properties view, type the name of the function in the Name box.
- If your function only performs some actions but does not return any result of the calculations, specify that this function returns nothing by choosing Just action (returns nothing) from the group of buttons.
- Otherwise, if you need to obtain the result of calculations, select Returns value. User-defined function can return a value of absolutely any Java class.
- You can choose one of the most-used types (boolean, int, double, String) using the corresponding option from the Type drop-down list. However, if you need to define a function returning the value of some other Java class, choose Other... option from the drop-down list and type the required class name in the edit box located next to it.
-
Type the function expression in the Function body field.
The final line of the expression should be the return expression:
return <expression that returns the value of specified return type>; - If required, define arguments to pass input data to the function.
- General
-
Name — The name of the function. The name is used to identify and access the function.
Show name — If selected, the function name is displayed on a presentation diagram.
Ignore — If selected, the function is excluded from the model.
Just action (returns nothing) — If selected, the function just performs some actions and returns nothing.
Returns value — If selected, the function returns the result of calculations. You can specify the type of the function’s return value in the Type field below.
Type — [Visible if the option Returns value is selected] Here you can specify the type of the function’s return value. A function can return a value of some most-used types (boolean, int, double, String), or a value of some Other Java class (in this case specify the Java class name in the edit box below).
Arguments — Here you can define some optional arguments for your function to enable passing some additional input data necessary for function calculations. Each row of the table specifies one particular argument. To remove an argument, hover the mouse over the corresponding row in the table and click the Delete row button to the right. Rearrange arguments in the table by hovering the mouse over the corresponding row and dragging up or down the icon displayed on the left.
Function body — Type here the function body. If the function returns some value, the final line should be the return expression.
- Expert
-
Access — The access type of the function. There are four access types:
- public — the function can be accessed from anywhere.
- private — the function can be accessed from this agent class only.
- protected — the function can be accessed from this agent class and its subclasses.
- default — the function can be accessed anywhere within this model.
Visible — If selected, the function is visible on the presentation at runtime.
You may define some optional arguments for your function to enable passing some additional input data necessary for function calculations.
To define arguments of the function
- Select the
function in the graphical editor.
- In the Properties view, define function arguments in the Arguments table.
- Each row of the table specifies one particular parameter.
- In the Name cell of the table, type the parameter name.
- In the Type cell of the table, specify the type of the parameter. Click in the cell and choose the required type from the drop-down list or type it in the cell.
If you have defined any parameters for the function, you must pass parameter values any time you call the function. The values must be provided in the order they are defined in the function arguments table.
Since the order of parameters in the table is significant, AnyLogic enables changing the parameter order.
To delete the argument
- Hover the mouse over the corresponding row in the Arguments table and click the Delete row button to the right.
To reorder arguments
- Hover the mouse over the corresponding row in the Arguments table and drag up or down the icon shown to the left.
To call a function, write the function name followed by parentheses. If the function has arguments, you need to pass the argument values separated by commas inside the parentheses. The values must be provided in the order they are defined in the function arguments table in the Arguments section of the function properties.
Examples:
stopObject();
moveTo(15, 27);
By default, a function can be called from any place of your model. You can restrict the access scope for the function by changing the function’s access type. Choose the required access type using the Access drop-down list in the function’s properties Expert section.
-
How can we improve this article?
-