AgentModel

open class AgentModel(parent: ModelElement, name: String? = null) : ProcessModel(source)

Container for an agent-based modeling layer that runs alongside KSL's process view. Following the precedent of ksl.modeling.entity.TaskProcessingSystem, an AgentModel ships two kinds of agent — transient and permanent — plus the supporting machinery (a shared message bus, inner-class Statechart and AgentMailbox) that lets both kinds work uniformly.

Transient and permanent split:

  • Agent: a transient Entity-based actor (an inner class). A QObject under the hood, not a ModelElement, so it can be constructed during a replication. No automatic per-agent statistics. Use for arrival-driven populations, swarms, dynamic coalitions.

  • PermanentAgent: a ModelElement-derived setup-time actor. Constructed before simulate() runs. Full KSL lifecycle hooks and can register per-agent Response/TWResponse statistics. Use for structural cast members, named role-holders, anything where you want individual stats.

Both implement AgentLike (mailbox + name + currentTime) so the same Statechart and AgentMailbox machinery serves both.

Messaging:

  • A single BlockingQueue message bus is pre-allocated at AgentModel construction. All sends route through it; mailboxes are POJOs that filter by recipient reference. This is what makes transient agents possible — agent construction no longer creates a ModelElement child.

  • Per-mailbox queue statistics are not collected automatically. Permanent agents can register their own Response/TWResponse observers if they want individual stats (see the ksl.modeling.entity.TaskProcessingSystem.TaskProcessorPerformance pattern).

Statechart:

  • Statechart is an inner class of AgentModel, not a ModelElement. It uses the outer's protected schedule(...) and executive to schedule timeout events and register conditional actions. Lifecycle (start/stop on replication boundaries) is driven by AgentModel.initialize() / afterReplication() for transient Agents, and by each PermanentAgent / AgentResource's own lifecycle hooks for the permanent case.

Parameters

parent

the parent model element

name

an optional name for the agent model

Constructors

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

Types

Link copied to clipboard
open inner class Agent(aName: String? = null) : ProcessModel.Entity, AgentLike

An autonomous, reactive actor. An Agent is a ProcessModel.Entity (and therefore a QObject, not a ModelElement), so instances can be constructed at any time — including during a running replication. Each agent gets a POJO mailbox for receiving messages and may optionally have a statechart.

Link copied to clipboard
protected inner class AgentGenerator<A : AgentModel.Agent>(agentFactory: () -> A, timeUntilFirst: RVariableIfc, timeBetween: RVariableIfc, maxAgents: Long = Long.MAX_VALUE, timeOfLast: Double = Double.POSITIVE_INFINITY, var activationPriority: Int = KSLEvent.DEFAULT_PRIORITY + 1, name: String? = null) : EventGenerator

Schedules the creation of agents over time and activates each agent's default process. Mirror of ksl.modeling.entity.ProcessModel.EntityGenerator but produces Agent instances. The agent must have defaultProcess configured.

Link copied to clipboard
open inner class AgentMailbox<M : AgentMessage>

A typed mailbox that holds AgentMessage values for a single AgentLike owner.

Link copied to clipboard

Per-agent statistics observer for a PermanentAgent. Modeled on ksl.modeling.entity.TaskProcessingSystem.TaskProcessorPerformance: a ModelElement that subscribes to a specific mailbox via MailboxObserver (and optionally a statechart via StatechartObserver) and publishes standard KSL Response/TWResponse instances.

Link copied to clipboard

Observer hook notified when an agent — Agent (setup-time or transient/runtime), PermanentAgent, or AgentResource — is constructed and registered with this model. Its purpose is to hand external/integration/reporting code a reference to every agent that comes into existence, including transient agents created during a replication that are not in the agents registry and could not otherwise be referenced.

Link copied to clipboard
open inner class Context<A : AgentLike>(name: String? = null) : ModelElement

A typed collection of agents with optional Projections attached. Modeled on Repast Simphony's context concept: membership lives here, structure (positions, edges, networks) lives in attached projections.

Link copied to clipboard

Observer hook for an AgentMailbox. Called for every successful delivery (whether the message was queued or handed off directly to a waiting receiver) and for every successful consumption (whether via tryTake, consume, or Waiter match). Used by AgentPerformance to gather mailbox traffic statistics without coupling the mailbox to the statistics machinery.

Link copied to clipboard
open inner class PermanentAgent(name: String? = null) : ModelElement, AgentLike

A setup-time agent that is a ModelElement. Use when you need KSL lifecycle hooks (initialize, warmUp, afterReplication) on the agent itself, or want to own per-agent Response/TWResponse statistics. Modeled on ksl.modeling.entity.TaskProcessingSystem.TaskProcessor: a ModelElement wrapper that holds its own POJO mailbox and statechart and drives their lifecycle through its own hooks.

Link copied to clipboard
inner class Statechart

A flat statechart attached to an AgentLike owner. Behavior consists of a finite set of named states; the chart is always in exactly one state once started.

Link copied to clipboard

Observer hook for a Statechart. Fires on every state entry, state exit, and completed transition. Used by AgentPerformance to gather statechart-state statistics (time-in-state, entry counts, transition counts).

Properties

Link copied to clipboard
Link copied to clipboard

All setup-time agents (Agent, PermanentAgent, AgentResource) registered with this model. Transient Agents created during a replication are not added to this list — they manage their own lifecycle via the immediate-start path in statechart { }.

Link copied to clipboard

Number of currently-attached AgentRegistryObservers (for diagnostics).

Functions

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

Register an AgentRegistryObserver to be notified when any agent (setup-time or transient) is constructed and registered with this model. Typically attached at setup time. See AgentRegistryObserver.

Link copied to clipboard

Remove a previously-attached AgentRegistryObserver. No-op if not attached.

Link copied to clipboard
protected open override fun dispose(entity: ProcessModel.Entity)

When an entity is disposed (e.g., a transient Agent whose process completed and is being reclaimed), stop its statechart so its subscriptions are torn down promptly rather than waiting for the end-of-replication sweep.

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