source: src/Actions/Reaction_impl_pre.hpp@ 27d0bc

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

Added Action::outputAsPython() and ActionQueue::outputAsPython().

  • these work in exactly the same way as outputAsCLI().
  • Property mode set to 100644
File size: 9.9 KB
Line 
1/*
2 * Reaction_impl.hpp
3 *
4 * Created on: Oct 13, 2011
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 <boost/preprocessor/cat.hpp>
34#include <boost/preprocessor/comparison/equal.hpp>
35#include <boost/preprocessor/comparison/not_equal.hpp>
36#include <boost/preprocessor/debug/assert.hpp>
37#include <boost/preprocessor/iteration/local.hpp>
38#include <boost/preprocessor/punctuation/comma_if.hpp>
39#include <boost/preprocessor/seq/elem.hpp>
40#include <boost/preprocessor/seq/seq.hpp>
41#include <boost/preprocessor/seq/size.hpp>
42#include <boost/preprocessor/seq/transform.hpp>
43
44#include "Actions/ActionQueue.hpp"
45#include "Actions/toCLIString.hpp"
46
47// some derived names: if CATEGORY is not given, we don't prefix with it
48#ifdef CATEGORY
49#define REACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
50#define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
51#define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
52#define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
53#else
54#define REACTION BOOST_PP_CAT(ACTIONNAME, Action)
55#define COMMAND ACTIONNAME
56#define STATE BOOST_PP_CAT(ACTIONNAME, State)
57#define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
58#endif
59
60// check if no lists given
61#ifndef paramtypes
62#define MAXPARAMTYPES 0
63#else
64#define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
65#endif
66#ifndef statetypes
67#define MAXSTATETYPES 0
68#else
69#define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
70#endif
71
72// check user has given name and category
73#ifndef ACTIONNAME
74ERROR: No "ACTIONNAME" defined in: __FILE__
75#endif
76
77// calculate numbers and check whether all have same size
78#ifdef paramtokens
79BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
80 ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
81)
82#endif
83#ifdef paramreferences
84BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
85 ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
86)
87#endif
88
89#ifdef statetypes
90BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
91 ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
92)
93#endif
94
95// set the return type
96#ifndef returntype
97BOOST_PP_ASSERT_MSG(0,\
98 ERROR: No returntype is defined in __FILE__ \
99)
100#endif
101#define RETURNTYPE returntype
102
103// print a list of type ref followed by a separator, i.e. "int i;"
104#define initialiser_print(z,n,initialiserlist) \
105 BOOST_PP_SEQ_ELEM(n, initialiserlist) \
106 (BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(n, initialiserlist))),
107
108// print a list of ref(_ref) followed by a separator, i.e. "id(_id),"
109#define type_print(z,n,TYPELIST, VARLIST, separator) \
110 BOOST_PP_SEQ_ELEM(n, TYPELIST) \
111 BOOST_PP_SEQ_ELEM(n, VARLIST)\
112 separator
113
114// print a list of type ref followed, i.e. "int i, double position"
115#define type_list(z,n,TYPELIST,VARLIST) \
116 BOOST_PP_COMMA_IF(n)\
117 BOOST_PP_SEQ_ELEM(n, TYPELIST) \
118 BOOST_PP_SEQ_ELEM(n, VARLIST)
119
120// prints dialog->query calls for paramtypes with tokens
121#define dialog_print(z,n,unused) \
122 dialog->query<\
123 BOOST_PP_SEQ_ELEM(n, paramtypes)\
124 >(\
125 BOOST_PP_SEQ_ELEM(n, paramtokens)\
126 , Traits.getDescription()\
127 );
128
129// prints command line call for this Action for paramtypes with tokens
130#define outputAsCLI_print(z,n,output) \
131 output << \
132 BOOST_PP_IF(n, " --", "--") \
133 << \
134 BOOST_PP_SEQ_ELEM(n, paramtokens) \
135 << " \"" << toCLIString(params. \
136 BOOST_PP_SEQ_ELEM(n, paramreferences) \
137 .get()) \
138 << "\"";
139
140// prints if statement to check two strings (paramtokens[n] vs. TOKEN)
141#define checkpresenttoken_print(z, n, TOKEN, booltoken) \
142 if ( std::string(\
143 BOOST_PP_SEQ_ELEM(n, paramtokens) )\
144 == getName()) \
145 booltoken = false;
146
147// prints command line call for this Action for paramtypes with tokens
148#define outputAsPython_print(z,n,output) \
149 output << \
150 BOOST_PP_IF(n, ", ", "") \
151 << "\"" << toCLIString(params. \
152 BOOST_PP_SEQ_ELEM(n, paramreferences) \
153 .get()) \
154 << "\"";
155
156// prints set/queryCurrentValue (command) for paramreferences and paramtokens
157#define value_print(z, n, container, prefix) \
158 prefix \
159 BOOST_PP_SEQ_ELEM(n, container)\
160 .set(\
161 BOOST_PP_SEQ_ELEM(n, container)\
162 );
163
164// prints set/queryCurrentValue (command) for paramreferences and paramtokens
165#define valuetype_print(z,n,container, types, prefix) \
166 prefix \
167 BOOST_PP_SEQ_ELEM(n, container) \
168 .setAsString( \
169 BOOST_PP_SEQ_ELEM(n, container) \
170 );
171
172#define stringtype std::string
173
174#define type2string(s, data, elem) \
175 stringtype
176
177
178#include "Actions/ActionRegistry.hpp"
179//#include "Actions/ActionTraits.hpp"
180#include "UIElements/Dialog.hpp"
181
182#ifdef paramtokens
183#define statenecessary 1
184#endif
185#ifndef statetokens
186#define statenecessary 1
187#endif
188
189namespace MoleCuilder {
190
191// =========== constructor ===========
192REACTION::REACTION () :
193 Reaction< RETURNTYPE >(ActionTraits< REACTION >())
194{}
195
196// =========== destructor ===========
197REACTION::~REACTION ()
198{
199 //std::cout << "Action REACTION is being destroyed." << std::endl;
200}
201
202// =========== fill a dialog ===========
203Dialog* REACTION::fillDialog(Dialog *dialog) {
204 ASSERT(dialog,"No Dialog given when filling actionname's dialog");
205#if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
206 dialog->queryEmpty(TOKEN, Traits.getDescription());
207#else
208#define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
209#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
210#include BOOST_PP_LOCAL_ITERATE()
211#endif
212 return dialog;
213};
214
215// =========== output as CLI ===========
216void REACTION::outputAsCLI(std::ostream &ost) const {
217 // check whether TOKEN is also an option
218 // is a bit ugly as preprocessor cannot compare strings
219 bool status = true;
220#if defined paramtokens && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
221#define BOOST_PP_LOCAL_MACRO(n) checkpresenttoken_print(~, n, TOKEN, status)
222#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
223#include BOOST_PP_LOCAL_ITERATE()
224#endif
225 if (status) { ost << "--" << TOKEN; }
226 // then print option along with each argument if set
227#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
228#define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
229#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
230#include BOOST_PP_LOCAL_ITERATE()
231#endif
232}
233
234// =========== output as PYTHTON ===========
235void REACTION::outputAsPython(std::ostream &ost, const std::string &prefix) const {
236 // print prefix and action command
237 ost << prefix << "." << BOOST_PP_STRINGIZE( COMMAND ) << "(";
238 // then print option along with each argument if set
239#if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
240#define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
241#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
242#include BOOST_PP_LOCAL_ITERATE()
243#endif
244 ost << ")" << std::endl;
245}
246
247// =========== command for calling action directly ===========
248RETURNTYPE COMMAND(
249#if defined paramtypes && defined paramreferences
250#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
251#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
252#include BOOST_PP_LOCAL_ITERATE()
253#endif
254)
255{
256 Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
257 //REACTION::PARAMS params;
258#if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
259#define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, setCurrentValue, )
260#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
261#include BOOST_PP_LOCAL_ITERATE()
262#endif
263 ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
264 Reaction< RETURNTYPE > * resultaction = static_cast<Reaction< RETURNTYPE > *>(ToCall);
265 ASSERT( resultaction != NULL,
266 " COMMAND - static cast to Reaction type failed.");
267 ASSERT( resultaction->hasResult (),
268 " _COMMAND - result is not done yet?");
269 return resultaction->getResult();
270};
271
272RETURNTYPE BOOST_PP_CAT( COMMAND, _stringargs)(
273#if defined paramtypes && defined paramreferences
274#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
275#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
276#include BOOST_PP_LOCAL_ITERATE()
277#endif
278 ) {
279 Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
280 //REACTION::PARAMS params;
281#if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
282#define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, setCurrentValueByString, )
283#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
284#include BOOST_PP_LOCAL_ITERATE()
285#endif
286 ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
287 Reaction< RETURNTYPE > * resultaction = static_cast<Reaction< RETURNTYPE > *>(ToCall);
288 ASSERT( resultaction != NULL,
289 " COMMAND - static cast to Reaction type failed.");
290 ASSERT( resultaction->hasResult (),
291 " _COMMAND - result is not done yet?");
292 return resultaction->getResult();
293};
294
295}
296
297// free up defines
298#undef paramtypes
299#undef paramtokens
300#undef paramreferences
301#undef MAXPARAMTYPES
302
303#undef returntype
304#undef RETURNTYPE
305
306#undef type2string
307#undef stringtype
308#undef initialiser_print
309#undef type_print
310#undef type_list
311#undef dialog_print
312#undef value_print
313#undef valuetype_print
314
315#undef REACTION
316#undef COMMAND
317#undef PARAMS
318#undef STATE
319
320#undef ACTIONNAME
321#undef CATEGORY
322#undef TOKEN
Note: See TracBrowser for help on using the repository browser.