Class Scope

java.lang.Object
org.kigalisim.engine.state.Scope
All Implemented Interfaces:
UseKey

public class Scope extends Object implements UseKey
Object defining a scope within the engine including variables accessible from that scope.

Manages scope hierarchy and variable access across different context levels in the engine.

  • Constructor Details

    • Scope

      public Scope(String stanza, String application, String substance)
      Convenience constructor for creating a scope without a variable manager.
      Parameters:
      stanza - The name of stanza or null if in global scope
      application - The name of the application or null if in stanza or higher scope
      substance - The name of the substance or null if in application or higher scope
    • Scope

      public Scope(String stanza, String application, String substance, VariableManager variableManager)
      Create a new scope.
      Parameters:
      stanza - The name of stanza or null if in global scope
      application - The name of the application or null if in stanza or higher scope
      substance - The name of the substance or null if in application or higher scope
      variableManager - The variable manager to reach variables accessible from this scope or null if no variables accessible
    • Scope

      public Scope()
      Default constructor for creating a global scope with no context.

      Creates a scope with all empty contexts (stanza, application, substance, and key). This is useful for initializing a scope at the global level.

  • Method Details

    • getStanza

      public String getStanza()
      Get the name of the stanza where this scope resides.
      Returns:
      The name of the current stanza or null if in global scope
    • getStanzaOptional

      public Optional<String> getStanzaOptional()
      Get the name of the stanza where this scope resides as an Optional.
      Returns:
      Optional containing the name of the current stanza, or empty if in global scope
    • getApplication

      public String getApplication()
      Get the name of the application where this scope resides.
      Specified by:
      getApplication in interface UseKey
      Returns:
      The name of the current application or null if in stanza or higher scope
    • getApplicationOptional

      public Optional<String> getApplicationOptional()
      Get the name of the application where this scope resides as an Optional.
      Returns:
      Optional containing the name of the current application, or empty if in stanza or higher scope
    • getSubstance

      public String getSubstance()
      Get the name of the substance where this scope resides.
      Specified by:
      getSubstance in interface UseKey
      Returns:
      The name of the current substance or null if in application or higher scope
    • getSubstanceOptional

      public Optional<String> getSubstanceOptional()
      Get the name of the substance where this scope resides as an Optional.
      Returns:
      Optional containing the name of the current substance, or empty if in application or higher scope
    • getWithSubstance

      public Scope getWithSubstance(String newSubstance)
      Create a new scope derived from this scope at the substance level.
      Parameters:
      newSubstance - The name of the substance in which the new scope resides
      Returns:
      New scope at the given substance
    • getWithApplication

      public Scope getWithApplication(String newApplication)
      Create a new scope derived from this scope at the application level.
      Parameters:
      newApplication - The name of the application in which the new scope resides
      Returns:
      New scope at the given application
    • getWithStanza

      public Scope getWithStanza(String newStanza)
      Create a new scope derived from this scope at the stanza level.
      Parameters:
      newStanza - The name of the stanza in which the new scope resides
      Returns:
      New scope at the given stanza
    • defineVariable

      public void defineVariable(String name)
      Define a variable in the current scope.

      Define a new variable in the current scope or throw an error if a variable of this name already exists in this scope at the current context level.

      Parameters:
      name - The name of the variable to define
    • setVariable

      public void setVariable(String name, EngineNumber value)
      Set the value of a variable already defined.

      Set the value of a variable or throw an error if no variable of the given name is accessible from the current scope.

      Parameters:
      name - The name of the variable to set
      value - The new value of the variable
    • getVariable

      public EngineNumber getVariable(String name)
      Get the value of a variable already defined.

      Get the value of a variable or throw an error if no variable of the given name is accessible from the current scope.

      Parameters:
      name - The name of the variable to get
      Returns:
      Value of the given variable
    • getKey

      public String getKey()
      Get a unique key for this scope based on application and substance.

      The key is lazily initialized and cached. It consists of the application and substance names separated by a tab character, with "-" used for null values.

      Specified by:
      getKey in interface UseKey
      Returns:
      The unique key for this scope