本项目为 MoonBit 生态系统提供了一个轻量级、类型安全且高可扩展的有限状态机 (Finite State Machine, FSM) 核心引擎。 在复杂系统工程中(例如 WebAssembly 游戏开发中的 NPC 逻辑控制、UI 组件的复杂状态流转,以及网络协议栈的握手解析),状态机是必不可少的基础设施。然而,目前 MoonBit 官方及第三方包管理平台中尚缺乏通用的状态图控制框架
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
MoonBit-FSM
moon-fsmis an auditable finite state machine component for MoonBit projects that need workflow-style state progression without adopting a full BPM runtime. The remediation release for OSC2026 focuses on reviewable engineering value: structured transition errors, context-updating actions, execution history, validator reports, workflow-oriented examples, and reproducible CI.Package Identity
Rz-coder8848/moon-fsm0.1.1Why This Library
Typical uses for this package include:
The library keeps the public surface small while making the transition path auditable in code review and acceptance review.
Re-Review Capabilities
try_send(event) -> Result[Unit, TransitionError].send(event) -> Result[Unit, String].history().on_enter/on_exit.Install
Minimal Example
Workflow Example
The remediation release adds a composite approval workflow example:
Run it locally with:
The vending machine example was further revised after the formal acceptance feedback on July 17, 2026. It no longer relies on duplicate
(state, event)definitions, and now demonstrates a blocked purchase attempt followed by a successful retry after more coins are inserted.Core API
Builder::new()creates a workflow definition.transition()andtransition_if()add plain and guarded transitions.transition_do()andtransition_if_do()attach context-updating actions.build()materializes anEngine.try_send()returnsTransitionErrorvalues for structured handling.history()returns successful transition records.last_error()exposes the most recent runtime failure.validate_report()summarizes reachability and duplicate-definition issues.to_mermaid()exports reviewer-friendly diagrams.API details live in docs/api_reference.md.
Examples
moon run examples/traffic_lightmoon run examples/vending_machinemoon run examples/game_npcmoon run examples/approval_workflowmoon run cmd/fsm-cliVerification
The current MoonBit 0.10.3-compatible verification set is:
moon fmt --deny-warnandmoon info --deny-warnare not used because the current CLI does not expose those flags; the repository instead runs the equivalent supported checks above. On Windows machines without a system C compiler, localmoon test --deny-warn --target allmay stop at thenativetarget; the CI workflow remains the source of truth for full multi-target coverage.Release Alignment
0.1.0was the initial Mooncakes publication.0.1.1is the OSC2026 re-review remediation release.Documentation
Notes For Reviewers
Contributing
Small, reviewable changes are preferred. Before opening a PR, run the same verification commands listed above and keep examples executable.