AnyLogic
Expand
Font size

Generic parameters

Agents can have generic parameters. Generics allow you to abstract over types. Using generic parameters, you can tune up agents for each particular use case and thus greatly improve the flexibility and reuse of your agent type.

You can study the usage of generic parameters by the example of Process Modeling Library objects as nearly all of them have generic parameters. Let’s study, for instance, the Delay object. This object is used to model how agents passing through this flowchart block are delayed for some time. Let’s assume you have a model, where all agents are of the same Customer type (inherited from the Agent base type) with some custom fields. Assume you need to perform some actions in the On exit parameter to store the delay time of this Delay in the Customer’s serviceTime field. However, although the agents in the process flow will be of the Customer type, the objects in the flowchart will continue treating them as Agent and will not allow you to explicitly address the additional functionality of Customer. But you do not want to cast your agents explicitly to the original type each time you want to access some custom fields of transient agents.

In former times, you had to write ((Customer)agent).serviceTime = delayTime in that case. And in the same manner you had to cast the agent to its original type every time you accessed custom agent’s fields in the object’s dynamic parameters. Now you can simply specify Customer in the Agent type property of Delay and type agent.serviceTime = delayTime. By specifying the agent type for Delay, you tell the object that the agent variable accessible in its dynamic parameters is of the Customer type and you can access its custom fields directly.

If agents of various types pass through the same flowchart block, say, some of the Customer type and some of the Staff type, you should leave the Agent type default value (Agent) and analyze the type manually by writing e.g.:
if (agent instanceof Customer) ((Customer)agent).serviceTime = delayTime.

To make your agents parameterized

  1. Go to the Advanced Java properties section of the agent type, where you want to use generic parameters, select the Parameterized type checkbox and specify the list of generic parameter type names in the Type parameters field to the right, say:
    T, T1.
  2. Whenever you need generic parameters inside this agent, specify your generic parameter types instead of well-defined Java class names. Commonly, parameters and function arguments of generic types are declared.
  3. For all instances of this agent type, go to the Properties view and specify the list of actual Java class names that will substitute the corresponding generic parameter types in this agent instance, in the Generic parameters field, say, Teller, Customer. The actual parameter types need to be provided in the same order as you did with generics: in the described example all parameters of the T type in this object instance will be treated as Teller, and all parameters of type T1 — as Customer.
How can we improve this article?