source: src/Actions/ActionQueue.hpp@ 7672284

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 7672284 was 10aee4, checked in by Frederik Heber <heber@…>, 9 years ago

Cleaned up ActionQueue's interface: Actions are always cloned.

  • Action's dstor needs to be public as we have a clone() function.
  • ActionRegistrys' (and AQ's) getActionByName now returns const ref. We must not return a ptr as it may not get deleted elsewhere. We are handing out prototypes.
  • queueAction(action*,...) is private to prevent leakage, queueAction(string, ...) is the public interface.
  • some other small functions are now private, too.
  • MakroActions now need to clone() the prototype on prepare() and delete them on unprepare().
  • ActionSequence deletes contained actions.
  • FIX: ActionSequenceUnitTest cannot check on not-called property of removed actions anymore as these are gone now (they are deleted on removal and they are cloned on insertion).
  • DOCU: Updated documentation on Actions.
  • Property mode set to 100644
File size: 8.8 KB
Line 
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
31void stopQueue();
32void waitQueue();
33#endif
34
35class CommandLineParser;
36
37namespace MoleCuilder {
38
39class ActionHistory;
40class ActionRegistry;
41class ActionTrait;
42class DryRunAdvocate;
43
44namespace 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 */
57class ActionQueue : public Singleton<ActionQueue>, public Observable
58{
59 friend class Singleton<ActionQueue>;
60public:
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 /** Returns the spawned action by token \a name.
82 *
83 * Action is checked into internal actionqueue.
84 *
85 * \return ref to newly spawned action
86 */
87 const Action& getActionByName(const std::string &name);
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 */
94 bool isActionKnownByName(const std::string &name) const;
95
96 /** Register an Action with the ActionRegistry.
97 *
98 * \param _action action to add
99 */
100 void registerAction(Action *_action);
101
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 */
113 const ActionTrait& getActionsTrait(const std::string &name) const;
114
115 /** Print the current contents of the actionqueue as CLI instantiated list of Actions.
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
123 /** Print the current contents of the actionqueue as Python instantiated list of Actions.
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
131 /** Undoes last called Acfriend void ::cleanUp();tion.
132 *
133 */
134 void undoLast();
135
136 /** Redoes last undone Action.
137 *
138 */
139 void redoLast();
140
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
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
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
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
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
182private:
183 //!> grant Action access to internal history functions.
184 friend class Action;
185 //!> grant CommandLineParser access to stop and clearQueue()
186 friend class ::CommandLineParser;
187 //!> grant Advocate access to setting dryrun
188 friend class DryRunAdvocate;
189
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
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
204 /** Advocate function to add status message to the list.
205 *
206 */
207 void pushStatus(const std::string &_msg)
208 { StatusList.pushMessage(_msg); }
209
210 /** Wrapper function to clear ActionHistory.
211 *
212 */
213 void clear();
214
215 /** Clears all actions present in the actionqueues from \a _fromAction.
216 *
217 * @param _fromAction 0 if all Actions to clear or else
218 */
219 void clearQueue(const size_t _fromAction = 0);
220
221#ifdef HAVE_ACTION_THREAD
222 boost::thread &getRunThread()
223 { return run_thread; }
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
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();
254
255 /** Moves all action from tempqueue into real queue.
256 *
257 */
258 void insertTempQueue();
259
260#endif
261
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
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
284private:
285 /** Private cstor for ActionQueue.
286 *
287 * Must be private as is singleton.
288 *
289 */
290 ActionQueue();
291
292 /** Dstor for ActionQueue.
293 *
294 */
295 ~ActionQueue();
296
297private:
298 friend const Action *Queuedetail::lastChanged<Action>();
299 static const Action *_lastchangedaction;
300
301 //!> ActionRegistry to spawn new actions
302 ActionRegistry *AR;
303
304 //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
305 ActionHistory *history;
306
307 //!> internal actionqueue of actions
308 ActionQueue_t actionqueue;
309
310 //!> indicates that the last action has failed
311 bool lastActionOk;
312
313 //!> point to current action in actionqueue
314 size_t CurrentAction;
315
316#ifdef HAVE_ACTION_THREAD
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
324 mutable boost::mutex mtx_queue;
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
333 mutable boost::mutex mtx_idle;
334#endif
335
336 //!> internal list of status messages from Actions for UIs to display
337 ActionStatusList StatusList;
338
339 //!> internal flag whether to call or skip actions (i.e. do a dry run)
340 bool dryrun_flag;
341};
342namespace Queuedetail {
343 template <> inline const Action* lastChanged<Action>() { return ActionQueue::_lastchangedaction; }
344}
345
346};
347
348#endif /* ACTIONQUEUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.