Class ModuleCohesionTracker

java.lang.Object
org.ek9lang.compiler.phase5.ModuleCohesionTracker

class ModuleCohesionTracker extends Object
Tracks module-level cohesion by recording constructs and their intra-module type references. Used to detect modules with low cohesion (disconnected construct groups that could be separate modules).

Thread-safe: designed to be used during parallel file processing where multiple PreIRListener instances (one per file) share a single ModuleCohesionTracker.

Cohesion is calculated using connected component analysis (Union-Find), similar to LCOM4:

  • Nodes = constructs (classes, records, traits, functions) in the module
  • Edges = intra-module type references (construct A references type B, both in same module)
  • Connected components = groups of related constructs
  • More components = lower cohesion = module should possibly be split

Based on package cohesion principles (Martin 2002 "Agile Software Development").

Complexity: O(C + R) where C = constructs, R = references - linear, not N-squared.

  • Constructor Details

    • ModuleCohesionTracker

      ModuleCohesionTracker()
  • Method Details

    • registerConstruct

      void registerConstruct(String moduleName, String constructFqn)
      Register a construct (class, record, trait, function, etc.) as belonging to a module.
      Parameters:
      moduleName - the module where the construct is defined
      constructFqn - the fully qualified name of the construct
    • recordReference

      void recordReference(String moduleName, String fromConstructFqn, ISymbol referencedType)
      Record a type reference from one construct to another. Only records if both constructs are in the same module (intra-module reference).
      Parameters:
      moduleName - the module being analyzed
      fromConstructFqn - the construct making the reference
      referencedType - the type being referenced
    • calculateDisconnectedGroups

      int calculateDisconnectedGroups(String moduleName)
      Calculate the number of disconnected construct groups in a module. Uses Union-Find algorithm for O(C * alpha(C)) complexity.

      This method is NOT thread-safe and should only be called during the sequential validation phase after all parallel file processing is complete.

      Parameters:
      moduleName - the module to analyze
      Returns:
      the number of disconnected groups (1 = fully cohesive, more = less cohesive)
    • getConstructCount

      int getConstructCount(String moduleName)
      Get the count of constructs registered in a module.
      Parameters:
      moduleName - the module to check
      Returns:
      count of constructs
    • getConstructs

      Set<String> getConstructs(String moduleName)
      Get all constructs registered in a module (for tooling/diagnostics).
      Parameters:
      moduleName - the module to check
      Returns:
      immutable set of construct FQNs (may be empty, never null)
    • getTrackedModules

      Set<String> getTrackedModules()
      Get all tracked module names.
      Returns:
      immutable set of module names being tracked
    • isExempt

      static boolean isExempt(String moduleName)
      Check if a module is exempt from module-level cohesion threshold checking. Built-in modules (org.ek9.*) are exempt because they are utility modules by design.
      Parameters:
      moduleName - the module to check
      Returns:
      true if exempt (built-in modules)