Resource

open class Resource @JvmOverloads constructor(parent: ModelElement, name: String? = null, capacity: Int = 1) : ProcessModel, ResourceCIfc(source)

A Resource represents a number of common units that can be allocated to entities. A resource has an initial capacity that can be changed during a replication.

A resource is busy if at least 1 unit has been allocated. A resource becomes busy when it has allocations. If a seize-request occurs and the resource has capacity to fill it completely, then the request is allocated the requested amount. If insufficient capacity is available at the time of the request, then the request waits until the requested units can be allocated.

If b(t) is the number of units allocated at time t, and c(t) is the current capacity of the resource, then the number of available units, a(t), is defined as a(t) = c(t) - b(t). Thus, a resource is idle if a(t) = c(t). Since a resource is busy if b(t) > 0, busy and idle are complements of each other. A resource is either busy b(t) > 0 or idle b(t) = 0. If a(t) > 0, then the resource has units that can be allocated.

The utilization of a resource is defined as the ratio of the average number of busy units to the average number of active units. Units are active if they are part of the current capacity of the resource.

Subclasses of Resource implement additional state behavior.

Parameters

parent

the parent holding this resource

name

the name of the resource

capacity

the initial capacity of the resource. Cannot be changed during a replication. The default capacity is 1.

Inheritors

Constructors

Link copied to clipboard
constructor(parent: ModelElement, name: String? = null, capacity: Int = 1)

Types

Link copied to clipboard
inner class CapacityChangeNotice(val capacity: Int = 0, val duration: Double = Double.POSITIVE_INFINITY, val priority: Int = KSLEvent.DEFAULT_PRIORITY)
Link copied to clipboard
object Companion
Link copied to clipboard
protected inner class InactiveState(aName: String, stateStatistics: Boolean = false) : Resource.ResourceState
Link copied to clipboard
protected open inner class ResourceState(aName: String, stateStatistics: Boolean = false) : State

Properties

Link copied to clipboard

Whether each replication starts the behavior. With false, a replication starts without it and startBehavior starts it when the model wants it, so an experiment can compare runs with and without the behavior. Set while the model is not running.

Link copied to clipboard

The process of the running behavior, so that another process can interrupt its delays (for example with interruptDelay); null when no behavior is running.

Link copied to clipboard
open override val busyState: StateAccessorIfc

Access to the busy state. Busy means at least 1 unit of the resource is allocated.

Link copied to clipboard
open override var capacity: Int

The current capacity of the resource. In general, it can be 0 or greater

Link copied to clipboard

The default rule is IGNORE. This can be changed via the useSchedule() function or when there is no schedule being used. The rule cannot be changed when there are pending capacity changes.

Link copied to clipboard
open override var defaultReportingOption: Boolean

Returns the default reporting option. True means that the response should appear on the default reports

Link copied to clipboard

A resource can be allocated to 0 or more entities. An entity that is using a resource can have more than 1 allocation of the resource. The key to this map represents the entities that are using the resource. The element of this map represents the list of allocations allocated to the entity.

Link copied to clipboard
open override val failedState: StateAccessorIfc

Access to the failed state. Failed means that no units of the resource can be allocated until it is repaired. An implementation that cannot fail keeps the default, a state that is never entered.

Link copied to clipboard
open override val hasAvailableUnits: Boolean

If a(t) is greater than zero

Link copied to clipboard

True if a behavior, of either form, has been declared for this resource.

Link copied to clipboard
open override val hasBusyUnits: Boolean

If b(t) is greater than zero

Link copied to clipboard

True if failures have been declared with failures.

Link copied to clipboard
open override val idleState: StateAccessorIfc

Access to the idle state. Idle means that no units of the resource are allocated.

Link copied to clipboard
open override val inactiveState: StateAccessorIfc

Access to the inactive state. Inactive means that the capacity of the resource is 0

