source: src/Actions/Action.hpp@ 126867

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

Actions no longer register themselves, ActionQueue::addAction() introduced.

  • eventually, Actions should be constrained to the ActionQueue alone. They may be created externally but their control has to be delivered to the ActionQueue.
  • Hence, we reversed the registering process: Actions do not register themselves with the ActionRegistry but as almost all are created in ActionRegistry::fillRegistry() they are registered there as well.
  • Actions cstor and dstor are now protected, ActionRegistry is friend.
  • Action needs to befriend Registry<T> and ActionSequenceTest to grant access to dstor.
  • Property mode set to 100644
File size: 9.3 KB
Line 
1/*
2 * Action.hpp
3 *
4 * Created on: Dec 8, 2009
5 * Author: crueger
6 */
7
8#ifndef ACTION_HPP_
9#define ACTION_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <string>
17
18#include <boost/preprocessor/list/adt.hpp>
19
20/** Used in .def files in paramdefaults define to set that no default value exists.
21 * We define NOPARAM_DEFAULT here, as it is used in .def files and needs to be present
22 * before these are included.
23 */
24#define NOPARAM_DEFAULT BOOST_PP_NIL
25
26// forward declaration
27template <typename T> class Registry;
28
29namespace MoleCuilder {
30 class ActionHistory;
31 class ActionRegistry;
32 class ActionSequence;
33}
34class ActionSequenceTest;
35class Dialog;
36
37#include "Actions/ActionParameters.hpp"
38#include "Actions/ActionState.hpp"
39#include "Actions/ActionTrait.hpp"
40
41
42namespace MoleCuilder {
43
44/** Actions are Command patterns to allow for undoing and redoing.
45 *
46 * Each specific Action derives from this class to implement a certain functionality.
47 *
48 * Actions describe something that has to be done.
49 * Actions can be passed around, stored, performed and undone (Command-Pattern:
50 * http://en.wikipedia.org/wiki/Command_pattern).
51 *
52 * Unique to each Action is its ActionTrait, i.e. the options it requires
53 * to perform a certain function. E.g. in order to execute a "add atom" Action
54 * we need to know a position and an element. These options have certain
55 * properties, see \ref OptionTrait and \ref ActionTrait wherein these are stored.
56 * Essentially, each option is stored as an \ref OptionTrait instance and
57 * contains the token, default value, a description, the type, ...
58 *
59 * ActionTrait itself is also an OptionTrait because the command token may actually
60 * coincide with an option-token. E.g. this allows "...--add-atom 3" to mean
61 * both execute the action under token "add-atom" and that the option "add-atom"
62 * (the new atom's \ref element number) should contain 3.
63 *
64 * \ref ActionTrait contains a map of all associated options. With this in the cstor
65 * we register not only the Action with the \ref ActionRegistry but also each of
66 * its \link options OptionTrait \endlink with the \ref OptionRegistry.
67 *
68 * The token of the option is unique, but two Action's may share the same token if:
69 * -# they have the same default value
70 * -# they have the same type
71 *
72 * This requirement is easy because if you need to store some option of another
73 * type, simply think of a new suitable name for it.
74 *
75 * The actual value, i.e. "3" in the "add-atom" example, is taken from the
76 * ValueStorage, see \ref Dialog for how this is possible.
77 *
78 * \note There is a unit test that checks on the consistency of all registered
79 * options, also in "--enable-debug"-mode assertions will check that an option
80 * has not been registered before under another type.
81 *
82 */
83class Action
84{
85 friend class ActionHistory;
86 friend class ActionRegistry;
87 friend class ActionSequence;
88 template <typename T> friend class ::Registry;
89 //!> TextMenu needs to instantiate some specific Actions, grant access to cstor
90 friend class TextMenu;
91
92 // some unit tests on Actions
93 friend class ::ActionSequenceTest;
94public:
95
96 enum QueryOptions {Interactive, NonInteractive};
97
98protected:
99 /**
100 * Standard constructor of Action Base class
101 *
102 * All Actions need to have a name. The second flag indicates, whether the action should
103 * be registered with the ActionRegistry. If the Action is registered the name of the
104 * Action needs to be unique for all Actions that are registered.
105 *
106 * \note NO reference for \a _Traits as we do have to copy it, otherwise _Traits would have
107 * to be present throughout the program's run.
108 *
109 * \param Traits information class to this action
110 */
111 Action(const ActionTrait &_Traits);
112 virtual ~Action();
113
114public:
115 /**
116 * This method is used to call an action. The basic operations for the Action
117 * are carried out and if necessary/possible the Action is added to the History
118 * to allow for undo of this action.
119 *
120 * If the call needs to undone you have to use the History, to avoid destroying
121 * invariants used by the History.
122 *
123 * Note that this call can be Interactive (i.e. a dialog will ask the user for
124 * necessary information) and NonInteractive (i.e. the information will have to
125 * be present already within the ValueStorage class or else a MissingArgumentException
126 * is thrown)
127 */
128 void call(enum QueryOptions state = Interactive);
129
130 /**
131 * This method provides a flag that indicates if an undo mechanism is implemented
132 * for this Action. If this is true, and this action was called last, you can
133 * use the History to undo this action.
134 */
135 virtual bool canUndo()=0;
136
137 /**
138 * This method provides a flag, that indicates if the Action changes the state of
139 * the application in a way that needs to be undone for the History to work.
140 *
141 * If this is false the Action will not be added to the History upon calling. However
142 * Actions called before this one will still be available for undo.
143 */
144 virtual bool shouldUndo()=0;
145
146 /**
147 * Indicates whether the Action can do it's work at the moment. If this
148 * is false calling the action will result in a no-op.
149 */
150 virtual bool isActive();
151
152 /**
153 * Returns the name of the Action.
154 */
155 const std::string getName() const;
156
157 /**
158 * returns a detailed help message.
159 */
160 const std::string help() const;
161
162 /**
163 * Traits resemble all necessary information that "surrounds" an action, such as
164 * its name (for ActionRegistry and as ref from string to instance and vice versa),
165 * which menu, which position, what parameters, their types, if it is itself a
166 * parameter and so on ...
167 *
168 * Note that is important that we do not use a reference here. We want to copy the
169 * information in the Action's constructor and have it contained herein. Hence, we
170 * also have our own copy constructor for ActionTrait. Information should be
171 * encapsulated in the Action, no more references to the outside than absolutely
172 * necessary.
173 */
174 const ActionTrait Traits;
175
176protected:
177 /** Removes the static entities Action::success and Action::failure.
178 * This is only to be called on the program's exit, i.e. in cleanUp(),
179 * as these static entities are used throughout all Actions.
180 */
181 static void removeStaticStateEntities();
182
183 /** Creates the static entities Action::success and Action::failure.
184 * This is only to be called by ActionHistory.
185 */
186 static void createStaticStateEntities();
187
188 /**
189 * This method is called by the History, when an undo is performed. It is
190 * provided with the corresponding state produced by the performCall or
191 * performRedo method and needs to provide a state that can be used for redo.
192 */
193 ActionState::ptr undo(ActionState::ptr);
194
195 /**
196 * This method is called by the History, when a redo is performed. It is
197 * provided with the corresponding state produced by the undo method and
198 * needs to produce a State that can then be used for another undo.
199 */
200 ActionState::ptr redo(ActionState::ptr);
201
202 /**
203 * This special state can be used to indicate that the Action was successful
204 * without providing a special state. Use this if your Action does not need
205 * a specialized state.
206 */
207 static ActionState::ptr success;
208
209 /**
210 * This special state can be returned, to indicate that the action could not do it's
211 * work, was aborted by the user etc. If you return this state make sure to transactionize
212 * your Actions and unroll the complete transaction before this is returned.
213 */
214 static ActionState::ptr failure;
215
216 /**
217 * This creates the dialog requesting the information needed for this action from the user
218 * via means of the user interface.
219 */
220 Dialog * createDialog();
221
222 /** Virtual function that starts the timer.
223 *
224 */
225 virtual void startTimer() const {};
226
227 /** Virtual function that ends the timer.
228 *
229 */
230 virtual void endTimer() const {};
231
232private:
233
234 /**
235 * This is called internally before the action is processed. This adds necessary queries
236 * to a given dialog to obtain parameters for the user for processing the action accordingly.
237 * The dialog will be given to the user before Action::performCall() is initiated, values
238 * are transfered via ValueStorage.
239 */
240 virtual Dialog * fillDialog(Dialog*)=0;
241
242 /**
243 * This is called internally when the call is being done. Implement this method to do the actual
244 * work of the Action. Implement this in your Derived classes. Needs to return a state that can be
245 * used to undo the action.
246 */
247 virtual ActionState::ptr performCall()=0;
248
249 /**
250 * This is called internally when the undo process is chosen. This Method should use the state
251 * produced by the performCall method to return the state of the application to the state
252 * it had before the Action.
253 */
254 virtual ActionState::ptr performUndo(ActionState::ptr)=0;
255
256 /**
257 * This is called internally when the redo process is chosen. This method shoudl use the state
258 * produced by the performUndo method to return the application to the state it should have after
259 * the action.
260 *
261 * Often this method can be implement to re-use the performCall method. However if user interaction
262 * or further parameters are needed, those should be taken from the state and not query the user
263 * again.
264 */
265 virtual ActionState::ptr performRedo(ActionState::ptr)=0;
266};
267
268}
269
270#endif /* ACTION_HPP_ */
Note: See TracBrowser for help on using the repository browser.