| 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 |
|
|---|
| 26 | class MapOfActionsTest;
|
|---|
| 27 |
|
|---|
| 28 | class Box;
|
|---|
| 29 | class atom;
|
|---|
| 30 | class element;
|
|---|
| 31 | class molecule;
|
|---|
| 32 | class Vector;
|
|---|
| 33 |
|
|---|
| 34 | namespace po = boost::program_options;
|
|---|
| 35 |
|
|---|
| 36 | using 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 | */
|
|---|
| 136 | class MapOfActions : public Singleton<MapOfActions> {
|
|---|
| 137 | friend class Singleton<MapOfActions>;
|
|---|
| 138 | friend class MapOfActionsTest;
|
|---|
| 139 | public:
|
|---|
| 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 | // instantiates and puts all known actions into the ActionRegistry
|
|---|
| 167 | void populateActions();
|
|---|
| 168 |
|
|---|
| 169 | bool isCurrentValuePresent(const char *name) const;
|
|---|
| 170 | void queryCurrentValue(const char * name, class atom * &_T);
|
|---|
| 171 | void queryCurrentValue(const char * name, const element * &_T);
|
|---|
| 172 | void queryCurrentValue(const char * name, class molecule * &_T);
|
|---|
| 173 | void queryCurrentValue(const char * name, class Box &_T);
|
|---|
| 174 | void queryCurrentValue(const char * name, class Vector &_T);
|
|---|
| 175 | void queryCurrentValue(const char * name, class BoxVector &_T);
|
|---|
| 176 | void queryCurrentValue(const char * name, std::vector<atom *>&_T);
|
|---|
| 177 | void queryCurrentValue(const char * name, std::vector<const element *>&_T);
|
|---|
| 178 | void queryCurrentValue(const char * name, std::vector<molecule *>&_T);
|
|---|
| 179 | void queryCurrentValue(const char * name, boost::filesystem::path&_T);
|
|---|
| 180 |
|
|---|
| 181 | template<typename T> void queryCurrentValue(const char * name, T &_T)
|
|---|
| 182 | {
|
|---|
| 183 | if (typeid( T ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
|
|---|
| 184 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
|---|
| 185 | throw MissingValueException(__FILE__, __LINE__);
|
|---|
| 186 | _T = lexical_cast<T>(CurrentValueMap[name].c_str());
|
|---|
| 187 | CurrentValueMap.erase(name);
|
|---|
| 188 | } else
|
|---|
| 189 | throw IllegalTypeException(__FILE__,__LINE__);
|
|---|
| 190 | }
|
|---|
| 191 | template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T)
|
|---|
| 192 | {
|
|---|
| 193 | T temp;
|
|---|
| 194 | if (typeid( std::vector<T> ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
|
|---|
| 195 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
|---|
| 196 | throw MissingValueException(__FILE__, __LINE__);
|
|---|
| 197 | std::istringstream stream(CurrentValueMap[name]);
|
|---|
| 198 | CurrentValueMap.erase(name);
|
|---|
| 199 | while (!stream.fail()) {
|
|---|
| 200 | stream >> temp >> std::ws;
|
|---|
| 201 | _T.push_back(temp);
|
|---|
| 202 | }
|
|---|
| 203 | } else
|
|---|
| 204 | throw IllegalTypeException(__FILE__,__LINE__);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | void setCurrentValue(const char * name, class atom * &_T);
|
|---|
| 208 | void setCurrentValue(const char * name, const element * &_T);
|
|---|
| 209 | void setCurrentValue(const char * name, class molecule * &_T);
|
|---|
| 210 | void setCurrentValue(const char * name, class Box &_T);
|
|---|
| 211 | void setCurrentValue(const char * name, class Vector &_T);
|
|---|
| 212 | void setCurrentValue(const char * name, class BoxVector &_T);
|
|---|
| 213 | void setCurrentValue(const char * name, std::vector<atom *>&_T);
|
|---|
| 214 | void setCurrentValue(const char * name, std::vector<const element *>&_T);
|
|---|
| 215 | void setCurrentValue(const char * name, std::vector<molecule *>&_T);
|
|---|
| 216 | void setCurrentValue(const char * name, boost::filesystem::path&_T);
|
|---|
| 217 |
|
|---|
| 218 | template<class T> void setCurrentValue(const char * name, T &_T)
|
|---|
| 219 | {
|
|---|
| 220 | std::ostringstream stream;
|
|---|
| 221 | if (typeid( T ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
|
|---|
| 222 | stream << _T;
|
|---|
| 223 | CurrentValueMap[name] = stream.str();
|
|---|
| 224 | } else
|
|---|
| 225 | throw IllegalTypeException(__FILE__,__LINE__);
|
|---|
| 226 | }
|
|---|
| 227 | template<class T> void setCurrentValue(const char * name, std::vector<T> &_T)
|
|---|
| 228 | {
|
|---|
| 229 | std::ostringstream stream;
|
|---|
| 230 | if (typeid( std::vector<T> ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
|
|---|
| 231 | std::ostringstream stream;
|
|---|
| 232 | for (typename std::vector<T>::iterator iter = _T.begin(); iter != _T.end(); ++iter) {
|
|---|
| 233 | stream << (*iter) << " ";
|
|---|
| 234 | }
|
|---|
| 235 | CurrentValueMap[name] = stream.str();
|
|---|
| 236 | } else
|
|---|
| 237 | throw IllegalTypeException(__FILE__,__LINE__);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | private:
|
|---|
| 242 | // private constructor and destructor
|
|---|
| 243 | MapOfActions();
|
|---|
| 244 | virtual ~MapOfActions();
|
|---|
| 245 |
|
|---|
| 246 | // lookup list from our configs to the ones of CommandLineParser
|
|---|
| 247 | std::map< std::set<std::string> *, po::options_description *> CmdParserLookup;
|
|---|
| 248 |
|
|---|
| 249 | // map of the action names and their description
|
|---|
| 250 | std::map<std::string, std::string> CurrentValueMap;
|
|---|
| 251 | std::map<std::string, std::string> DescriptionMap;
|
|---|
| 252 | std::map<std::string, std::string> ShortFormMap;
|
|---|
| 253 | std::map<std::string, const std::type_info * > TypeMap;
|
|---|
| 254 | std::map<const std::type_info *, enum OptionTypes > TypeEnumMap;
|
|---|
| 255 | };
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 | #endif /* MAPOFACTIONS_HPP_ */
|
|---|