rFSMTools
rFSM Tools
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
rfsm.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2017 iCub Facility
5  * Authors: Ali Paikan <ali.paikan@iit.it>, Nicolo' Genesio <nicolo.genesio@iit.it>
6  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
7  */
8 
9 #ifndef RFSM_H
10 #define RFSM_H
11 
12 
13 #include <string>
14 #include <vector>
15 #include <map>
16 
17 namespace rfsm {
18  class StateMachine;
19  class StateCallback;
20  class StateGraph;
21  class LuaTraceCallback;
22 }
23 
24 #ifndef luaL_reg
25  #define luaL_reg luaL_Reg
26 #endif
27 
33 public:
34  virtual ~StateCallback() {}
38  virtual void entry() {}
39 
43  virtual void doo() {}
44 
48  virtual void exit() {}
49 };
50 
51 
56 public:
61  virtual void onTrace(const std::string& message ) { }
62 };
63 
64 
70 public:
71  struct LuaFuncCode {
72  int startLine;
73  int endLine;
74  std::string fileName;
75  };
76 
77 
78  struct State {
79  std::string name;
80  std::string type;
84  bool operator==(const State& s) const {
85  return (s.name == name);
86  }
87  };
88 
89  struct Transition {
90  std::string source;
91  std::string target;
92  std::vector<std::string> events;
93  int priority;
94  bool operator==(const Transition& s) const {
95  //return (s.source == source) && (s.target == target) && (s.events == events);
96  return (s.source == source) && (s.target == target);
97  }
98  };
99 
100  typedef std::vector<Transition>::iterator TransitionItr;
101  typedef std::vector<State>::iterator StateItr;
102 
103  void clear();
104 
105 public:
109  std::vector<State> states;
113  std::vector<Transition> transitions;
114 
115 
116 };
117 
118 
123 public:
124 
129  StateMachine(bool verbose=false);
130 
134  virtual ~StateMachine();
135 
140  const std::string getFileName();
141 
147  bool load(const std::string& filename);
148 
153  bool run();
154 
160  bool step(unsigned int n=1);
161 
167  bool sendEvent(const std::string& event);
168 
175  bool sendEvents(unsigned int n, ...);
176 
182  bool doString(const std::string& command);
183 
189  bool doFile(const std::string& filename);
190 
195  void addLuaPackagePath(const std::string& path);
196 
200  void close();
201 
208  bool setStateCallback(const std::string& state, rfsm::StateCallback& callback);
209 
214  const std::string getCurrentState();
215 
220  const std::vector<std::string>& getEventsList();
221 
227  bool getEventQueue(std::vector<std::string>& equeue);
228 
234 
243  bool enablePreStepHook();
244 
253  bool enablePostStepHook();
254 
262  bool catchPrintOutput();
263 
264 public:
269  virtual void onPreStep();
270 
275  virtual void onPostStep();
276 
282  virtual void onWarning(const std::string message);
283 
288  virtual void onError(const std::string message);
289 
295  virtual void onInfo(const std::string message);
296 
297 private:
302  virtual void onTrace(const std::string& message );
303 
304 private:
305  class Private;
306  Private * const mPriv;
307  bool verbose;
308 };
309 
310 
311 #endif // RFSM_H
bool doString(const std::string &command)
doString execute a generic lua command
The StateGraph class represents the rFSM state graph in term of states and the transitions among them...
Definition: rfsm.h:69
virtual ~StateCallback()
Definition: rfsm.h:34
std::string source
Definition: rfsm.h:90
Definition: rfsm.h:17
std::vector< State > states
states is a list of all states
Definition: rfsm.h:109
StateMachine(bool verbose=false)
StateMachine load and execute a rFSM state machine written in LUA.
const std::vector< std::string > & getEventsList()
getEventsList retrieves all available events in the state machine
const rfsm::StateGraph & getStateGraph()
getStateGraph return the rFSM state graph
virtual void onPreStep()
if pre-step hook function of rFSM is enabled this callback is called before stepping the state machin...
bool operator==(const State &s) const
Definition: rfsm.h:84
void addLuaPackagePath(const std::string &path)
addLuaPackagePath add a new path to lua package.path
std::string fileName
Definition: rfsm.h:74
std::vector< State >::iterator StateItr
Definition: rfsm.h:101
LuaFuncCode exit
Definition: rfsm.h:83
virtual void onError(const std::string message)
this is called on every error message generated from rFSM
std::vector< std::string > events
Definition: rfsm.h:92
virtual void onTrace(const std::string &message)
onTrace is called on lua traceback
The rfsm::StateMachine class.
Definition: rfsm.h:122
virtual void entry()
entry is called on rFSM state.entry
Definition: rfsm.h:38
bool operator==(const Transition &s) const
Definition: rfsm.h:94
Private *const mPriv
Definition: rfsm.h:305
The rfsm::StateCallback class can be used to implement the rFSM state callbacs in c++...
Definition: rfsm.h:32
The LuaTraceCallback class.
Definition: rfsm.h:55
std::string name
Definition: rfsm.h:79
virtual void exit()
exit is called on rFSM state.exit
Definition: rfsm.h:48
void close()
closes the state machine if it is already loaded
virtual void doo()
doo is called on rFSM state.doo
Definition: rfsm.h:43
bool setStateCallback(const std::string &state, rfsm::StateCallback &callback)
setStateCallback set an StateCallback object for a given state
std::vector< Transition > transitions
transitions is a list of all transitions
Definition: rfsm.h:113
virtual void onWarning(const std::string message)
this is called on every warning message generated from rFSM in verbose mode (i.e. ...
LuaFuncCode doo
Definition: rfsm.h:82
bool getEventQueue(std::vector< std::string > &equeue)
getEventQueue gets the current events in the rFSM event queue
std::string type
Definition: rfsm.h:80
bool catchPrintOutput()
catchPrintOutput redirect the output of the lua 'print()' function to rfsm::StateMachine::onInfo() ...
std::string target
Definition: rfsm.h:91
bool step(unsigned int n=1)
step calls rfs.step(n)
bool sendEvents(unsigned int n,...)
sendEvents calls rfsm.send_events(...)
std::vector< Transition >::iterator TransitionItr
Definition: rfsm.h:100
bool doFile(const std::string &filename)
doFile execute a lua file
bool enablePostStepHook()
enablePostStepHook enables the post-step hook function of the rFSM.
const std::string getCurrentState()
getCurrentState returns the current activated state
bool load(const std::string &filename)
loads and initializes a rFSM state machine
virtual ~StateMachine()
~StateMachine
LuaFuncCode entry
Definition: rfsm.h:81
bool sendEvent(const std::string &event)
sendEvent calls rfsm.send_events(event)
const std::string getFileName()
getFileName
bool run()
run calls rfsm.run()
virtual void onInfo(const std::string message)
this is called on every info message generated from rFSM in verbose mode (i.e.
virtual void onTrace(const std::string &message)
onTrace is called on lua traceback
Definition: rfsm.h:61
virtual void onPostStep()
if post-step hook function of rFSM is enabled this callback is called after stepping the state machin...
bool enablePreStepHook()
enablePreStepHook enables the pre-step hook function of the rFSM.