| [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 | 
 | 
|---|
| [74459a] | 18 | 
 | 
|---|
 | 19 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| [415ddd] | 20 | #include <boost/thread.hpp>
 | 
|---|
| [74459a] | 21 | #endif
 | 
|---|
| [690741] | 22 | #include <vector>
 | 
|---|
| [1d3563] | 23 | 
 | 
|---|
| [f54cda] | 24 | #include "Actions/Action.hpp"
 | 
|---|
| [b5b01e] | 25 | #include "Actions/ActionState.hpp"
 | 
|---|
| [26b4eb4] | 26 | #include "Actions/ActionStatusList.hpp"
 | 
|---|
| [628577] | 27 | 
 | 
|---|
| [74459a] | 28 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| [415ddd] | 29 | void stopQueue();
 | 
|---|
 | 30 | void waitQueue();
 | 
|---|
| [74459a] | 31 | #endif
 | 
|---|
| [415ddd] | 32 | 
 | 
|---|
| [628577] | 33 | namespace MoleCuilder {
 | 
|---|
 | 34 | 
 | 
|---|
| [6367dd] | 35 | class ActionHistory;
 | 
|---|
| [ed3944] | 36 | class ActionRegistry;
 | 
|---|
 | 37 | class ActionTrait;
 | 
|---|
| [628577] | 38 | 
 | 
|---|
 | 39 | /** This class combines the whole handling of Actions into a single class.
 | 
|---|
 | 40 |  *
 | 
|---|
| [1d3563] | 41 |  * It spawns new Actions by its internal ActionRegistry. Spawned Actions are
 | 
|---|
 | 42 |  * automatically queued and placed into a History after execution.
 | 
|---|
| [628577] | 43 |  */
 | 
|---|
 | 44 | class ActionQueue : public Singleton<ActionQueue>
 | 
|---|
 | 45 | {
 | 
|---|
 | 46 |   friend class Singleton<ActionQueue>;
 | 
|---|
 | 47 | public:
 | 
|---|
| [690741] | 48 |   typedef std::vector<std::string> ActionTokens_t;
 | 
|---|
| [af5384] | 49 |   typedef std::vector< Action * > ActionQueue_t;
 | 
|---|
| [1d3563] | 50 | 
 | 
|---|
| [05c989] | 51 |   /** Queues the Action with \a name to be called.
 | 
|---|
 | 52 |    *
 | 
|---|
| [7fc447] | 53 |    * \param name token of Action to actionqueue
 | 
|---|
| [f54cda] | 54 |    * \param state whether Actions needs to be filled via a Dialog or not
 | 
|---|
| [05c989] | 55 |    */
 | 
|---|
| [f54cda] | 56 |   void queueAction(const std::string &name, enum Action::QueryOptions state = Action::Interactive);
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 |   /** Queues the Action with \a name to be called.
 | 
|---|
 | 59 |    *
 | 
|---|
 | 60 |    * \param _action action to add
 | 
|---|
 | 61 |    * \param state whether Actions needs to be filled via a Dialog or not
 | 
|---|
 | 62 |    */
 | 
|---|
 | 63 |   void queueAction(Action *_action, enum Action::QueryOptions state = Action::Interactive);
 | 
|---|
| [05c989] | 64 | 
 | 
|---|
| [1d3563] | 65 |   /** Returns the spawned action by token \a name.
 | 
|---|
 | 66 |    *
 | 
|---|
| [7fc447] | 67 |    * Action is checked into internal actionqueue.
 | 
|---|
| [1d3563] | 68 |    *
 | 
|---|
 | 69 |    * \return pointer to newly spawned action
 | 
|---|
 | 70 |    */
 | 
|---|
| [a6ceab] | 71 |   Action* getActionByName(const std::string &name);
 | 
|---|
| [1d3563] | 72 | 
 | 
|---|
 | 73 |   /** Checks whether the Action is known by the given \a name.
 | 
|---|
 | 74 |    *
 | 
|---|
 | 75 |    * \param name token of action to check
 | 
|---|
 | 76 |    * \return true - token is known, false - else
 | 
|---|
 | 77 |    */
 | 
|---|
| [a6ceab] | 78 |   bool isActionKnownByName(const std::string &name) const;
 | 
|---|
| [1d3563] | 79 | 
 | 
|---|
| [126867] | 80 |   /** Register an Action with the ActionRegistry.
 | 
|---|
 | 81 |    *
 | 
|---|
 | 82 |    * \param _action action to add
 | 
|---|
 | 83 |    */
 | 
|---|
 | 84 |   void registerAction(Action *_action);
 | 
|---|
 | 85 | 
 | 
|---|
| [690741] | 86 |   /** Returns the vector with the tokens of all currently known Actions.
 | 
|---|
 | 87 |    *
 | 
|---|
 | 88 |    * \return list of all tokens
 | 
|---|
 | 89 |    */
 | 
|---|
 | 90 |   const ActionTokens_t getListOfActions() const;
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |   /** Returns the trait to an Action.
 | 
|---|
 | 93 |    *
 | 
|---|
 | 94 |    * \param name name of Action
 | 
|---|
 | 95 |    * \return const ref to its default Trait.
 | 
|---|
 | 96 |    */
 | 
|---|
| [a6ceab] | 97 |   const ActionTrait& getActionsTrait(const std::string &name) const;
 | 
|---|
| [690741] | 98 | 
 | 
|---|
| [7fc447] | 99 |   /** Print the current contents of the actionqueue as CLI instantiated list of Actions.
 | 
|---|
| [46b181] | 100 |    *
 | 
|---|
 | 101 |    * This is useful for storing the current session.
 | 
|---|
 | 102 |    *
 | 
|---|
 | 103 |    * \param output output stream to print to
 | 
|---|
 | 104 |    */
 | 
|---|
 | 105 |   void outputAsCLI(std::ostream &output) const;
 | 
|---|
 | 106 | 
 | 
|---|
| [7fc447] | 107 |   /** Print the current contents of the actionqueue as Python instantiated list of Actions.
 | 
|---|
| [477012] | 108 |    *
 | 
|---|
 | 109 |    * This is useful for storing the current session.
 | 
|---|
 | 110 |    *
 | 
|---|
 | 111 |    * \param output output stream to print to
 | 
|---|
 | 112 |    */
 | 
|---|
 | 113 |   void outputAsPython(std::ostream &output) const;
 | 
|---|
 | 114 | 
 | 
|---|
| [af5384] | 115 |   /** Undoes last called Acfriend void ::cleanUp();tion.
 | 
|---|
| [6367dd] | 116 |    *
 | 
|---|
 | 117 |    */
 | 
|---|
 | 118 |   void undoLast();
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |   /** Redoes last undone Action.
 | 
|---|
 | 121 |    *
 | 
|---|
 | 122 |    */
 | 
|---|
 | 123 |   void redoLast();
 | 
|---|
 | 124 | 
 | 
|---|
| [a61dbb] | 125 |   /** Return status of last executed action.
 | 
|---|
 | 126 |    *
 | 
|---|
 | 127 |    * \return true - action executed correctly, false - else
 | 
|---|
 | 128 |    */
 | 
|---|
 | 129 |   bool getLastActionOk() const
 | 
|---|
 | 130 |   {  return lastActionOk; }
 | 
|---|
 | 131 | 
 | 
|---|
| [74459a] | 132 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| [415ddd] | 133 |   boost::thread &getRunThread()
 | 
|---|
 | 134 |   { return run_thread; }
 | 
|---|
| [74459a] | 135 | #endif
 | 
|---|
| [af5384] | 136 | 
 | 
|---|
| [26b4eb4] | 137 |   /** Getter to ref to list of status messages.
 | 
|---|
 | 138 |    *
 | 
|---|
 | 139 |    * This is meant for UIs to registers as Observables.
 | 
|---|
 | 140 |    *
 | 
|---|
 | 141 |    * \return ref to StatusList variable
 | 
|---|
 | 142 |    */
 | 
|---|
 | 143 |   ActionStatusList& getStatusList()
 | 
|---|
 | 144 |   { return StatusList; }
 | 
|---|
 | 145 | 
 | 
|---|
| [6367dd] | 146 | private:
 | 
|---|
 | 147 |   //!> grant Action access to internal history functions.
 | 
|---|
 | 148 |   friend class Action;
 | 
|---|
 | 149 | 
 | 
|---|
 | 150 |   /** Wrapper function to add state to ActionHistory.
 | 
|---|
 | 151 |    *
 | 
|---|
 | 152 |    * \param _Action Action whose state to add
 | 
|---|
 | 153 |    * \param _state state to add
 | 
|---|
 | 154 |    */
 | 
|---|
 | 155 |   void addElement(Action* _Action, ActionState::ptr _state);
 | 
|---|
 | 156 | 
 | 
|---|
| [26b4eb4] | 157 |   /** Advocate function to add status message to the list.
 | 
|---|
 | 158 |    *
 | 
|---|
 | 159 |    */
 | 
|---|
 | 160 |   void pushStatus(const std::string &_msg)
 | 
|---|
 | 161 |   { StatusList.pushMessage(_msg); }
 | 
|---|
 | 162 | 
 | 
|---|
| [6367dd] | 163 |   /** Wrapper function to clear ActionHistory.
 | 
|---|
 | 164 |    *
 | 
|---|
 | 165 |    */
 | 
|---|
 | 166 |   void clear();
 | 
|---|
 | 167 | 
 | 
|---|
| [74459a] | 168 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| [415ddd] | 169 |   /** Runs the ActionQueue.
 | 
|---|
 | 170 |    *
 | 
|---|
 | 171 |    */
 | 
|---|
 | 172 |   void run();
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 |   friend void ::stopQueue();
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 |   /** Stops the internal thread.
 | 
|---|
 | 177 |    *
 | 
|---|
 | 178 |    */
 | 
|---|
 | 179 |   void stop();
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |   friend void ::waitQueue();
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 |   /** Wait till all currently queued actions are processed.
 | 
|---|
 | 184 |    *
 | 
|---|
 | 185 |    */
 | 
|---|
 | 186 |   void wait();
 | 
|---|
| [74459a] | 187 | #endif
 | 
|---|
| [415ddd] | 188 | 
 | 
|---|
| [975b83] | 189 |   /** Insert an action after CurrentAction.
 | 
|---|
 | 190 |    *
 | 
|---|
 | 191 |    * This is implemented only to allow action's COMMAND to work. If we
 | 
|---|
 | 192 |    * were to use queueAction, actions would come after all other already
 | 
|---|
 | 193 |    * present actions.
 | 
|---|
 | 194 |    */
 | 
|---|
 | 195 |   void insertAction(Action *_action, enum Action::QueryOptions state);
 | 
|---|
 | 196 | 
 | 
|---|
| [415ddd] | 197 |   /** Moves all action from tempqueue into real queue.
 | 
|---|
 | 198 |    *
 | 
|---|
 | 199 |    */
 | 
|---|
 | 200 |   void insertTempQueue();
 | 
|---|
 | 201 | 
 | 
|---|
| [628577] | 202 | private:
 | 
|---|
| [1d3563] | 203 |   /** Private cstor for ActionQueue.
 | 
|---|
 | 204 |    *
 | 
|---|
 | 205 |    * Must be private as is singleton.
 | 
|---|
 | 206 |    *
 | 
|---|
 | 207 |    */
 | 
|---|
| [628577] | 208 |   ActionQueue();
 | 
|---|
| [1d3563] | 209 | 
 | 
|---|
 | 210 |   /** Dstor for ActionQueue.
 | 
|---|
 | 211 |    *
 | 
|---|
 | 212 |    */
 | 
|---|
| [628577] | 213 |   ~ActionQueue();
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 |   //!> ActionRegistry to spawn new actions
 | 
|---|
| [ed3944] | 216 |   ActionRegistry *AR;
 | 
|---|
| [1d3563] | 217 | 
 | 
|---|
| [6367dd] | 218 |   //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
 | 
|---|
 | 219 |   ActionHistory *history;
 | 
|---|
 | 220 | 
 | 
|---|
| [7fc447] | 221 |   //!> internal actionqueue of actions
 | 
|---|
 | 222 |   ActionQueue_t actionqueue;
 | 
|---|
| [af5384] | 223 | 
 | 
|---|
| [7fc447] | 224 |   //!> point to current action in actionqueue
 | 
|---|
| [af5384] | 225 |   size_t CurrentAction;
 | 
|---|
| [415ddd] | 226 | 
 | 
|---|
 | 227 |   //!> internal temporary actionqueue of actions used by insertAction()
 | 
|---|
 | 228 |   ActionQueue_t tempqueue;
 | 
|---|
 | 229 | 
 | 
|---|
| [a61dbb] | 230 |   //!> indicates that the last action has failed
 | 
|---|
 | 231 |   bool lastActionOk;
 | 
|---|
 | 232 | 
 | 
|---|
| [74459a] | 233 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| [415ddd] | 234 |   //!> internal thread to call Actions
 | 
|---|
 | 235 |   boost::thread run_thread;
 | 
|---|
 | 236 | 
 | 
|---|
 | 237 |   //!> internal mutex to synchronize access to queue
 | 
|---|
 | 238 |   boost::mutex mtx_queue;
 | 
|---|
 | 239 | 
 | 
|---|
 | 240 |   //!> conditional variable notifying when run_thread is idling
 | 
|---|
 | 241 |   boost::condition_variable cond_idle;
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 |   //!> flag indicating whether run_thread is idle or not
 | 
|---|
 | 244 |   bool run_thread_isIdle;
 | 
|---|
 | 245 | 
 | 
|---|
 | 246 |   //!> internal mutex to synchronize access to run_thread_isIdle
 | 
|---|
 | 247 |   boost::mutex mtx_idle;
 | 
|---|
| [74459a] | 248 | #endif
 | 
|---|
| [26b4eb4] | 249 | 
 | 
|---|
 | 250 |   //!> internal list of status messages from Actions for UIs to display
 | 
|---|
 | 251 |   ActionStatusList StatusList;
 | 
|---|
| [628577] | 252 | };
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 | };
 | 
|---|
 | 255 | 
 | 
|---|
 | 256 | #endif /* ACTIONQUEUE_HPP_ */
 | 
|---|