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