Package modelarium.multithreading.utils
Class WorkerCache
java.lang.Object
modelarium.multithreading.utils.WorkerCache
Provides local, thread-specific caching for a simulation worker.
This cache is used to reduce the cost of repeatedly fetching agents or environment data from the coordinator during simulation ticks. It supports storing agent queries and environmental state locally for reuse within a tick.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a single agent to the cache.voidaddAgentFilter(Predicate<Agent> agentFilter) Adds a new agent filter to the list of cached filters.voidAdds a full set of agents to the cache.voidaddEnvironment(Environment environment) Caches a reference to the environment for reuse within a tick.voidclear()Clears the entire cache.booleandoesAgentExist(String agentName) Checks if a specific agent exists in the cache.booleandoesAgentFilterExist(Predicate<Agent> agentFilter) Checks whether a specific agent filter has already been applied and cached.booleanChecks whether an environment instance has already been cached.Retrieves an agent from the cache by name.Returns the cached environment instance.getFilteredAgents(Predicate<Agent> agentFilter) Retrieves a filtered set of agents using the cached filter.
-
Constructor Details
-
WorkerCache
public WorkerCache(boolean isAgentCopiesHeld) Constructs a new worker cache.- Parameters:
isAgentCopiesHeld- whether this cache should hold deep copies of agents
-
-
Method Details
-
clear
public void clear()Clears the entire cache. Should be called at the end of each tick. -
doesAgentFilterExist
Checks whether a specific agent filter has already been applied and cached.- Parameters:
agentFilter- the predicate used to filter agents- Returns:
- true if this filter has already been used
-
addAgentFilter
Adds a new agent filter to the list of cached filters.- Parameters:
agentFilter- the predicate used to filter agents
-
getFilteredAgents
Retrieves a filtered set of agents using the cached filter.- Parameters:
agentFilter- the predicate used to filter agents- Returns:
- a filtered
AgentSet
-
doesAgentExist
Checks if a specific agent exists in the cache.- Parameters:
agentName- the name of the agent- Returns:
- true if the agent is cached
-
getAgent
Retrieves an agent from the cache by name.- Parameters:
agentName- the name of the agent- Returns:
- the cached
Agent
-
addAgent
Adds a single agent to the cache.- Parameters:
agent- the agent to cache
-
addAgents
Adds a full set of agents to the cache.- Parameters:
agentSet- the agent set to add
-
doesEnvironmentExist
public boolean doesEnvironmentExist()Checks whether an environment instance has already been cached.- Returns:
- true if an environment is stored
-
getEnvironment
Returns the cached environment instance.- Returns:
- the cached
Environment
-
addEnvironment
Caches a reference to the environment for reuse within a tick.- Parameters:
environment- the environment instance to cache
-