source: src/Actions/ActionQueue.hpp@ dd6c07

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 dd6c07 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
RevLine 
[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]31void stopQueue();
32void waitQueue();
[74459a]33#endif
[415ddd]34
[11d433]35class CommandLineParser;
36
[628577]37namespace MoleCuilder {
38
[6367dd]39class ActionHistory;
[ed3944]40class ActionRegistry;
41class ActionTrait;
[f3db60]42class DryRunAdvocate;
[628577]43
[29b52b]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
[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]57class ActionQueue : public Singleton<ActionQueue>, public Observable
[628577]58{
59 friend class Singleton<ActionQueue>;
60public:
[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
[6367dd]188private:
189 //!> grant Action access to internal history functions.
190 friend class Action;
[11d433]191 //!> grant CommandLineParser access to stop and clearQueue()
192 friend class ::CommandLineParser;
[f3db60]193 //!> grant Advocate access to setting dryrun
194 friend class DryRunAdvocate;
[6367dd]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
[26b4eb4]203 /** Advocate function to add status message to the list.
204 *
205 */
206 void pushStatus(const std::string &_msg)
207 { StatusList.pushMessage(_msg); }
208
[6367dd]209 /** Wrapper function to clear ActionHistory.
210 *
211 */
212 void clear();
213
[601ef8]214 /** Clears all actions present in the actionqueues from \a _fromAction.
[7f1a1a]215 *
[601ef8]216 * @param _fromAction 0 if all Actions to clear or else
[7f1a1a]217 */
[601ef8]218 void clearQueue(const size_t _fromAction = 0);
[7f1a1a]219
[74459a]220#ifdef HAVE_ACTION_THREAD
[601ef8]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
[415ddd]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();
[601ef8]251
252 /** Moves all action from tempqueue into real queue.
253 *
254 */
255 void insertTempQueue();
256
[74459a]257#endif
[415ddd]258
[975b83]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
[f3db60]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
[628577]281private:
[1d3563]282 /** Private cstor for ActionQueue.
283 *
284 * Must be private as is singleton.
285 *
286 */
[628577]287 ActionQueue();
[1d3563]288
289 /** Dstor for ActionQueue.
290 *
291 */
[628577]292 ~ActionQueue();
293
[29b52b]294private:
295 friend const Action *Queuedetail::lastChanged<Action>();
296 static const Action *_lastchangedaction;
297
[628577]298 //!> ActionRegistry to spawn new actions
[ed3944]299 ActionRegistry *AR;
[1d3563]300
[6367dd]301 //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
302 ActionHistory *history;
303
[7fc447]304 //!> internal actionqueue of actions
305 ActionQueue_t actionqueue;
[af5384]306
[601ef8]307 //!> indicates that the last action has failed
308 bool lastActionOk;
309
310#ifdef HAVE_ACTION_THREAD
[7fc447]311 //!> point to current action in actionqueue
[af5384]312 size_t CurrentAction;
[415ddd]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;
[74459a]331#endif
[26b4eb4]332
333 //!> internal list of status messages from Actions for UIs to display
334 ActionStatusList StatusList;
[f3db60]335
336 //!> internal flag whether to call or skip actions (i.e. do a dry run)
337 bool dryrun_flag;
[628577]338};
[8859b5]339namespace Queuedetail {
340 template <> inline const Action* lastChanged<Action>() { return ActionQueue::_lastchangedaction; }
341}
[628577]342
343};
344
345#endif /* ACTIONQUEUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.