source: src/Actions/ActionQueue.hpp@ fae462

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since fae462 was f3db60, checked in by Frederik Heber <heber@…>, 10 years ago

Added DryRun and NoDryRun actions.

  • actions (un)set a flag inside ActionQueue to which only those permitted by an advocate pattern have access.
  • added testsuite tests for both dry-run, no-dry-run, and storing sessions.
  • explained use of action in userguide.
  • Property mode set to 100644
File size: 8.6 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 /** 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
188private:
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
281private:
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
294private:
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};
339namespace Queuedetail {
340 template <> inline const Action* lastChanged<Action>() { return ActionQueue::_lastchangedaction; }
341}
342
343};
344
345#endif /* ACTIONQUEUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.