Counter

open class Counter @JvmOverloads constructor(parent: ModelElement, name: String? = null, initialValue: Double = 0.0, countLimit: Double = Double.POSITIVE_INFINITY, stopOnLimit: Boolean = false) : ModelElement, CounterIfc, CounterCIfc, DoublePairEmitterIfc(source)

A Counter tracks cumulative non-negative values over the course of a simulation.

Unlike a standard Variable, a Counter is specifically designed for tally-based metrics (e.g., number of parts produced, number of customers served). It provides built-in support for threshold-based actions, such as stopping the simulation when a target production count is met.

Key Behaviors:

  • Monotonicity: Increments must be non-negative to ensure the counter represents a cumulative total.

  • Lifecycle Management: Automatically resets to initialValue at the start of each replication to ensure experimental repeatability.

  • Threshold Signaling: When the counter reaches replicationCountLimit, it triggers all registered CountActionIfc callbacks.

  • Across-Replication Statistics: Automatically captures the final value of each replication for higher-level statistical analysis.

Usage Example:

val partsProduced = Counter(this, "PartsProduced", countLimit = 100.0, stopOnLimit = true)
// The simulation will now automatically call executive.stop() when value hits 100.0
partsProduced.increment()

Parameters

parent

The ModelElement that owns this counter, typically a Model.

name

An optional descriptive name for the counter, used in logs and reports.

initialValue

The value the counter starts with at the beginning of every replication. Defaults to 0.0.

countLimit

The threshold at which CountActionIfc actions are triggered. Defaults to Double.POSITIVE_INFINITY.

stopOnLimit

If true, a StoppingAction is automatically attached to this counter, which will terminate the simulation replication once the countLimit is reached. Note: This only applies if countLimit is finite.

Inheritors

Constructors

Link copied to clipboard
constructor(parent: ModelElement, name: String? = null, initialValue: Double = 0.0, countLimit: Double = Double.POSITIVE_INFINITY, stopOnLimit: Boolean = false)

Properties

Link copied to clipboard

Returns a StatisticAccessorIfc for the across replication statistics that have been collected on this Counter

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
override val domain: Interval
Link copied to clipboard
open override var emissionsOn: Boolean

If true, the response will emit a pair Pair(time, value) every time a new value is assigned

Link copied to clipboard
@set:KSLControl(controlType = ControlType.DOUBLE, name = "initialCounterLimit", lowerBound = 0.0)
open override var initialCounterLimit: Double

Sets the initial value of the count limit. Only relevant prior to each replication. Changing during a replication has no effect until the next replication.

Link copied to clipboard
@set:KSLControl(controlType = ControlType.DOUBLE, name = "initialValue", lowerBound = 0.0)
open override var initialValue: Double

Sets the initial value of the variable. Only relevant prior to each replication. Changing during a replication has no effect until the next replication.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open override var previousTimeOfChange: Double
Link copied to clipboard
open override var previousValue: Double

The previous value, before the current value changed

Link copied to clipboard

Changes the count action limit during the replication. WARNING: This value will automatically be reset to the initialCountLimit at the beginning of each replication so that each replication starts in the same state. If you want to control the count limit for each replication, you should use the initialCountLimit.

Link copied to clipboard
open override var timeOfChange: Double
Link copied to clipboard
Link copied to clipboard
open override var value: Double

Functions

Link copied to clipboard
open override fun addCountLimitAction(action: CountActionIfc)
Link copied to clipboard
open override fun addCountLimitStoppingAction(initialCountLimit: Int): CountActionIfc
Link copied to clipboard
protected fun assignInitialValue(value: Double)

Assigns the value of the counter to the supplied value. Ensures that time of change is 0.0 and previous value and previous time of change are the same as the current value and current time without notifying any update observers

Link copied to clipboard
protected open fun assignValue(newValue: Double)
Link copied to clipboard
protected open override fun beforeExperiment()

This method should be overridden by subclasses that need logic to be performed prior to an experiment. The beforeExperiment method allows model elements to be set up prior to the first replication within an experiment. It is called once before any replications occur.

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
fun increment(increase: Double = 1.0)

Increments the value of the variable by the amount supplied. Throws an IllegalArgumentException if the value is negative.

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
protected fun notifyCountLimitActions()
Link copied to clipboard
open override fun removeCountLimitAction(action: CountActionIfc)
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
fun resetCounter(value: Double, notifyUpdateObservers: Boolean)

Resets the counter to the supplied value.

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.