Class
MTLExecutionContext
Manages the execution state for MTL template generation.
@MainActor final class MTLExecutionContext
Overview
The execution context maintains all runtime state during template execution, including variable bindings, writer stack, indentation, protected areas, and model access. It coordinates between MTL templates and the underlying AQL expression evaluator.
Overview
The execution context provides:
-
Variable scoping: Stack-based variable scopes with push/pop operations
-
Expression evaluation: Integration with AQL for expression evaluation
-
Indentation management: Stack-based indentation tracking
-
Writer management: Stack of writers for nested file blocks
-
Protected areas: Preservation of user code sections
-
Trace support: Model-to-text traceability links
-
Model access: Registration and access to input models
Variable Scoping
Variables are managed in a stack of scopes:
context.setVariable("x", value: 10) // Global scope
context.pushScope()
context.setVariable("x", value: 20) // Local scope (shadows global)
await context.getVariable("x") // Returns 20
context.popScope()
await context.getVariable("x") // Returns 10
Writer Stack
File blocks create new writers that are pushed onto a stack:
// Main output (stdout)
context.write("Top level")
// Open file block - pushes new writer
try await context.openFile(url: "output.txt", mode: .create, charset: "UTF-8")
context.write("File content") // Goes to output.txt
try await context.closeFile() // Pops writer, finalizes file
context.write("Top level again") // Back to stdout
Example Usage
let module = MTLModule(...)
let strategy = MTLInMemoryStrategy()
let context = MTLExecutionContext(
module: module,
generationStrategy: strategy
)
// Register models
await context.registerModel("input", resource: inputModel)
// Execute template
context.setVariable("model", value: modelRoot)
let template = module.templates["generateClass"]!
// ... execute template body ...
// Finalize
try await context.finalize()
Topics
Initializers
Creates a new MTL execution context.
Instance Properties
Returns the current indentation level.
Whether debug mode is enabled.
The MTL module being executed.
Returns the protected area manager for advanced operations.
Instance Methods
Adds a traceability link from a source model element to generated text.
Closes the current file, finalizing its content and popping its writer.
Returns a summary of the current execution state for debugging.
Evaluates an MTL expression using the AQL evaluator.
Finalizes the execution context after generation completes.
Returns the currently generated text from the main writer.
Retrieves a registered model by alias.
Retrieves the preserved content for a protected area.
Returns all recorded trace links.
Retrieves a variable from the current or enclosing scopes.
Opens a new file for output, pushing a new writer onto the stack.
Pops the current indentation level from the stack.
Pops the current variable scope from the stack.
Pushes a new indentation level onto the stack.
Pushes a new variable scope onto the stack.
Registers an input model for template access.
Scans a file for protected areas before regeneration.
Sets the preserved content for a protected area.
Sets a variable in the current scope.
Writes text to the current writer.
Writes a line of text followed by a newline to the current writer.
Relationships
Conforms To
See Also
Execution
Protocol for MTL text generation output strategies.