Class 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
    • 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,
                        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 class ModelElement
      • 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.
        Specified by:
        seize in interface SeizeIfc
        Parameters:
        request -
      • 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 class ModelElement
        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: