source: src/Actions/ActionQueue.hpp@ 0b6b77

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 0b6b77 was 26b4eb4, checked in by Frederik Heber <heber@…>, 11 years ago

Added observable ActionStatusList to ActionQueue.

  • this allows to fetch Status messages from Actions at a central place.
  • Action::pushStatus() as befriended proxy to ActionQueue::pushStatus() added.
  • equipped ActionStatusList with a thread lock to protect access.
  • Property mode set to 100644
File size: 5.7 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
19#ifdef HAVE_ACTION_THREAD
20#include <boost/thread.hpp>
21#endif
22#include <vector>
23
24#include "Actions/Action.hpp"
25#include "Actions/ActionState.hpp"
26#include "Actions/ActionStatusList.hpp"
27
28#ifdef HAVE_ACTION_THREAD
29void stopQueue();
30void waitQueue();
31#endif
32
33namespace MoleCuilder {
34
35class ActionHistory;
36class ActionRegistry;
37class ActionTrait;
38
39/** This class combines the whole handling of Actions into a single class.
40 *
41 * It spawns new Actions by its internal ActionRegistry. Spawned Actions are
42 * automatically queued and placed into a History after execution.
43 */
44class ActionQueue : public Singleton<ActionQueue>
45{
46 friend class Singleton<ActionQueue>;
47public:
48 typedef std::vector<std::string> ActionTokens_t;
49 typedef std::vector< Action * > ActionQueue_t;
50
51 /** Queues the Action with \a name to be called.
52 *
53 * \param name token of Action to actionqueue
54 * \param state whether Actions needs to be filled via a Dialog or not
55 */
56 void queueAction(const std::string &name, enum Action::QueryOptions state = Action::Interactive);
57
58 /** Queues the Action with \a name to be called.
59 *
60 * \param _action action to add
61 * \param state whether Actions needs to be filled via a Dialog or not
62 */
63 void queueAction(Action *_action, enum Action::QueryOptions state = Action::Interactive);
64
65 /** Returns the spawned action by token \a name.
66 *
67 * Action is checked into internal actionqueue.
68 *
69 * \return pointer to newly spawned action
70 */
71 Action* getActionByName(const std::string &name);
72
73 /** Checks whether the Action is known by the given \a name.
74 *
75 * \param name token of action to check
76 * \return true - token is known, false - else
77 */
78 bool isActionKnownByName(const std::string &name) const;
79
80 /** Register an Action with the ActionRegistry.
81 *
82 * \param _action action to add
83 */
84 void registerAction(Action *_action);
85
86 /** Returns the vector with the tokens of all currently known Actions.
87 *
88 * \return list of all tokens
89 */
90 const ActionTokens_t getListOfActions() const;
91
92 /** Returns the trait to an Action.
93 *
94 * \param name name of Action
95 * \return const ref to its default Trait.
96 */
97 const ActionTrait& getActionsTrait(const std::string &name) const;
98
99 /** Print the current contents of the actionqueue as CLI instantiated list of Actions.
100 *
101 * This is useful for storing the current session.
102 *
103 * \param output output stream to print to
104 */
105 void outputAsCLI(std::ostream &output) const;
106
107 /** Print the current contents of the actionqueue as Python instantiated list of Actions.
108 *
109 * This is useful for storing the current session.
110 *
111 * \param output output stream to print to
112 */
113 void outputAsPython(std::ostream &output) const;
114
115 /** Undoes last called Acfriend void ::cleanUp();tion.
116 *
117 */
118 void undoLast();
119
120 /** Redoes last undone Action.
121 *
122 */
123 void redoLast();
124
125#ifdef HAVE_ACTION_THREAD
126 boost::thread &getRunThread()
127 { return run_thread; }
128#endif
129
130 /** Getter to ref to list of status messages.
131 *
132 * This is meant for UIs to registers as Observables.
133 *
134 * \return ref to StatusList variable
135 */
136 ActionStatusList& getStatusList()
137 { return StatusList; }
138
139private:
140 //!> grant Action access to internal history functions.
141 friend class Action;
142
143 /** Wrapper function to add state to ActionHistory.
144 *
145 * \param _Action Action whose state to add
146 * \param _state state to add
147 */
148 void addElement(Action* _Action, ActionState::ptr _state);
149
150 /** Advocate function to add status message to the list.
151 *
152 */
153 void pushStatus(const std::string &_msg)
154 { StatusList.pushMessage(_msg); }
155
156 /** Wrapper function to clear ActionHistory.
157 *
158 */
159 void clear();
160
161#ifdef HAVE_ACTION_THREAD
162 /** Runs the ActionQueue.
163 *
164 */
165 void run();
166
167 friend void ::stopQueue();
168
169 /** Stops the internal thread.
170 *
171 */
172 void stop();
173
174 friend void ::waitQueue();
175
176 /** Wait till all currently queued actions are processed.
177 *
178 */
179 void wait();
180#endif
181
182 /** Insert an action after CurrentAction.
183 *
184 * This is implemented only to allow action's COMMAND to work. If we
185 * were to use queueAction, actions would come after all other already
186 * present actions.
187 */
188 void insertAction(Action *_action, enum Action::QueryOptions state);
189
190 /** Moves all action from tempqueue into real queue.
191 *
192 */
193 void insertTempQueue();
194
195private:
196 /** Private cstor for ActionQueue.
197 *
198 * Must be private as is singleton.
199 *
200 */
201 ActionQueue();
202
203 /** Dstor for ActionQueue.
204 *
205 */
206 ~ActionQueue();
207
208 //!> ActionRegistry to spawn new actions
209 ActionRegistry *AR;
210
211 //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
212 ActionHistory *history;
213
214 //!> internal actionqueue of actions
215 ActionQueue_t actionqueue;
216
217 //!> point to current action in actionqueue
218 size_t CurrentAction;
219
220 //!> internal temporary actionqueue of actions used by insertAction()
221 ActionQueue_t tempqueue;
222
223#ifdef HAVE_ACTION_THREAD
224 //!> internal thread to call Actions
225 boost::thread run_thread;
226
227 //!> internal mutex to synchronize access to queue
228 boost::mutex mtx_queue;
229
230 //!> conditional variable notifying when run_thread is idling
231 boost::condition_variable cond_idle;
232
233 //!> flag indicating whether run_thread is idle or not
234 bool run_thread_isIdle;
235
236 //!> internal mutex to synchronize access to run_thread_isIdle
237 boost::mutex mtx_idle;
238#endif
239
240 //!> internal list of status messages from Actions for UIs to display
241 ActionStatusList StatusList;
242};
243
244};
245
246#endif /* ACTIONQUEUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.