Link copied to clipboard
@set:KSLControl(controlType = ControlType.INTEGER, lowerBound = 0.0)
open override var initialCapacity: Int

The initial capacity of the resource at the start of the replication. The initial capacity must be greater than 0.

Link copied to clipboard

The initial request queue notification rule for controlling the order in which queues are notified for processing requests after a capacity change.

Link copied to clipboard
open override val instantaneousUtil: Double

If b(t) is the number of busy units, and c(t) is the current capacity, then the instantaneous utilization iu(t) is

Link copied to clipboard
protected val instantUtilTW: TWResponse
Link copied to clipboard

What happens to work in service when this resource's units are taken away, by a failure or by a more urgent request: FINISH (the default) lets it complete, RESUME holds the holder's delay and goes on with the time left, RESTART holds it and starts it again in full. A failure or a preemption can override it for itself. Set while the model is not running.

Link copied to clipboard

True while the behavior is running in this replication, from its activation until it ends: by returning from its block, by a stop, by the model terminating its process (as clearing the queue it waits in does), or at the end of the replication.

Link copied to clipboard

True when a stop after the current cycle has been asked for and the cycle has not yet finished.

Link copied to clipboard
open override val isBusy: Boolean

True when some units are allocated, whatever the capacity, and the resource is not failed. A resource whose capacity has just gone to zero stays busy until its allocated units are released. A failed resource is in the failed state even with units allocated (see isFailed), so it is neither busy nor idle.

Link copied to clipboard
override val isFailed: Boolean

True from a fail until the repair that follows it. Final: the resource's availability depends on it.

Link copied to clipboard
open override val isIdle: Boolean

True when no units are allocated, the capacity is greater than zero and the resource is not failed. A resource with zero capacity and nothing allocated is inactive, not idle (see isInactive).

Link copied to clipboard
open override val isInactive: Boolean

True when no units are allocated and the capacity is zero, as when a resource is off shift and its last unit has been released.

Link copied to clipboard

True while this resource has been taken off shift with goOffShift.

Link copied to clipboard

Indicates whether capacity changes are pending. The resource cannot allocate units when capacity changes are pending because released busy units will be used to fill the capacity change.

Link copied to clipboard

The number of consecutive cycles of a cyclic behavior that may end at the simulated time they began; one more is taken to be looping without waiting, and throws. Large enough for a legitimate burst, such as a cycle that takes each of many messages already waiting. Set while the model is not running; at least 1.

Link copied to clipboard
protected val myBusyProp: Response
Link copied to clipboard

The busy state, keeps track of when the resource has an allocation A resource is busy if any of its units are allocated.

Link copied to clipboard
protected val myCapacity: TWResponse
Link copied to clipboard

Tracks which queues have requests targeting the resource

Link copied to clipboard
Link copied to clipboard

The failed state, keeps track of when no units are available because the resource is failed. It takes precedence over the other three until the resource is repaired.

Link copied to clipboard
protected val myFractionBusy: Response
Link copied to clipboard
protected val myIdleProp: Response
Link copied to clipboard

The idle state keeps track of when no units have been allocated

Link copied to clipboard
protected val myInactiveProp: Response
Link copied to clipboard

The inactive state, keeps track of when no units are available because the resource's capacity is zero.

Link copied to clipboard
protected var myNoticeCount: Int
Link copied to clipboard
protected val myNumBusy: TWResponse
Link copied to clipboard
Link copied to clipboard
protected val mySeizeCounter: Counter
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open override val numAvailableUnits: Int

The units that can be allocated now: the capacity less the busy units and less any units held for a reservation still collecting them. None while the resource is failed.

Link copied to clipboard
open override var numBusy: Int

The number of busy units at any time t, b(t)

Link copied to clipboard
open override val numBusyUnits: TWResponseCIfc

Statistical response representing the number of busy units of the resource.

Link copied to clipboard

The number of preemptions of this resource in the replication: each preempt that took units from at least one holder, or reserved them from holders that finish their work.

