Package modelarium.agents
Class AgentSet
java.lang.Object
modelarium.agents.AgentSet
A collection class for managing
Agent instances, with support for:
- Optional deep copying of agents on insertion
- Fast lookup by agent name
- Filtering, duplication, and setup routines
- Randomised iteration
This class is iterable and designed to support both sequential and parallel simulation use cases.
-
Constructor Summary
ConstructorsConstructorDescriptionAgentSet()Constructs an empty agent set without deep copying.AgentSet(boolean isStoringAgentCopies) Constructs an empty agent set with optional deep copy behaviour.Constructs a new agent set from a list of agents, without deep copying.Constructs a new agent set from a list of agents, with optional deep copying. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a list of agents to the set.voidAdds an agent to the set.voidAdds all agents from anotherAgentSetthat do not already exist in this set.voidclear()Clears the agent set entirely.booleandoesAgentExist(String agentName) Checks if an agent exists in the set by name.Returns a duplicate of this agent set.get(int index) Retrieves an agent by index.Retrieves an agent by name.Returns the list of agents in this set.getFilteredAgents(Predicate<Agent> agentFilter) Returns a filtered view of the agent set.Returns a randomised iterator over the agents in this set.iterator()Standard iterator over the agents in the order they were added.voidsetIsStoringAgentCopiesFlag(boolean isStoringAgentCopies) Updates the internal flag for whether agent copies should be stored.voidsetup()Callssetup()on all agents in the set.intsize()Returns the number of agents in the set.voidUpdates this set with all agents from another set.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
AgentSet
public AgentSet(boolean isStoringAgentCopies) Constructs an empty agent set with optional deep copy behaviour.- Parameters:
isStoringAgentCopies- whether added agents should be deep copied
-
AgentSet
Constructs a new agent set from a list of agents, with optional deep copying.- Parameters:
agentsList- list of agents to addisStoringAgentCopies- whether to store deep copies
-
AgentSet
Constructs a new agent set from a list of agents, without deep copying.- Parameters:
agentsList- list of agents to add
-
AgentSet
public AgentSet()Constructs an empty agent set without deep copying.
-
-
Method Details
-
setIsStoringAgentCopiesFlag
public void setIsStoringAgentCopiesFlag(boolean isStoringAgentCopies) Updates the internal flag for whether agent copies should be stored.- Parameters:
isStoringAgentCopies- true to enable deep copy on addition
-
add
Adds an agent to the set. If the agent already exists, it will be replaced.- Parameters:
agent- the agent to add
-
add
Adds a list of agents to the set.- Parameters:
agents- list of agents to add
-
add
Adds all agents from anotherAgentSetthat do not already exist in this set. Existing agents (by name) are not modified or replaced.- Parameters:
agentSet- the agent set to add from
-
get
Retrieves an agent by name.- Parameters:
agentName- the agent's unique name- Returns:
- the agent instance
-
get
Retrieves an agent by index.- Parameters:
index- the index of the agent- Returns:
- the agent at the given position
-
getAsList
Returns the list of agents in this set.- Returns:
- a list of agent instances
-
size
public int size()Returns the number of agents in the set.- Returns:
- the size of the agent set
-
clear
public void clear()Clears the agent set entirely. -
doesAgentExist
Checks if an agent exists in the set by name.- Parameters:
agentName- the name to check- Returns:
- true if the agent exists
-
update
Updates this set with all agents from another set. Existing agents are replaced if names match.- Parameters:
otherAgentSet- the other agent set to pull from
-
getFilteredAgents
Returns a filtered view of the agent set.- Parameters:
agentFilter- a predicate to apply to each agent- Returns:
- a new
AgentSetcontaining only matching agents
-
getRandomIterator
Returns a randomised iterator over the agents in this set.- Returns:
- an iterator that yields agents in random order
-
setup
public void setup()Callssetup()on all agents in the set. Should be called before the simulation begins. -
duplicate
Returns a duplicate of this agent set. If deep copy is enabled, agents will be duplicated as well.- Returns:
- a new
AgentSetwith the same agents
-
iterator
Standard iterator over the agents in the order they were added.
-