source: src/Actions/MapOfActions.hpp@ 23359f

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 23359f was 2a6a2c, checked in by Frederik Heber <heber@…>, 14 years ago

Removed MapOfActions::populateActions().

  • Action prototypes are now instantiated within the object file, as they register automatically with the ActionRegistry and can henceforth be used.
    • new define INSTANCE with a sort of unique name.
    • instance is const and hence not to be used directly.
    • MapOfActions::populateActions() removed in CommandLineWindow, QtMainWindow and TextWindow classes.
    • NOTE: this hopefully remedies the problem of forgetting to add new Actions.
  • Registry does not cleanup() in its destructor, but function may be called from derived class if desired.
    • All Action prototypes are destroyed at end of program and hence cleanup() would cause memory corruption.
  • Property mode set to 100644
File size: 10.2 KB
Line 
1/*
2 * MapOfActions.hpp
3 *
4 * Created on: 10.05.2010
5 * Author: heber
6 */
7
8#ifndef MAPOFACTIONS_HPP_
9#define MAPOFACTIONS_HPP_
10
11#include <boost/filesystem.hpp>
12#include <boost/lexical_cast.hpp>
13#include <boost/program_options.hpp>
14
15#include <map>
16#include <set>
17#include <vector>
18#include <typeinfo>
19
20
21#include "Exceptions/IllegalTypeException.hpp"
22#include "Exceptions/MissingValueException.hpp"
23#include "Helpers/Assert.hpp"
24#include "Patterns/Singleton.hpp"
25
26class MapOfActionsTest;
27
28class Box;
29class atom;
30class element;
31class molecule;
32class Vector;
33
34namespace po = boost::program_options;
35
36using boost::lexical_cast;
37
38/** Central class for adding functionality to the code.
39 *
40 * In Molecuilder everything that can be done - such as adding atoms,
41 * translating molecules, saving bind information - is an Action.
42 *
43 * In order to reference Action's with what the user sees, this class is the
44 * mediator.
45 *
46 * An Action is described to the user by:
47 * -# a name (this is the most important information)
48 * -# a description
49 * -# a shortform (single letter for use on the command line)
50 * -# a text menu it resides in
51 * -# the type of its argument
52 * -# the command line category
53 *
54 * The Action::NAME is the most important information because every Action
55 * registers itself automatically with the ActionRegistry and can be retrieved
56 * therefrom and from this MapOfActions simply by knowing its name alone.
57 *
58 * In the constructor of MapOfActions all this is set.
59 *
60 * Note that Action will require input from the user. This is done via class
61 * Query.
62 *
63 * And note also that MapOfActions actually contains more than just all
64 * Actions: There are a number of names that actually are just arguments to
65 * actions (e.g. "output-file").
66 *
67 * <h1> Howto add an Action</h1>
68 *
69 * Let us assume your new action (class) is called SuperDuperAction, consisting
70 * of two files SuperDuperAction.cpp and SuperDuperAction.hpp.
71 *
72 * Furthermore, let's say you Action needs two values: a double value as a
73 * upper threshold and a string which is the name of the output file.
74 *
75 * <h2> Command Line preliminaries </h2>
76 *
77 * You have to decide whether (for the command line) it makes sense to have an
78 * extra argument requesting the arguments, or one should be the argument of
79 * your action. I.e. your action name is "super-duper", then the use may
80 * call your action like this:
81 *
82 * ./molecuilder --super-duper 4 --output-file test.dat
83 *
84 * Here, we have picked the threshold as the value for your action and the
85 * name of the output file is given by an additional argument. Of course,
86 * it can be the other way round or by two arguments such as here:
87 *
88 * ./molecuilder --super-duper --threshold 4 --output-file test.dat
89 *
90 * It depends on what possible arguments are already there (don't make up
91 * new ones if present ones actually make sense for your action) and which
92 * argument is more natural or closer to what your action does.
93 *
94 * <h2> Menu preliminaries </h2>
95 *
96 * Whatever you decide, your action will need some Query dialogs to request
97 * the necessary information from the user, either via a command line
98 * argument (--output-file) via a text dialog (referenced by "output-file")
99 * or via a graphical dialog (same reference). And therein, the names
100 * of the arguments have to re-appear.
101 *
102 * Then, the following steps have to be done to incorporate your Action:
103 * -# create a unique name for your action (no capital letters) to reference
104 * it, this name has to appear in the file SuperDuperAction.cpp, e.g.
105 * "super-duper"
106 * -# pick names the other required arguments, best if they are already
107 * present in the MapOfActions. They have to appear in Query's in the
108 * code of your Action.
109 * -# With this name create entries in the following maps for the action
110 * name and for each names of a desired addtional argument if not present:
111 * -# DescriptionMap, a catchy description of what your action does
112 * -# TypeMap, see MapOfActions::OptionTypes for possible types of the single
113 * argument it takes.
114 * -# MenuContainsActionMap, in which menu should your action appear
115 * -# ShortFormMap (optional), single letter for command line call
116 * -# DefaultValueMap (optional), the default value (always a string)
117 * -# add to one of the command line sets by the following categories
118 * -# generic - generic options (i.e. not one of the others)
119 * -# config - action/argument only considers internal bevahior, user
120 * does not have to see it while still having full functionality
121 * -# hidden - this should be hidden from the user
122 * -# visible - this should be visible to the user
123 * -# inputfile - this should only be parsed from an input file, not
124 * from command line
125 * -# add to a menu, i.e. make an entry in MenuContainsActionMap.
126 * -# add header file SuperDuperAction.hpp to MapOfActions.cpp and instantiate
127 * your action in populateMenu() (mind the sorting: 1. menu,
128 * 2. alphabetical)
129 *
130 * And that's.
131 *
132 * Now, your action can be called from the command line, within the text
133 * menu and the graphical user interface.
134 *
135 */
136class MapOfActions : public Singleton<MapOfActions> {
137 friend class Singleton<MapOfActions>;
138 friend class MapOfActionsTest;
139public:
140 enum OptionTypes { None, Boolean, File, Integer, ListOfIntegers, Double, ListOfDoubles, String, ListOfStrings, Vector, ListOfVectors, Box, Molecule, ListOfMolecules, Atom, ListOfAtoms, Element, ListOfElements };
141
142 // getter for the action descriptions and short forms
143 const std::string getDescription(std::string actionname);
144 const std::string getKeyAndShortForm(std::string actionname);
145 const std::string getShortForm(std::string actionname);
146 std::map <std::string, std::string> getShortFormToActionMap();
147 const std::string getCurrentValue(std::string actionname);
148
149 void AddOptionsToParser();
150
151 // check presence and getter for action type
152 bool hasValue(std::string actionname);
153 bool isShortFormPresent(std::string shortform);
154 const std::string getValueType(std::string actionname);
155 const std::type_info * getType(std::string actionname);
156
157 std::set<std::string> generic;
158 std::set<std::string> config;
159 std::set<std::string> hidden;
160 std::set<std::string> visible;
161 std::set<std::string> inputfile;
162
163 std::multimap <std::string, std::string> MenuContainsActionMap;
164 std::map <std::string, std::pair<std::string,std::string> > MenuDescription;
165
166 bool isCurrentValuePresent(const char *name) const;
167 void queryCurrentValue(const char * name, class atom * &_T);
168 void queryCurrentValue(const char * name, const element * &_T);
169 void queryCurrentValue(const char * name, class molecule * &_T);
170 void queryCurrentValue(const char * name, class Box &_T);
171 void queryCurrentValue(const char * name, class Vector &_T);
172 void queryCurrentValue(const char * name, class BoxVector &_T);
173 void queryCurrentValue(const char * name, std::vector<atom *>&_T);
174 void queryCurrentValue(const char * name, std::vector<const element *>&_T);
175 void queryCurrentValue(const char * name, std::vector<molecule *>&_T);
176 void queryCurrentValue(const char * name, boost::filesystem::path&_T);
177
178 template<typename T> void queryCurrentValue(const char * name, T &_T)
179 {
180 if (typeid( T ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
181 if (CurrentValueMap.find(name) == CurrentValueMap.end())
182 throw MissingValueException(__FILE__, __LINE__);
183 _T = lexical_cast<T>(CurrentValueMap[name].c_str());
184 CurrentValueMap.erase(name);
185 } else
186 throw IllegalTypeException(__FILE__,__LINE__);
187 }
188 template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T)
189 {
190 T temp;
191 if (typeid( std::vector<T> ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
192 if (CurrentValueMap.find(name) == CurrentValueMap.end())
193 throw MissingValueException(__FILE__, __LINE__);
194 std::istringstream stream(CurrentValueMap[name]);
195 CurrentValueMap.erase(name);
196 while (!stream.fail()) {
197 stream >> temp >> std::ws;
198 _T.push_back(temp);
199 }
200 } else
201 throw IllegalTypeException(__FILE__,__LINE__);
202 }
203
204 void setCurrentValue(const char * name, class atom * &_T);
205 void setCurrentValue(const char * name, const element * &_T);
206 void setCurrentValue(const char * name, class molecule * &_T);
207 void setCurrentValue(const char * name, class Box &_T);
208 void setCurrentValue(const char * name, class Vector &_T);
209 void setCurrentValue(const char * name, class BoxVector &_T);
210 void setCurrentValue(const char * name, std::vector<atom *>&_T);
211 void setCurrentValue(const char * name, std::vector<const element *>&_T);
212 void setCurrentValue(const char * name, std::vector<molecule *>&_T);
213 void setCurrentValue(const char * name, boost::filesystem::path&_T);
214
215 template<class T> void setCurrentValue(const char * name, T &_T)
216 {
217 std::ostringstream stream;
218 if (typeid( T ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
219 stream << _T;
220 CurrentValueMap[name] = stream.str();
221 } else
222 throw IllegalTypeException(__FILE__,__LINE__);
223 }
224 template<class T> void setCurrentValue(const char * name, std::vector<T> &_T)
225 {
226 std::ostringstream stream;
227 if (typeid( std::vector<T> ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
228 std::ostringstream stream;
229 for (typename std::vector<T>::iterator iter = _T.begin(); iter != _T.end(); ++iter) {
230 stream << (*iter) << " ";
231 }
232 CurrentValueMap[name] = stream.str();
233 } else
234 throw IllegalTypeException(__FILE__,__LINE__);
235 }
236
237
238private:
239 // private constructor and destructor
240 MapOfActions();
241 virtual ~MapOfActions();
242
243 // lookup list from our configs to the ones of CommandLineParser
244 std::map< std::set<std::string> *, po::options_description *> CmdParserLookup;
245
246 // map of the action names and their description
247 std::map<std::string, std::string> CurrentValueMap;
248 std::map<std::string, std::string> DescriptionMap;
249 std::map<std::string, std::string> ShortFormMap;
250 std::map<std::string, const std::type_info * > TypeMap;
251 std::map<const std::type_info *, enum OptionTypes > TypeEnumMap;
252};
253
254
255#endif /* MAPOFACTIONS_HPP_ */
Note: See TracBrowser for help on using the repository browser.