Link copied to clipboard
open override var numTimesReleased: Int

The number of times that the resource has been released (deallocated)

Link copied to clipboard
open override var numTimesSeized: Int

The number of times that the resource has been seized (allocated)

Link copied to clipboard

When the pending capacity change ends. NaN when no change is pending (see isPendingCapacityChange). Positive infinity when the pending change lasts until another change supersedes it (a notice with an infinite duration, as shift changes use), or when its end is not yet known because, under the WAIT rule, it is still waiting for busy units to be released.

Link copied to clipboard
open override val previousState: StateAccessorIfc

The last (previous) state before the current state.

Link copied to clipboard

Changing this during a replication will only affect the current replication. The rule will be reset to the initial setting at the beginning of each replication.

Link copied to clipboard
open override val scheduledUtil: ResponseCIfc

Statistical response representing the utilization of the resource. This is the time average number of busy units divided by the time average capacity: the utilization of the capacity actually on shift.

Link copied to clipboard
open override val seizeCounter: CounterCIfc

The number of allocations made from the resource: one per allocation whatever its amount, including direct allocate() calls. It counts starts of service, not completions or throughput.

Link copied to clipboard
open override var selectionCriteria: Double

A general attribute that can be used to assist with selecting resources

Link copied to clipboard

Time when state time accumulation started. May be greater than 0 because of warmup.

Link copied to clipboard
open override val state: StateAccessorIfc

The current state of the resource.

Link copied to clipboard

Indicates if proportion of time spent in states (idle, busy, inactive) is automatically reported

Link copied to clipboard

time average instantaneous utilization (see instantaneousUtil): time with no capacity counts as zero when no unit is busy, and as one while units are still busy. Under a capacity schedule it generally differs from scheduledUtil, which divides the average busy by the average capacity, and either can be the larger.

Link copied to clipboard

Total time that the resource has been idle, busy, or inactive

Functions

Link copied to clipboard

Add an allocation listener. Allocation listeners are notified when (after) units are allocated to an entity and after units are deallocated.

Link copied to clipboard
protected open override fun afterReplication()

It is essential that subclasses that override this function call the super. This ensures that suspended entities are cleaned up after a replication

Link copied to clipboard
protected open fun afterUnitsRestored()

Called when this resource's units come back by other than a release: at the end of a capacity increase, including the one a goOnShift makes, and of a repair, and at a goOnShift that raises no capacity because the capacity was put back directly while off shift. It is called once for each, except under CapacityChangeRule.WAIT when the return to shift waits behind a decrease still in progress: then once at the return and once when the increase is made. It is not called once the run has ended. A subclass whose waiting requests are not reached by the offer to the resource's own queues offers its units here.

Link copied to clipboard
protected fun allocationNotification(allocation: Allocation)

Notifies any attached listeners when units are allocated, once the change is complete.

Link copied to clipboard
Link copied to clipboard
fun behavior(block: suspend KSLProcessBuilder.() -> Unit)

Declares a behavior whose block runs once from time 0 of every replication, for a set-up that is not a cycle, such as a delayed start. Otherwise as cyclicBehavior, which is the usual form; a block that loops by itself cannot be stopped after its current cycle or guarded against a cycle that never waits.

Link copied to clipboard
fun canAllocate(amountNeeded: Int = 1): Boolean

A resource may fill a request in many ways. This function indicates if the request for the amount needed can be allocated immediately, without any wait based on the current state of the resource. Since the underlying state of the resource may be more complex than indicated by the state exposed in the API, it is important to use this method to determine if the requested amount can be allocated.

Link copied to clipboard

Represents the actions that occur when a capacity change's duration is completed.

Link copied to clipboard

It is an error to try to change the capacity directly via this method if the resource is using a capacity change schedule.

Link copied to clipboard
protected open fun checkBehaviorAllowed()

