ifandelse / machina.js

js ex machina - finite state machines in JavaScript
http://machina-js.org/
Other
1.93k stars 147 forks source link

Getting comprehensive state of hierarchical FSM #92

Closed insonifi closed 9 years ago

insonifi commented 9 years ago

Hello, first of all, I want to express my gratitude for your work. I've been looking for appropriate solution for my current project and FSM seems to be the right choice. So, here's my question. I need to make a hierarchical FSM, to track state of alarm detector. Here's an example:

var alarm = new machina.Fsm({
    namespace: 'alarm',
    initialState: 'idle',
    states: {
        alarmed: {
            md_stop: 'idle'
        },
        idle: {
            md_start: 'alarmed'
        }
    }
});
var detector = new machina.Fsm({
    namespace: 'detector',
    initialState: 'disarmed',
    states: {
        armed: {
            _child: alarm,
            disarm: 'disarmed'
        },
        disarmed: {
            arm: 'armed'
        }
    }
});
detector.handle('arm');
detector.handle('md_start');
console.log(detector.state);

In above example, I was expecting to see something like: armed.alarmed. But in fact, only get top-level FSM state. Is it possible to get combined state of all chained FSMs?

ifandelse commented 9 years ago

@insonifi This is definitely something I've been planning to add. Would it work for your needs if it was an additional property, something like compositeState or fullState? That's the direction I've been leaning in lately, mainly because it would be a breaking change otherwise, and also because I still need to be able to track each FSM's state individually.

insonifi commented 9 years ago

Absolutely. At the moment I have no preference, since I just started to implement it in my code. And I don't have any intention on breaking stuff. To my ear compositeState is more sound.

ifandelse commented 9 years ago

@insonifi Sounds good! It's a holiday here in the US, so I hope to get some time in on this later today...will keep you posted.

ifandelse commented 9 years ago

@insonifi I'm curious - are you using machina primarily in node, the browser, or both? If you're using it in the browser, do you have to support older browsers without Object.defineProperty? I have a working compositeState approach locally that is a method call, but I'm really tempted to roll this as a feature-detected add-on and make it a property with a getter only. Maybe that's a bad idea :smile: - I tend to err on the side of wider support, just so tired of IE8 and the like.

insonifi commented 9 years ago

@ifandelse I use browser and my personal requirement for this particular project was: green browsers only. :) Because I'm using vanilla WebSockets. Also in my company we don't support anything lower than IE9. Still I recognize that wider support is what we should aim for, in general.

insonifi commented 9 years ago

@ifandelse Is there any chance you already have some ETA in your mind? I'm eager to start using it in my project :)

ifandelse commented 9 years ago

@insonifi Sorry - other projects sucked up my free time this past week. I have it working, just need to add some unit tests. I'm travelling this evening, but might be able to add the tests and publish it tonight when I get to the hotel.

insonifi commented 9 years ago

@ifandelse sorry I'm pushing it. I really appreciate your effort! And looking forward to have it working.

ifandelse commented 9 years ago

@insonifi OK - it's up (v1.1.0). Turns out, I did need to use a method call instead of a property...primarily because it made the most sense for BehavioralFsm instances (where client/data is separate from the FSM itself). I've updated the README and am about to update the wiki. Keep me posted on how this works out for you.

insonifi commented 9 years ago

Sorry, I was carried away by some UI issues. I'll start working on it right away.

ifandelse commented 9 years ago

@insonifi no worries at all! :smile: I'd pull latest (v1.1.2).

insonifi commented 9 years ago

:) Oh, i'm falling behind. I'll update.