Context

open inner class Context<A : AgentLike>(name: String? = null) : ModelElement(source)

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.

Why a separate abstraction from AgentModel._agents:

  • The model-level registry tracks every setup-time agent for lifecycle dispatch. Contexts are user-defined groupings — pedestrians vs. vehicles, friendlies vs. adversaries, members of organization X — and the same agent can belong to multiple contexts.

  • Projections need a stable, queryable membership set to attach to. Contexts give it to them; projections subscribe to add / remove via Projection.onAgentJoined / onAgentLeft.

Context is a ModelElement so it gets a stable name in the model, lifecycle hooks (in case future projections need them), and a place in KSL reporting output. Membership changes during a replication are fine; the context itself must be constructed before simulate().

Generic in the agent type A — typically AgentModel.Agent or a user subclass. Bounded by AgentLike so contexts can also hold PermanentAgent or AgentResource instances.

Constructors

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

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Read-only view of the current membership.

Link copied to clipboard

Read-only view of attached projections.

Link copied to clipboard
val size: Int

Number of agents currently in this context.

Functions

Link copied to clipboard
fun add(agent: A)

Add agent to this context. Notifies every attached projection via onAgentJoined. No-op if already a member.

Link copied to clipboard
fun <P : Projection<A>> addProjection(projection: P): P

Attach projection to this context. The projection's lifecycle hooks fire on subsequent add / remove operations. Existing members do NOT retroactively trigger onAgentJoined — projections that need to initialize state for all existing members should do so explicitly.

Link copied to clipboard
fun clear()

Clear all members and notify projections. Rarely needed now that initialize resets runtime membership automatically; still available for models that want to empty the context mid-replication.

Link copied to clipboard
operator fun contains(agent: A): Boolean

True if agent is currently a member.

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

Iterate the current membership snapshot.

Link copied to clipboard
protected open override fun initialize()

Reset context membership at the start of every replication.

Link copied to clipboard
inline fun <T : A> ofType(): List<T>

Members of subtype T.

Link copied to clipboard
fun remove(agent: A)

Remove agent from this context. Notifies every attached projection via onAgentLeft. No-op if not a member.

Link copied to clipboard
fun where(predicate: (A) -> Boolean): List<A>

Members matching predicate.