Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Kris Jusiak
@krzysztof-jusiak

msm-lite: C++14 Meta State Machine Library

Yours scalable C++14 header only eUML-like meta state machine library with no dependencies
Kris Jusiak
@krzysztof-jusiak
Version 1.0.0 released!
Kris Jusiak
@krzysztof-jusiak

@Speckobst
Hi, short question: Is it possible to access a member of an event from within a guard? E.g. event e1 has the member 'id' and the guard has to check if the given id is known.
@Speckobst
In the meantime I found out it's possible: 'auto guard = [ ] (auto ev) {...}' is all I had to write.
@Speckobst
Follow up question: How can I access members of the Sourcestate, the Targetstate or the Fsm itself from within a guard?
@krzysztof-jusiak

  1. Yea, as you said, auto guard = { return event.id == 42; };
  2. No, msm-lite has different approach towards states as they don't store objects, instead objects might be injected into guards/actions via SM constructor
    auto guard = { ... };
    and then sm{42, data, more_data}// order is not important, it's better to use di for that tho
  3. You can access sm, however, it's not advised
    auto guard = [] (sm<auto>& fsm, auto event, external_data&) { ... }

Check out full code samples here -> http://boost-experimental.github.io/msm-lite/examples/index.html
@Speckobst
Thanks alot! What is the idiomatic way if you have some data that is generated by an event (e.g. path to x,y) that can't be stored as a state but has to be saved anyway? E.g. an event requests to drive to position x,y and the appropriate path is generated. After leaving and reentering the subchart (e.g. pause) either the position or the path needs to be retained. I don't know if this is the idiomatic way for boost::msm, but it is feasible. Any ideas how to translate this to msm-lite? I'd love to try out this library in our project.
@krzysztof-jusiak

I would just create an object to store required data and inject it into all required guards/actions.

Something like that:

struct drive { position pos; }; // event
struct data { position from; position to; };

auto is_not_london = [](const data& d) { return d.to != London; }
auto action = [](data& d, auto event) { d.path = event.to; }

"InA"_s + event<drive> [ is_not_london ] / [](data& d, auto event) { d.from = event.pos; } = "InB"_s
"InB"_s + event<drive> / [](data& d, auto event) { d.to = event.pos; } = "Go"_s
"Go"_s + event<update> / [](data& d) { ... use d.to/d.from here... }

@Speckobst
Okay, so I first have to check if I can restructure my current sm to fit to msm-lite. Thanks again for your help!


I copied discussion here as it was in the boost.di chat, sorry about the confusion.

