CryptoPatrick / code

Code · Formal models

harel

Parse, validate and write W3C SCXML statecharts in Rust.

What it is

David Harel’s statecharts extend finite state machines with nested states, parallel regions and history, which makes complex behaviour (a microwave, a traffic light, a voice dialogue) much easier to describe. SCXML is the W3C standard for writing statecharts as XML.

harel parses SCXML documents into typed Rust structures (<state>, <parallel>, <final>, transitions, data model, <invoke>), checks that they are well formed, and writes them back out as XML.

Using it

use harel::{parse_scxml, validate, to_xml};

let xml = std::fs::read_to_string("examples/microwave.scxml")?;
let chart = parse_scxml(&xml)?;     // typed statechart

validate(&chart)?;                  // structural checks (targets exist, ids are unique, ...)
println!("{} top-level states, initial = {:?}", chart.states.len(), chart.initial);

let round_trip = to_xml(&chart);    // back to SCXML

parse_scxml_with_options adjusts how strictly the input is read. The repository ships example charts: a microwave (also a parallel variant), a traffic light, a calculator and a blackjack game.

Ideas for using it

  • Model checking input. Translate a statechart into a transition system and check safety properties with a model checker, for example “the door never opens while heating”.
  • Lint statecharts in CI. Validate every .scxml file in a repository on each commit.
  • Generate code or diagrams. Walk the typed structure to emit a Rust state machine or a Graphviz diagram.

Status

Prototype: parsing, validation and writing work; there is no interpreter that runs a chart.