source: src/Actions/Action_impl_pre.hpp@ 9b56f34

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 Candidate_v1.7.0 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 9b56f34 was 9b56f34, checked in by Frederik Heber <heber@…>, 12 years ago

ActionParameters need special format when given to CLI.

  • introduced toCLIString which converts some parameters types to the required format, e.g. Vector as "x,y,z", ...
  • Property mode set to 100644
File size: 12.8 KB
RevLine 
[0b2ce9]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
[b4fa106]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
[0b2ce9]24 * -# CATEGORY, e.g. Analysis
25 * -# ACTIONNAME, e.g. MolecularVolume
26 */
27
[56f73b]28// include config.h
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
[9ee38b]32
[862b6a]33#include "CodePatterns/Chronos.hpp"
34
[9ee38b]35#include <boost/preprocessor/cat.hpp>
[b4fa106]36#include <boost/preprocessor/expand.hpp>
[9ee38b]37#include <boost/preprocessor/comparison/equal.hpp>
38#include <boost/preprocessor/comparison/not_equal.hpp>
[6ba9ba]39#include <boost/preprocessor/control/expr_if.hpp>
[9ee38b]40#include <boost/preprocessor/control/if.hpp>
41#include <boost/preprocessor/debug/assert.hpp>
[46b181]42#include <boost/preprocessor/facilities/empty.hpp>
[9ee38b]43#include <boost/preprocessor/iteration/local.hpp>
[6ba9ba]44#include <boost/preprocessor/list/adt.hpp>
[9ee38b]45#include <boost/preprocessor/punctuation/comma_if.hpp>
46#include <boost/preprocessor/repetition/repeat.hpp>
47#include <boost/preprocessor/seq/elem.hpp>
[46b181]48#include <boost/preprocessor/seq/filter.hpp>
[9ee38b]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
[628577]54#include "Actions/ActionQueue.hpp"
[9b56f34]55#include "Actions/toCLIString.hpp"
[f10b0c]56#include "Parameters/Parameter.hpp"
57
[649aaa]58
[e4afb4]59// some derived names: if CATEGORY is not given, we don't prefix with it
60#ifdef CATEGORY
[9ee38b]61#define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
62#define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
63#define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
64#define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
[e4afb4]65#else
66#define ACTION BOOST_PP_CAT(ACTIONNAME, Action)
67#define COMMAND ACTIONNAME
68#define STATE BOOST_PP_CAT(ACTIONNAME, State)
69#define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
70#endif
[2a6a2c]71#define INSTANCE BOOST_PP_CAT(this_, BOOST_PP_CAT(ACTIONNAME, _instance))
[9ee38b]72
73// check if no lists given
[b4fa106]74#ifndef paramtypes
75#define MAXPARAMTYPES 0
[9ee38b]76#else
[b4fa106]77#define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
78#endif
79#ifndef statetypes
80#define MAXSTATETYPES 0
81#else
82#define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
[9ee38b]83#endif
[6ba9ba]84#ifndef paramdefaults
85#define MAXPARAMDEFAULTS 0
86// this is required for valid_print "else part"
87#define sequencer(z,n,data) \
88 BOOST_PP_SEQ_PUSH_BACK( data, NOPARAM_DEFAULT)
89#define paramdefaults BOOST_PP_REPEAT( MAXPARAMTYPES, sequencer, BOOST_PP_SEQ_NIL )
90#else
91#define MAXPARAMDEFAULTS BOOST_PP_SEQ_SIZE(paramdefaults)
92#endif
93#define PARAM_DEFAULT(x) \
94 (x, BOOST_PP_NIL)
[9ee38b]95
96// check user has given name and category
97#ifndef ACTIONNAME
98ERROR: No "ACTIONNAME" defined in: __FILE__
99#endif
100
101// calculate numbers and check whether all have same size
[b4fa106]102#ifdef paramtokens
103BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
104 ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
[9ee38b]105)
106#endif
[b4fa106]107#ifdef paramreferences
108BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
109 ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
110)
111#endif
112
113#ifdef statetypes
114BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
115 ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
[9ee38b]116)
117#endif
118
119// print a list of type ref followed by a separator, i.e. "int i;"
[b4fa106]120#define initialiser_print(z,n,initialiserlist) \
121 BOOST_PP_SEQ_ELEM(n, initialiserlist) \
122 (BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(n, initialiserlist))),
123
124// print a list of ref(_ref) followed by a separator, i.e. "id(_id),"
125#define type_print(z,n,TYPELIST, VARLIST, separator) \
126 BOOST_PP_SEQ_ELEM(n, TYPELIST) \
127 BOOST_PP_SEQ_ELEM(n, VARLIST)\
[9ee38b]128 separator
129
130// print a list of type ref followed, i.e. "int i, double position"
[b4fa106]131#define type_list(z,n,TYPELIST,VARLIST) \
[9ee38b]132 BOOST_PP_COMMA_IF(n)\
[b4fa106]133 BOOST_PP_SEQ_ELEM(n, TYPELIST) \
134 BOOST_PP_SEQ_ELEM(n, VARLIST)
[9ee38b]135
[b4fa106]136// prints dialog->query calls for paramtypes with tokens
[9ee38b]137#define dialog_print(z,n,unused) \
138 dialog->query<\
[b4fa106]139 BOOST_PP_SEQ_ELEM(n, paramtypes)\
[f10b0c]140 >( params. \
141 BOOST_PP_SEQ_ELEM(n, paramreferences)\
142 ,\
[b4fa106]143 BOOST_PP_SEQ_ELEM(n, paramtokens)\
[e4afb4]144 , Traits.getDescription()\
145 );
[9ee38b]146
[46b181]147// prints command line call for this Action for paramtypes with tokens
148#define outputAsCLI_print(z,n,output) \
149 output << \
150 BOOST_PP_IF(n, BOOST_PP_STRINGIZE( ) , BOOST_PP_STRINGIZE() ) \
151 << "--" << \
152 BOOST_PP_SEQ_ELEM(n, paramtokens) \
[9b56f34]153 << " \"" << toCLIString(params. \
154 BOOST_PP_SEQ_ELEM(n, paramreferences) \
155 .get()) \
156 << "\"";
[46b181]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
[6ba9ba]165// print an initialiser list, i.e. "var( token, valid (,default) )(,)"
166#define valid_print(z,n,TOKENLIST, VARLIST, VALIDLIST, DEFAULTLIST) \
[649aaa]167 BOOST_PP_COMMA_IF(n) \
168 BOOST_PP_SEQ_ELEM(n, VARLIST) \
169 ( \
170 BOOST_PP_SEQ_ELEM(n, TOKENLIST) \
171 , \
172 BOOST_PP_SEQ_ELEM(n, VALIDLIST) \
[6ba9ba]173 BOOST_PP_COMMA_IF( BOOST_PP_NOT( BOOST_PP_LIST_IS_NIL( BOOST_PP_SEQ_ELEM(n, DEFAULTLIST) ) ) ) \
174 BOOST_PP_EXPR_IF( \
175 BOOST_PP_NOT( BOOST_PP_LIST_IS_NIL( BOOST_PP_SEQ_ELEM(n, DEFAULTLIST) ) ), \
176 BOOST_PP_LIST_FIRST( BOOST_PP_SEQ_ELEM(n, DEFAULTLIST) )) \
[649aaa]177 )
178
[6ba9ba]179// print an initialiser list, i.e. "var( valid . var )(,)"
[649aaa]180#define validcopy_print(z,n,TOKENLIST, VARLIST, VALID) \
181 BOOST_PP_COMMA_IF(n) \
182 BOOST_PP_SEQ_ELEM(n, VARLIST) \
183 ( \
184 VALID . \
185 BOOST_PP_SEQ_ELEM(n, VARLIST) \
186 )
187
[9ee38b]188// prints set/queryCurrentValue (command) for paramreferences and paramtokens
[f10b0c]189#define value_print(z, n, container, prefix) \
190 prefix \
191 BOOST_PP_SEQ_ELEM(n, container)\
192 .set(\
193 BOOST_PP_SEQ_ELEM(n, container)\
[9ee38b]194 );
195
[88ba1f]196// prints set/queryCurrentValue (command) for paramreferences and paramtokens
[f10b0c]197#define valuetype_print(z,n,container, types, prefix) \
198 prefix \
199 BOOST_PP_SEQ_ELEM(n, container) \
200 .setAsString( \
201 BOOST_PP_SEQ_ELEM(n, container) \
[88ba1f]202 );
203
204#define stringtype std::string
205
206#define type2string(s, data, elem) \
207 stringtype
208
209
[3139b2]210//#include "Actions/ActionTraits.hpp"
[0b2ce9]211#include "UIElements/Dialog.hpp"
212
[e4afb4]213#ifdef paramtokens
214#define statenecessary 1
215#endif
216#ifndef statetokens
217#define statenecessary 1
218#endif
[9ee38b]219
[ce7fdc]220namespace MoleCuilder {
221
[b4fa106]222// =========== memento to remember the state when undoing ===========
[e4afb4]223#ifdef statenecessary
[b4fa106]224class STATE : public ActionState {
225public:
226 STATE(
227#if defined statetypes && defined statereferences // if we have parameters, we have to add "_" before each reference and add the params as the last one
228#define OP(s,data,elem) BOOST_PP_CAT(data, elem) // OP to add "_"
229#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)))
230#else /// if not, params is only list
231#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, (const ACTION::PARAMS &), (_params))
232#endif
233#define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES)
234#include BOOST_PP_LOCAL_ITERATE()
235) :
236#if defined statetypes && defined statereferences // do we have parameters at all?
237BOOST_PP_REPEAT(MAXSTATETYPES, initialiser_print, statereferences)
238#endif
239params(_params)
240 {}
[0b2ce9]241
[b4fa106]242#if defined statetypes && defined statereferences // do we have parameters at all?
243#define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, statetypes, statereferences, ;)
244#define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES-1)
245#include BOOST_PP_LOCAL_ITERATE()
246#endif
247 ACTION::PARAMS params;
248};
[e4afb4]249#endif /* statenecessary */
[0b2ce9]250
[2a6a2c]251// (const) prototype to be placed into the ActionRegistry (must be deleted by registry itself)
[83e90c]252//const ACTION INSTANCE;
253//boost::shared_ptr< ACTION > INSTANCE( new ACTION() );
[2a6a2c]254
[b4fa106]255// =========== constructor ===========
[0b2ce9]256ACTION::ACTION () :
[126867]257 Action(ActionTraits< ACTION >())
[0b2ce9]258{}
259
[b4fa106]260// =========== destructor ===========
[0b2ce9]261ACTION::~ACTION ()
[e4afb4]262{
263 //std::cout << "Action ACTION is being destroyed." << std::endl;
264}
[0b2ce9]265
[649aaa]266// =========== parameter constructor ===========
267ACTION::PARAMS::PARAMS()
268#if defined paramtokens && defined paramreferences && defined paramvalids
269 :
[6ba9ba]270#define BOOST_PP_LOCAL_MACRO(n) valid_print(~, n, paramtokens, paramreferences, paramvalids, paramdefaults)
[649aaa]271#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
272#include BOOST_PP_LOCAL_ITERATE()
273#endif
274 {}
275
276ACTION::PARAMS::PARAMS(const PARAMS &p)
277#if defined paramtokens && defined paramreferences
278 :
279#define BOOST_PP_LOCAL_MACRO(n) validcopy_print(~, n, paramtokens, paramreferences, p)
280#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
281#include BOOST_PP_LOCAL_ITERATE()
282#endif
283 {}
284
[b4fa106]285// =========== fill a dialog ===========
[0b2ce9]286Dialog* ACTION::fillDialog(Dialog *dialog) {
287 ASSERT(dialog,"No Dialog given when filling actionname's dialog");
[b4fa106]288#if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
[e4afb4]289 dialog->queryEmpty(TOKEN, Traits.getDescription());
[0b2ce9]290#else
291#define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
[b4fa106]292#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
[0b2ce9]293#include BOOST_PP_LOCAL_ITERATE()
294#endif
295 return dialog;
296};
297
[46b181]298// =========== output as CLI ===========
299void ACTION::outputAsCLI(std::ostream &ost) const {
300 // check whether TOKEN is also an option
301 // is a bit ugly as preprocessor cannot compare strings
302 bool status = true;
303#if defined paramtokens && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
304#define BOOST_PP_LOCAL_MACRO(n) checkpresenttoken_print(~, n, TOKEN, status)
305#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
306#include BOOST_PP_LOCAL_ITERATE()
307#endif
308 if (status) { ost << "--" << TOKEN; }
309 // then print option along with each argument if set
310#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
311#define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
312#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
313#include BOOST_PP_LOCAL_ITERATE()
314#endif
315}
316
[862b6a]317// =========== time the action ===========
318// we need this here to have the correct function name
319void ACTION::startTimer() const { Chronos::getInstance().startTiming( std::string( TOKEN ) ); }
320void ACTION::endTimer() const { Chronos::getInstance().endTiming( std::string( TOKEN ) ); }
321
[b4fa106]322// =========== command for calling action directly ===========
323void COMMAND(
[f10b0c]324#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
[b4fa106]325#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
326#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
327#include BOOST_PP_LOCAL_ITERATE()
328#endif
[05736a]329)
[0b2ce9]330{
[1d3563]331 ACTION *ToCall = dynamic_cast<ACTION*>(ActionQueue::getInstance().getActionByName( TOKEN )); //->clone(params);
[ddde10]332 //ACTION::PARAMS params;
[f10b0c]333#if defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
334#define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, paramreferences, ToCall->params.)
[b4fa106]335#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
[0b2ce9]336#include BOOST_PP_LOCAL_ITERATE()
337#endif
[f54cda]338 ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
[0b2ce9]339};
340
[88ba1f]341void BOOST_PP_CAT( COMMAND, _stringargs)(
[f10b0c]342#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
[88ba1f]343#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
344#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
345#include BOOST_PP_LOCAL_ITERATE()
346#endif
347 ) {
[1d3563]348 ACTION *ToCall = dynamic_cast<ACTION*>(ActionQueue::getInstance().getActionByName( TOKEN )); //->clone(params);
[88ba1f]349 //ACTION::PARAMS params;
[f10b0c]350#if defined paramtypes && defined paramtypes && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
351#define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, paramreferences, paramtypes, ToCall->params. )
[88ba1f]352#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
353#include BOOST_PP_LOCAL_ITERATE()
354#endif
[f54cda]355 ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
[88ba1f]356};
357
[ce7fdc]358}
359
[b4fa106]360// free up defines
[649aaa]361#undef paramvalids
[b4fa106]362#undef paramtypes
363#undef paramtokens
364#undef paramreferences
[649aaa]365#undef paramdescriptions
366#undef paramdefaults
[b4fa106]367#undef MAXPARAMTYPES
[6ba9ba]368#undef MAXPARAMDEFAULTS
[b4fa106]369#undef statetypes
370#undef statereferences
371#undef MAXSTATETYPES
[6ba9ba]372#undef PARAM_DEFAULT
[0b2ce9]373
[88ba1f]374#undef type2string
375#undef stringtype
[a02f78]376#undef initialiser_print
377#undef type_print
378#undef type_list
379#undef dialog_print
[6ba9ba]380#undef sequencer
[649aaa]381#undef valid_print
382#undef validcopy_print
[a02f78]383#undef value_print
[88ba1f]384#undef valuetype_print
[a02f78]385
[9ee38b]386#undef ACTION
387#undef COMMAND
388#undef PARAMS
389#undef STATE
[2a6a2c]390#undef INSTANCE
[b4fa106]391
392#undef ACTIONNAME
393#undef CATEGORY
[9ee38b]394#undef TOKEN
Note: See TracBrowser for help on using the repository browser.