Class AgentHierarchyManager

java.lang.Object
com.tnsai.agents.hierarchy.AgentHierarchyManager

public class AgentHierarchyManager extends Object
Manages hierarchical relationships between agents.

This class extracts parent-child hierarchy management from the Agent class, providing methods for:

  • Parent-child relationship management (get/set parent, add/remove children)
  • Task delegation to child agents
  • Task escalation to parent agent

Usage:


 // In Agent constructor
 this.hierarchyManager = new AgentHierarchyManager(this);

 // Delegate hierarchy methods
 public Agent getParent() {
     return hierarchyManager.getParent();
 }
 
See Also:
  • Constructor Details

    • AgentHierarchyManager

      public AgentHierarchyManager(AgentHierarchyManager.HierarchyContext context)
      Creates a new hierarchy manager for the given agent context.
      Parameters:
      context - the agent context providing access to agent internals
  • Method Details

    • getParent

      public Agent getParent()
      Returns the parent agent in a hierarchical structure.
      Returns:
      parent agent, or null if this is a root agent
    • setParent

      public void setParent(Agent parent)
      Sets the parent agent in a hierarchical structure.

      This method automatically maintains bidirectional consistency:

      • Removes this agent from the previous parent's children (if any)
      • Adds this agent to the new parent's children list
      Parameters:
      parent - the new parent agent (null to make this a root agent)
    • getChildren

      public List<Agent> getChildren()
      Returns the child agents in a hierarchical structure.
      Returns:
      unmodifiable list of child agents (never null)
    • addChild

      public void addChild(Agent child)
      Adds a child agent to this agent's hierarchy.

      This method automatically maintains bidirectional consistency by setting this agent as the child's parent.

      Example (building a team):

      
       Agent supervisor = new SupervisorAgent();
       Agent developer = new DeveloperAgent();
       Agent tester = new TesterAgent();
      
       supervisor.addChild(developer);
       supervisor.addChild(tester);
      
       // supervisor.getChildren() contains both developer and tester
       // developer.getParent() == supervisor
       
      Parameters:
      child - the child agent to add
    • addChildren

      public void addChildren(Agent... children)
      Adds multiple child agents at once.

      Example:

      
       supervisor.addChildren(developer, tester, designer);
       
      Parameters:
      children - the child agents to add
    • removeChild

      public void removeChild(Agent child)
      Removes a child agent from this agent's hierarchy.
      Parameters:
      child - the child agent to remove
    • delegateToChild

      public void delegateToChild(String childId, String actionName, Map<String,Object> parameters)
      Delegates an action to a child agent.

      Sends a task request message to the specified child agent, asking it to execute the named action with the given parameters.

      Example:

      
       // Supervisor delegates coding task to developer
       supervisor.delegateToChild(developer.id(), "writeCode", Map.of(
           "task", "Implement user authentication",
           "language", "Java"
       ));
       
      Parameters:
      childId - the ID of the child agent
      actionName - the action to execute
      parameters - the action parameters
      Throws:
      IllegalStateException - if no communication manager or child not found
    • escalateToParent

      public void escalateToParent(String actionName, Map<String,Object> parameters)
      Escalates an action to the parent agent.

      Sends a task request message to the parent agent, asking it to handle the named action. Useful when:

      • The child lacks permissions for an action
      • The task requires coordination across siblings
      • An error requires higher-level handling

      Example:

      
       // Developer escalates to supervisor when blocked
       developer.escalateToParent("requestApproval", Map.of(
           "reason", "Deployment to production requires sign-off"
       ));
       
      Parameters:
      actionName - the action to execute
      parameters - the action parameters
      Throws:
      IllegalStateException - if no parent or no communication manager