Package com.tnsai.roles.registry
Class RoleRegistry
java.lang.Object
com.tnsai.roles.registry.RoleRegistry
Central registry for managing exclusive Role assignments.
RoleRegistry enforces the shared=false constraint from
RoleSpec annotation, ensuring that non-shared roles
can only be assigned to one agent at a time.
Key Features
- Thread-safe role assignment tracking
- Exclusive role enforcement (
shared=false) - Role-to-agent ownership lookup
- Automatic role release on agent stop
Usage Example
RoleRegistry registry = RoleRegistry.getInstance();
// Assign a role (throws if exclusive and already assigned)
registry.assignRole(agent, new LeaderRole());
// Check if a role is shared
boolean isShared = registry.isShared(LeaderRole.class);
// Get the owner of an exclusive role
Optional<Agent> owner = registry.getOwner(LeaderRole.class);
// Release a role
registry.releaseRole(agent, role);
Integration with @RoleSpec
When a Role class has @RoleSpec(shared=false), only one
agent can hold that role at a time. Attempting to assign the same role
to another agent will throw RoleAssignmentException.
- Author:
- TnsAI Team
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidassignRole(Agent agent, Role role) Assigns a role to an agent.voidclear()Clears all registrations (for testing).static RoleRegistryReturns the singleton instance.Gets the current owner of an exclusive role.Gets all role classes assigned to an agent.booleanChecks if an agent has a specific role assigned.booleanChecks if a role class is shared (can be assigned to multiple agents).voidreleaseAllRoles(Agent agent) Releases all roles from an agent.voidreleaseRole(Agent agent, Role role) Releases a role from an agent.
-
Method Details
-
getInstance
Returns the singleton instance.- Returns:
- the RoleRegistry instance
-
assignRole
Assigns a role to an agent.If the role has
@RoleSpec(shared=false)and is already assigned to another agent, throwsRoleAssignmentException.- Parameters:
agent- the agent to assign the role torole- the role to assign- Throws:
RoleAssignmentException- if the role is exclusive and already assignedIllegalArgumentException- if agent or role is null
-
releaseRole
Releases a role from an agent.If the role is exclusive, it becomes available for other agents.
- Parameters:
agent- the agent releasing the rolerole- the role to release
-
releaseAllRoles
Releases all roles from an agent.Called when an agent is stopped or destroyed.
- Parameters:
agent- the agent
-
getOwner
Gets the current owner of an exclusive role.- Parameters:
roleClass- the role class- Returns:
- the owning agent, or empty if not assigned or if role is shared
-
hasRole
Checks if an agent has a specific role assigned.- Parameters:
agent- the agentroleClass- the role class- Returns:
- true if the agent has this role
-
getRoles
Gets all role classes assigned to an agent.- Parameters:
agent- the agent- Returns:
- unmodifiable set of role classes
-
clear
public void clear()Clears all registrations (for testing).
-