Package com.tnsai.agents.hierarchy
Class HierarchyBuilder
java.lang.Object
com.tnsai.agents.hierarchy.HierarchyBuilder
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 Summary
Modifier and TypeMethodDescriptionbuild()Builds the hierarchy and returns the communication manager.Adds a child agent to the current agent.static HierarchyBuildercreate()Creates a new hierarchy builder.static AgentCommunicationManagerCreates a communication manager from an existing agent hierarchy.parent()Moves context back to the parent of the current agent.Sets the root agent of the hierarchy.toRoot()Moves context to the root agent.withManager(AgentCommunicationManager manager) Uses a custom communication manager instead of creating a new one.
-
Method Details
-
create
Creates a new hierarchy builder.- Returns:
- new builder instance
-
from
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
Sets the root agent of the hierarchy.- Parameters:
agent- the root agent- Returns:
- this builder
-
child
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
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
Moves context to the root agent. Useful for adding top-level siblings.- Returns:
- this builder
-
withManager
Uses a custom communication manager instead of creating a new one.- Parameters:
manager- the communication manager to use- Returns:
- this builder
-
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
-