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 |
|
---|
18 | #include "CodePatterns/Observer/Channels.hpp"
|
---|
19 | #include "CodePatterns/Observer/Observable.hpp"
|
---|
20 |
|
---|
21 | #ifdef HAVE_ACTION_THREAD
|
---|
22 | #include <boost/thread.hpp>
|
---|
23 | #endif
|
---|
24 | #include <vector>
|
---|
25 |
|
---|
26 | #include "Actions/Action.hpp"
|
---|
27 | #include "Actions/ActionState.hpp"
|
---|
28 | #include "Actions/ActionStatusList.hpp"
|
---|
29 |
|
---|
30 | #ifdef HAVE_ACTION_THREAD
|
---|
31 | void stopQueue();
|
---|
32 | void waitQueue();
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | class CommandLineParser;
|
---|
36 |
|
---|
37 | namespace MoleCuilder {
|
---|
38 |
|
---|
39 | class ActionHistory;
|
---|
40 | class ActionRegistry;
|
---|
41 | class ActionTrait;
|
---|
42 | class DryRunAdvocate;
|
---|
43 |
|
---|
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 |
|
---|
52 | /** This class combines the whole handling of Actions into a single class.
|
---|
53 | *
|
---|
54 | * It spawns new Actions by its internal ActionRegistry. Spawned Actions are
|
---|
55 | * automatically queued and placed into a History after execution.
|
---|
56 | */
|
---|
57 | class ActionQueue : public Singleton<ActionQueue>, public Observable
|
---|
58 | {
|
---|
59 | friend class Singleton<ActionQueue>;
|
---|
60 | public:
|
---|
61 | typedef std::vector<std::string> ActionTokens_t;
|
---|
62 | typedef std::vector< Action * > ActionQueue_t;
|
---|
63 |
|
---|
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 |
|
---|
74 | /** Queues the Action with \a name to be called.
|
---|
75 | *
|
---|
76 | * \param name token of Action to actionqueue
|
---|
77 | * \param state whether Actions needs to be filled via a Dialog or not
|
---|
78 | */
|
---|
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 | */
|
---|
86 | void queueAction(const Action * const _action, enum Action::QueryOptions state = Action::Interactive);
|
---|
87 |
|
---|
88 | /** Returns the spawned action by token \a name.
|
---|
89 | *
|
---|
90 | * Action is checked into internal actionqueue.
|
---|
91 | *
|
---|
92 | * \return pointer to newly spawned action
|
---|
93 | */
|
---|
94 | Action* getActionByName(const std::string &name);
|
---|
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 | */
|
---|
101 | bool isActionKnownByName(const std::string &name) const;
|
---|
102 |
|
---|
103 | /** Register an Action with the ActionRegistry.
|
---|
104 | *
|
---|
105 | * \param _action action to add
|
---|
106 | */
|
---|
107 | void registerAction(Action *_action);
|
---|
108 |
|
---|
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 | */
|
---|
120 | const ActionTrait& getActionsTrait(const std::string &name) const;
|
---|
121 |
|
---|
122 | /** Print the current contents of the actionqueue as CLI instantiated list of Actions.
|
---|
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 |
|
---|
130 | /** Print the current contents of the actionqueue as Python instantiated list of Actions.
|
---|
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 |
|
---|
138 | /** Undoes last called Acfriend void ::cleanUp();tion.
|
---|
139 | *
|
---|
140 | */
|
---|
141 | void undoLast();
|
---|
142 |
|
---|
143 | /** Redoes last undone Action.
|
---|
144 | *
|
---|
145 | */
|
---|
146 | void redoLast();
|
---|
147 |
|
---|
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 |
|
---|
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 |
|
---|
167 | #ifdef HAVE_ACTION_THREAD
|
---|
168 | boost::thread &getRunThread()
|
---|
169 | { return run_thread; }
|
---|
170 | #endif
|
---|
171 |
|
---|
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 |
|
---|
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 |
|
---|
188 | private:
|
---|
189 | //!> grant Action access to internal history functions.
|
---|
190 | friend class Action;
|
---|
191 | //!> grant CommandLineParser access to stop and clearQueue()
|
---|
192 | friend class ::CommandLineParser;
|
---|
193 | //!> grant Advocate access to setting dryrun
|
---|
194 | friend class DryRunAdvocate;
|
---|
195 |
|
---|
196 | /** Wrapper function to add state to ActionHistory.
|
---|
197 | *
|
---|
198 | * \param _Action Action whose state to add
|
---|
199 | * \param _state state to add
|
---|
200 | */
|
---|
201 | void addElement(Action* _Action, ActionState::ptr _state);
|
---|
202 |
|
---|
203 | /** Advocate function to add status message to the list.
|
---|
204 | *
|
---|
205 | */
|
---|
206 | void pushStatus(const std::string &_msg)
|
---|
207 | { StatusList.pushMessage(_msg); }
|
---|
208 |
|
---|
209 | /** Wrapper function to clear ActionHistory.
|
---|
210 | *
|
---|
211 | */
|
---|
212 | void clear();
|
---|
213 |
|
---|
214 | /** Clears all actions present in the actionqueues from \a _fromAction.
|
---|
215 | *
|
---|
216 | * @param _fromAction 0 if all Actions to clear or else
|
---|
217 | */
|
---|
218 | void clearQueue(const size_t _fromAction = 0);
|
---|
219 |
|
---|
220 | #ifdef HAVE_ACTION_THREAD
|
---|
221 |
|
---|
222 | /** Clears the temporary queue.
|
---|
223 | *
|
---|
224 | */
|
---|
225 | void clearTempQueue();
|
---|
226 |
|
---|
227 | /** Sets the run_thread_isIdle flag.
|
---|
228 | *
|
---|
229 | * @param _flag state to set to
|
---|
230 | */
|
---|
231 | void setRunThreadIdle(const bool _flag);
|
---|
232 |
|
---|
233 | /** Runs the ActionQueue.
|
---|
234 | *
|
---|
235 | */
|
---|
236 | void run();
|
---|
237 |
|
---|
238 | friend void ::stopQueue();
|
---|
239 |
|
---|
240 | /** Stops the internal thread.
|
---|
241 | *
|
---|
242 | */
|
---|
243 | void stop();
|
---|
244 |
|
---|
245 | friend void ::waitQueue();
|
---|
246 |
|
---|
247 | /** Wait till all currently queued actions are processed.
|
---|
248 | *
|
---|
249 | */
|
---|
250 | void wait();
|
---|
251 |
|
---|
252 | /** Moves all action from tempqueue into real queue.
|
---|
253 | *
|
---|
254 | */
|
---|
255 | void insertTempQueue();
|
---|
256 |
|
---|
257 | #endif
|
---|
258 |
|
---|
259 | /** Insert an action after CurrentAction.
|
---|
260 | *
|
---|
261 | * This is implemented only to allow action's COMMAND to work. If we
|
---|
262 | * were to use queueAction, actions would come after all other already
|
---|
263 | * present actions.
|
---|
264 | */
|
---|
265 | void insertAction(Action *_action, enum Action::QueryOptions state);
|
---|
266 |
|
---|
267 | /** Sets the current state of the \a isDryRun flag.
|
---|
268 | *
|
---|
269 | * \param _dryrun true - Actions will not get executed anymore, false - else
|
---|
270 | */
|
---|
271 | void setDryRun(const bool _dryrun)
|
---|
272 | { dryrun_flag = _dryrun; }
|
---|
273 |
|
---|
274 | /** Checks whether next Action should be skipped or not.
|
---|
275 | *
|
---|
276 | * \param _nextaction next action to execute to inspect whether it unsets dryrun_flag
|
---|
277 | * \return true - dryrun_flag set and \a _nextaction is not unsetting dry run
|
---|
278 | */
|
---|
279 | bool isDryRun(const Action *_nextaction) const;
|
---|
280 |
|
---|
281 | private:
|
---|
282 | /** Private cstor for ActionQueue.
|
---|
283 | *
|
---|
284 | * Must be private as is singleton.
|
---|
285 | *
|
---|
286 | */
|
---|
287 | ActionQueue();
|
---|
288 |
|
---|
289 | /** Dstor for ActionQueue.
|
---|
290 | *
|
---|
291 | */
|
---|
292 | ~ActionQueue();
|
---|
293 |
|
---|
294 | private:
|
---|
295 | friend const Action *Queuedetail::lastChanged<Action>();
|
---|
296 | static const Action *_lastchangedaction;
|
---|
297 |
|
---|
298 | //!> ActionRegistry to spawn new actions
|
---|
299 | ActionRegistry *AR;
|
---|
300 |
|
---|
301 | //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
|
---|
302 | ActionHistory *history;
|
---|
303 |
|
---|
304 | //!> internal actionqueue of actions
|
---|
305 | ActionQueue_t actionqueue;
|
---|
306 |
|
---|
307 | //!> indicates that the last action has failed
|
---|
308 | bool lastActionOk;
|
---|
309 |
|
---|
310 | #ifdef HAVE_ACTION_THREAD
|
---|
311 | //!> point to current action in actionqueue
|
---|
312 | size_t CurrentAction;
|
---|
313 |
|
---|
314 | //!> internal temporary actionqueue of actions used by insertAction()
|
---|
315 | ActionQueue_t tempqueue;
|
---|
316 |
|
---|
317 | //!> internal thread to call Actions
|
---|
318 | boost::thread run_thread;
|
---|
319 |
|
---|
320 | //!> internal mutex to synchronize access to queue
|
---|
321 | boost::mutex mtx_queue;
|
---|
322 |
|
---|
323 | //!> conditional variable notifying when run_thread is idling
|
---|
324 | boost::condition_variable cond_idle;
|
---|
325 |
|
---|
326 | //!> flag indicating whether run_thread is idle or not
|
---|
327 | bool run_thread_isIdle;
|
---|
328 |
|
---|
329 | //!> internal mutex to synchronize access to run_thread_isIdle
|
---|
330 | boost::mutex mtx_idle;
|
---|
331 | #endif
|
---|
332 |
|
---|
333 | //!> internal list of status messages from Actions for UIs to display
|
---|
334 | ActionStatusList StatusList;
|
---|
335 |
|
---|
336 | //!> internal flag whether to call or skip actions (i.e. do a dry run)
|
---|
337 | bool dryrun_flag;
|
---|
338 | };
|
---|
339 | namespace Queuedetail {
|
---|
340 | template <> inline const Action* lastChanged<Action>() { return ActionQueue::_lastchangedaction; }
|
---|
341 | }
|
---|
342 |
|
---|
343 | };
|
---|
344 |
|
---|
345 | #endif /* ACTIONQUEUE_HPP_ */
|
---|