source: src/Actions/MapOfActions.hpp@ 0c9cc3

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 0c9cc3 was ab9a27, checked in by Frederik Heber <heber@…>, 15 years ago

MapOfActions::TypeMap now contains type_info.

We are going to use the information contained in MapOfActions to split Dialog (information gathering) from the performCall (execution) of the Action.
Therefore, Dialogs will store information as string in MapOfActions' maps and the Actions will retrieve the needed information therefrom.

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