source: src/Actions/Action_impl_pre.hpp@ af5384

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

Actions can now clone() and split off prepare() from call().

  • ActionQueue now separates call phase into clone(), prepare(), and call().
  • ActionQueue's dstor releases Actions in deque.
  • CurrentAction is an index as push_back always invalidates iterator.
  • ActionQueue_t is now a simple vector of ptrs.
  • call() is now non-interactive, (maybe) interactive part is placed in prepare() where dialogs are used to fill parameters.
  • only fully prepared Actions are placed in ActionQueue.
  • ActionQueue obtains action instances from Registry, copies, prepares them, and places them in the queue.
  • Action::clone() gets QueryOptions as param. This lets us either clone without params or really copy the instance.
  • If action is called as COMMAND, params have been filled already.
  • Reactions require a ActionQueue::getLastAction() to actually obtain result of (cloned) Action.
  • FIX: Calculation now has const has...() and getResult().
  • Property mode set to 100644
File size: 13.9 KB
Line 
1/*
2 * Action_impl.hpp
3 *
4 * Created on: Aug 25, 2010
5 * Author: heber
6 */
7
8/** These macros define the following functions, necessary but repetitive for
9 * every Action:
10 * -# Dialog* fillDialog()
11 * -# action command (e.g. AnalysisMolecularVolume() )
12 * -# void getParametersfromValuStorage()
13 * -# struct Action...Parameters
14 *
15 * For this, the user has the define the following values, each with
16 * parenthesis, for the values/parameters the action needs
17 * -# paramtypes, e.g. (int)(double)
18 * -# paramtokens, e.g. ("Z")("length")
19 * -# paramreferences, e.g. (Z)(length)
20 * and for additional values/parameters to save in the state
21 * -# statetypes, e.g. (int)(double)
22 * -# statereferences, e.g. (Z)(length)
23 * and the name and category of the action
24 * -# CATEGORY, e.g. Analysis
25 * -# ACTIONNAME, e.g. MolecularVolume
26 */
27
28// include config.h
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32
33#include "CodePatterns/Chronos.hpp"
34
35#include <boost/preprocessor/cat.hpp>
36#include <boost/preprocessor/expand.hpp>
37#include <boost/preprocessor/comparison/equal.hpp>
38#include <boost/preprocessor/comparison/not_equal.hpp>
39#include <boost/preprocessor/control/expr_if.hpp>
40#include <boost/preprocessor/control/if.hpp>
41#include <boost/preprocessor/debug/assert.hpp>
42#include <boost/preprocessor/facilities/empty.hpp>
43#include <boost/preprocessor/iteration/local.hpp>
44#include <boost/preprocessor/list/adt.hpp>
45#include <boost/preprocessor/punctuation/comma_if.hpp>
46#include <boost/preprocessor/repetition/repeat.hpp>
47#include <boost/preprocessor/seq/elem.hpp>
48#include <boost/preprocessor/seq/filter.hpp>
49#include <boost/preprocessor/seq/push_back.hpp>
50#include <boost/preprocessor/seq/seq.hpp>
51#include <boost/preprocessor/seq/size.hpp>
52#include <boost/preprocessor/seq/transform.hpp>
53
54#include "Actions/ActionQueue.hpp"
55#include "Actions/toCLIString.hpp"
56#include "Actions/toPythonString.hpp"
57#include "Parameters/Parameter.hpp"
58
59
60// some derived names: if CATEGORY is not given, we don't prefix with it
61#ifdef CATEGORY
62#define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
63#define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
64#define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
65#define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
66#else
67#define ACTION BOOST_PP_CAT(ACTIONNAME, Action)
68#define COMMAND ACTIONNAME
69#define STATE BOOST_PP_CAT(ACTIONNAME, State)
70#define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
71#endif
72#define INSTANCE BOOST_PP_CAT(this_, BOOST_PP_CAT(ACTIONNAME, _instance))
73
74// check if no lists given
75#ifndef paramtypes
76#define MAXPARAMTYPES 0
77#else
78#define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
79#endif
80#ifndef statetypes
81#define MAXSTATETYPES 0
82#else
83#define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
84#endif
85#ifndef paramdefaults
86#define MAXPARAMDEFAULTS 0
87// this is required for valid_print "else part"
88#define sequencer(z,n,data) \
89 BOOST_PP_SEQ_PUSH_BACK( data, NOPARAM_DEFAULT)
90#define paramdefaults BOOST_PP_REPEAT( MAXPARAMTYPES, sequencer, BOOST_PP_SEQ_NIL )
91#else
92#define MAXPARAMDEFAULTS BOOST_PP_SEQ_SIZE(paramdefaults)
93#endif
94#define PARAM_DEFAULT(x) \
95 (x, BOOST_PP_NIL)
96
97// check user has given name and category
98#ifndef ACTIONNAME
99ERROR: No "ACTIONNAME" defined in: __FILE__
100#endif
101
102// calculate numbers and check whether all have same size
103#ifdef paramtokens
104BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
105 ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
106)
107#endif
108#ifdef paramreferences
109BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
110 ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
111)
112#endif
113
114#ifdef statetypes
115BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
116 ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
117)
118#endif
119
120// print a list of type ref followed by a separator, i.e. "int i;"
121#define initialiser_print(z,n,initialiserlist) \
122 BOOST_PP_SEQ_ELEM(n, initialiserlist) \
123 (BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(n, initialiserlist))),
124
125// print a list of ref(_ref) followed by a separator, i.e. "id(_id),"
126#define type_print(z,n,TYPELIST, VARLIST, separator) \
127 BOOST_PP_SEQ_ELEM(n, TYPELIST) \
128 BOOST_PP_SEQ_ELEM(n, VARLIST)\
129 separator
130
131// print a list of type ref followed, i.e. "int i, double position"
132#define type_list(z,n,TYPELIST,VARLIST) \
133 BOOST_PP_COMMA_IF(n)\
134 BOOST_PP_SEQ_ELEM(n, TYPELIST) \
135 BOOST_PP_SEQ_ELEM(n, VARLIST)
136
137// prints dialog->query calls for paramtypes with tokens
138#define dialog_print(z,n,unused) \
139 dialog->query<\
140 BOOST_PP_SEQ_ELEM(n, paramtypes)\
141 >( params. \
142 BOOST_PP_SEQ_ELEM(n, paramreferences)\
143 ,\
144 BOOST_PP_SEQ_ELEM(n, paramtokens)\
145 , Traits.getDescription()\
146 );
147
148// prints command line call for this Action for paramtypes with tokens
149#define outputAsCLI_print(z,n,output) \
150 output << \
151 BOOST_PP_IF(n, " --", "--") \
152 << \
153 BOOST_PP_SEQ_ELEM(n, paramtokens) \
154 << " " << toCLIString(params. \
155 BOOST_PP_SEQ_ELEM(n, paramreferences) \
156 .get());
157
158// prints if statement to check two strings (paramtokens[n] vs. TOKEN)
159#define checkpresenttoken_print(z, n, TOKEN, booltoken) \
160 if ( std::string(\
161 BOOST_PP_SEQ_ELEM(n, paramtokens) )\
162 == getName()) \
163 booltoken = false;
164
165// prints command line call for this Action for paramtypes with tokens
166#define outputAsPython_print(z,n,output) \
167 output << \
168 BOOST_PP_IF(n, ", ", "") \
169 << "\"" << toPythonString(params. \
170 BOOST_PP_SEQ_ELEM(n, paramreferences) \
171 .get()) \
172 << "\"";
173
174// print an initialiser list, i.e. "var( token, valid (,default) )(,)"
175#define valid_print(z,n,TOKENLIST, VARLIST, VALIDLIST, DEFAULTLIST) \
176 BOOST_PP_COMMA_IF(n) \
177 BOOST_PP_SEQ_ELEM(n, VARLIST) \
178 ( \
179 BOOST_PP_SEQ_ELEM(n, TOKENLIST) \
180 , \
181 BOOST_PP_SEQ_ELEM(n, VALIDLIST) \
182 BOOST_PP_COMMA_IF( BOOST_PP_NOT( BOOST_PP_LIST_IS_NIL( BOOST_PP_SEQ_ELEM(n, DEFAULTLIST) ) ) ) \
183 BOOST_PP_EXPR_IF( \
184 BOOST_PP_NOT( BOOST_PP_LIST_IS_NIL( BOOST_PP_SEQ_ELEM(n, DEFAULTLIST) ) ), \
185 BOOST_PP_LIST_FIRST( BOOST_PP_SEQ_ELEM(n, DEFAULTLIST) )) \
186 )
187
188// print an initialiser list, i.e. "var( valid . var )(,)"
189#define validcopy_print(z,n,TOKENLIST, VARLIST, VALID) \
190 BOOST_PP_COMMA_IF(n) \
191 BOOST_PP_SEQ_ELEM(n, VARLIST) \
192 ( \
193 VALID . \
194 BOOST_PP_SEQ_ELEM(n, VARLIST) \
195 )
196
197// prints set/queryCurrentValue (command) for paramreferences and paramtokens
198#define value_print(z, n, container, prefix) \
199 prefix \
200 BOOST_PP_SEQ_ELEM(n, container)\
201 .set(\
202 BOOST_PP_SEQ_ELEM(n, container)\
203 );
204
205// prints set/queryCurrentValue (command) for paramreferences and paramtokens
206#define valuetype_print(z,n,container, types, prefix) \
207 prefix \
208 BOOST_PP_SEQ_ELEM(n, container) \
209 .setAsString( \
210 BOOST_PP_SEQ_ELEM(n, container) \
211 );
212
213#define stringtype std::string
214
215#define type2string(s, data, elem) \
216 stringtype
217
218
219//#include "Actions/ActionTraits.hpp"
220#include "UIElements/Dialog.hpp"
221
222#ifdef paramtokens
223#define statenecessary 1
224#endif
225#ifndef statetokens
226#define statenecessary 1
227#endif
228
229namespace MoleCuilder {
230
231// =========== memento to remember the state when undoing ===========
232#ifdef statenecessary
233class STATE : public ActionState {
234public:
235 STATE(
236#if defined statetypes && defined statereferences // if we have parameters, we have to add "_" before each reference and add the params as the last one
237#define OP(s,data,elem) BOOST_PP_CAT(data, elem) // OP to add "_"
238#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_PUSH_BACK(statetypes, const ACTION::PARAMS &), BOOST_PP_SEQ_TRANSFORM(OP, _, BOOST_PP_SEQ_PUSH_BACK(statereferences, params)))
239#else /// if not, params is only list
240#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, (const ACTION::PARAMS &), (_params))
241#endif
242#define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES)
243#include BOOST_PP_LOCAL_ITERATE()
244) :
245#if defined statetypes && defined statereferences // do we have parameters at all?
246BOOST_PP_REPEAT(MAXSTATETYPES, initialiser_print, statereferences)
247#endif
248params(_params)
249 {}
250
251#if defined statetypes && defined statereferences // do we have parameters at all?
252#define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, statetypes, statereferences, ;)
253#define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES-1)
254#include BOOST_PP_LOCAL_ITERATE()
255#endif
256 ACTION::PARAMS params;
257};
258#endif /* statenecessary */
259
260// (const) prototype to be placed into the ActionRegistry (must be deleted by registry itself)
261//const ACTION INSTANCE;
262//boost::shared_ptr< ACTION > INSTANCE( new ACTION() );
263
264// =========== constructor ===========
265ACTION::ACTION () :
266 Action(ActionTraits< ACTION >())
267{}
268
269// =========== destructor ===========
270ACTION::~ACTION ()
271{
272 //std::cout << "Action ACTION is being destroyed." << std::endl;
273}
274
275// =========== parameter constructor ===========
276ACTION::PARAMS::PARAMS()
277#if defined paramtokens && defined paramreferences && defined paramvalids
278 :
279#define BOOST_PP_LOCAL_MACRO(n) valid_print(~, n, paramtokens, paramreferences, paramvalids, paramdefaults)
280#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
281#include BOOST_PP_LOCAL_ITERATE()
282#endif
283 {}
284
285ACTION::PARAMS::PARAMS(const PARAMS &p)
286#if defined paramtokens && defined paramreferences
287 :
288#define BOOST_PP_LOCAL_MACRO(n) validcopy_print(~, n, paramtokens, paramreferences, p)
289#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
290#include BOOST_PP_LOCAL_ITERATE()
291#endif
292 {}
293
294// =========== clone Action ===========
295Action* ACTION::clone(enum QueryOptions flag) const
296{
297 if (flag == Interactive)
298 return new ACTION();
299 else
300 return new ACTION(*this);
301}
302
303// =========== fill a dialog ===========
304Dialog* ACTION::fillDialog(Dialog *dialog) {
305 ASSERT(dialog,"No Dialog given when filling actionname's dialog");
306#if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
307 dialog->queryEmpty(TOKEN, Traits.getDescription());
308#else
309#define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
310#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
311#include BOOST_PP_LOCAL_ITERATE()
312#endif
313 return dialog;
314};
315
316// =========== output as CLI ===========
317void ACTION::outputAsCLI(std::ostream &ost) const {
318 // check whether TOKEN is also an option
319 // is a bit ugly as preprocessor cannot compare strings
320 bool status = true;
321#if defined paramtokens && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
322#define BOOST_PP_LOCAL_MACRO(n) checkpresenttoken_print(~, n, TOKEN, status)
323#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
324#include BOOST_PP_LOCAL_ITERATE()
325#endif
326 if (status) {
327 ost << "--" << TOKEN;
328#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
329 ost << " ";
330#endif
331 }
332 // then print option along with each argument if set
333#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
334#define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
335#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
336#include BOOST_PP_LOCAL_ITERATE()
337#endif
338}
339
340// =========== output as PYTHON ===========
341void ACTION::outputAsPython(std::ostream &ost, const std::string &prefix) const {
342 // print prefix and action command
343 ost << prefix << "." << BOOST_PP_STRINGIZE( COMMAND ) << "(";
344 // then print option along with each argument if set
345#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
346#define BOOST_PP_LOCAL_MACRO(n) outputAsPython_print(~, n, ost)
347#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
348#include BOOST_PP_LOCAL_ITERATE()
349#endif
350 ost << ")" << std::endl;
351}
352
353// =========== time the action ===========
354// we need this here to have the correct function name
355void ACTION::startTimer() const { Chronos::getInstance().startTiming( std::string( TOKEN ) ); }
356void ACTION::endTimer() const { Chronos::getInstance().endTiming( std::string( TOKEN ) ); }
357
358// =========== command for calling action directly ===========
359void COMMAND(
360#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
361#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
362#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
363#include BOOST_PP_LOCAL_ITERATE()
364#endif
365)
366{
367 ACTION *ToCall = dynamic_cast<ACTION*>(ActionQueue::getInstance().getActionByName( TOKEN )); //->clone(params);
368 //ACTION::PARAMS params;
369#if defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
370#define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, paramreferences, ToCall->params.)
371#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
372#include BOOST_PP_LOCAL_ITERATE()
373#endif
374 ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
375};
376
377void BOOST_PP_CAT( COMMAND, _stringargs)(
378#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
379#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
380#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
381#include BOOST_PP_LOCAL_ITERATE()
382#endif
383 ) {
384 ACTION *ToCall = dynamic_cast<ACTION*>(ActionQueue::getInstance().getActionByName( TOKEN )); //->clone(params);
385 //ACTION::PARAMS params;
386#if defined paramtypes && defined paramtypes && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
387#define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, paramreferences, paramtypes, ToCall->params. )
388#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
389#include BOOST_PP_LOCAL_ITERATE()
390#endif
391 ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
392};
393
394}
395
396// free up defines
397#undef paramvalids
398#undef paramtypes
399#undef paramtokens
400#undef paramreferences
401#undef paramdescriptions
402#undef paramdefaults
403#undef MAXPARAMTYPES
404#undef MAXPARAMDEFAULTS
405#undef statetypes
406#undef statereferences
407#undef MAXSTATETYPES
408#undef PARAM_DEFAULT
409
410#undef type2string
411#undef stringtype
412#undef initialiser_print
413#undef type_print
414#undef type_list
415#undef dialog_print
416#undef sequencer
417#undef valid_print
418#undef validcopy_print
419#undef value_print
420#undef valuetype_print
421
422#undef ACTION
423#undef COMMAND
424#undef PARAMS
425#undef STATE
426#undef INSTANCE
427
428#undef ACTIONNAME
429#undef CATEGORY
430#undef TOKEN
Note: See TracBrowser for help on using the repository browser.