source: src/Actions/MapOfActions.hpp@ db7cb0

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

Added boost::filesystem usage for input files.

  • parameters for Action input now have type boost::filesystem.
  • rewritten Actions to use boost::filesystems
  • in the progress of writing FileQtQuery to present a file dialog.
  • Property mode set to 100644
File size: 10.1 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#include "Exceptions/IllegalTypeException.hpp"
21#include "Exceptions/MissingValueException.hpp"
22#include "Helpers/Assert.hpp"
23#include "Patterns/Singleton.hpp"
24
25class MapOfActionsTest;
26
27class Box;
28class atom;
29class element;
30class molecule;
31class Vector;
32
33namespace po = boost::program_options;
34namespace fs = boost::filesystem;
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, 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 std::string getDescription(std::string actionname);
144 std::string getKeyAndShortForm(std::string actionname);
145 std::string getShortForm(std::string actionname);
146 std::map <std::string, std::string> getShortFormToActionMap();
147
148 void AddOptionsToParser();
149
150 // check presence and getter for action type
151 bool hasValue(std::string actionname);
152 bool isShortFormPresent(std::string shortform);
153 std::string getValueType(std::string actionname);
154
155 std::set<std::string> generic;
156 std::set<std::string> config;
157 std::set<std::string> hidden;
158 std::set<std::string> visible;
159 std::set<std::string> inputfile;
160
161 std::multimap <std::string, std::string> MenuContainsActionMap;
162 std::map <std::string, std::pair<std::string,std::string> > MenuDescription;
163
164 // instantiates and puts all known actions into the ActionRegistry
165 void populateActions();
166
167 bool isCurrentValuePresent(const char *name) const;
168 void queryCurrentValue(const char * name, class atom * &_T);
169 void queryCurrentValue(const char * name, const element * &_T);
170 void queryCurrentValue(const char * name, class molecule * &_T);
171 void queryCurrentValue(const char * name, class Box &_T);
172 void queryCurrentValue(const char * name, class Vector &_T);
173 void queryCurrentValue(const char * name, class BoxVector &_T);
174 void queryCurrentValue(const char * name, std::vector<atom *>&_T);
175 void queryCurrentValue(const char * name, std::vector<const element *>&_T);
176 void queryCurrentValue(const char * name, std::vector<molecule *>&_T);
177 void queryCurrentValue(const char * name, fs::path&_T);
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 (CurrentValue.find(name) == CurrentValue.end())
182 throw MissingValueException(__FILE__, __LINE__);
183 _T = lexical_cast<T>(CurrentValue[name].c_str());
184 CurrentValue.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 (CurrentValue.find(name) == CurrentValue.end())
193 throw MissingValueException(__FILE__, __LINE__);
194 std::istringstream stream(CurrentValue[name]);
195 CurrentValue.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, fs::path&_T);
214 template<class T> void setCurrentValue(const char * name, T &_T)
215 {
216 std::ostringstream stream;
217 if (typeid( T ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
218 stream << _T;
219 CurrentValue[name] = stream.str();
220 } else
221 throw IllegalTypeException(__FILE__,__LINE__);
222 }
223 template<class T> void setCurrentValue(const char * name, std::vector<T> &_T)
224 {
225 std::ostringstream stream;
226 if (typeid( std::vector<T> ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
227 std::ostringstream stream;
228 for (typename std::vector<T>::iterator iter = _T.begin(); iter != _T.end(); ++iter) {
229 stream << (*iter) << " ";
230 }
231 CurrentValue[name] = stream.str();
232 } else
233 throw IllegalTypeException(__FILE__,__LINE__);
234 }
235
236
237private:
238 // private constructor and destructor
239 MapOfActions();
240 virtual ~MapOfActions();
241
242 // lookup list from our configs to the ones of CommandLineParser
243 std::map< std::set<std::string> *, po::options_description *> CmdParserLookup;
244
245 // map of the action names and their description
246 std::map<std::string, std::string> CurrentValue;
247 std::map<std::string, std::string> DescriptionMap;
248 std::map<std::string, std::string> ShortFormMap;
249 std::map<std::string, const std::type_info * > TypeMap;
250 std::map<const std::type_info *, enum OptionTypes > TypeEnumMap;
251};
252
253
254#endif /* MAPOFACTIONS_HPP_ */
Note: See TracBrowser for help on using the repository browser.