Class TopologicalIndex

java.lang.Object
software.amazon.smithy.codegen.core.TopologicalIndex
All Implemented Interfaces:
KnowledgeIndex

public final class TopologicalIndex extends Object implements KnowledgeIndex
Creates a reverse-topological ordering of shapes.

This kind of reverse topological ordering is useful for languages like C++ that need to define shapes before they can be referenced. Only non-recursive shapes are reverse-topologically ordered using getOrderedShapes(). However, recursive shapes are queryable through getRecursiveShapes(). When this returned Set is iterated, recursive shapes are ordered by their degree of recursion (the number of edges across all recursive closures), and then by shape ID when multiple shapes have the same degree of recursion.

The recursion closures of a shape can be queried using getRecursiveClosure(ToShapeId). This method returns a list of paths from the shape back to itself. This list can be useful for code generation to generate different code based on if a recursive path passes through particular types of shapes.

  • Constructor Details

    • TopologicalIndex

      public TopologicalIndex(Model model)
  • Method Details

    • of

      public static TopologicalIndex of(Model model)
      Creates a new TopologicalIndex.
      Parameters:
      model - Model to create the index from.
      Returns:
      The created (or previously cached) TopologicalIndex.
    • getOrderedShapes

      public Set<Shape> getOrderedShapes()
      Gets all reverse-topologically ordered shapes, including members.

      When the returned Set is iterated, shapes are returned in reverse-topological. Note that the returned set does not contain recursive shapes.

      Returns:
      Non-recursive shapes in a reverse-topological ordered Set.
    • getRecursiveShapes

      public Set<Shape> getRecursiveShapes()
      Gets all shapes that have edges that are part of a recursive closure, including container shapes (list/set/map/structure/union) and members.

      When iterated, the returned Set is ordered from fewest number of edges to the most number of edges in the recursive closures, and then alphabetically by shape ID when there are multiple entries with the same number of edges.

      Returns:
      All shapes that are part of a recursive closure.
    • isRecursive

      public boolean isRecursive(ToShapeId shape)
      Checks if the given shape has edges with recursive references.
      Parameters:
      shape - Shape to check.
      Returns:
      True if the shape has recursive edges.
    • getRecursiveClosure

      public Set<PathFinder.Path> getRecursiveClosure(ToShapeId shape)
      Gets the recursive closure of a given shape represented as PathFinder.Path objects.

      The first element of each path is the given shape, and the last element of each path is the first shape that is encountered a second time in the path (i.e., the point of recursion).

      Parameters:
      shape - Shape to get the recursive closures of.
      Returns:
      The closures of the shape, or an empty Set if the shape is not recursive.