mbsimcontrol  4.0.0
MBSim Control Module
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
27namespace MBSimControl {
28
33 class StateMachine : public Signal {
34 struct Transition {
35 Transition(int dest_, Signal *sig_, double s0_, int dir_) : dest(dest_), sig(sig_), s0(s0_), dir(dir_) { }
36 int dest;
37 Signal *sig;
38 double s0;
39 int dir;
40 double newSig{0};
41 double oldSig{0};
42 std::string signalStr;
43 };
44 struct State {
45 State(std::string name_, double val_) : name(std::move(name_)), val(val_), t0(0) { }
46 void addTransition(const Transition &trans_) { trans.emplace_back(trans_); }
47 std::string name;
48 double val;
49 double t0;
50 std::vector<Transition> trans;
51 };
52 public:
53 StateMachine(const std::string &name="") : Signal(name) { }
54 void initializeUsingXML(xercesc::DOMElement *element) override;
55 void init(InitStage stage, const MBSim::InitConfigSet &config) override;
56 void addState(const std::string &name, double val) { state.emplace_back(State(name,val)); }
57 Transition& addTransition(const std::string &name, const std::string &dest, Signal *sig, double s0=0, int dir=0);
58 void setInitialState(const std::string &name);
59 void setRootFinding(bool rf_) { rf = rf_; }
60 const State& getActiveState() const { return state[int(curis(0))]; }
61 void updateSignal() override;
62 int getSignalSize() const override { return 1; }
63 void calcisSize() override { isSize = 1; }
64 void calcsvSize() override;
65 void updateStopVector() override;
66 void checkActive(int j) override;
67 void aboutToUpdateInternalState() override;
68
69 private:
70 std::vector<State> state;
71 std::vector<Transition> transition;
72 bool rf{false};
73 std::string stateStr;
74 };
75
76}
77
78#endif
Signal.
Definition: signal_.h:38
State machine.
Definition: state_machine.h:33
std::string name
Definition: state_machine.h:44
Definition: state_machine.h:34