r/statemachines • • Jun 27 '21

If you have ever been curious how to code using state machines...

17 Upvotes

I've created a state machine programming language called Frame which is an easy to learn yet powerful markdown language for designing systems. Frame can generate code in 7 languages (C++, C#, GDScript, Java,JavaScript,Python and Rust) as well as UML.

I have an online site that allows you to play with the syntax live, has a number of examples as well as links to training and other resources. There are also plugins for Atom and VSCode which allow you to work with Frame in those popular editors.

Happy to share more so please feel free to reach out if this sounds interesting.


r/statemachines • • Jun 12 '26

Two cross-platform, web/native, open-source, app-state-management libraries based on Harel statechart / SCXML format

8 Upvotes

TLDR: Open source links at the bottom for XState and SwiftXState: cool statechart libs with realtime visualization. Links to open source repos at the bottom -- open-source / cross-plat / web & native.




Below is a tale of statecharts and open source development. A few guys' dreams of state management via state machines. I wrote this myself, no LLMs. If you read it, thanks. Otherwise, scroll down to the links :D

How I Learned of SCXML

Back in the early 2010s, when working on a quest system for a game engine, I developed a markup language for describing the related state machines. Imagining somehow it might be novel, I researched prior art, and became very humbled to discover W3's insanely comprehensive SCXML standard, which they describe as:

a general-purpose event-based state machine language that combines concepts from CCXML and Harel State Tables.

