source: src/Actions/MapOfActions.hpp@ 7cd469

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

Using ostringstream to convert all templated values in MapOfActions::queryCurrentValue().

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