Package com.amalgamasimulation.service
Class ServiceWithResources<T,R>
java.lang.Object
com.amalgamasimulation.service.Service<T>
com.amalgamasimulation.service.FixedCapacityService<T>
com.amalgamasimulation.service.ServiceWithResources<T,R>
- Type Parameters:
T- type of units being servicedR- type of resources
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:
ServiceWithResources.ResourceSeizingRule.FIRST_AVAILABLE- the first available resource gets seized to service every next unit. Using this rule can lead to a significant disproportion in utilization of the resources.ServiceWithResources.ResourceSeizingRule.LEAST_USED- the resource that spent seized the least time is seized to service every next unit. This rule statistically leads to level-loading the utilization of all resources.
- Author:
- Vitaliy Chernenko
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class com.amalgamasimulation.service.Service
servicedUnits, serviceRequestedCallbacks, serviceRequestRemovedCallbacks, serviceStartedCallbacks, serviceStartedCallbacksByRequests -
Constructor Summary
ConstructorsConstructorDescriptionServiceWithResources(Engine engine, List<R> resources, ServiceWithResources.ResourceSeizingRule rule) Creates a new instance of theServiceWithResourcesclass with one of the standard resource seizing rules.ServiceWithResources(Engine engine, List<R> resources, BiFunction<List<R>, T, R> resourceSeizingRule) Creates a new instance of theServiceWithResourcesclass with a custom resource seizing rule passed in the form of a lambda.Creates a new instance of theServiceWithResourcesclass with a custom resource seizing rule passed in the form of a lambda. -
Method Summary
Modifier and TypeMethodDescriptionReturns a non-empty unmodifiable list of resources that are used to service the units.getServicingResource(T unit) Returns a resource that is servicing the specified unit.doublegetTotalResourceBusyTime(R resource) Returns the total duration the specified resource has been seized.booleanisResourceAvailable(R resource) Checks if the specified resource is currently available, i.e.booleanisResourceSeized(R resource) Checks if the specified resource is currently seized and thus is servicing some unit.voidplaceRequest(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.voidplaceRequest(T unit, Consumer<Service<T>> onServiceStarted) Places the request for service of the specified unit.voidFinishes the service of the specified unit, i.e.Methods inherited from class com.amalgamasimulation.service.FixedCapacityService
cancelRequest, getCapacityMethods inherited from class com.amalgamasimulation.service.Service
addServiceRequestRemovedCallback, addServiceStartedHandler, addWaitingUnit, addWaitingUnitArrivedHandler, getServicedUnits, getWaitingUnits, removeServiceRequestRemovedCallback, removeServiceStartedHandler, removeWaitingUnit, removeWaitingUnitArrivedHandler, takeNext
-
Constructor Details
-
ServiceWithResources
public ServiceWithResources(Engine engine, List<R> resources, ServiceWithResources.ResourceSeizingRule rule) Creates a new instance of theServiceWithResourcesclass with one of the standard resource seizing rules.- Parameters:
engine- an instance ofEnginethat runs the simulation model which this service belongs toresources- list of resources, must be non-empty, must not containnullsrule- one of the standard resource selection rules defined in theServiceWithResources.ResourceSeizingRuleenumeration
-
ServiceWithResources
public ServiceWithResources(Engine engine, List<R> resources, Function<List<R>, R> resourceSeizingRule) Creates a new instance of theServiceWithResourcesclass with a custom resource seizing rule passed in the form of a lambda.- Parameters:
engine- an instance ofEnginethat runs the simulation model which this service belongs toresources- list of resources, must be non-empty, must not containnullsresourceSeizingRule- 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 theServiceWithResourcesclass with a custom resource seizing rule passed in the form of a lambda.- Parameters:
engine- an instance ofEnginethat runs the simulation model which this service belongs toresources- list of resources, must be non-empty, must not containnullsresourceSeizingRule- 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
Description copied from class:ServicePlaces the request for service of the specified unit. Such placement can result in one of the two following outcomes:- 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(). - If the specified unit cannot start being served immediately, the unit
goes into the list of waiting units -
Service.getWaitingUnits().
- Overrides:
placeRequestin classFixedCapacityService<T>- Parameters:
unit- unit that request the serviceonServiceStarted- callback called when the unit start being served. Can be called immediately if no queuing is needed
- 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 -
-
placeRequest
Places the request for service of the specified unit and allows to handle resource used to service the unit in the callback. SeeplaceRequest(Object, Consumer)documentation for more detailed logic description.- Parameters:
unit- unit that request the serviceonServiceStarted- 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
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
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
Checks if the specified resource is currently available, i.e. not seized.If the specified resource is not contained in the
getResources()list, returnstrue- Parameters:
resource- specified resource- Returns:
trueif the specified resource is currently available,falseotherwise
-
isResourceSeized
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, returnsfalse- Parameters:
resource- specified resource- Returns:
trueif the specified resource is currently seized,falseotherwise
-
getServicingResource
Returns a resource that is servicing the specified unit. If the specified unit is not currently serviced, returnsnull.- Parameters:
unit- specified unit- Returns:
- resource that is servicing the specified unit, or
null
-
release
Description copied from class:ServiceFinishes the service of the specified unit, i.e. releases the "server" used to serve this unit. Removes the specified unit fromService.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:
releasein classFixedCapacityService<T>- Parameters:
unit- unit that finishes being served
-