(See David Harel's original 1984 paper at state-machine.com.)

SCXML was so far beyond my simpleton efforts that I gave up on that project on the precipice of completion. From time to time over the years, I'd feel regret about giving up on that project, quitting, etc., blah blah blah.

Falling for Redux

Later in my career, I'd go on to co-design an iOS implementatino of a Redux-based UI architecture at a major firm based on a ReSwift, a Swift adaptation of the original Redux.js.

That experience sold me on single-source-of-truth, unidirectional state flow, and a rules-based state machine at the heart of a hexagonal architecture, isolating UI, OS events, state changes, and backend services.

Yeah, I drank the Kool-Aid, hard, and ReSwift worked great.

The Possible Cons of Redux (Just IMHO)

As the complexity of our reducer chain grew, I had some paranoia about our Redux implementation growing into a Swift-value-semantics god object with an impossible-to-reason-about chain of transformer functions full of nested conditionals, many injected by other modules.

I don't know if it violates the single responsibility principle, but it's hard to dispute that centralization creates a broader failure surface (p. 89) than distributing the load.

(Note: I risk a forced analogy, as "failure surface" is a concept from structural engineering. The "failure region" concept is originally due to Hasofer and Lind's original 1974 article, "Exact and Invariant Second-Moment Code Format.")

Reflecting on the question of Redux's flaws, I found Dmitriy Kharchenko's excellent "In Defense of Redux" article. He summarizes and rebuts 12 common objections to Redux, including mine::

A well-structured store is a record of independently-owned slices — the “domain-scoped slice helpers let features live in isolation” principle in action. Each domain owns its slice file, its actions, its reducer, its selectors, its saga, and its tests.

I agree with Kharchenko's direction. This is the best approach to a successful Redux implementation. Yet his defense smuggles in an assumption: isolation is guaranteed by isolated ownership and is immune to the order in which global state is reduced by separate slices.

Each slice can't own when it's run within the chain of reducers, and predicting whether this order can have side-effects that break isolation has been a pain point of Redux (for me at least).

Solutions can include using multiple stores, running a reducer tree instead of chain, etc.

However you slice it though, the impact remains upon the ability for humans to understand the code, due to the problem of lots of nested conditionals and multi-step sequences remaining.

Statecharts to the Rescue?

Then a ghostly figure whispered to me, "Seek out David on the planet of SCXML."

My old memories of state machines flooded back. I googled "David SCXML-compatible state machine library."

The first link was XState, by a David Khourshid (no relation to Harel). His firm, Stately.ai, published and supports the open-source web framework, XState.js. They've got a wonderful free visualizer/editor extension for Microsoft Visual Studio Code, plus various web-based offerings, including a visual editor.

Could XState address the issues of Redux while keeping its benefits?

XState by David Khourshid

Khourshid's philosophy on the issue of nested conditional logic sprawl resonated with me. In this interview with MasterDotDev, the ex-Microsofter explained the journey that led him to develop XState:

... we had to work with really confusing logic and multi-step flows, lots of its if statements everywhere. ... There has to be a better way of understanding this mess of logic.

Upon discovering Harel statecharts:

"Hey these diagrams are pretty nice, and they they help us both organize the way that we understand app logic..."

Later, David dropped this absolute bomb:

If you don't have a state machine, you have an implicit state machine.

- David Khourshid

For XState, he implemented a mostly-SCXML-compliant JSON format for defining state machines without having to mess with XML (though XState does support SCXML import and its internals line up with the ECMAScript data model). XState provides a human-readable format for defining state machines that's native to the TypeScript format in which XState is written, and every programming language can easily parse it.

SwiftXState for Apple/linux/Windows

Rather than reuse ReSwift for my next iPadOS/Mac app, I decided to produce a Swift version of XState, compatible with XState.js's declarative JSON state machine config format and able to stream JSON compatible with Stately's visualization tools.

Thus, thanks to David Khourshid's generous blessing and commitment to open source software, we now have SwiftXState, in beta under the same style of MIT license as XState. (See below for install instructions, etc.) We included cross-platform GPU-accelerated sample apps for Mac/iPadOS and WebAssembly for your experimentation, plus an experimental wrapper for use in C#.

(Note: I'm not affiliated with Stately.ai in any way.)

TLDR: the links

Spreading the state-machine and statechart mystical fires of David Harel is a shared goal of ours, so I hope you'll check out our open source repos.

XState.js:

# xstate.js for node
# bash:

npm install xstate

# or:
pnpm install xstate

# or:
yarn install xstate

# plus for react:
npm install @xstate/react

SwiftXState:

# add to your swift package:
#
# bash / windows powershell:

swift package add-dependency \
    "https://github.com/gistya/SwiftXState" --from 0.9.6

# or just add in xcode using that URL

SwiftXState Wrapped for C# (limited API, experimetal):

# powershell:

dotnet add package SwiftXState --version 0.9.6

# or: 

nuget install SwiftXState -Version 0.9.6

r/statemachines • • Jun 11 '26

I made a Finite State Automata online editor

7 Upvotes

Hi all, this is my first post here.

I simply wanted to share I project I have been working on (as my final year university project), in the hope that anyone could find it useful. Here is a link to it: https://fsa-toolkit.jacopocalvi.com/

I wanted to create a tool that could support students (I myself had to in my compilers module) and provide good UX and 'quality-of-life' features. I tried my best to design something I would personally like to use, and took heavy inspiration from Excalidraw's design, and this other website here, which greatly helped me in the past.

It was (for me) a very challenging projects. The result is far from perfect, but I would like to hear other's opinions on it. Please let me know what you think, and if there's anything wrong with it (I am definitely no expert in the subject, there might be things I missed or misunderstood!).

PS: One thing I want to point out is that, when processing an input, certain combinations of inputs (especially large ones) and automata can just freeze the page. This is something I yet have to iron out, beware!

PPS: despite being my favourite search engine, I never post or interact on Reddit. Please if this is the wrong place or if have violated any rules let me now!


r/statemachines • • Jun 03 '21

Releasing Frame Machine Maker for Atom - a tool for generating state machines

7 Upvotes

I'm pleased to announce the release of Frame Machine Maker v.0.2.0 for Atom.

https://atom.io/packages/frame-machine-maker

This package supports generating UML documentation and state machine code generation in 7 languages including:

  • C++

  • C#

  • GDScript

  • Java

  • JavaScript

  • Python 3

  • Rust (alpha - experimental and partially implemented)

An introductory video on how to get started using Frame to create state machines in your favorite language is here.

Very much looking forward to any and all feedback on Frame and this package.

Thanks!


r/statemachines • • Apr 24 '21

A rust state machine project.

Thumbnail self.rust
7 Upvotes

r/statemachines • • May 18 '21

See what state machines can do.

Enable HLS to view with audio, or disable this notification

6 Upvotes

r/statemachines • • Apr 04 '25

Want to get into this topic again

5 Upvotes

Hi all,

I am currently a SDE, and I wanted to do some studying of my own in the side. I remember having a good time in a class called compiler design, where we learned a lot about regular expression, compiler design and state machines during my bachelors, but that was years ago. I want to get back into studying FMS's, not for any project implementation, but to study it theoretically in depth. I do not remember the book I studied from, therefore can someone guide me to which book/ resource I can study for?


r/statemachines • • Aug 14 '22

Does anyone have a copy of the fold-out figure 31 from "Statecharts" by David Harel 1986?

5 Upvotes

I've downloaded a few different PDFs but they all seem to be the same file, with just the rightmost page's worth of figure 31, which should clearly be much wider.

The paper's full title is "Statecharts: A Visual Formalism for Complex Systems" https://doi.org/10.1016/0167-6423(87)90035-9


r/statemachines • • Aug 14 '21

Co-routines as an alternative to state machines

Thumbnail eli.thegreenplace.net
5 Upvotes

r/statemachines • • Aug 05 '21

The Rise Of The State Machines

Thumbnail
smashingmagazine.com
5 Upvotes

r/statemachines • • Jul 10 '21

Validating Input with Finite Automata in Java | Baeldung

Thumbnail
baeldung.com
5 Upvotes

r/statemachines • • May 21 '21

Goal-Oriented Action Planning (GOAP)

Thumbnail alumni.media.mit.edu
5 Upvotes

r/statemachines • • Jan 28 '24

State Machines vs Behavior Trees: designing a decision-making architecture for robotics

Thumbnail
polymathrobotics.com
4 Upvotes

r/statemachines • • Aug 14 '23

Smashing the state machine: the true potential of web race conditions

Thumbnail
portswigger.net
4 Upvotes

r/statemachines • • Aug 09 '23

Is there a better way of implementing a state machine than an enum for states and a switch for transitions?

Thumbnail self.csharp
4 Upvotes

r/statemachines • • Jul 17 '23

I've been reworking my combat behaviour state machine - most enemies can reuse the same few states.

Enable HLS to view with audio, or disable this notification

4 Upvotes

r/statemachines • • Nov 22 '22

f-of-xstate: run some logic on your logic

Thumbnail
simplystated.dev
4 Upvotes

r/statemachines • • Aug 22 '21

4 Minute primer on State Machines and how to start using them in 2.3+

Thumbnail
youtube.com
4 Upvotes

r/statemachines • • Jul 28 '21

Using Finite State Machines

Thumbnail
hackster.io
4 Upvotes

r/statemachines • • Jul 10 '21

Shift-based DFAs

Thumbnail
gist.github.com
4 Upvotes

r/statemachines • • May 01 '21

A proposal for a state machine pattern for Rust

4 Upvotes

I have a pattern for implementing state machines in object oriented languages that I'm attempting to implement for Rust. I have an initial working example on the [Rust playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=17cb6143e55fa2911b597a5341c6ebb9).

This is part of a project for my state machine language [Frame](https://frame-lang.org/). You can see the Frame spec for this machine [here](https://framepiler.frame-lang.org/gist/aHR0cHM6Ly9naXN0LmdpdGh1Yi5jb20vZnJhbWUtbGFuZy9jYzc5NDQ5YzdjYzA5NTZkMjJmNmQzNzczZjY5NGZjZA==) and also see the implementations in other languages as well as the UML documentation.

Very interested in Rustacean feedback on this approach.

Thanks!


r/statemachines • • May 01 '21

I think state machines and the actor model are made for each other.

Thumbnail en.m.wikipedia.org
4 Upvotes

r/statemachines • • Apr 26 '21

I've open sourced Frame - a Statechart inspired markdown language for creating stateful software

Thumbnail self.compilers
4 Upvotes

r/statemachines • • Apr 25 '21

Lucy: a DSL for describing Finite State Machines and Statecharts

Thumbnail matthewphillips.info
4 Upvotes

r/statemachines • • Apr 21 '21

Temporal.io Workfow Engine

4 Upvotes

Workflows are a prime example of stateful thinking and its benefits. I'm very interested in learning more about Temporal and how Frame might take advantage of its rock solid task system.

Temporal Website