BlockingQueue

class BlockingQueue<T : ModelElement.QObject> @JvmOverloads constructor(parent: ModelElement, capacity: Int = Int.MAX_VALUE, name: String? = null) : ModelElement(source)

A blocking queue facilitates the exchange of information between two or more entities that are experiencing processes. Note that an entity can experience one process at a time.

If the capacity is finite, then placing an item in the queue may cause the sender to block its current process. Receivers of items from the queue may block if the number of items meeting their requirements are not available at the time of the request. By default, all queues are FIFO, but their disciplines can be set by the user. Instances are intended to be used to communicate between KSLProcesses via the shared mutable state of the queue. Statistics on blocking and usage of the communication channel are automatically reported.

Senders send in the order of the sender queue. A process that sends to a full queue blocks until something removes an item, so with a finite capacity a process must not rely on itself, or on a process waiting on it, to empty a queue it sends to. A replication that ends with a sender blocked for long gets a warning.

Parameters

parent

the parent of the queue

capacity

the capacity of the queue, by default Int.MAX_VALUE (infinite)

name

the name of the queue

Constructors

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

Types

Link copied to clipboard
inner class AmountRequest(receiver: ProcessModel.Entity, predicate: (T) -> Boolean, val amountRequested: Int, priority: Int) : BlockingQueue.ChannelRequest

Represents a request by an entity to receive a given amount of items from the channel that meet the criteria (predicate).

Link copied to clipboard
open inner class ChannelRequest(val receiver: ProcessModel.Entity, val predicate: (T) -> Boolean, priority: Int) : ModelElement.QObject

Represents a request by an entity to receive a given amount of items from the channel that meet the criteria (predicate).

Link copied to clipboard
object Companion
Link copied to clipboard

Allows the next request that can be filled to be selected regardless of its location within the request queue.

Link copied to clipboard

The default is to select the next based on the queue discipline. This class can be used to define a default method for selecting the next receiver's request to receive items from the channel. The default is to use the queue discipline. The request at the head of the queue is selected if it can be filled. If it can't, no request is selected, whatever could be filled further back.

Link copied to clipboard

Interface for defining request selection

Properties

Link copied to clipboard

The number of available slots in the channel based on the capacity

Link copied to clipboard

True if a send would go ahead now rather than wait. A send waits if the channel is full, or if its free slot is promised to an entity already in line to send. The channel can therefore have room while this is false, but only for the rest of the current simulated instant.

Link copied to clipboard
@set:KSLControl(controlType = ControlType.INTEGER, lowerBound = 1.0)
var capacity: Int

The default size for the queue for each replication. This property cannot be changed during a replication.

Link copied to clipboard

The channel that holds items that are being sent or received.

Link copied to clipboard

True if the channel does not contain any items

Link copied to clipboard

True if the channel contains items

Link copied to clipboard

True if the channel is at its capacity

Link copied to clipboard

True if the channel is not at its capacity

Link copied to clipboard

The user of the BlockingQueue can supply a function that will select the next entity that is waiting to receive items from the queue's channel after new items are added to the channel, and after a request has been filled.

Link copied to clipboard

The queue that holds requests by entities to remove items from the channel because the channel does not have the requested amount of items that meet the desired selection criteria.

Link copied to clipboard

The queue that holds entities wanting to place items in to the channel that are waiting (blocked) because there is no available capacity

Link copied to clipboard

The user of the BlockingQueue can supply a function that will select, after items are removed from the channel, the next entity that is blocked waiting to send items to the queue's channel because the channel was full.

Functions

Link copied to clipboard
fun availableForRemoval(condition: Predicate<T>): List<T>

Finds the items that can be removed directly from the channel. Does not remove the items.

fun availableForRemoval(condition: (T) -> Boolean = { true }): List<T>

Finds the items that can be removed directly from the channel. Passing these to removeAllFromChannel() can't fail because of a waiting request. Does not remove the items.

Link copied to clipboard
protected open override fun beforeReplication()

This method should be overridden by subclasses that need actions performed prior to each replication. It is called prior to each replication and can be used to initialize the model element. It is called before initialize() is called.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun countByChannel(predicate: Predicate<T>): Int
fun countByChannel(predicate: (T) -> Boolean): Int
Link copied to clipboard
fun filterChannel(predicate: Predicate<T>): List<T>
fun filterChannel(predicate: (T) -> Boolean): List<T>

Finds the items in the channel according to the condition. Does not remove the items from the channel.

Link copied to clipboard
fun receiveFromChannel(condition: Predicate<T>, waitStats: Boolean = true): List<T>

Finds the items in the channel according to the condition and removes them directly, skipping the line of waiting requests. The channel must not be empty, and no waiting request can want any of the items found; otherwise an IllegalStateException will occur and nothing is removed. Use availableForRemoval() to take only the items that no request wants.

Link copied to clipboard

Removes the next item directly from the channel, skipping the line of waiting requests. The channel must not be empty, and no waiting request can want the next item; otherwise an IllegalStateException will occur. Use canRemoveFromChannel() to check.

Link copied to clipboard

Removes and terminates all the requests waiting in the queue

Link copied to clipboard

Removes and terminates all the entities waiting in the send queue

Link copied to clipboard
fun removeAllFromChannel(c: Collection<T>, waitStats: Boolean = true)

Attempts to remove all the supplied items directly from the channel, skipping the line of waiting requests. Throws an IllegalStateException, and removes nothing, if all the items are not present in the channel or if a waiting request wants any of them. An empty collection removes nothing. Use availableForRemoval() to find items that no request wants. Removing items from the channel triggers the entities waiting to send more items into the channel if the channel was full.

Link copied to clipboard

Removes the request from the queue and tells the associated entity to terminate its process. The process that was suspended because the entity's request was placed in the queue is immediately terminated.

Link copied to clipboard

Removes the entity from the send queue and tells the entity to terminate its process. The process that was suspended because the entity was placed in the queue is immediately terminated.

Link copied to clipboard
protected open override fun replicationEnded()

Reports any entity still blocked sending when a replication ends.

Link copied to clipboard

The first waiting request that wants the item, which is the reason the item can't be removed directly from the channel. A request wants an item that passes its predicate, whether or not the request has been told to resume.

Link copied to clipboard