Class
MTLGenerator
Main execution engine for MTL model-to-text transformations.
@MainActor final class MTLGenerator
Overview
The MTL generator orchestrates the execution of MTL templates, managing the execution context, template invocation, query evaluation, and generation statistics. It coordinates all aspects of text generation from model inputs.
Overview
The generator provides:
-
Template execution: Invoke templates with parameter binding
-
Query evaluation: Execute side-effect-free queries
-
Macro expansion: Expand macro invocations (Advanced feature)
-
Statistics tracking: Monitor execution performance and outcomes
-
Debugging support: Optional detailed execution logging
-
Module support: Handle module inheritance and imports (Advanced feature)
Basic Usage
// Create module with templates
let module = MTLModule(
name: "MyGenerator",
metamodels: [:],
templates: [
"main": MTLTemplate(
name: "main",
parameters: [MTLVariable(name: "model", type: "Model")],
body: ...,
isMain: true
)
]
)
// Create generator
let strategy = MTLInMemoryStrategy()
let generator = MTLGenerator(module: module, generationStrategy: strategy)
// Register models
var models: [String: Resource] = [:]
models["IN"] = inputModel
// Generate
try await generator.generate(
mainTemplate: "main",
arguments: [modelRoot],
models: models
)
// Check results
print(generator.statistics.summary())
Template Execution
Templates are executed with parameter binding:
// Template with parameters
let template = MTLTemplate(
name: "generateClass",
parameters: [
MTLVariable(name: "class", type: "Class"),
MTLVariable(name: "package", type: "String")
],
body: ...
)
// Execute with arguments
try await generator.executeTemplate(
template,
arguments: [classElement, "com.example"]
)
Statistics
The generator tracks detailed statistics:
-
Execution time
-
Success/failure status
-
Templates executed
-
Files generated
-
Lines generated
-
Protected areas preserved
-
Last error (if any)
print("Execution time: \(generator.statistics.executionTime)s")
print("Templates executed: \(generator.statistics.templatesExecuted)")
print("Files generated: \(generator.statistics.filesGenerated)")
Topics
Initializers
Creates a new MTL generator for the specified module.
Instance Properties
The MTL module being executed.
Statistics about the current/last generation run.
Instance Methods
Enables or disables debug mode.
Executes a query and returns its result.
Executes a template with the specified arguments.
Expands a macro invocation with the specified arguments.
Executes the specified main template with the given arguments.
Returns a detailed summary of the current statistics.