Dynamic events are used to schedule any number of concurrent and independent events. For example, a communication channel that can to transmit an arbitrary number of messages simultaneously can be modeled using dynamic events created for each message. Another example is a server with infinite capacity.
There are two types of events: events and dynamic events. Both are responsible for scheduling some user-defined actions in the model. The difference between the dynamic event and the event is that a dynamic event deletes itself after its action is executed, while a user-controlled event survives and can be restarted.
The main advantage of dynamic events is that multiple instances of the same event can be scheduled concurrently within a model.
Another feature of dynamic events is the ability to initialize each instance of the dynamic event with some specific data. While scheduling a new instance of a dynamic event, you can pass the required data using the arguments of the event. These arguments are accessible in the action of the dynamic event.
However, dynamic events are more complicated to use than regular events. For this reason, we recommend using them only in the following cases:
- When you expect several events, performing similar actions, to be scheduled at the same time in your model.
- When the action of your dynamic event depends on some specific information.
To define a dynamic event
-
Drag the
Dynamic Event element from the Basic Elements palette onto the graphical diagram of the agent.
-
Go to the Properties view and change the Name of the event.
Since AnyLogic generates a Java class for each dynamic event you add onto the diagram, you should follow the Java naming conventions. Start the name of the dynamic event with an uppercase letter.
- If the action of the dynamic event depends on some specific information, you can pass that information to the event using the arguments of the dynamic event. Define the arguments in the Arguments table. Each row of the table specifies one particular argument with a given Name and Type.
- Specify the action you want to execute when the event occurs in the Action field. Here you can access the arguments of the dynamic event, if you have defined any.
- General
-
Name — The name of the dynamic event. The name is used to identify the dynamic event. AnyLogic uses this name when generating the function that schedules a new instance of the dynamic event.
Since AnyLogic generates a Java class for each dynamic event you add onto diagram, you should follow Java naming conventions. Start the name of the dynamic event with an uppercase letter.Show name — If selected, the name of the dynamic event is displayed on the presentation diagram.
Ignore — If selected, the dynamic event is excluded from the model.
Action — The Java code to execute when the event occurs. In the code, you can access the Arguments of the dynamic event if you have defined any.
- Arguments
-
Defines the set of optional arguments used to pass some specific information that will be used in the action of a particular instance of the dynamic event. Each row of the table specifies one particular argument. Every parameter should be given in the default format: Name and Type.
Since the order of the Arguments in the table matters (when scheduling a new instance of a dynamic event, you pass argument values exactly in the order they are defined in this table), you can change the order of the Arguments by moving them up and down using thebutton that appears to the left of each row when you hover over it. To remove a parameter, click the
button that appears to the right.
- Expert
-
Visible — If selected, the dynamic event is visible on the presentation during runtime.
- Controlling events
-
Function Description void reset() Discards the scheduled occurrence of the event and unregisters this instance of the dynamic event at the agent. double getRest() Returns the time remaining before the scheduled occurrence of the event, in model time units. double getRest(TimeUnits units) Returns the time remaining before the scheduled occurrence of the event, in given time units.
units — a time unit constant
Dynamic events are scheduled differently from regular events. To schedule an additional instance of a dynamic event, use the function automatically generated by AnyLogic for each dynamic event on the agent diagram: create_%dynamic event name%(timeout, timeUnits, parameter1, parameter2, …).
Example:
Suppose your dynamic event is named ArrivalEvent. To schedule an arrival to occur in 15 minutes from the current model time, call the following function:
create_ArrivalEvent(15, MINUTE);
Here you pass the timeout to the function as the first parameter and specify the time units for the timeout. To specify the time units, enter one of the following time unit constants:
- MILLISECOND
- SECOND
- MINUTE
- HOUR
- DAY
- WEEK
- MONTH
- YEAR
There is also a short function notation, when you specify only the timeout value and omit the time units:
create_ArrivalEvent(15);
In this case, the model time units are assumed. For example, if the model time units are hours, the dynamic event ArrivalEvent will be scheduled to occur in 15 hours from the current time.
If you have defined any arguments for the dynamic event in its Arguments table, you pass the parameter values to the function using the function arguments, in the same order they appear in the Arguments table of the event in the Properties. For example, the DeliveryEvent dynamic event has two arguments: weight and sender. The following is an example a function call that passes values to an event being created:
create_DeliveryEvent(2, DAY, 12, this );
You can access the arguments in the Action code of the event.
Time counting begins when an instance of a dynamic event is created. When the event timeout expires, AnyLogic executes the event’s action and then deletes the instance of the dynamic event. If the reset() function of the dynamic event is called before the timeout expires, the instance of the event is deleted and its action is never performed.
Unlike events, which can be accessed simply by name, there is no easy way to access instances of a dynamic event.
You can use the agent function getDynamicEvents(), which returns a list of all currently active dynamic event instances within an agent. This can be useful, for example, when you want to cancel all of these dynamic events. However, it would be impossible to distinguish one dynamic event from another, In other words, you will need to store references to currently active dynamic events somewhere. You can create your own collection called dynamicEvents and add the newly created instance of the dynamic event to it:
MyDynamicEvent de = create_MyDynamicEvent(7.5);
dynamicEvents.add(de);
-
How can we improve this article?
-