Uli
@ubeh
Have you tried using forward declarations with msm-lite? I tried to define a composite fsm in a class definition and forward declare it in the class declaration but I had no success. The problem is that I could not specify the correct concrete type of the composite fsm and/or provide the correct constructor: struct composite_sm : public msm::sm<composite> { composite_sm(composite& c, sub_sm& s) : msm::sm<composite>(c, s) {} };
Compare with your example. Any hint on how to fix this is very welcome. I have put a minimal example into a github repository.
Kris Jusiak
@krzysztof-jusiak
Thanks @Speckobst for the example, forward declarations should be supported as complete types are only required for guards and actions, however, there might be some issues with that. I will take a look into it, can you elaborate a bit what exactly you were using as a forward declaration please, so that I will be able to reproduce, cheers
Uli
@ubeh
I'm pleased to do so. I will go over the example to make it more clear what my intentions are and what went wrong! I will also provide a working example with boost:msm. I'll give you an update here when I have had time to do it.
Kris Jusiak
@krzysztof-jusiak
great, thanks @Speckobst
kohjaen
@kohjaen
Hi there. Firstly, thanks for this useful bit of code. Have been using boost msm for some time, and really like it. And this is great for smaller platforms. I have a simple question, and please forgive me if the answer is there, but i haven't read it yet : is there support for deferred events? Once again many thanks...I hope the boost libs remove this from being experimental to a core part of the library...
Kris Jusiak
@krzysztof-jusiak
Hi @kohjaen. Sorry for the late response. Somehow, I didn't get any notification about your message or it ended up in the spam folder. Anyway, the newest master supports deferred events so you can give it a try. Examples, here -> https://github.com/boost-experimental/msm-lite/blob/master/test/ft/ft_defer.cpp
Kris Jusiak
@krzysztof-jusiak
MSM-lite has changed name to SML (State Machine Lite/Language/Library) in order to avoid clashes with Boost.MSM library
kohjaen
@kohjaen
Hi @krzysztof-jusiak . No worries and many thanks. Deferred states are not an often used feature (in the projects I have worked on I have used them maybe 3 times), but one that are the solution to a few problems in a complex system. At the moment I am working on micro controllers, so 'simple' stuff, and will be using your SML there. Thanks once again, will try out the deferred SM features you added...
Egor Pugin
@egorpugin
Great news, thanks!
RegardsIM
@RegardsIM
Hello Kris, any news about support of exception handling in VS 2015? Now I receive "sml.hpp(71): error C2062: type 'void' unexpected" as soon as I try to throw std::exception from the action body
Uli
@ubeh
Hello Kris, sorry for the delay, but I have finally found time to come back to my old problem (see chat from 2016-05-03): I want to forward declare my FSM like I did back when I used boost::msm. Unfortunately, forward declarations and template classes don't go very well.
I have set up a repository at https://github.com/Speckobst/fsm_examples to clarify the problem. Example 1 and 2 show solutions with an interface or rather with a forward declaration of the fsm. Example 3 is the reimplementation of example 1 with boost:sml. Unfortunately, I could not reimplement example 2 with boost::sml.
Uli
@ubeh
I would be grateful, if you could look into this.
Uli
@ubeh
Ah, and by the way, I'm very pleased with SML! I'm using it as a direct replacement for boost::msm / smach in a scientific project and it's fantastic! Great syntax, extensive functionality, low compile times.
Uli
@ubeh
The URL to my examples has changed. You find the examples under: https://github.com/ubeh/fsm_examples
Uli
@ubeh
Good news, @krzysztof-jusiak , I solved the problem. :smile: I have updated the example in case anybody else encounters a similar problem and needs a working example.
kohjaen
@kohjaen
Hi @krzysztof-jusiak, once again, many thanks for this awesome little library. I come from using boost msm to using msm-lite, to sml. One thing which I still havent figured out a workaround for, is when I use states to limit actions (i.e. only a certain thing can happen in a certain state) -> so its a no-transition (i.e. UML diagram is an arrow starting and ending at the same state with a simple action, no changing of states). What I find is that, if I have on_entry/on_exit defined, these are always called when that happens. I know for certain that boost msm doesnt call on_entry/on_exit in this case (I rely on this for my designs). Any ideas/workarounds? Many thanks once again...
kohjaen
@kohjaen
PS: the way this is achieved using boost msm is to use a 'none' for the next state in the transition table...
kohjaen
@kohjaen
Hey Uli...yeah! Soooooo simple...many many many thanks, and apologies for missing it...it would appear there is lots of voodoo magic behind these variadic templates! best regards.
Uli
@ubeh
No problem, happy to help!
Richard Hodges
@madmongo1
hi there, how do I get a reference to the state machine from an action?
I want an action to start a delay which will eventually post an event back into the state machine, so I need to capture a reference to the state machine
Richard Hodges
@madmongo1
hmm. almost there. How do I get an sm from an sm_impl?
auto start_killin = {
std::cout << typeid(sm).name() << std::endl;
auto& io = sml::aux::get<goblin_io&>(deps);
io.kill_timer.expires_from_now(boost::posix_time::seconds(1));
io.kill_timer.async_wait(io.strand.wrap(& {
if (not err) {
sml::back::sm_impl
// sm.process_event(kill_a_pleb());
}
}));
};
Richard Hodges
@madmongo1
Ah, no worries. I figured it out. This part of the interface may need some work. This is a common pattern for me as I use state machines in io-intensive programs.
Having said that, this is way better than MSM etc.
auto start_killin = [](auto&& event, auto&& sm, auto&&deps, auto&& subs) {
    std::cout << typeid(subs).name() << std::endl;
    std::cout << typeid(sm).name() << std::endl;
    auto& io = sml::aux::get<goblin_io&>(deps);
    io.kill_timer.expires_from_now(boost::posix_time::seconds(1));
    io.kill_timer.async_wait(io.strand.wrap([&](auto err) {
        if (not err) {
            sm.process_event(kill_a_pleb(), deps, subs);
        }
    }));
};
and can we have moveable events please? I often need to pass events over a thread boundary or carry function objects in events (e.g. completion handlers). It would be nice if the events could be std::moved through the state machine rather than copied.
Richard Hodges
@madmongo1
ok, sorry to spam this chat. I have created a little demo that performs interaction between async asio and boost.sml.
First, if you find it useful please by all means use it in the documentation.
Second, I'd be grateful if you could critique the techniques used - particularly around my hack to re-post events to the state machine from a completion handler initiated by an action. Notice I have to manufacture a pseudo-event. I strongly suspect that there's a better way.
https://github.com/madmongo1/sml-goblinz
Richard Hodges
@madmongo1
pushed another branch async_events_from_substates where I am trying to get an asio event into a substate in order to trigger a state transition out and back into the enclosing state. Can't for the life of me figure out how to do that.
md-rafi
@md-rafi
Hello All, I am a new bee to state machines and I was exploring Boost State Chart and MSM.
But recently i came to know about Boost.SML. I would like to know is it a better option to go with SML instead of Boost MSM ?
My requirement is to have state machines in my project which should allow to make state machines configurable and should not demand compilation everytime.
Jeffrey Graham
@jeffreyscottgraham
Any plan to support scxml?
Jeffrey Graham
@jeffreyscottgraham
Where can I find the code for the "system" example from emBO++2017 talk "Embedding A META State Machine" ???
Michał Fita
@michalfita
Is this room alive?
shyun3
@shyun3
the examples aren't compiling
Daniel
@lumpidu
Hi, I'm trying to replace SMC with boost.sml and want to model popping events from substates to the super state, anybody any examples ?
Shareq Rabbani
@shareq2005
When will this be production ready ?
When will this be a part of the boost release ?
marcpawl
@marcpawl
Has anybody figure out how to add an attribute to the state machine? Looking for something like BOOST_MSM_EUML_DECLARE_ATTRIBUTE? If I add any attributes then I get a compile failure in make_transiition_table.
Gerard Choinka
@gchoinka
here is an example how to use sml in compiler explorer https://godbolt.org/z/-v_RQB
Balázs Benics
@BBenics_twitter
Can a state and event contain data member? Like to transfer knowledge from one state to the next?