Giter Club home page Giter Club logo

Comments (4)

andrew-gresyk avatar andrew-gresyk commented on August 25, 2024

State IDs are 0-based, steadily increasing for every (including empty) state in the hierarchy.

They can be queried using static StateID stateId<TState>() function available on all Roots, Controls and Plans.

Example

from hfsm2.

moonxorsun avatar moonxorsun commented on August 25, 2024

Thanks for responding so quickly to my request.

Sorry, i'm not native English, I‘m not sure if I have expressed it correctly.

let's example basic_audio_player

int
main() noexcept {
    // construct everything
    Context title;
    Logger logger;
    FSM::Instance machine(title, &logger);

    // do the work :)
    machine.react(Play{"any"});            // Idle -> Playing
    machine.react(Stop{});                 // Playing -> Idle

    if (machine.isActive<Idle>()) {
        // do something
    } else if (machine.isActive<Playing>()) {
        // do something
    } else if (machine.isActive<Paused>()) {
        // do something
    } else {
        // do something
    }

/*
    switch (machine.current_state_id()) { // my stupid idea
    case FSM::stateId<Idle>(): {
        // do something
        break;
    } case FSM::stateId<Playing>(): {
        // do something
        break;
    } case FSM::stateId<Paused>(): {
        // do something
        break;
    } default:
        // do something
    }
*/

    return 0;
}

So, what is elegant approach that implement switch (machine.current_state_id()).

Thanks again,

Kiwi

from hfsm2.

andrew-gresyk avatar andrew-gresyk commented on August 25, 2024
  1. A hierarchical FSM might or might not have only one active state. E.g. in test_internal_transitions, when B_1_1 is active, B and B_1_2 are also active.
    Flat state machines do though, and FFSM2 does have StateID Root::activeStateId().
  2. The case you're describing is what we can call 'a passive FSM', i.e. when external-to-FSM code is checking its current configuration and does something based on that (let's call it a passive FSM).
    Alternatively, I prefer using a different paradigm (active FSMs), by moving the logic dependent on which state is active into the state methods.
    I.e. the logic from the if statement in your example above I'd move into another react() method for all relevant states:
struct DoMoreWork {}; // new event

struct Idle : Base {
    void react(const DoMoreWork& event, FullControl& control) {
        // do more work here
    }
};

struct Playing : Base {
    void react(const DoMoreWork& event, FullControl& control) {
        // do more work here
    }
};

struct Paused : Base {
    void react(const DoMoreWork& event, FullControl& control) {
        // do more work here
    }
};

int main() {
    // ...

    machine.react(DoMoreWork{}); // more work gets done here
}

from hfsm2.

moonxorsun avatar moonxorsun commented on August 25, 2024

I got it now, thank you

from hfsm2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.