moonbit-computer-interlocking

    MoonBit Railway Computer Interlocking (CI) Safety Logic Engine

    railway
    interlocking
    safety-critical
    computer-interlocking
    moonbit
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    2 months ago
    Downloads
    30

    No dependencies

    #MoonBit Computer Interlocking

    MoonBit CI License

    A pure MoonBit safety-logic engine for railway station computer interlocking. It models station topology, derives route locking rules, rejects hostile routes, supervises sectional release, and provides a deterministic 10 ms simulation cycle for repeatable testing.

    The project is a safety-logic prototype and verification-oriented simulator. It is not a certified railway signalling product and must not be connected to live signalling equipment without an independent safety case, hardware qualification, and domain-authorised deployment process.

    #Capabilities

    • Station graph modelling for track sections, turnouts, signals, edges, and route blueprints.
    • Transactional route locking with rollback when a switch, track, signal, or safety check fails.
    • Main-route and flank-protection locking, hostile-route rejection, and a fixed 256-bit RouteBitSet fast path.
    • Occupied-before-clear sectional release: a clear section that has never been occupied cannot be released.
    • Rich safety audits for diagnostics and a fixed-field safety summary for the scan cycle.
    • Fault-to-red fail-safe handling with explicit route-fault recovery.
    • Reusable standard, hostile-route, fault, and malformed-topology scenarios.
    • Native CLI demo and deterministic benchmark counters.

    #Architecture

    topology -> interlocking -> conflict | | | +----------> simulator <------+ | scenarios -> cli | diagnostics

    topology owns station infrastructure. interlocking owns route state, locking, release, and safety invariants. conflict provides the fixed-width hostile-route matrix and detailed topology checks. simulator executes the bounded event queue and fixed route-slot scan. diagnostics and scenarios provide higher-level reports and repeatable acceptance workloads.

    #Requirements

    • MoonBit stable toolchain. The current local verification uses MoonBit 0.1.20260819 and compiler 0.10.9.
    • Native target support for the CLI and wall-clock benchmark wrapper.

    #Quick start

    moon fmt --check moon check --deny-warn moon test --deny-warn --target all moon build --target native moon run --target native src/cli

    To inspect the generated public package interfaces:

    moon info

    #CLI

    The executable runs the interactive station demonstration when no command is provided:

    moon run --target native src/cli

    Focused commands are useful for scripts and review:

    moon run --target native src/cli -- status moon run --target native src/cli -- request 1 moon run --target native src/cli -- step 10 moon run --target native src/cli -- audit moon run --target native src/cli -- scenario standard moon run --target native src/cli -- scenario hostile moon run --target native src/cli -- scenario fault moon run --target native src/cli -- scenario topology moon run --target native src/cli -- benchmark

    The scenario commands return deterministic counters and a pass/fail result. The audit command runs the richer diagnostic report outside the hot scan path.

    #Safety model

    An entry signal may show a proceed aspect only after the route resources are locked and the fixed-field safety summary reports no fatal condition. The summary checks turnouts, flank protection, hostile routes, route sections, signal state, fault state, and sectional-release history.

    When a route section faults, the entry signal is forced to red and the active route enters RouteFault. Locked resources remain protected until an explicit cancel/recovery operation. Sectional release requires all of the following:

    1. The section was observed occupied.
    2. The section is currently clear.
    3. The train has entered the route.
    4. For downstream sections, the preceding section has already been released.

    The fixed scan path visits route slots 1 through 255 numerically and consumes counter-only release and safety results. The rich audit API allocates diagnostic arrays by design and should be called outside the 10 ms cycle.

    #Repeatable scenarios

    The src/scenarios package provides four reusable workloads:

    • standard: request a standard route, traverse every section, and complete legal sectional release.
    • hostile: activate one route and verify that its hostile counterpart is rejected by the bitset path.
    • fault: inject a detector fault, verify red signal and RouteFault, then recover through explicit cancellation.
    • topology: construct malformed geometry and verify structural diagnostics.

    #Testing and benchmark reproduction

    The repository CI runs formatting, strict checking, generated-interface drift checks, all-target tests with coverage, native build, and the deterministic benchmark. Run the local benchmark counters with:

    moon run --target native src/cli -- benchmark

    For repeated command-level wall-clock measurements on PowerShell:

    pwsh -NoProfile -File scripts/benchmark.ps1 -Runs 5

    The measured local reference and exact output are recorded in docs/benchmark-results.md. Wall-clock values depend on the operating system, CPU, cache state, and system load; CI checks the deterministic counters rather than using a machine-specific timing gate.

    #Repository layout

    src/topology station graph and structural validation src/interlocking route table, locking, release, audit, lifecycle src/conflict 256-bit hostile matrix and topology conflict checks src/simulator event queue, train model, and 10 ms scan kernel src/layouts reusable station blueprints src/diagnostics report aggregation and rendering src/scenarios repeatable safety workloads src/benchmarks deterministic benchmark runner src/cli native terminal entrypoint docs measured benchmark documentation

    #License

    This project is licensed under the Apache License 2.0.