| [628577] | 1 | /* | 
|---|
|  | 2 | * ActionQueue.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Aug 16, 2013 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef ACTIONQUEUE_HPP_ | 
|---|
|  | 9 | #define ACTIONQUEUE_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "CodePatterns/Singleton.hpp" | 
|---|
|  | 17 |  | 
|---|
| [29b52b] | 18 | #include "CodePatterns/Observer/Channels.hpp" | 
|---|
|  | 19 | #include "CodePatterns/Observer/Observable.hpp" | 
|---|
| [74459a] | 20 |  | 
|---|
|  | 21 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [415ddd] | 22 | #include <boost/thread.hpp> | 
|---|
| [74459a] | 23 | #endif | 
|---|
| [690741] | 24 | #include <vector> | 
|---|
| [1d3563] | 25 |  | 
|---|
| [f54cda] | 26 | #include "Actions/Action.hpp" | 
|---|
| [b5b01e] | 27 | #include "Actions/ActionState.hpp" | 
|---|
| [26b4eb4] | 28 | #include "Actions/ActionStatusList.hpp" | 
|---|
| [628577] | 29 |  | 
|---|
| [74459a] | 30 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [415ddd] | 31 | void stopQueue(); | 
|---|
|  | 32 | void waitQueue(); | 
|---|
| [74459a] | 33 | #endif | 
|---|
| [415ddd] | 34 |  | 
|---|
| [11d433] | 35 | class CommandLineParser; | 
|---|
|  | 36 |  | 
|---|
| [628577] | 37 | namespace MoleCuilder { | 
|---|
|  | 38 |  | 
|---|
| [6367dd] | 39 | class ActionHistory; | 
|---|
| [ed3944] | 40 | class ActionRegistry; | 
|---|
|  | 41 | class ActionTrait; | 
|---|
| [628577] | 42 |  | 
|---|
| [29b52b] | 43 | namespace Queuedetail { | 
|---|
|  | 44 | template <class T> const T* lastChanged() | 
|---|
|  | 45 | { | 
|---|
|  | 46 | ASSERT(0, "Queuedetail::lastChanged() - only specializations may be used."); | 
|---|
|  | 47 | return NULL; | 
|---|
|  | 48 | } | 
|---|
|  | 49 | } | 
|---|
|  | 50 |  | 
|---|
| [628577] | 51 | /** This class combines the whole handling of Actions into a single class. | 
|---|
|  | 52 | * | 
|---|
| [1d3563] | 53 | * It spawns new Actions by its internal ActionRegistry. Spawned Actions are | 
|---|
|  | 54 | * automatically queued and placed into a History after execution. | 
|---|
| [628577] | 55 | */ | 
|---|
| [29b52b] | 56 | class ActionQueue : public Singleton<ActionQueue>, public Observable | 
|---|
| [628577] | 57 | { | 
|---|
|  | 58 | friend class Singleton<ActionQueue>; | 
|---|
|  | 59 | public: | 
|---|
| [690741] | 60 | typedef std::vector<std::string> ActionTokens_t; | 
|---|
| [af5384] | 61 | typedef std::vector< Action * > ActionQueue_t; | 
|---|
| [1d3563] | 62 |  | 
|---|
| [29b52b] | 63 | //!> channels for this observable | 
|---|
|  | 64 | enum NotificationType { | 
|---|
|  | 65 | ActionQueued,  // new action was queued | 
|---|
|  | 66 | NotificationType_MAX  // denotes the maximum of available notification types | 
|---|
|  | 67 | }; | 
|---|
|  | 68 |  | 
|---|
|  | 69 | //>! access to last changed element (atom or molecule) | 
|---|
|  | 70 | template <class T> const T* lastChanged() const | 
|---|
|  | 71 | { return Queuedetail::lastChanged<T>(); } | 
|---|
|  | 72 |  | 
|---|
| [05c989] | 73 | /** Queues the Action with \a name to be called. | 
|---|
|  | 74 | * | 
|---|
| [7fc447] | 75 | * \param name token of Action to actionqueue | 
|---|
| [f54cda] | 76 | * \param state whether Actions needs to be filled via a Dialog or not | 
|---|
| [05c989] | 77 | */ | 
|---|
| [f54cda] | 78 | void queueAction(const std::string &name, enum Action::QueryOptions state = Action::Interactive); | 
|---|
|  | 79 |  | 
|---|
|  | 80 | /** Queues the Action with \a name to be called. | 
|---|
|  | 81 | * | 
|---|
|  | 82 | * \param _action action to add | 
|---|
|  | 83 | * \param state whether Actions needs to be filled via a Dialog or not | 
|---|
|  | 84 | */ | 
|---|
| [7f1a1a] | 85 | void queueAction(const Action * const _action, enum Action::QueryOptions state = Action::Interactive); | 
|---|
| [05c989] | 86 |  | 
|---|
| [1d3563] | 87 | /** Returns the spawned action by token \a name. | 
|---|
|  | 88 | * | 
|---|
| [7fc447] | 89 | * Action is checked into internal actionqueue. | 
|---|
| [1d3563] | 90 | * | 
|---|
|  | 91 | * \return pointer to newly spawned action | 
|---|
|  | 92 | */ | 
|---|
| [a6ceab] | 93 | Action* getActionByName(const std::string &name); | 
|---|
| [1d3563] | 94 |  | 
|---|
|  | 95 | /** Checks whether the Action is known by the given \a name. | 
|---|
|  | 96 | * | 
|---|
|  | 97 | * \param name token of action to check | 
|---|
|  | 98 | * \return true - token is known, false - else | 
|---|
|  | 99 | */ | 
|---|
| [a6ceab] | 100 | bool isActionKnownByName(const std::string &name) const; | 
|---|
| [1d3563] | 101 |  | 
|---|
| [126867] | 102 | /** Register an Action with the ActionRegistry. | 
|---|
|  | 103 | * | 
|---|
|  | 104 | * \param _action action to add | 
|---|
|  | 105 | */ | 
|---|
|  | 106 | void registerAction(Action *_action); | 
|---|
|  | 107 |  | 
|---|
| [690741] | 108 | /** Returns the vector with the tokens of all currently known Actions. | 
|---|
|  | 109 | * | 
|---|
|  | 110 | * \return list of all tokens | 
|---|
|  | 111 | */ | 
|---|
|  | 112 | const ActionTokens_t getListOfActions() const; | 
|---|
|  | 113 |  | 
|---|
|  | 114 | /** Returns the trait to an Action. | 
|---|
|  | 115 | * | 
|---|
|  | 116 | * \param name name of Action | 
|---|
|  | 117 | * \return const ref to its default Trait. | 
|---|
|  | 118 | */ | 
|---|
| [a6ceab] | 119 | const ActionTrait& getActionsTrait(const std::string &name) const; | 
|---|
| [690741] | 120 |  | 
|---|
| [7fc447] | 121 | /** Print the current contents of the actionqueue as CLI instantiated list of Actions. | 
|---|
| [46b181] | 122 | * | 
|---|
|  | 123 | * This is useful for storing the current session. | 
|---|
|  | 124 | * | 
|---|
|  | 125 | * \param output output stream to print to | 
|---|
|  | 126 | */ | 
|---|
|  | 127 | void outputAsCLI(std::ostream &output) const; | 
|---|
|  | 128 |  | 
|---|
| [7fc447] | 129 | /** Print the current contents of the actionqueue as Python instantiated list of Actions. | 
|---|
| [477012] | 130 | * | 
|---|
|  | 131 | * This is useful for storing the current session. | 
|---|
|  | 132 | * | 
|---|
|  | 133 | * \param output output stream to print to | 
|---|
|  | 134 | */ | 
|---|
|  | 135 | void outputAsPython(std::ostream &output) const; | 
|---|
|  | 136 |  | 
|---|
| [af5384] | 137 | /** Undoes last called Acfriend void ::cleanUp();tion. | 
|---|
| [6367dd] | 138 | * | 
|---|
|  | 139 | */ | 
|---|
|  | 140 | void undoLast(); | 
|---|
|  | 141 |  | 
|---|
|  | 142 | /** Redoes last undone Action. | 
|---|
|  | 143 | * | 
|---|
|  | 144 | */ | 
|---|
|  | 145 | void redoLast(); | 
|---|
|  | 146 |  | 
|---|
| [c01fec] | 147 | /** Checks whether there is one completed Action stored in ActionHistory in the past. | 
|---|
|  | 148 | * | 
|---|
|  | 149 | * @return true - at least one Action to undo present, false - else | 
|---|
|  | 150 | */ | 
|---|
|  | 151 | bool canUndo() const; | 
|---|
|  | 152 |  | 
|---|
|  | 153 | /** Checks whether there is one completed Action stored in ActionHistory in the future. | 
|---|
|  | 154 | * | 
|---|
|  | 155 | * @return true - at least one Action to redo present, false - else | 
|---|
|  | 156 | */ | 
|---|
|  | 157 | bool canRedo() const; | 
|---|
|  | 158 |  | 
|---|
| [a61dbb] | 159 | /** Return status of last executed action. | 
|---|
|  | 160 | * | 
|---|
|  | 161 | * \return true - action executed correctly, false - else | 
|---|
|  | 162 | */ | 
|---|
|  | 163 | bool getLastActionOk() const | 
|---|
|  | 164 | {  return lastActionOk; } | 
|---|
|  | 165 |  | 
|---|
| [74459a] | 166 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [415ddd] | 167 | boost::thread &getRunThread() | 
|---|
|  | 168 | { return run_thread; } | 
|---|
| [74459a] | 169 | #endif | 
|---|
| [af5384] | 170 |  | 
|---|
| [26b4eb4] | 171 | /** Getter to ref to list of status messages. | 
|---|
|  | 172 | * | 
|---|
|  | 173 | * This is meant for UIs to registers as Observables. | 
|---|
|  | 174 | * | 
|---|
|  | 175 | * \return ref to StatusList variable | 
|---|
|  | 176 | */ | 
|---|
|  | 177 | ActionStatusList& getStatusList() | 
|---|
|  | 178 | { return StatusList; } | 
|---|
|  | 179 |  | 
|---|
| [6367dd] | 180 | private: | 
|---|
|  | 181 | //!> grant Action access to internal history functions. | 
|---|
|  | 182 | friend class Action; | 
|---|
| [11d433] | 183 | //!> grant CommandLineParser access to stop and clearQueue() | 
|---|
|  | 184 | friend class ::CommandLineParser; | 
|---|
| [6367dd] | 185 |  | 
|---|
|  | 186 | /** Wrapper function to add state to ActionHistory. | 
|---|
|  | 187 | * | 
|---|
|  | 188 | * \param _Action Action whose state to add | 
|---|
|  | 189 | * \param _state state to add | 
|---|
|  | 190 | */ | 
|---|
|  | 191 | void addElement(Action* _Action, ActionState::ptr _state); | 
|---|
|  | 192 |  | 
|---|
| [26b4eb4] | 193 | /** Advocate function to add status message to the list. | 
|---|
|  | 194 | * | 
|---|
|  | 195 | */ | 
|---|
|  | 196 | void pushStatus(const std::string &_msg) | 
|---|
|  | 197 | { StatusList.pushMessage(_msg); } | 
|---|
|  | 198 |  | 
|---|
| [6367dd] | 199 | /** Wrapper function to clear ActionHistory. | 
|---|
|  | 200 | * | 
|---|
|  | 201 | */ | 
|---|
|  | 202 | void clear(); | 
|---|
|  | 203 |  | 
|---|
| [601ef8] | 204 | /** Clears all actions present in the actionqueues from \a _fromAction. | 
|---|
| [7f1a1a] | 205 | * | 
|---|
| [601ef8] | 206 | * @param _fromAction 0 if all Actions to clear or else | 
|---|
| [7f1a1a] | 207 | */ | 
|---|
| [601ef8] | 208 | void clearQueue(const size_t _fromAction = 0); | 
|---|
| [7f1a1a] | 209 |  | 
|---|
| [74459a] | 210 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [601ef8] | 211 |  | 
|---|
|  | 212 | /** Clears the temporary queue. | 
|---|
|  | 213 | * | 
|---|
|  | 214 | */ | 
|---|
|  | 215 | void clearTempQueue(); | 
|---|
|  | 216 |  | 
|---|
|  | 217 | /** Sets the run_thread_isIdle flag. | 
|---|
|  | 218 | * | 
|---|
|  | 219 | * @param _flag state to set to | 
|---|
|  | 220 | */ | 
|---|
|  | 221 | void setRunThreadIdle(const bool _flag); | 
|---|
|  | 222 |  | 
|---|
| [415ddd] | 223 | /** Runs the ActionQueue. | 
|---|
|  | 224 | * | 
|---|
|  | 225 | */ | 
|---|
|  | 226 | void run(); | 
|---|
|  | 227 |  | 
|---|
|  | 228 | friend void ::stopQueue(); | 
|---|
|  | 229 |  | 
|---|
|  | 230 | /** Stops the internal thread. | 
|---|
|  | 231 | * | 
|---|
|  | 232 | */ | 
|---|
|  | 233 | void stop(); | 
|---|
|  | 234 |  | 
|---|
|  | 235 | friend void ::waitQueue(); | 
|---|
|  | 236 |  | 
|---|
|  | 237 | /** Wait till all currently queued actions are processed. | 
|---|
|  | 238 | * | 
|---|
|  | 239 | */ | 
|---|
|  | 240 | void wait(); | 
|---|
| [601ef8] | 241 |  | 
|---|
|  | 242 | /** Moves all action from tempqueue into real queue. | 
|---|
|  | 243 | * | 
|---|
|  | 244 | */ | 
|---|
|  | 245 | void insertTempQueue(); | 
|---|
|  | 246 |  | 
|---|
| [74459a] | 247 | #endif | 
|---|
| [415ddd] | 248 |  | 
|---|
| [975b83] | 249 | /** Insert an action after CurrentAction. | 
|---|
|  | 250 | * | 
|---|
|  | 251 | * This is implemented only to allow action's COMMAND to work. If we | 
|---|
|  | 252 | * were to use queueAction, actions would come after all other already | 
|---|
|  | 253 | * present actions. | 
|---|
|  | 254 | */ | 
|---|
|  | 255 | void insertAction(Action *_action, enum Action::QueryOptions state); | 
|---|
|  | 256 |  | 
|---|
| [628577] | 257 | private: | 
|---|
| [1d3563] | 258 | /** Private cstor for ActionQueue. | 
|---|
|  | 259 | * | 
|---|
|  | 260 | * Must be private as is singleton. | 
|---|
|  | 261 | * | 
|---|
|  | 262 | */ | 
|---|
| [628577] | 263 | ActionQueue(); | 
|---|
| [1d3563] | 264 |  | 
|---|
|  | 265 | /** Dstor for ActionQueue. | 
|---|
|  | 266 | * | 
|---|
|  | 267 | */ | 
|---|
| [628577] | 268 | ~ActionQueue(); | 
|---|
|  | 269 |  | 
|---|
| [29b52b] | 270 | private: | 
|---|
|  | 271 | friend const Action *Queuedetail::lastChanged<Action>(); | 
|---|
|  | 272 | static const Action *_lastchangedaction; | 
|---|
|  | 273 |  | 
|---|
| [628577] | 274 | //!> ActionRegistry to spawn new actions | 
|---|
| [ed3944] | 275 | ActionRegistry *AR; | 
|---|
| [1d3563] | 276 |  | 
|---|
| [6367dd] | 277 | //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized | 
|---|
|  | 278 | ActionHistory *history; | 
|---|
|  | 279 |  | 
|---|
| [7fc447] | 280 | //!> internal actionqueue of actions | 
|---|
|  | 281 | ActionQueue_t actionqueue; | 
|---|
| [af5384] | 282 |  | 
|---|
| [601ef8] | 283 | //!> indicates that the last action has failed | 
|---|
|  | 284 | bool lastActionOk; | 
|---|
|  | 285 |  | 
|---|
|  | 286 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [7fc447] | 287 | //!> point to current action in actionqueue | 
|---|
| [af5384] | 288 | size_t CurrentAction; | 
|---|
| [415ddd] | 289 |  | 
|---|
|  | 290 | //!> internal temporary actionqueue of actions used by insertAction() | 
|---|
|  | 291 | ActionQueue_t tempqueue; | 
|---|
|  | 292 |  | 
|---|
|  | 293 | //!> internal thread to call Actions | 
|---|
|  | 294 | boost::thread run_thread; | 
|---|
|  | 295 |  | 
|---|
|  | 296 | //!> internal mutex to synchronize access to queue | 
|---|
|  | 297 | boost::mutex mtx_queue; | 
|---|
|  | 298 |  | 
|---|
|  | 299 | //!> conditional variable notifying when run_thread is idling | 
|---|
|  | 300 | boost::condition_variable cond_idle; | 
|---|
|  | 301 |  | 
|---|
|  | 302 | //!> flag indicating whether run_thread is idle or not | 
|---|
|  | 303 | bool run_thread_isIdle; | 
|---|
|  | 304 |  | 
|---|
|  | 305 | //!> internal mutex to synchronize access to run_thread_isIdle | 
|---|
|  | 306 | boost::mutex mtx_idle; | 
|---|
| [74459a] | 307 | #endif | 
|---|
| [26b4eb4] | 308 |  | 
|---|
|  | 309 | //!> internal list of status messages from Actions for UIs to display | 
|---|
|  | 310 | ActionStatusList StatusList; | 
|---|
| [628577] | 311 | }; | 
|---|
| [8859b5] | 312 | namespace Queuedetail { | 
|---|
|  | 313 | template <>       inline  const Action* lastChanged<Action>() { return ActionQueue::_lastchangedaction; } | 
|---|
|  | 314 | } | 
|---|
| [628577] | 315 |  | 
|---|
|  | 316 | }; | 
|---|
|  | 317 |  | 
|---|
|  | 318 | #endif /* ACTIONQUEUE_HPP_ */ | 
|---|