Class SimulationContext
- All Implemented Interfaces:
Context
- Direct Known Subclasses:
AgentSimulationContext,EnvironmentSimulationContext
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 Summary
Modifier and TypeMethodDescriptionvoidAdds an agent to the current core's local agent set, creating the context the agent needs to run.voidAdds each agent in a list to the current core's local agent set, creating the contexts the agents need to run.voidAdds each agent in an agent set to the current core's local agent set, creating the contexts the agents need to run.protected AttributeBase<?> Returns the attribute currently being run on the owning entity.protected AttributeSet<?, ?> Returns the attribute set currently being run on the owning entity.protected Configconfig()Returns the model's configuration settings.booleandoesAgentExistInThisCore(String agentName) Returns whether an agent with the given name exists on the current core.protected Entity<?, ?, ?, ?> entity()Returns the entity this context belongs to.Retrieves an agent by name, whether it lives on this core or (in a synchronised model) on another core.getClock()Returns the model's clock.intReturns the model's current population size.Returns the model's environment.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.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.Returns the random generator the owning entity can use.abstract AttributeBase<?> Returns the attribute currently being run on the owning entity.abstract AttributeSet<?, ?> Returns the attribute set currently being run on the owning entity.abstract Entity<?, ?, ?, ?> Returns the entity this context belongs to.voidKills the agent with the given name.voidkillAgent(ReadOnlyAgent agent) Kills the agent of the givenReadOnlyAgentinstance.voidkillAgents(List<String> agentNames) voidkillAgents(ReadOnlyAgentSet agentSet) Kills all the agents in a givenReadOnlyAgentSetinstance.protected AgentSetReturns the entity's local agent set.
-
Method Details
-
getClock
Returns the model's clock. -
doesAgentExistInThisCore
Returns whether an agent with the given name exists on the current core.- Specified by:
doesAgentExistInThisCorein interfaceContext- 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
Returns the random generator the owning entity can use.- Specified by:
getRandomin interfaceContext- Returns:
- the entity's
RandomGeneratorinstance
-
localAgentSet
Returns the entity's local agent set.- Returns:
- the local agent set
-
entity
Returns the entity this context belongs to.- Returns:
- the owning entity
-
attributeSet
Returns the attribute set currently being run on the owning entity.- Returns:
- the current attribute set
-
attribute
Returns the attribute currently being run on the owning entity.- Returns:
- the current attribute
-
config
Returns the model's configuration settings.- Returns:
- the global model settings
-
getThisEntity
Returns the entity this context belongs to. Must be implemented by subclasses.- Returns:
- the owning entity
-
getThisAttributeSet
Returns the attribute set currently being run on the owning entity. Must be implemented by subclasses.- Returns:
- the current attribute set
-
getThisAttribute
Returns the attribute currently being run on the owning entity. Must be implemented by subclasses.- Returns:
- the current attribute
-
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
Adds an agent to the current core's local agent set, creating the context the agent needs to run. -
addAgents
Adds each agent in an agent set to the current core's local agent set, creating the contexts the agents need to run. -
addAgents
Adds each agent in a list to the current core's local agent set, creating the contexts the agents need to run. -
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:
getCurrentPopulationSizein interfaceContext- Returns:
- the current population size as an int
-
getAgent
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.
-
getFilteredAgents
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:
getFilteredAgentsin interfaceContext- 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:
getFilteredAgentsin interfaceContext- Parameters:
filter- a predicate to apply to each agentincludeDeadAgents- 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
Kills the agent with the given name. -
killAgent
Kills the agent of the givenReadOnlyAgentinstance. -
killAgents
- Specified by:
killAgentsin interfaceContext- Parameters:
agentNames- the list of names of agents to kill
-
killAgents
Kills all the agents in a givenReadOnlyAgentSetinstance.- Specified by:
killAgentsin interfaceContext- Parameters:
agentSet- the immutable agent set of agents to kill
-