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