rad  5.1.0
Executor.h
Go to the documentation of this file.
1 
10 /*
11  * scampl4cpp/engine
12  *
13  * Copyright by European Southern Observatory, 2012
14  * All rights reserved
15  *
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  *
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  * Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29  * 02111-1307 USA.
30  */
31 
32 #ifndef SCXML4CPP_EXECUTOR_H
33 #define SCXML4CPP_EXECUTOR_H
34 
35 #ifndef __cplusplus
36 #error This is a C++ include file and cannot be used from plain C
37 #endif
38 
39 #ifndef SCXML4CPP_STATECOMPARATOR_H
41 #endif
42 
43 #ifndef SCXML4CPP_EVENTQUEUE_H
44 #include "scxml4cpp/EventQueue.h"
45 #endif
46 
47 #ifndef SCXML4CPP_HELPER_H
48 #include "scxml4cpp/Helper.h"
49 #endif
50 
51 #include <queue>
52 #include <list>
53 #include <string>
54 
55 namespace scxml4cpp {
56 
57 class StateMachine;
58 class State;
59 class Transition;
60 class Event;
61 class EventQueue;
62 class Context;
63 class EventListener;
64 class StatusListener;
65 
73 class Executor {
74  public:
79  SILENT = 0,
81  DEFFERRED
82  };
83 
91  Executor(StateMachine& stateMachine, Context* context, EventQueue& events);
92 
96  ~Executor();
97 
106  std::string formatStatus(const bool usefullyqualified = false);
107 
111  std::string formatModel();
112 
116  std::list<State*> getStatus();
117 
123  void printStatus(const bool usefullyqualified = false);
124 
130  void setContext(Context* context);
131 
137  void setEventHandlingPolicy(const EventHandlingPolicy policy);
138 
143 
149  void addEventListener(EventListener* eventListener);
150 
156  void removeEventListener(EventListener* eventListener);
157 
161  void removeAllEventListener();
162 
168  void addStatusListener(StatusListener* statusListener);
169 
175  void removeStatusListener(StatusListener* statusListener);
176 
181 
186  void startSM();
187 
192  void stopSM();
193 
197  void start();
198 
202  void stop();
203 
208  void run();
209 
218  void postEvent(Event* e);
219 
224  void processEvent();
225 
232  void processEvent(Event* e);
233 
237  bool isRunning();
238 
242  bool isFinal();
243 
244  private:
245  StateMachine& mStateMachine;
246 
247  /*
248  * use std::list<State*> instead of std::set<State*>
249  * to preserve the insertion order.
250  *
251  * Note that std::set<> is ordered container and the default order
252  * is given by comparing the State* pointers. The values of the pointers
253  * depends on the memory allocation algorithm. Most of the times but NOT
254  * always new pointers have larger values.
255  *
256  * By using std::list we lose the check of already exiting elements
257  * in the container. However this check has only linear complexity.
258  * Better adding the check and keep the order, than keeping the check
259  * and have to sort again after.
260  */
261  std::list<State*> mCurrentStatus;
262  std::list<State*> mPreviousStatus;
263  std::list<State*> mStatesToInvoke;
264 
265  std::queue<Event*> mInternalEvents;
266  EventQueue& mExternalEvents;
267  bool mContinue; // is interpreted started
268  bool mFinal; // is top level final state reached
269  StateComparator mStateComparator;
270  Context* mContext;
271  Helper mHelper;
272  EventHandlingPolicy mEventHandlingPolicy;
273  std::list<EventListener*> mEventListeners;
274  std::list<StatusListener*> mStatusListeners;
275 
276  void addUniqueStateToList(State* s, std::list<State*>& l);
277 
278  //void addStateToInvoke(State* s);
279  void addStateToCurrentStatus(State* s);
280 
281  StateComparator& getStateComparator();
282  void processInternalEvents();
283  void exitInterpreter();
284  std::list<Transition*> selectEventlessTransitions();
285  std::list<Transition*> selectTransitions(Event* e);
286  void microstep(std::list<Transition*>& enabledTransitions);
287  void exitStates(std::list<Transition*>& enabledTransitions);
288  void executeTransitionContent(std::list<Transition*>& enabledTransitions);
289  void enterStates(std::list<Transition*>& enabledTransitions);
290  void addStatesToEnter(State* s, State* root, std::list<State*>& statesToEnter,
291  std::list<State*>& statesForDefaultEntry);
292 
293  void notifyEventListeners(Event* e);
294  void notifyStatusListeners(std::list<State*>& status);
295 
296  Executor(const Executor&);
297  Executor& operator=(const Executor&);
298 };
299 
300 } // namespace scxml4cpp
301 
302 #endif // SCXML4CPP_EXECUTOR_H
EventQueue header.
Helper header.
StateComparator header.
Definition: Context.h:58
Definition: EventListener.h:57
Definition: EventQueue.h:55
Definition: Event.h:66
Definition: Executor.h:73
void startSM()
Definition: Executor.cpp:154
std::list< State * > getStatus()
Definition: Executor.cpp:144
~Executor()
Definition: Executor.cpp:69
void setContext(Context *context)
Definition: Executor.cpp:71
bool isRunning()
Definition: Executor.cpp:298
bool isFinal()
Definition: Executor.cpp:303
void stopSM()
Definition: Executor.cpp:290
void processEvent()
Definition: Executor.cpp:321
void start()
Definition: Executor.cpp:267
EventHandlingPolicy
Definition: Executor.h:78
@ SILENT
Definition: Executor.h:79
@ REJECT
Event is silently rejected if transition is not enabled.
Definition: Executor.h:80
@ DEFFERRED
Event is rejected with notification, if transition is not enabled.
Definition: Executor.h:81
std::string formatModel()
Definition: Executor.cpp:139
void printStatus(const bool usefullyqualified=false)
Definition: Executor.cpp:149
void run()
Definition: Executor.cpp:257
Executor(StateMachine &stateMachine, Context *context, EventQueue &events)
Definition: Executor.cpp:57
void removeEventListener(EventListener *eventListener)
Definition: Executor.cpp:95
void removeAllStatusListener()
Definition: Executor.cpp:124
void setEventHandlingPolicy(const EventHandlingPolicy policy)
Definition: Executor.cpp:77
void addEventListener(EventListener *eventListener)
Definition: Executor.cpp:87
void stop()
Definition: Executor.cpp:274
std::string formatStatus(const bool usefullyqualified=false)
Definition: Executor.cpp:134
void addStatusListener(StatusListener *statusListener)
Definition: Executor.cpp:108
void removeStatusListener(StatusListener *statusListener)
Definition: Executor.cpp:116
EventHandlingPolicy getEventHandlingPolicy()
Definition: Executor.cpp:82
void postEvent(Event *e)
Definition: Executor.cpp:308
void removeAllEventListener()
Definition: Executor.cpp:103
Definition: Helper.h:57
Definition: StateComparator.h:53
Definition: StateMachine.h:64
Definition: State.h:60
Definition: StatusListener.h:60
Definition: Action.h:40
Definition: testCoroActivity.cpp:16