Package jsl.modeling.elements.entity
Class Resource
- java.lang.Object
-
- jsl.simulation.ModelElement
-
- jsl.simulation.SchedulingElement
-
- jsl.modeling.elements.entity.Resource
-
- All Implemented Interfaces:
SeizeIfc
,ObservableIfc
,GetNameIfc
,IdentityIfc
- Direct Known Subclasses:
MobileResource
,SpatialResource
public class Resource extends SchedulingElement implements SeizeIfc
A Resource has a given capacity of units that can be requested and allocated. A Resource can be in any of four states (Busy, Idle, Failed, Inactive). A resource is in the failed state if it is not in the other states and has become unavailable because of a failure. A resource is in the inactive state if it is not in the other states and has become unavailable because of a capacity change. A resource is in the idle state if it is not in the other states and has (some) units available for use. A resource is in the busy state if it is not in the other states and it has no available units for use. Units of a resource are allocated to an instance of Entity via the allocate() method. When units of a resource are allocated, an instance of an Allocation is created. An allocation notes that an entity has so many units of a resource. When all the units of a resource have been allocated, it is considered busy (as a whole). If it is failed, it is considered failed as a whole, i.e. no units are available. If it is inactive, it is considered inactive as a whole (i.e. no units are available). Units may be obtained from a resource via the seize() method. If enough units are available to fill the request the required number of units are allocated to the request. If the request cannot be totally filled, it waits until additional units become available. The default waiting protocol is determined by the priority of the request and the order of seize attempts. A resource cannot allocate more units than its currently available capacity. Resources are intimately tied to the Request class. If units of a resource are allocated, they are associated with the request. The only way to return the units of the resource is to use the instance of Request and the release() methods of Resource. An instance of a Request can only be using the units of one resource at a time. A request might not have all of its requested units allocated, see allocate(Request, int) because of partial filling. NOTE: failed and inactive are not currently implemented within this package
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jsl.simulation.ModelElement
ModelElement.EventBuilderIfc<T>, ModelElement.EventScheduler<T>, ModelElement.ModelElementComparator, ModelElement.RequestBuilder, ModelElement.RequestDurationIfc, ModelElement.RequestTimeIfc, ModelElement.RequestUsingIfc, ModelElement.TimedUpdateEventAction, ModelElement.TimeUnitIfc<T>, ModelElement.WarmUpEventAction
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<Allocation>
myAllocations
The allocations that currently using this resourceprotected java.util.Set<ResourceSet>
myResourceSets
The set of resource sets currently holding this resourceprotected OrderedList<Request>
myWaitingRequests
Holds requests that are waiting for some units of the resource-
Fields inherited from class jsl.simulation.ModelElement
AFTER_EXPERIMENT, AFTER_REPLICATION, BEFORE_EXPERIMENT, BEFORE_REPLICATION, CONDITIONAL_ACTION_REGISTRATION, DEFAULT_TIMED_EVENT_PRIORITY, INITIALIZED, MONTE_CARLO, myAfterExperimentOption, myAfterReplicationOption, myBeforeExperimentOption, myBeforeReplicationOption, myInitializationOption, myLengthOfWarmUp, myModelElements, myMonteCarloOption, myObservableComponent, myReplicationEndedOption, myTimedUpdateActionListener, myTimedUpdateEvent, myTimedUpdateInterval, myTimedUpdateOption, myTimedUpdatePriority, myWarmUpActionListener, myWarmUpEvent, myWarmUpIndicator, myWarmUpOption, myWarmUpPriority, NONE, REMOVED_FROM_MODEL, REPLICATION_ENDED, TIME_UNIT_DAY, TIME_UNIT_HOUR, TIME_UNIT_MILLISECOND, TIME_UNIT_MINUTE, TIME_UNIT_SECOND, TIME_UNIT_WEEK, TIMED_UPDATE, UPDATE, WARMUP
-
-
Constructor Summary
Constructors Constructor Description Resource(ModelElement parent)
Resource(ModelElement parent, int initialCapacity)
Resource(ModelElement parent, int initialCapacity, java.lang.String name)
Resource(ModelElement parent, java.lang.String name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addAllocation(Allocation a)
protected void
addWaitingRequest(Request request)
Adds the request to the list of waiting requests based on the Comparable interface for RequestAllocation
allocate(Entity entity)
Allocates one unit to the supplied entity.Allocation
allocate(Entity entity, int amtToAllocate)
Allocates the amount indicated to the supplied entity.java.lang.String
asString()
Allows sub-classes to provide more detail than toString() to represent the ModelElement as a Stringprotected void
attachResourceSet(ResourceSet set)
void
attachStateChangeListener(ResourceStateChangeListenerIfc listener)
protected void
decrementNumberBusy(int amt)
protected void
detachResourceSet(ResourceSet set)
void
detachStateChangeListener(ResourceStateChangeListenerIfc listener)
int
getCapacity()
The current capacity of the resource.int
getInitialCapacity()
Gets the initial capacity of the resourceint
getMaximumCapacity()
Gets the initial capacity of the resourceint
getNumberAvailable()
Returns the number of units that are currently available for useboolean
hasAvailableUnits()
Returns true if getNumberAvailable() > 0protected void
incrementNumberBusy(int amt)
protected void
initialize()
This method should be overridden by subclasses that need actions performed to initialize prior to a replication.boolean
isBusy()
Checks to see if the resource is busyboolean
isFailed()
Checks if the resource is failedboolean
isIdle()
Checks if the resource is idle.boolean
isInactive()
Checks to see if the resource is inactiveprotected void
notifyResourceSets()
protected void
notifyStateChangeListeners()
protected void
partiallyAllocate(Request request)
Sub-classes can implement a mechanism to give a partially fillable request, some units of the resource.protected Request
peekNextRequest()
Returns the next request without removing itprotected void
processWaitingRequests()
Processes any waiting requests.void
release(Allocation a)
Releases allocation, calls release(a.getEntity(), a.getAmountAllocated())Allocation
release(Entity entity)
Releases all of the units of this resource that were previously allocated to the supplied entity.Allocation
release(Entity entity, int releaseAmount)
Releases the specified number of units of this resource that were previously allocated to the supplied entity.protected void
removeAllocation(Allocation a)
protected Request
removeNextRequest()
Removes the next request from the waiting list of requestsprotected void
removeWaitingRequest(Request request)
Removes the request from the list of waiting requestsRequest
seize(Entity entity, int amtNeeded, int priority, boolean partialFillFlag, AllocationListenerIfc listener)
Seizes the resource and associates the entity and listener with the requestRequest
seize(Entity entity, int amtNeeded, int priority, AllocationListenerIfc listener)
Seizes the resource and associates the entity and listener with the request.Request
seize(Entity entity, int amtNeeded, AllocationListenerIfc listener)
Seizes the resource and associates the entity and listener with the request.Request
seize(Entity entity, AllocationListenerIfc listener)
Seizes the resource and associates the entity and listener with the request.void
seize(Request request)
Seizes the resource using the request.void
setInitialCapacity(int capacity)
Sets the initial capacity of the resource.protected void
setMaximumCapacity(int capacity)
Sets the initial capacity of the resource.protected void
setState(ModelElementState state)
-
Methods inherited from class jsl.simulation.SchedulingElement
cancelEvent, rescheduleEvent, rescheduleEvent, rescheduleEvent, rescheduleEvent, scheduleEvent, scheduleEvent, scheduleEvent, scheduleEvent, scheduleEvent, scheduleEvent, scheduleEvent, scheduleEvent, scheduleEvent, scheduleEvent
-
Methods inherited from class jsl.simulation.ModelElement
addObserver, addObserverToModelElements, afterExperiment, afterExperiment_, afterReplication, afterReplication_, beforeExperiment, beforeExperiment_, beforeReplication, beforeReplication_, buildEventGenerator, cancelTimedUpdateEvent, cancelWarmUpEvent, changeParentModelElement, checkForAfterExperiment, checkForAfterReplication, checkForBeforeExperiment, checkForBeforeReplication, checkForConditionalActionRegistration, checkForInitialize, checkForMonteCarlo, checkForRemoveFromModel, checkForReplicationEnded, checkForTimedUpdate, checkForUpdate, checkForWarmUp, checkObserverState, checkWarmUpOption, clearModelElementObservers, contains, countObservers, createEntity, createEntity, createQObject, day, deleteObserver, deleteObserverFromModelElements, deleteObservers, findModelElementWithWarmUpEvent, getAfterExperimentOption, getAfterReplicationOption, getAllCounters, getAllModelElements, getAllRandomElements, getAllResponseVariables, getAllVariables, getBeforeExperimentOption, getBeforeReplicationOption, getChildModelElementIterator, getCurrentReplicationNumber, getDefaultEntityType, getExecutive, getExperiment, getId, getInitializationOption, getInitializationOrderAsString, getLeftPreOrderTraversalCount, getLengthOfWarmUp, getModel, getModelElementCompartor, getModelElementsAsString, getModelElementsAsString, getModelElementsAsString, getMonteCarloOption, getName, getNumberOfModelElements, getObserverState, getParentModelElement, getPreviousObserverState, getReplicationEndedOption, getRightPreOrderTraversalCount, getSimulation, getSpatialModel, getStringLabel, getThisElementsCounters, getThisElementsModelElements, getThisElementsRandomVariables, getThisElementsResponseVariables, getThisElementsVariables, getTime, getTimedUpdateInterval, getTimedUpdateOption, getUpdateNotificationFlag, getVariable, getWarmUpEventTime, getWarmUpOption, hour, indent, initialize_, isTimedUpdateEventScheduled, isWarmedUp, isWarmUpEventScheduled, isWarmUpScheduled, millisecond, minute, montecarlo, montecarlo_, notifyAfterExperimentObservers, notifyAfterReplicationObservers, notifyBeforeExperimentObservers, notifyBeforeReplicationObservers, notifyConditionalActionRegistrationObservers, notifyInitializationObservers, notifyMonteCarloObservers, notifyObservers, notifyObservers, notifyRemovingFromModelObservers, notifyReplicationEndedObservers, notifyTimedUpdateObservers, notifyUpdateObservers, notifyWarmUpObservers, registerConditionalActions, registerConditionalActions_, removedFromModel, removeFromModel, replicationEnded, replicationEnded_, schedule, second, seize, setAfterExperimentOption, setAfterExperimentOptionForModelElements, setAfterReplicationOption, setAfterReplicationOptionForModelElements, setBeforeExperimentOption, setBeforeExperimentOptionForModelElements, setBeforeReplicationOption, setBeforeReplicationOptionForModelElements, setInitializationOption, setInitializationOptionForModelElements, setLengthOfWarmUp, setModel, setMonteCarloOption, setMonteCarloOptionForModelElements, setName, setObserverState, setParentModelElement, setReplicationEndedOption, setReplicationEndedOptionForModelElements, setSpatialModel, setStringLabel, setTimedUpdateInterval, setTimedUpdateOption, setTimedUpdateOptionForModelElements, setUpdateNotificationFlag, setWarmUpOption, setWarmUpOptionForModelElements, stopExecutive, stopExecutive, timedUpdate, timedUpdate_, toString, update, useControls, warmUp, warmUp_, week
-
-
-
-
Field Detail
-
myAllocations
protected java.util.List<Allocation> myAllocations
The allocations that currently using this resource
-
myWaitingRequests
protected OrderedList<Request> myWaitingRequests
Holds requests that are waiting for some units of the resource
-
myResourceSets
protected java.util.Set<ResourceSet> myResourceSets
The set of resource sets currently holding this resource
-
-
Constructor Detail
-
Resource
public Resource(ModelElement parent)
-
Resource
public Resource(ModelElement parent, java.lang.String name)
-
Resource
public Resource(ModelElement parent, int initialCapacity)
-
Resource
public Resource(ModelElement parent, int initialCapacity, java.lang.String name)
-
-
Method Detail
-
initialize
protected void initialize()
Description copied from class:ModelElement
This method should be overridden by subclasses that need actions performed to initialize prior to a replication. It is called once before each replication occurs if the model element wants initialization. It is called after beforeReplication() is called- Overrides:
initialize
in classModelElement
-
detachStateChangeListener
public void detachStateChangeListener(ResourceStateChangeListenerIfc listener)
-
attachStateChangeListener
public void attachStateChangeListener(ResourceStateChangeListenerIfc listener)
-
setState
protected void setState(ModelElementState state)
-
notifyResourceSets
protected void notifyResourceSets()
-
notifyStateChangeListeners
protected void notifyStateChangeListeners()
-
attachResourceSet
protected final void attachResourceSet(ResourceSet set)
-
detachResourceSet
protected final void detachResourceSet(ResourceSet set)
-
addAllocation
protected void addAllocation(Allocation a)
-
removeAllocation
protected void removeAllocation(Allocation a)
-
incrementNumberBusy
protected void incrementNumberBusy(int amt)
-
decrementNumberBusy
protected void decrementNumberBusy(int amt)
-
setMaximumCapacity
protected void setMaximumCapacity(int capacity)
Sets the initial capacity of the resource. This only changes it for when the resource is initialized.- Parameters:
capacity
-
-
allocate
public Allocation allocate(Entity entity)
Allocates one unit to the supplied entity. Conditions for using this method include: 1) entity must not be null 2) getNumberAvailable() must be > 0- Parameters:
entity
-- Returns:
- The Allocation that associates the entity with this resource
-
allocate
public Allocation allocate(Entity entity, int amtToAllocate)
Allocates the amount indicated to the supplied entity. Conditions for using this method include: 1) entity must not be null 2) the amount to allocate must be > 0 3) the amount to allocate must be <= getNumberAvailable()- Parameters:
entity
-amtToAllocate
-- Returns:
- The Allocation that associates the entity with this resource
-
addWaitingRequest
protected void addWaitingRequest(Request request)
Adds the request to the list of waiting requests based on the Comparable interface for Request- Parameters:
request
-
-
removeWaitingRequest
protected void removeWaitingRequest(Request request)
Removes the request from the list of waiting requests- Parameters:
request
-
-
peekNextRequest
protected Request peekNextRequest()
Returns the next request without removing it- Returns:
-
removeNextRequest
protected Request removeNextRequest()
Removes the next request from the waiting list of requests- Returns:
-
partiallyAllocate
protected void partiallyAllocate(Request request)
Sub-classes can implement a mechanism to give a partially fillable request, some units of the resource. The default is not to partially fill. In other words, nothing happens in this method. Implementors must ensure: 1) the resource that request can eventually be satisfied by the resource 2) the same resource is used to give all desired units to the request- Parameters:
request
-
-
seize
public final void seize(Request request)
Seizes the resource using the request. Conditions: 1) request must not be null 2) request.getEntity() must not be null 3) The request must not have been seized with another resource 4) The request must have a ResourceAllocationListener attached.
-
seize
public final Request seize(Entity entity, AllocationListenerIfc listener)
Seizes the resource and associates the entity and listener with the request. No partial filling, 1 unit, default seize priority- Parameters:
entity
-listener
-- Returns:
-
seize
public final Request seize(Entity entity, int amtNeeded, int priority, AllocationListenerIfc listener)
Seizes the resource and associates the entity and listener with the request. No partial filling- Parameters:
entity
-amtNeeded
-priority
-listener
-- Returns:
-
seize
public final Request seize(Entity entity, int amtNeeded, AllocationListenerIfc listener)
Seizes the resource and associates the entity and listener with the request. Default seize priority and no partial filling- Parameters:
entity
-amtNeeded
-listener
-- Returns:
-
seize
public final Request seize(Entity entity, int amtNeeded, int priority, boolean partialFillFlag, AllocationListenerIfc listener)
Seizes the resource and associates the entity and listener with the request- Parameters:
entity
-amtNeeded
-priority
-partialFillFlag
-listener
-- Returns:
-
release
public final void release(Allocation a)
Releases allocation, calls release(a.getEntity(), a.getAmountAllocated())- Parameters:
a
-
-
release
public final Allocation release(Entity entity)
Releases all of the units of this resource that were previously allocated to the supplied entity. 1) The supplied entity must not be null. 2) The supplied entity must have units of this resource allocated to it- Parameters:
entity
-- Returns:
-
release
public final Allocation release(Entity entity, int releaseAmount)
Releases the specified number of units of this resource that were previously allocated to the supplied entity. 1) The supplied entity must not be null. 2) The supplied entity must have units of this resource allocated to it 3) The release amount must be > 0 and <= the amount allocated to the entity- Parameters:
entity
-releaseAmount
-- Returns:
-
processWaitingRequests
protected void processWaitingRequests()
Processes any waiting requests.
-
asString
public java.lang.String asString()
Description copied from class:ModelElement
Allows sub-classes to provide more detail than toString() to represent the ModelElement as a String- Overrides:
asString
in classModelElement
- Returns:
- a detailed String representation
-
isIdle
public final boolean isIdle()
Checks if the resource is idle.- Returns:
- true if idle, false otherwise
-
isBusy
public final boolean isBusy()
Checks to see if the resource is busy- Returns:
-
isFailed
public final boolean isFailed()
Checks if the resource is failed- Returns:
- true if idle, false otherwise
-
isInactive
public final boolean isInactive()
Checks to see if the resource is inactive- Returns:
-
getMaximumCapacity
public final int getMaximumCapacity()
Gets the initial capacity of the resource- Returns:
-
getInitialCapacity
public final int getInitialCapacity()
Gets the initial capacity of the resource- Returns:
-
setInitialCapacity
public void setInitialCapacity(int capacity)
Sets the initial capacity of the resource. This only changes it for when the resource is initialized.- Parameters:
capacity
-
-
getCapacity
public final int getCapacity()
The current capacity of the resource. The maximum number of units that are currently scheduled as active- Returns:
-
getNumberAvailable
public final int getNumberAvailable()
Returns the number of units that are currently available for use- Returns:
-
hasAvailableUnits
public final boolean hasAvailableUnits()
Returns true if getNumberAvailable() > 0- Returns:
-
-