mbsimcontrol  4.0.0
MBSim Control Module
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
state_machine.h
1 /* Copyright (C) 2004-2022 MBSim Development Team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact: martin.o.foerg@gmail.com
18  */
19 
20 #ifndef _STATE_MACHINE_H_
21 #define _STATE_MACHINE_H_
22 
23 #include <utility>
24 
25 #include "mbsimControl/signal_.h"
26 
27 namespace MBSimControl {
28 
33  class StateMachine : public Signal {
34  struct Transition {
35  Transition(int dest_, Signal *sig_, double s0_) : dest(dest_), sig(sig_), s0(s0_) { }
36  int dest;
37  Signal *sig;
38  double s0;
39  std::string signalStr;
40  };
41  struct State {
42  State(std::string name_, double val_) : name(std::move(name_)), val(val_), t0(0) { }
43  void addTransition(const Transition &trans_) { trans.emplace_back(trans_); }
44  std::string name;
45  double val;
46  double t0;
47  std::vector<Transition> trans;
48  };
49  public:
50  StateMachine(const std::string &name="") : Signal(name) { }
51  void initializeUsingXML(xercesc::DOMElement *element) override;
52  void init(InitStage stage, const MBSim::InitConfigSet &config) override;
53  void addState(const std::string &name, double val) { state.emplace_back(State(name,val)); }
54  Transition& addTransition(const std::string &name, const std::string &dest, Signal *sig, double s0=0);
55  void setInitialState(const std::string &name);
56  const State& getActiveState() const { return state[int(curis(0))]; }
57  void updateSignal() override;
58  int getSignalSize() const override { return 1; }
59  void calcisSize() override { isSize = 1; }
60  void calcsvSize() override;
61  void updateStopVector() override;
62  void checkActive(int j) override;
63 
64  private:
65  std::vector<State> state;
66  std::vector<Transition> transition;
67  std::string stateStr;
68  };
69 
70 }
71 
72 #endif
Signal.
Definition: signal_.h:38
Definition: state_machine.h:34
Definition: state_machine.h:41
State machine.
Definition: state_machine.h:33