msm etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
msm etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

9 Ağustos 2017 Çarşamba

msm state Sınıfı

Tanımlama
Örnek
Şöyle yaparız.
struct A:msmf::state<> {
  ...
}
Örnek
Kalıtım kullanarak şöyle yaparız.
struct State : boost::msm::front::state<> {};

struct State1 : State {
  ...
}
Örnek
struct içine alan tanımlayabiliriz. Şöyle yaparız.
// States
struct State1:msmf::state<> {
  ...
  bool z = true; // example of a condition
};
Action
State içinde bir inner struct olarak tanımlanır. Şöyle yaparız.
struct State2 : State {
  template<typename Event, typename Fsm>
  void on_entry(Event const&, Fsm& fsm) {
    ...
  }

  template<typename Event, typename Fsm>
  void on_exit(Event const&, Fsm&) {
    ...
  }

  struct Action {
    template<typename Fsm>
    void operator()(Event1 const &, Fsm &fsm, State1&, State2&) {
      ...
    }
  }
}
state_machine_def sınıfının geçiş tablosu şöyle yapılır.
using initial_state = State1;
struct TransitionTable : boost::mpl::vector<
  boost::msm::front::Row<State1, event::Event1, State2, State2::Action>,
  boost::msm::front::Row<State1, event::Event3, State3>,
  boost::msm::front::Row<State2, event::Event1, State2>,
  boost::msm::front::Row<State1, event::Event1, State2>,      
> {};
using transition_table = TransitionTable;
on_entry metodu
Şöyle yaparız.
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
  ...
}
on_exit metodu
Şöyle yaparız.
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
  ...
}

msm state_machine_def Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <boost/msm/front/state_machine_def.hpp>
Şu satırı dahil ederiz.
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
Tanımlama
Şöyle yaparız.
// ----- State machine
struct Sm : msmf::state_machine_def<Sm> {
  ...
};
Başlangıç state belirtilir. Şöyle yaparız.
struct Sm_:msmf::state_machine_def<Sm_>
{
  // States
  struct State1:msmf::state<> {
    ...
  };
  struct State2:msmf::state<> {
    ...
  };
  // Set initial state
  typedef State1 initial_state;

  // Guards
  struct Guard1 {
    template <class Event, class Fsm, class Source, class Target>
    bool operator()(Event const&, Fsm& fsm, Source& src, Target&) const {
      ...
      bool transitionAllowed = ...;
      return transitionAllowed;
    }
  };

  // Transition table
  struct transition_table:mpl::vector<
      //          Start   Event   Next    Action      Guard
      //          source and target is the same
      msmf::Row < State1, Event1, State1, msmf::none, Guard1 >,

      //          source and target is different
      msmf::Row < State1, Event2, State2, msmf::none, Guard1 >
  > {};
  ...
};
exception_caught metodu
Şöyle yaparız.
template <class FSM,class Event>
void exception_caught (Event const&,FSM&,std::exception& e) {
  std::cout << e.what() << std::endl;
}
transition_table alanı
Bu sınıfı içinde state'ler tutulur. Start State, Event, Next State, Action, Guard tanımlanır. Guard geçişin yapılıp yapılamayacağını belirtir. Guard tanımlamak için şöyle yaparız.
// Guards
struct Guard1 {
  template <class Event, class Fsm, class Source, class Target>
  bool operator()(Event const&, Fsm& fsm, Source& src, Target&) const {
    ...
    bool transitionAllowed = ...
    return transitionAllowed;
  }
};
İki state arasında geçiş olup olmadığını anlamak için şöyle yaparız.
bool transition = !std::is_same<Source, Target>::value;
Fsm sınıfı içindeki bir şeye erişmek için şöyle yaparız.
bool transitionAllowed = fsm.y;
src state içindeki bir şeye erişmek için şöyle yaparız.
bool transitionAllowed = src.z;
Örnek
Şöyle yaparız.
// Set initial state
typedef mpl::vector<A, B> initial_state;
// Transition table
struct transition_table:mpl::vector<
//          Start  Event   Next       Action      Guard
msmf::Row < A,     After2, A,         Action,     msmf::none >,
msmf::Row < B,     After5, B,         Action,     msmf::none >
> {};
Örnek
Şöyle yaparız
// Transition table
struct transition_table:mpl::vector<
      //          Start   Event   Next    Action      Guard
      //          source and target is the same
      msmf::Row < State1, Event1, State1, msmf::none, Guard1 >,

      //          source and target is different
      msmf::Row < State1, Event2, State2, msmf::none, Guard1 >
> {};