Introspecção
Nesta página
.transitions() retorna um array de registros { from, to, event, after? } que
descreve toda a tabela de transições da máquina — útil para geradores de
grafos, renderizadores de diagramas, validadores e para testar a própria
definição da máquina.
A anotação after Ns em uma transição registra um atraso informativo no campo
after do registro. O runtime não avança automaticamente após esse tempo;
combine com every/after do capítulo de concorrência se quiser transições
acionadas por tempo real.
Máquina Timed com anotações after; inspeciona a tabela e filtra a linha do evento "finish".
// Feature: `.transitions()` — list every transition the machine knows
// Syntax: returns an array of records `{ from, to, event, after? }`.
// Useful for tooling: graph generators, diagram renderers, validators.
// When to use: render the machine, generate documentation, diff two
// machine definitions, machine-test the machine.
// `after Ns` annotates a transition with a delay, recorded in the
// table but not enforced by the runtime — feed it to a scheduler if
// you need real time.
machine Timed {
state Idle, Running, Done
initial Idle
Idle -> Running on start after 0s
Running -> Done on finish after 5s
Done -> Idle on reset
}
let t = Timed.new()
t.send("start")
print(t.state)
// expected: Running
t.send("finish")
print(t.state)
// expected: Done
// Inspect the transition table — print only the "finish" row.
let trans = t.transitions()
for row in trans {
if row.event == "finish" {
print(row.event) // expected: finish
print(row.from) // expected: Running
print(row.to) // expected: Done
}
}
Como .transitions() retorna dados comuns — registros em um array — você pode
iterar, filtrar e serializar com qualquer ferramenta da linguagem, sem dependência
de APIs especiais de reflexão.
Veja também