Class LoopGraphBuildingStrategy

java.lang.Object
com.alibaba.cloud.ai.graph.agent.flow.strategy.AbstractFlowGraphBuildingStrategy
com.alibaba.cloud.ai.graph.agent.flow.strategy.LoopGraphBuildingStrategy
All Implemented Interfaces:
FlowGraphBuildingStrategy

public class LoopGraphBuildingStrategy extends AbstractFlowGraphBuildingStrategy
Converts a LoopAgent into its corresponding StateGraph.

Structure of the loop graph: START -> LoopInitLoop -> LoopDispatchNode (condition met -> SubAgentNode -> LoopDispatchNode; condition not met -> END)

Since:
2025/8/25
Author:
vlsmb
  • Constructor Details

    • LoopGraphBuildingStrategy

      public LoopGraphBuildingStrategy()
  • Method Details

    • buildCoreGraph

      protected void buildCoreGraph(FlowGraphBuilder.FlowGraphConfig config) throws GraphStateException
      Description copied from class: AbstractFlowGraphBuildingStrategy
      Abstract method that subclasses must implement to build their specific graph structure. All common setup (graph creation, hook filtering, node addition, etc.) is already done. Subclasses can directly access protected fields: graph, rootAgent, beforeModelHooks, afterModelHooks, entryNode, exitNode.

      Note: Subclasses should NOT call connectBeforeModelHookEdges() or connectAfterModelHookEdges() in this method. Instead, override the hook methods (connectBeforeModelHooks(), connectAfterModelHooks()) if custom hook connection logic is needed.

      Overrides:
      buildCoreGraph in class AbstractFlowGraphBuildingStrategy
      Parameters:
      config - the flow graph configuration
      Throws:
      GraphStateException - if graph construction fails
    • connectBeforeAgentHooks

      protected void connectBeforeAgentHooks() throws GraphStateException
      Connects beforeAgent hooks directly to the loop processor. In loop scenarios, beforeAgent hooks execute once before the loop starts, not before each iteration (which is handled by beforeModel hooks).
      Overrides:
      connectBeforeAgentHooks in class AbstractFlowGraphBuildingStrategy
      Throws:
      GraphStateException - if graph construction fails
    • connectBeforeModelHooks

      protected void connectBeforeModelHooks() throws GraphStateException
      Empty implementation - beforeModel hooks are connected within the loop in buildCoreGraph. This prevents the default behavior of connecting them outside the loop.
      Overrides:
      connectBeforeModelHooks in class AbstractFlowGraphBuildingStrategy
      Throws:
      GraphStateException - if graph construction fails
    • connectAfterModelHooks

      protected void connectAfterModelHooks() throws GraphStateException
      Empty implementation - afterModel hooks are connected within the loop in buildCoreGraph. This prevents the default behavior of connecting them outside the loop.
      Overrides:
      connectAfterModelHooks in class AbstractFlowGraphBuildingStrategy
      Throws:
      GraphStateException - if graph construction fails
    • getStrategyType

      public String getStrategyType()
      Description copied from interface: FlowGraphBuildingStrategy
      Returns the type identifier for this strategy. This is used for registration and lookup purposes.
      Returns:
      the strategy type identifier
    • generateKeyStrategyFactory

      public KeyStrategyFactory generateKeyStrategyFactory(FlowGraphBuilder.FlowGraphConfig config)
      Description copied from interface: FlowGraphBuildingStrategy
      Generates a KeyStrategyFactory based on the root agent and sub-agents.
      Parameters:
      config - the configuration containing agents
      Returns:
      the generated KeyStrategyFactory
    • validateConfig

      public void validateConfig(FlowGraphBuilder.FlowGraphConfig config)
      Validates loop-specific configuration requirements. - Loop strategy must be provided and be an instance of LoopStrategy - Exactly one sub-agent must be provided (loops execute a single agent repeatedly)
      Parameters:
      config - the configuration to validate