Called by cyclicBehavior and behavior before a behavior is stored. A subclass that cannot have a behavior in some state (an agent resource with a statechart) throws here.

Link copied to clipboard

Adds two statistics for this resource's failures, whether they come from failures or from calls of fail: the proportion of time it is failed (PTimeFailed) and the number of failures in a replication (NumFailures). Without them the failed state still keeps its own times, and no element is added to the model. Called while the model is not running; a second call does nothing.

Link copied to clipboard
fun cyclicBehavior(block: suspend KSLProcessBuilder.() -> Unit)

Declares a behavior that the library repeats: block is one cycle, run again as soon as it ends, from time 0 of every replication. This is the usual form of a resource's own behavior, such as a break taken every so often:

Link copied to clipboard
protected fun deallocationNotification(allocation: Allocation)

Notifies any attached listeners when units are deallocated, once the change is complete.

Link copied to clipboard
protected open override fun defaultExcludedControlNames(): Set<String>

A resource without a behavior has nothing for behaviorActiveAtStart to switch, so it is not a control.

Link copied to clipboard
fun fail(rule: InterruptRule = interruptRule)

Fails this resource now: no unit can be allocated until repair, and the requests waiting for it go on waiting. Its holders meet rule: under FINISH they complete their work, and the units they release are kept from the waiting requests until the repair; under RESUME and RESTART all their units go missing, so a holder's delay that depends on them is held, and a delay it starts while failed begins held. The capacity is unchanged, and capacity changes and shifts still apply underneath. A resource that is already failed throws; isFailed is the check.

Link copied to clipboard
fun failures(timeBetween: RVariableIfc, repairTime: RVariableIfc, basis: ResourceFailureBasis = ResourceFailureBasis.CALENDAR_TIME, rule: InterruptRule = interruptRule)

Declares that this resource fails and is repaired on its own, in every replication: it waits out timeBetween on basis, fails under rule, stays failed for repairTime, and is repaired, over and over. A failure that comes due while the resource is already failed by a call of fail waits for that repair, and its time between failures starts again from there. A call of repair during the repair time ends that failure early, and the time to the next failure counts from that repair. The declared failures repair only a failure they started: if the model fails the resource again during what was the repair time, that failure lasts until the model repairs it. The waiting is done by an entity of the resource's own, separate from a behavior it may have.

Link copied to clipboard

Takes this resource off shift until goOnShift: its capacity goes to zero, so new seize requests wait in their queues. Work already allocated finishes under the resource's capacityChangeRule, IGNORE by default: a unit that is busy finishes its current allocation, and when it is released it goes to the off-shift change, not to the next waiting request. A second call while off shift does nothing.

Link copied to clipboard
fun goOnShift()

Brings this resource back on shift, restoring the initialCapacity it had when this replication's first goOffShift was made. The requests waiting for it are offered the units as for any capacity increase. Nothing happens when the resource is on shift.

Link copied to clipboard
protected fun handleIgnoreRuleDeallocation(allocation: Allocation)
Link copied to clipboard
protected fun handleWaitRuleDeallocation(allocation: Allocation)
Link copied to clipboard
Link copied to clipboard
protected open override fun initialize()

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

Link copied to clipboard

Checks if the entity is using (has allocated units) of the resource.

Link copied to clipboard
Link copied to clipboard
protected fun notifyWaitingRequestsOfCapacityIncrease(available: Int, priority: Int)
Link copied to clipboard

Computes the number of different allocations of the resource held by the entity. Recall that allocations can be for different amounts.

Link copied to clipboard
fun onBehaviorStopped(action: () -> Unit)

Declares what to do after the behavior is stopped by stopBehavior, to put back what it changed, for example bringing the resource back on shift when a break was cut short. It does not suspend. It does not run when the behavior ends on its own or when a replication ends. A second call replaces the first. Declared while the model is not running.

Link copied to clipboard

