source: src/Actions/ActionQueue.hpp@ f27c2c

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 f27c2c was a87d1e2, checked in by Frederik Heber <heber@…>, 10 years ago

FIX: ActionQueue::isIdle() states whether actions are executed or not.

  • QtUIFactory uses this to wait till test running next action.
  • 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 /** 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 /** States whether the ActionQueue is currently executing Actions or done executing.
189 *
190 * \return true - ActionQueue is done executing Actions.
191 */
192 bool isIdle() const;
193
194private:
195 //!> grant Action access to internal history functions.
196 friend class Action;
197 //!> grant CommandLineParser access to stop and clearQueue()
198 friend class ::CommandLineParser;
199 //!> grant Advocate access to setting dryrun
200 friend class DryRunAdvocate;
201
202 /** Wrapper function to add state to ActionHistory.
203 *
204 * \param _Action Action whose state to add
205 * \param _state state to add
206 */
207 void addElement(Action* _Action, ActionState::ptr _state);
208
209 /** Advocate function to add status message to the list.
210 *
211 */
212 void pushStatus(const std::string &_msg)
213 { StatusList.pushMessage(_msg); }
214
215 /** Wrapper function to clear ActionHistory.
216 *
217 */
218 void clear();
219
220 /** Clears all actions present in the actionqueues from \a _fromAction.
221 *
222 * @param _fromAction 0 if all Actions to clear or else
223 */
224 void clearQueue(const size_t _fromAction = 0);
225
226#ifdef HAVE_ACTION_THREAD
227
228 /** Clears the temporary queue.
229 *
230 */
231 void clearTempQueue();
232
233 /** Sets the run_thread_isIdle flag.
234 *
235 * @param _flag state to set to
236 */
237 void setRunThreadIdle(const bool _flag);
238
239 /** Runs the ActionQueue.
240 *
241 */
242 void run();
243
244 friend void ::stopQueue();
245
246 /** Stops the internal thread.
247 *
248 */
249 void stop();
250
251 friend void ::waitQueue();
252
253 /** Wait till all currently queued actions are processed.
254 *
255 */
256 void wait();
257
258 /** Moves all action from tempqueue into real queue.
259 *
260 */
261 void insertTempQueue();
262
263#endif
264
265 /** Insert an action after CurrentAction.
266 *
267 * This is implemented only to allow action's COMMAND to work. If we
268 * were to use queueAction, actions would come after all other already
269 * present actions.
270 */
271 void insertAction(Action *_action, enum Action::QueryOptions state);
272
273 /** Sets the current state of the \a isDryRun flag.
274 *
275 * \param _dryrun true - Actions will not get executed anymore, false - else
276 */
277 void setDryRun(const bool _dryrun)
278 { dryrun_flag = _dryrun; }
279
280 /** Checks whether next Action should be skipped or not.
281 *
282 * \param _nextaction next action to execute to inspect whether it unsets dryrun_flag
283 * \return true - dryrun_flag set and \a _nextaction is not unsetting dry run
284 */
285 bool isDryRun(const Action *_nextaction) const;
286
287private:
288 /** Private cstor for ActionQueue.
289 *
290 * Must be private as is singleton.
291 *
292 */
293 ActionQueue();
294
295 /** Dstor for ActionQueue.
296 *
297 */
298 ~ActionQueue();
299
300private:
301 friend const Action *Queuedetail::lastChanged<Action>();
302 static const Action *_lastchangedaction;
303
304 //!> ActionRegistry to spawn new actions
305 ActionRegistry *AR;
306
307 //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
308 ActionHistory *history;
309
310 //!> internal actionqueue of actions
311 ActionQueue_t actionqueue;
312
313 //!> indicates that the last action has failed
314 bool lastActionOk;
315
316 //!> point to current action in actionqueue
317 size_t CurrentAction;
318
319#ifdef HAVE_ACTION_THREAD
320 //!> internal temporary actionqueue of actions used by insertAction()
321 ActionQueue_t tempqueue;
322
323 //!> internal thread to call Actions
324 boost::thread run_thread;
325
326 //!> internal mutex to synchronize access to queue
327 boost::mutex mtx_queue;
328
329 //!> conditional variable notifying when run_thread is idling
330 boost::condition_variable cond_idle;
331
332 //!> flag indicating whether run_thread is idle or not
333 bool run_thread_isIdle;
334
335 //!> internal mutex to synchronize access to run_thread_isIdle
336 boost::mutex mtx_idle;
337#endif
338
339 //!> internal list of status messages from Actions for UIs to display
340 ActionStatusList StatusList;
341
342 //!> internal flag whether to call or skip actions (i.e. do a dry run)
343 bool dryrun_flag;
344};
345namespace Queuedetail {
346 template <> inline const Action* lastChanged<Action>() { return ActionQueue::_lastchangedaction; }
347}
348
349};
350
351#endif /* ACTIONQUEUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.