Class ServiceWithResources<T,R>

Type Parameters:
T - type of units being serviced
R - type of resources

public class ServiceWithResources<T,R> extends FixedCapacityService<T>
A Service that has the list of resources used to process some units. A unit is always processed by one resource. The number of resources defines the maximum number of units that can be processed simultaneously. All other units are placed into the waiting queue.

There are the following default rules that define how the resources are seized:

A custom resource selection rule can be specified by passing a lambda into the constructor.
Author:
Vitaliy Chernenko
  • Constructor Details

    • ServiceWithResources

      public ServiceWithResources(Engine engine, List<R> resources, ServiceWithResources.ResourceSeizingRule rule)
      Creates a new instance of the ServiceWithResources class with one of the standard resource seizing rules.
      Parameters:
      engine - an instance of Engine that runs the simulation model which this service belongs to
      resources - list of resources, must be non-empty, must not contain nulls
      rule - one of the standard resource selection rules defined in the ServiceWithResources.ResourceSeizingRule enumeration
    • ServiceWithResources

      public ServiceWithResources(Engine engine, List<R> resources, Function<List<R>,R> resourceSeizingRule)
      Creates a new instance of the ServiceWithResources class with a custom resource seizing rule passed in the form of a lambda.
      Parameters:
      engine - an instance of Engine that runs the simulation model which this service belongs to
      resources - list of resources, must be non-empty, must not contain nulls
      resourceSeizingRule - rule of selecting one resource out of several available resources to process every next unit. The rule:
    • can rely on the list being always non-empty, and
    • must always return one of the resources in the list passed to the lambda
    • ServiceWithResources

      public ServiceWithResources(Engine engine, List<R> resources, BiFunction<List<R>,T,R> resourceSeizingRule)
      Creates a new instance of the ServiceWithResources class with a custom resource seizing rule passed in the form of a lambda.
      Parameters:
      engine - an instance of Engine that runs the simulation model which this service belongs to
      resources - list of resources, must be non-empty, must not contain nulls
      resourceSeizingRule - rule of selecting one resource out of several available resources to process every next unit. The rule:
    • can rely on the list being always non-empty, and
    • must always return one of the resources in the list passed to the lambda
  • Method Details

    • placeRequest

      public void placeRequest(T unit, Consumer<Service<T>> onServiceStarted)
      Description copied from class: Service
      Places the request for service of the specified unit. Such placement can result in one of the two following outcomes:
      1. If the specified unit can start being served immediately, the onServiceStarted callback is called immediately and the specified unit goes into the list of served units - Service.getServicedUnits().
      2. If the specified unit cannot start being served immediately, the unit goes into the list of waiting units - Service.getWaitingUnits().
      Overrides:
      placeRequest in class FixedCapacityService<T>
      Parameters:
      unit - unit that request the service
      onServiceStarted - callback called when the unit start being served. Can be called immediately if no queuing is needed
    • placeRequest

      public void placeRequest(T unit, BiConsumer<Service<T>,R> onServiceStarted)
      Places the request for service of the specified unit and allows to handle resource used to service the unit in the callback. See placeRequest(Object, Consumer) documentation for more detailed logic description.
      Parameters:
      unit - unit that request the service
      onServiceStarted - callback called when the unit starts being served. Resource used to service the unit is passed into the second argument of the callback. Can be called immediately if no queuing is needed
    • getTotalResourceBusyTime

      public double getTotalResourceBusyTime(R resource)
      Returns the total duration the specified resource has been seized. Includes the time the resource has been servicing its current unit, if any.
      Parameters:
      resource - specified resource
      Returns:
      total duration the specified resource has been seized so far
    • getResources

      public List<R> getResources()
      Returns a non-empty unmodifiable list of resources that are used to service the units. The number of these resources defines the number of units that can be processed simultaneously.
      Returns:
      an unmodifiable list of resources
    • isResourceAvailable

      public boolean isResourceAvailable(R resource)
      Checks if the specified resource is currently available, i.e. not seized.

      If the specified resource is not contained in the getResources() list, returns true

      Parameters:
      resource - specified resource
      Returns:
      true if the specified resource is currently available, false otherwise
    • isResourceSeized

      public boolean isResourceSeized(R resource)
      Checks if the specified resource is currently seized and thus is servicing some unit.

      If the specified resource is not contained in the getResources() list, returns false

      Parameters:
      resource - specified resource
      Returns:
      true if the specified resource is currently seized, false otherwise
    • getServicingResource

      public R getServicingResource(T unit)
      Returns a resource that is servicing the specified unit. If the specified unit is not currently serviced, returns null.
      Parameters:
      unit - specified unit
      Returns:
      resource that is servicing the specified unit, or null
    • release

      public void release(T unit)
      Description copied from class: Service
      Finishes the service of the specified unit, i.e. releases the "server" used to serve this unit. Removes the specified unit from Service.getServicedUnits() collection.

      After the unit finishes its service, it is likely that service of some other unit starts if there are units waiting for service.

      If the specified unit is not currently being served, throws a RuntimeException.

      Overrides:
      release in class FixedCapacityService<T>
      Parameters:
      unit - unit that finishes being served