FSM (8): deferred events + richer reactive DSL — completes the upgrade
Final phase of the FSM upgrade. Extends the reactive transition DSL: - Deferred events: on.defer<E>() holds an E that arrives while in the state (instead of dropping it) and re-posts it to the mailbox after the next transition (UML deferred-event semantics). The reactive loop awaits the declared + deferred types, accumulates held envelopes, and re-injects them via Machine::repost() on exit. - Entry/exit actions: on.enter(fn) / on.exit(fn) run when the reactive state is entered / left (around the wait loop). - Internal transitions: on.event<E>().internal(fn) runs fn on E but stays in the state — no exit/enter, no transition (EventEntry gains an `internal` flag; the loop runs the action and keeps waiting). 2 new tests (Defer): a deferred Click is held in Busy and replayed in Idle after Done (with enter/exit ordering asserted); three internal Ticks accumulate with the state entered exactly once. 1002 tests (999 green, 3 skipped), ASan/UBSan clean, -Werror clean. This finishes the requested FSM upgrade: one signature-inferred State, typed Success/Failure results, multi-waiter mailbox, parallel/orthogonal regions, history states, and the deferred/internal/entry-exit DSL. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2903eca3a6
commit
4af3ee6d0c
5 files changed
+216
-7
No files matched your search
@@ -0,0 +1,119 @@
|
||||
#include <Void/Fsm/Fsm.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::Step;
|
||||
namespace Fsm = Void::Fsm;
|
||||
|
||||
// Phase-6 reactive DSL: deferred events (held + replayed after a transition),
|
||||
// entry/exit actions (on.enter/on.exit), and internal transitions (handle an
|
||||
// event without leaving the state).
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Click {
|
||||
};
|
||||
struct Done {
|
||||
};
|
||||
|
||||
// Busy defers Click (held, not dropped) and runs enter/exit actions; on
|
||||
// Done it transitions to Idle, which then handles the replayed Click.
|
||||
class Flow : public Fsm::Machine<Flow>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
std::vector<std::string> log;
|
||||
|
||||
struct Busy : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
on.enter([](Flow& m) { m.log.push_back("enter"); });
|
||||
on.exit([](Flow& m) { m.log.push_back("exit"); });
|
||||
on.template defer<Click>();
|
||||
on.template event<Done>().template to<Idle>();
|
||||
}
|
||||
};
|
||||
struct Idle : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
on.template event<Click>()
|
||||
.action([](Flow& m, const Click&) {
|
||||
m.log.push_back("click");
|
||||
})
|
||||
.template to<End>();
|
||||
}
|
||||
};
|
||||
struct End : State {
|
||||
Task<Step> run() { co_return succeed(1); }
|
||||
};
|
||||
using States = Fsm::States<Busy, Idle, End>;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// @tag integration
|
||||
Void::Testing::Test deferred_event_replays_after_transition()
|
||||
{
|
||||
Flow fsm;
|
||||
fsm.start();
|
||||
fsm.send(Click{}); // deferred while in Busy (not dropped)
|
||||
fsm.send(Done{}); // Busy -> Idle; the held Click is replayed -> End
|
||||
const auto outcome = fsm.wait();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 1);
|
||||
// enter/exit actions fired around Busy; the deferred Click was handled.
|
||||
const std::vector<std::string> expected{"enter", "exit", "click"};
|
||||
Void::Testing::expect(fsm.log == expected);
|
||||
}
|
||||
|
||||
// ── internal transition ─────────────────────────────────────────────────────
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Tick {
|
||||
};
|
||||
struct Stop {
|
||||
};
|
||||
|
||||
// Counter handles Tick internally (no exit/enter, no transition); Stop
|
||||
// leaves with the accumulated count.
|
||||
class Counter : public Fsm::Machine<Counter>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
int ticks = 0;
|
||||
int enters = 0;
|
||||
|
||||
struct Run : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
on.enter([](Counter& m) { ++m.enters; });
|
||||
on.template event<Tick>().internal(
|
||||
[](Counter& m, const Tick&) { ++m.ticks; });
|
||||
on.template event<Stop>().template to<Done>();
|
||||
}
|
||||
};
|
||||
struct Done : State {
|
||||
Task<Step> run() { co_return succeed(self().ticks); }
|
||||
};
|
||||
using States = Fsm::States<Run, Done>;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// @tag integration
|
||||
Void::Testing::Test internal_transition_stays_in_state()
|
||||
{
|
||||
Counter fsm;
|
||||
fsm.start();
|
||||
fsm.send(Tick{}, Tick{}, Tick{}); // handled internally, no re-entry
|
||||
fsm.send(Stop{});
|
||||
const auto outcome = fsm.wait();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 3); // three internal ticks
|
||||
Void::Testing::expect_eq(fsm.enters, 1); // entered Run exactly once
|
||||
}
|
||||
Reference in new issue
Block a user