In Java to call a function (or method, which is more correct in object-oriented languages like Java, as any function is a method of a class) you write the function name followed by the argument values in parentheses. For example, this is a call of a triangular probability distribution function with three numeric arguments:
triangular( 2, 5, 14 )
And the next function call prints the coordinates of an agent to the model log with a timestamp:
traceln( time() + ": X = " + getX() + " Y = " + getY() );
The argument of this function call is a string expression with five components; three of them are also function calls: time(), getX(), and getY().
Even if a function has no arguments, you must put parentheses after the function name, like this: time()
A function may or may not return a value. For example, the call of time() returns the current model time of type double, and the call of traceln() does not return a value. If a function returns a value it can be used in an expression (like time() was used in the argument expression of traceln()). If a function does not return a value it can only be called as a statement (the semicolon after the call of traceln() indicates that this is a statement).
Most of the code you write in AnyLogic is the code of a
subclass of Agent
(a fundamental class of AnyLogic). For your
convenience, AnyLogic system functions and most frequently used Java
standard functions are available there directly (you do not need to
think which package or class they belong to, and can call those
functions without any prefixes). Below are some examples (these are
only a few functions out of several hundreds, see AnyLogic API
Reference for the full list).
The type name written before the function name indicates the type of the returned value. If the function does not return a value, we write void instead of type, but we are dropping it here.
functions (imported from java.lang.Math,
about 45 functions in total):
Functions related to the model time, date, or date elements
(about 20 functions):
distributions (over 30 distributions are supported):
Output to the model log and formatting:
Model execution control:
Navigation in the model structure and the execution
Agents have more functions available specific to the particular type of agent, for example:
Network and communication-related functions:
Space and movement-related functions:
The functions available in the current context, for example,
property field where you are entering some code, are always listed in
the code completion window that opens if you press Ctrl+Space (Mac OS:
Alt+Space), please refer here
for more details.
All elements in AnyLogic model (events, statecharts, table functions, plots, graphics shapes, controls, library objects, and so on) are mapped to Java objects and expose Java API (Application Programming Interface) to the user. You can retrieve information about the objects and control them using their API.
To call a function of a particular model element that is inside the agent you should put the element name followed by dot "." before the function call: <object>.<method call>
These are some examples of calling functions of the elements
current agent (the full list of functions for a particular
element is available in AnyLogic Help):
Scheduling and resetting events:
Sending messages to statecharts and obtaining their current
Adding a sample data point to a histogram:
Display a view area:
Changing the color of a shape:
Retrieving the current value of a checkbox:
This statement hides or shows the shape depending on the state of the checkbox:
Changing parameters and states of embedded agents:
Note that the parameter rate appears as Arrival rate in the source object
properties. To find out the Java names of the
parameters you should hover the mouse pointer on the properties.
You can easily find out which functions are offered by a particular
object is by using code
completion. In the code you are writing you
should type the object name, then dot ".", and then press Ctrl+Space
(Alt+Space on Mac). The pop-up window will display the list of
functions you can call.
You can define your own functions of agents, experiments, custom Java classes. For agents and experiments functions can be defined as objects in the graphical editor.
Another way of defining a function is writing its full Java declaration and implementation in Additional class code field in the Advanced property section of the agent type or experiment, see the figure below. The two definitions of the functions are absolutely equivalent, but having the function icon on the canvas is preferred because it is more visual and provides quicker access to the function code in design time.
Function defined in the Additional class code of the agent