AnyLogic
Expand
Font size

Resource functions

If you want to use the resource API to retrieve information about resources and modify them in any way, it is recommended you create a custom resource type for that purpose.

The easiest way to access the seized resource units is to use the specific actions of the Process Modeling Library blocks that deal with resources. For example, in the Service block you can access the just seized resource unit in the On seize unit action as unit. The other actions providing access to the seized resource units in this block are On task suspended, On task resumed, On task terminated.

When addressing a resource unit via the local variable (for example, in the On start action of the Downtime block), you need to typecast it to the actual type of the resource unit.

Consider the given model which has the coffeeMachine resource pool, containing the resource unit of the CoffeeMachine type.

Demo model: Maintenance of a Coffee Machine Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

The resource unit is accessed in this model not only using the local variable unit mentioned above, but also from the block that does not provide built-in access to resources. The Delay block fillInWater accesses the resource unit via the iterator in its On exit action:

Agent unit = coffeeMachine.iterator().next();

To work with the resource unit, you will need to cast its type. In this example, the code would be the following:

CoffeeMachine unit = (CoffeeMachine)coffeeMachine.iterator().next();

Upon calling and typecasting the resource unit, we can use any of the functions available to resources operating in the Process Modeling Library and Material Handling Library flowcharts.

The following functions return unmodifiable lists of resources. You can access individual resource units using iterators (see the example of iterator usage above).

  • To list the resources seized by an agent:
    agent.resourceUnits();
  • To list resource units seized by an agent in the service Service block:
    service.getResourceUnits(agent);
Resource info
Function Description
ResourcePool resourcePool() Returns the ResourcePool block this unit belongs to.
ResourceType getResourceType() Returns the type of resource (RESOURCE_STATIC, RESOURCE_MOVING, RESOURCE_PORTABLE).
boolean isBusy() Tests if the resource unit is currently busy: seized, moving to usage location or home location. Returns true if the unit is busy, otherwise false.
boolean isIdle() Tests if the resource unit is currently idle and ready to be used. Returns true if the unit is idle, otherwise false.
boolean isReserved() Returns true if this unit is reserved by some request.
Agent getReservedBy() Returns the agent which reserved this unit or null (if this unit is not currently reserved, see isReserved()).
Agent getServicedEntity() Returns the agent which currently possesses this resource unit. Returns null if this unit is either idle or is busy with some not agent-related task (e.g. resource maintenance).
Utilization statistics
Function Description
double getUtilization() Returns the unit utilization: the fraction of time the unit was busy. The returned unit utilization value lies in the range [0..1]. If the availability of the resource unit is defined in the ResourcePool block by a schedule, utilization will be calculated only for the operating hours of the resource unit.
double timeInState(ResourceUsageState state) Returns the time (in model time units) the unit has spent in the given “usage state” so far.

state — the state (ResourceUsageState.USAGE_IDLE or ResourceUsageState.USAGE_BUSY).
double timeInState(ResourceUsageState state, TimeUnits units) Returns the time the unit has spent in the given “usage state” so far (in the specified time units).

state — the state (ResourceUsageState.USAGE_IDLE or ResourceUsageState.USAGE_BUSY).
units — a constant defining time units.
void resetStats() Resets the unit utilization statistics, including the time it spent in different states.
Maintenance, failures, breaks, and custom task statistics
Function Description
double totalDowntime() Returns the sum of times (in model time units) that the resource unit has spent in downtime of any type.
double totalDowntime(TimeUnits units) Returns the sum of times (in specified time units) that the resource unit has spent in downtime of any type.

units — a constant defining time units
double totalRepairTime() Returns the sum of times (in model time units) that the resource unit has spent in downtime on repairs.
double totalRepairTime(TimeUnits units) Returns the sum of times (in specified time units) that the resource unit has spent in downtime on repairs.

units — a constant defining time units
double totalMaintenanceTime() Returns the sum of times (in model time units) that the resource unit has spent in downtime on maintenance.
double totalMaintenanceTime(TimeUnits units) Returns the sum of times (in specified time units) that the resource unit has spent in downtime on maintenance.

units — a constant defining time units
double totalBreaksTime() Returns the sum of times (in model time units) that the resource unit has spent in downtime on breaks.
double totalBreaksTime(TimeUnits units) Returns the sum of times (in specified time units) that the resource unit has spent in downtime on breaks.

units — a constant defining time units
double totalCustomTasksTime() Returns the sum of times (in model time units) that the resource unit has spent in downtime on custom tasks.
double totalCustomTasksTime(TimeUnits units) Returns the sum of times (in specified time units) that the resource unit has spent in downtime on custom tasks.

units — a constant defining time units
Attachment
Function Description
void setAttached(boolean attached) Attaches or detaches a resource unit to/from an agent that possesses it. May be called only for units that are seized by agents, otherwise an error will be thrown.

attached — pass true value to attach; false — to detach.
boolean isAttached() Tests if the resource unit is attached to the agent. Returns true if the unit is attached, otherwise returns false.
Task
Function Description
ResourceTaskType currentTaskType() Returns the type of current task, or null if the unit is idle.

Valid values:
TASK_ENTITY — the task executed during the period when the unit is seized by the agent.
TASK_END_OF_SHIFT — unit is between working shifts according to the schedule.
TASK_WRAP_UP — a wrap-up task executed after the unit has completed the main task and has been released by the agent.
TASK_BREAK — the unit is on break.
TASK_REPAIR — a task is specified as repairs. TASK_MAINTENANCE — a scheduled maintenance task.
TASK_CUSTOM — a custom task from the list  specified in Downtime block.
ResourceUnitTask currentTask() Returns the current task of the unit, or null if the unit is idle. The tasks are defined as classes, and the function returns the instance of the corresponding class.
Please note that there is a special task when the unit’s shift ends.

Valid values:
ResourceUnitBasicTask — either the unit is on break, or it’s executing the repairs task or a scheduled maintenance task.
ResourceUnitEntityServiceSubtask — the unit is executing a task while being seized by the agent.
ResourceUnitCustomTask — the unit is executing a custom task from the list specified in Downtime block.
ResourceUnitEndOfShiftTask — the unit is between shifts according to the schedule and doesn’t execute any actual tasks.
ResourceUnitWrapUpTask — the unit is executing a task set for execution after the unit has completed the main task and has been released by the agent.
Home location
Function Description
void setHomeLocation(INode home) Sets the home location for a resource unit.

home — the home node.
INode getHomeLocation() Returns a network node that is set as the home location for this resource unit.
void setHomePosition(double x, double y) Sets the coordinates of the resource unit’s home position.

x — the X-coordinate.
y — the Y-coordinate.
void setHomePosition(double x, double y, double z) Sets the coordinates of the resource unit’s home position.

x — the X-coordinate.
y — the Y-coordinate.
z — the Z-coordinate.
double getHomeX() Returns the X-coordinate of the resource unit’s home position.
double getHomeY() Returns the Y-coordinate of the resource unit’s home position.
double getHomeZ() Returns the Z-coordinate of the resource unit’s home position.
double getHomeRotation() Returns the rotation angle of the resource unit’s home position.
How can we improve this article?