Class SimulationContext

java.lang.Object
modelarium.entities.contexts.SimulationContext
All Implemented Interfaces:
Context
Direct Known Subclasses:
AgentSimulationContext, EnvironmentSimulationContext

public abstract sealed class SimulationContext extends Object implements Context permits AgentSimulationContext, EnvironmentSimulationContext
Provides a model element (either an agent or the environment) with access to relevant simulation resources such as the local environment, other agents, and shared utilities, including communication and caching systems.

This class abstracts access logic based on model settings, including:

  • Local access versus coordinated inter-thread access
  • Optional caching of agents and environments
  • Safe agent filtering with predicate functions
  • The associated model clock
  • Method Details

    • getClock

      public ReadOnlyClock getClock()
      Returns the model's clock.
      Specified by:
      getClock in interface Context
      Returns:
      a read-only view of the model's clock
    • doesAgentExistInThisCore

      public boolean doesAgentExistInThisCore(String agentName)
      Returns whether an agent with the given name exists on the current core.
      Specified by:
      doesAgentExistInThisCore in interface Context
      Parameters:
      agentName - the name of the agent to check for
      Returns:
      true if the agent exists in this core's local agent set, false otherwise
    • getRandom

      public RandomGenerator getRandom()
      Returns the random generator the owning entity can use.
      Specified by:
      getRandom in interface Context
      Returns:
      the entity's RandomGenerator instance
    • localAgentSet

      protected AgentSet localAgentSet()
      Returns the entity's local agent set.
      Returns:
      the local agent set
    • entity

      protected Entity<?,?,?,?> entity()
      Returns the entity this context belongs to.
      Returns:
      the owning entity
    • attributeSet

      protected AttributeSet<?,?> attributeSet()
      Returns the attribute set currently being run on the owning entity.
      Returns:
      the current attribute set
    • attribute

      protected AttributeBase<?> attribute()
      Returns the attribute currently being run on the owning entity.
      Returns:
      the current attribute
    • config

      protected Config config()
      Returns the model's configuration settings.
      Returns:
      the global model settings
    • getThisEntity

      public abstract Entity<?,?,?,?> getThisEntity()
      Returns the entity this context belongs to. Must be implemented by subclasses.
      Returns:
      the owning entity
    • getThisAttributeSet

      public abstract AttributeSet<?,?> getThisAttributeSet()
      Returns the attribute set currently being run on the owning entity. Must be implemented by subclasses.
      Returns:
      the current attribute set
    • getThisAttribute

      public abstract AttributeBase<?> getThisAttribute()
      Returns the attribute currently being run on the owning entity. Must be implemented by subclasses.
      Returns:
      the current attribute
    • getEnvironment

      public ReadOnlyEnvironment getEnvironment()
      Returns the model's environment.

      If the model's threads are not synchronised, the core's local environment is returned. Otherwise, the environment is taken from the cache if present, or requested from the co-ordinator and cached for the remainder of the tick.

      Returns:
      a read-only view of the model's Environment
    • addAgent

      public void addAgent(Agent agent)
      Adds an agent to the current core's local agent set, creating the context the agent needs to run.
      Specified by:
      addAgent in interface Context
      Parameters:
      agent - the agent to add
    • addAgents

      public void addAgents(AgentSet agentSet)
      Adds each agent in an agent set to the current core's local agent set, creating the contexts the agents need to run.
      Specified by:
      addAgents in interface Context
      Parameters:
      agentSet - the agents to add
    • addAgents

      public void addAgents(List<Agent> agentList)
      Adds each agent in a list to the current core's local agent set, creating the contexts the agents need to run.
      Specified by:
      addAgents in interface Context
      Parameters:
      agentList - the agents to add
    • getCurrentPopulationSize

      public int getCurrentPopulationSize()
      Returns the model's current population size.

      The current population size is looked up in the cache. If it hasn't been cached, the value is requested from the co-ordinator and then cached. If the model's threads are unsynchronised, the local current population size is returned instead.

      Specified by:
      getCurrentPopulationSize in interface Context
      Returns:
      the current population size as an int
    • getAgent

      public ReadOnlyAgent getAgent(String targetAgentName)
      Retrieves an agent by name, whether it lives on this core or (in a synchronised model) on another core.

      The agent is looked up in the local agent set first, then in the cache, and finally requested from the co-ordinator if the model's threads are synchronised. Agents retrieved from the co-ordinator are cached for the remainder of the tick.

      Specified by:
      getAgent in interface Context
      Parameters:
      targetAgentName - the name of the agent to retrieve
      Returns:
      a read-only view of the requested agent
    • getFilteredAgents

      public ReadOnlyAgentSet getFilteredAgents(Predicate<ReadOnlyAgent> filter)
      Retrieves the agents (excluding dead agents) matching a filter, drawn from the whole population in a synchronised model or from this core's local agents otherwise.

      Filter results are cached for the remainder of the tick, keyed by the filter instance itself.

      Specified by:
      getFilteredAgents in interface Context
      Parameters:
      filter - a predicate to apply to each agent
      Returns:
      a read-only view of the matching agents
    • getFilteredAgents

      public ReadOnlyAgentSet getFilteredAgents(Predicate<ReadOnlyAgent> filter, boolean includeDeadAgents)
      Retrieves the agents matching a filter, drawn from the whole population in a synchronised model or from this core's local agents otherwise.

      Filter results are cached for the remainder of the tick, keyed by the filter instance itself.

      Specified by:
      getFilteredAgents in interface Context
      Parameters:
      filter - a predicate to apply to each agent
      includeDeadAgents - a boolean which determines if the filtered agents should include the dead agents or not
      Returns:
      a read-only view of the matching agents
    • killAgent

      public void killAgent(String agentName)
      Kills the agent with the given name.
      Specified by:
      killAgent in interface Context
      Parameters:
      agentName - the agent's name as a string
    • killAgent

      public void killAgent(ReadOnlyAgent agent)
      Kills the agent of the given ReadOnlyAgent instance.
      Specified by:
      killAgent in interface Context
      Parameters:
      agent - the immutable agent to kill
    • killAgents

      public void killAgents(List<String> agentNames)
      Kills all the agents with names in a given List<String> instance.
      Specified by:
      killAgents in interface Context
      Parameters:
      agentNames - the list of names of agents to kill
    • killAgents

      public void killAgents(ReadOnlyAgentSet agentSet)
      Kills all the agents in a given ReadOnlyAgentSet instance.
      Specified by:
      killAgents in interface Context
      Parameters:
      agentSet - the immutable agent set of agents to kill