Class HierarchyBuilder

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

public class HierarchyBuilder extends Object
Utility class for building agent hierarchies with a fluent API. Simplifies construction of parent-child relationships and communication setup.

Example usage:

 AgentCommunicationManager manager = HierarchyBuilder.create()
     .root(supervisorAgent)
         .child(workerAgent1)
         .child(workerAgent2)
         .parent()
     .child(coordinatorAgent)
         .child(taskAgent1)
         .child(taskAgent2)
         .parent()
     .build();
 
  • Method Details

    • create

      public static HierarchyBuilder create()
      Creates a new hierarchy builder.
      Returns:
      new builder instance
    • from

      public static AgentCommunicationManager from(Agent root)
      Creates a communication manager from an existing agent hierarchy. Traverses the tree starting from root and registers all agents.

      Example usage:

       supervisor.addChildren(coordinator1, coordinator2);
       coordinator1.addChildren(worker1, worker2);
      
       AgentCommunicationManager manager = HierarchyBuilder.from(supervisor);
       
      Parameters:
      root - the root agent of the hierarchy
      Returns:
      communication manager with all agents registered
    • root

      public HierarchyBuilder root(Agent agent)
      Sets the root agent of the hierarchy.
      Parameters:
      agent - the root agent
      Returns:
      this builder
    • child

      public HierarchyBuilder child(Agent agent)
      Adds a child agent to the current agent. Sets the current agent as the context for further child additions.
      Parameters:
      agent - the child agent
      Returns:
      this builder
    • parent

      public HierarchyBuilder parent()
      Moves context back to the parent of the current agent. Allows building sibling agents.
      Returns:
      this builder
      Throws:
      IllegalStateException - if current agent has no parent
    • toRoot

      public HierarchyBuilder toRoot()
      Moves context to the root agent. Useful for adding top-level siblings.
      Returns:
      this builder
    • withManager

      public HierarchyBuilder withManager(AgentCommunicationManager manager)
      Uses a custom communication manager instead of creating a new one.
      Parameters:
      manager - the communication manager to use
      Returns:
      this builder
    • build

      public AgentCommunicationManager build()
      Builds the hierarchy and returns the communication manager. All agents are registered with the manager and configured.
      Returns:
      the communication manager with all agents registered
      Throws:
      IllegalStateException - if no root agent was set