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
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.
Visible – If selected, the function is visible on the presentation at runtime.
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.
– [Visible if the option
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,
String), or a value of some
Java class (in this case specify the Java class name in the field to the right).
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, select the corresponding row in the table and click the "cross" button. Rearrange arguments in the table using the "arrow" buttons.
Type here the function body. If the function returns some value, the final line should be the return expression.
Static – [Only visible if Library developer mode is enabled] If selected, the function is static. A static function does not need creating any instances of the agent type, in whose code it is defined. Static function defined in the MyClass agent type can be accessed from any place of the model as MyClass.function().
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.
System dynamics units – If selected, you will be able to specify measurement units for the value returned by this function in the edit box to the right. Having specified units for elements of your model, you may perform unit checking to find out dimension inconsistencies in the model.
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
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 just moving them up or down using the buttons and . To remove a parameter, select the parameter row in the table and click the button.
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.
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 Accessdrop-down list in the function's properties Advanced section.