Resources can be associated with a capacity change schedule. If a capacity change occurs, especially a decrease that makes the resource inactive or an increase that allows the resource to be available occurs, then queues that hold requests for the resource may want to be notified of the capacity change.

Link copied to clipboard

Removes the listener

Link copied to clipboard
fun repair()

Repairs this resource: the units its holders lost come back (a held delay goes on, or starts again under RESTART), the resource takes the state its capacity and busy units give, and its available units are offered to the waiting requests as for a capacity increase. A resource that is not failed throws; isFailed is the check.

Link copied to clipboard
protected open override fun replicationEnded()

This method should be overridden by subclasses that need actions performed when the replication ends and prior to the calling of afterReplication(). It is called when each replication ends and can be used to collect data from the model element, etc.

Link copied to clipboard
protected open fun resourceBecameActive()

This function is called from the InactiveState when the state is exited. The current behavior is to do nothing. That is, the normal operation of the resource occurs. This function can be used by subclasses to inject logic into this event.

Link copied to clipboard
protected fun resourceBecameInactive()

This function is called from the InactiveState when the state is entered. The purpose of this function is to permit the notification of entities that may have requests waiting in a queue that is supported by the resource that became inactive. There might not be requests in the queue by the entity when the resource becomes inactive; however, if there are, then the entity may want to not have the request wait during the inactive period. This allows the entity to react to this situation.

Link copied to clipboard
fun seizeTrigger(amountNeeded: Int = 1, seizePriority: Int = ProcessModel.SEIZE_PRIORITY, queue: RequestQ, recordRenegedWait: Boolean = true): WaitTrigger<Allocation>

A trigger for units of this resource, for use in a wait for the first of several things (waitForFirst). The trigger fires when the request it places in queue is served while the wait is open, or at once when the units can be allocated and no waiting request would be chosen first, as seize takes them. Its value is the allocation, made when the request is served and returned when the process resumes, as seize makes it; release it as any other. While the wait is open the request waits in queue as a seize request does. If the wait ends otherwise, the request leaves the queue.

Link copied to clipboard

Starts the behavior again at the current time, after the calling event, as a fresh process with fresh local state. It must have been declared, the model must be running, and it must not be running already; isBehaviorActive is the check.

Link copied to clipboard
fun stopBehavior(at: BehaviorStop = BehaviorStop.AFTER_CYCLE)

Stops the running behavior. With BehaviorStop.AFTER_CYCLE (the default) a cyclic behavior finishes its current cycle and starts no other; a second such call before the cycle ends throws, and isBehaviorStopRequested is the check. With BehaviorStop.NOW, and for a behavior declared with behavior, it ends at once: its process is terminated, releasing anything it holds and running its finally blocks, and a stop not yet begun cancels its start. A stop NOW after a pending AFTER_CYCLE is allowed. Then the action given to onBehaviorStopped runs.

Link copied to clipboard

If the resource is using a schedule, the resource stops listening for capacity changes and is no longer using a schedule. The current capacity will be used for the remainder of the replication.

Link copied to clipboard
open override fun toString(): String

Includes the model name, the id, the model element name, the parent name, and parent id

Link copied to clipboard

Computes the total number of units of the specified resource that are allocated to the entity.

Link copied to clipboard

Unregisters a queue from this resource, so that units freed on it are no longer offered to the requests in queue. If the queue was not registered, nothing occurs.

Link copied to clipboard
protected fun updateState()

Assigns the state the capacity and the busy units give, even when it is unchanged, as each change of either always has. While failed it does nothing: the failed state takes precedence until repair.

Link copied to clipboard
fun useSchedule(schedule: CapacitySchedule, changeRule: CapacityChangeRule)

Tells the resource to listen and react to capacity changes in the supplied Schedule. The model cannot be running when changing the schedule.

Link copied to clipboard
protected open override fun warmUp()

This method should be overridden by subclasses that need actions performed at the warm-up event during each replication. It is called once during each replication if the model element reacts to warm-up actions.