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