Class WorkerCache

java.lang.Object
modelarium.multithreading.utils.WorkerCache

public class WorkerCache extends Object
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 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

      public boolean doesAgentFilterExist(Predicate<Agent> agentFilter)
      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

      public void addAgentFilter(Predicate<Agent> agentFilter)
      Adds a new agent filter to the list of cached filters.
      Parameters:
      agentFilter - the predicate used to filter agents
    • getFilteredAgents

      public AgentSet getFilteredAgents(Predicate<Agent> agentFilter)
      Retrieves a filtered set of agents using the cached filter.
      Parameters:
      agentFilter - the predicate used to filter agents
      Returns:
      a filtered AgentSet
    • doesAgentExist

      public boolean doesAgentExist(String agentName)
      Checks if a specific agent exists in the cache.
      Parameters:
      agentName - the name of the agent
      Returns:
      true if the agent is cached
    • getAgent

      public Agent getAgent(String agentName)
      Retrieves an agent from the cache by name.
      Parameters:
      agentName - the name of the agent
      Returns:
      the cached Agent
    • addAgent

      public void addAgent(Agent agent)
      Adds a single agent to the cache.
      Parameters:
      agent - the agent to cache
    • addAgents

      public void addAgents(AgentSet agentSet)
      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

      public Environment getEnvironment()
      Returns the cached environment instance.
      Returns:
      the cached Environment
    • addEnvironment

      public void addEnvironment(Environment environment)
      Caches a reference to the environment for reuse within a tick.
      Parameters:
      environment - the environment instance to cache