Boost logo

Boost Users :

Subject: [Boost-users] [msm] State Machine and Dependency Injection
From: Kris (krzysztof_at_[hidden])
Date: 2014-08-05 09:06:36


Hi,

Recently I have implemented Dependency Injection library, which I proposed
to boost last week
(http://boost.2283326.n4.nabble.com/is-there-any-interest-in-a-dependency-injection-library-td4665526.html)
Right now I wanted to shared how dependency injection might be integrated
with Boost.MSM (eUML) and would really glad for any feedback.
Boost.MSM is a great library, unfortunately actions/guards have to have
empty constructor and whole
logic has to be handled via Finite State Machine. Such approach is hard to
maintain and test, so would be great to have possibility
to implement action/guards without FSM dependency and with proper dependency
injection instead.

More information:
http://krzysztof-jusiak.github.io/di/boost/libs/di/doc/html/di/examples/integration_with_boost_libraries/boost_meta_state_machine.html

Hello World:

class guard : public euml_action<guard> {
public:
    guard(shared_ptr<data> data = nullptr, shared_ptr<other_data> =
nullptr); // default constructor is required by eUML

    template<typename TEvent>
    bool operator()(const TEvent&) const; // no FSM dependency
};

class action : public euml_action<action> {
public:
    action(shared_ptr<data> data = nullptr, shared_ptr<other_data> =
nullptr); // default constructor is required by eUML

    template<typename TEvent>
    void operator()(const TEvent&); // no FSM dependency
};

class state_machine_ : public state_machine_def<state_machine_> {
public:
    using initial_state = state1;

    BOOST_MSM_EUML_DECLARE_TRANSITION_TABLE((
        state1() + event() [guard()] / action() == state2()
    ), transition_table)
};

using state_machine = back::state_machine<
    state_machine_
  , back::use_dependency_injection // Boost.DI dependency injection policy
>;

auto sm = di::make_injector().create<state_machine>(); // create state
machine / actions, guards and their dependencies will be injected via DI
framework
sm.process_event(event());

Hot it works?
        * we do implement actions/guards with any dependencies we need
        * we add use_dependency_injection policy in state machine definition
        * we create state machine using dependency injection framework
library

What MSM with DI actually give us?
        * actions/guards are really easy to test / code doesn't require FSM
dependency - loosely coupled
        * signature change of actions/guards may not require binding
configuration changes (if it's not an interface or it's an interface already
bound)
        * FSM is just a declarative abstraction - we don't need to test it
        * we can use all DI goods for controlling how dependencies will be
injected - see https://github.com/krzysztof-jusiak/di

Project (with workings tests): https://github.com/krzysztof-jusiak/msm
Comparison between usage of Boost.MSM with DI and without it:
http://krzysztof-jusiak.github.io/di/boost/libs/di/doc/html/di/examples/integration_with_boost_libraries/boost_meta_state_machine.html

Looking forward to hear from you. Any feedback is more than welcome.

Cheers, Kris

--
View this message in context: http://boost.2283326.n4.nabble.com/msm-State-Machine-and-Dependency-Injection-tp4666071.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net