| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [75dc28] | 8 | /* | 
|---|
|  | 9 | * ValueStorage.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Jul 22, 2010 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [bbbad5] | 20 | #include "Helpers/MemDebug.hpp" | 
|---|
| [75dc28] | 21 |  | 
|---|
| [4c5aaf] | 22 | #include "Patterns/Singleton_impl.hpp" | 
|---|
| [9d33ba] | 23 |  | 
|---|
| [e4afb4] | 24 | #include "Actions/OptionTrait.hpp" | 
|---|
|  | 25 | #include "Actions/OptionRegistry.hpp" | 
|---|
|  | 26 | #include "Actions/Values.hpp" | 
|---|
|  | 27 | #include "Actions/ValueStorage.hpp" | 
|---|
|  | 28 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
|  | 29 | #include "Descriptors/MoleculeIdDescriptor.hpp" | 
|---|
|  | 30 | #include "Helpers/Log.hpp" | 
|---|
|  | 31 | #include "Helpers/Verbose.hpp" | 
|---|
|  | 32 | #include "LinearAlgebra/BoxVector.hpp" | 
|---|
| [cca9ef] | 33 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| [e4afb4] | 34 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 35 | #include "atom.hpp" | 
|---|
|  | 36 | #include "Box.hpp" | 
|---|
|  | 37 | #include "element.hpp" | 
|---|
|  | 38 | #include "molecule.hpp" | 
|---|
|  | 39 | #include "periodentafel.hpp" | 
|---|
|  | 40 | #include "World.hpp" | 
|---|
|  | 41 |  | 
|---|
| [03c902] | 42 | ValueStorage::ValueStorage() : | 
|---|
| [e4afb4] | 43 | OptionRegistry_instance(OptionRegistry::getInstance()) | 
|---|
| [03c902] | 44 | {}; | 
|---|
| [75dc28] | 45 |  | 
|---|
|  | 46 | ValueStorage::~ValueStorage() {}; | 
|---|
|  | 47 |  | 
|---|
| [e4afb4] | 48 |  | 
|---|
|  | 49 | bool ValueStorage::isCurrentValuePresent(const char *name) const | 
|---|
|  | 50 | { | 
|---|
|  | 51 | return (CurrentValueMap.find(name) != CurrentValueMap.end()); | 
|---|
|  | 52 | } | 
|---|
|  | 53 |  | 
|---|
|  | 54 | void ValueStorage::queryCurrentValue(const char * name, const atom * &_T) | 
|---|
|  | 55 | { | 
|---|
|  | 56 | int atomID = -1; | 
|---|
|  | 57 | if (typeid( atom *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 58 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 59 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 60 | atomID = lexical_cast<int>(CurrentValueMap[name].c_str()); | 
|---|
|  | 61 | CurrentValueMap.erase(name); | 
|---|
|  | 62 | } else if (typeid( const atom *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 63 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 64 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 65 | atomID = lexical_cast<int>(CurrentValueMap[name].c_str()); | 
|---|
|  | 66 | CurrentValueMap.erase(name); | 
|---|
|  | 67 | } else | 
|---|
|  | 68 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 69 | _T = World::getInstance().getAtom(AtomById(atomID)); | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
|  | 72 | void ValueStorage::queryCurrentValue(const char * name, const element * &_T)  { | 
|---|
|  | 73 | int Z = -1; | 
|---|
|  | 74 | if (typeid(const element *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 75 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 76 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 77 | Z = lexical_cast<int>(CurrentValueMap[name].c_str()); | 
|---|
|  | 78 | CurrentValueMap.erase(name); | 
|---|
|  | 79 | } else | 
|---|
|  | 80 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 81 | _T = World::getInstance().getPeriode()->FindElement(Z); | 
|---|
|  | 82 | } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | void ValueStorage::queryCurrentValue(const char * name, const molecule * &_T) { | 
|---|
|  | 85 | int molID = -1; | 
|---|
|  | 86 | if (typeid( const molecule *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 87 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 88 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 89 | molID = lexical_cast<int>(CurrentValueMap[name].c_str()); | 
|---|
|  | 90 | CurrentValueMap.erase(name); | 
|---|
|  | 91 | } else | 
|---|
|  | 92 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 93 | _T = World::getInstance().getMolecule(MoleculeById(molID)); | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
|  | 96 | void ValueStorage::queryCurrentValue(const char * name, class Box &_T) { | 
|---|
| [cca9ef] | 97 | RealSpaceMatrix M; | 
|---|
| [e4afb4] | 98 | double tmp; | 
|---|
|  | 99 | if (typeid( Box ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 100 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 101 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 102 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
|  | 103 | stream >> tmp; | 
|---|
|  | 104 | M.set(0,0,tmp); | 
|---|
|  | 105 | stream >> tmp; | 
|---|
|  | 106 | M.set(0,1,tmp); | 
|---|
|  | 107 | M.set(1,0,tmp); | 
|---|
|  | 108 | stream >> tmp; | 
|---|
|  | 109 | M.set(0,2,tmp); | 
|---|
|  | 110 | M.set(2,0,tmp); | 
|---|
|  | 111 | stream >> tmp; | 
|---|
|  | 112 | M.set(1,1,tmp); | 
|---|
|  | 113 | stream >> tmp; | 
|---|
|  | 114 | M.set(1,2,tmp); | 
|---|
|  | 115 | M.set(2,1,tmp); | 
|---|
|  | 116 | stream >> tmp; | 
|---|
|  | 117 | M.set(2,2,tmp); | 
|---|
|  | 118 | _T = M; | 
|---|
|  | 119 | CurrentValueMap.erase(name); | 
|---|
|  | 120 | } else | 
|---|
|  | 121 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
|  | 124 | void ValueStorage::queryCurrentValue(const char * name, class Vector &_T) { | 
|---|
|  | 125 | if (typeid( Vector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 126 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
|  | 127 | CurrentValueMap.erase(name); | 
|---|
|  | 128 | stream >> _T[0]; | 
|---|
|  | 129 | stream >> _T[1]; | 
|---|
|  | 130 | stream >> _T[2]; | 
|---|
|  | 131 | } else | 
|---|
|  | 132 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 133 | } | 
|---|
|  | 134 |  | 
|---|
|  | 135 | void ValueStorage::queryCurrentValue(const char * name, class BoxVector &_T) { | 
|---|
|  | 136 | if (typeid( BoxVector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 137 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
|  | 138 | CurrentValueMap.erase(name); | 
|---|
|  | 139 | stream >> _T[0]; | 
|---|
|  | 140 | stream >> _T[1]; | 
|---|
|  | 141 | stream >> _T[2]; | 
|---|
|  | 142 | } else | 
|---|
|  | 143 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|
|  | 146 | void ValueStorage::queryCurrentValue(const char * name, std::vector<const atom *>&_T) | 
|---|
|  | 147 | { | 
|---|
|  | 148 | int atomID = -1; | 
|---|
|  | 149 | atom *Walker = NULL; | 
|---|
|  | 150 | if (typeid( std::vector<const atom *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 151 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 152 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 153 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
|  | 154 | CurrentValueMap.erase(name); | 
|---|
|  | 155 | while (!stream.fail()) { | 
|---|
|  | 156 | stream >> atomID >> ws; | 
|---|
|  | 157 | Walker = World::getInstance().getAtom(AtomById(atomID)); | 
|---|
|  | 158 | if (Walker != NULL) | 
|---|
|  | 159 | _T.push_back(Walker); | 
|---|
|  | 160 | atomID = -1; | 
|---|
|  | 161 | Walker = NULL; | 
|---|
|  | 162 | } | 
|---|
|  | 163 | } else | 
|---|
|  | 164 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 165 | } | 
|---|
|  | 166 |  | 
|---|
|  | 167 | void ValueStorage::queryCurrentValue(const char * name, std::vector<const element *>&_T) | 
|---|
|  | 168 | { | 
|---|
|  | 169 | int Z = -1; | 
|---|
|  | 170 | const element *elemental = NULL; | 
|---|
|  | 171 | if (typeid( std::vector<const element *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 172 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 173 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 174 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
|  | 175 | CurrentValueMap.erase(name); | 
|---|
|  | 176 | while (!stream.fail()) { | 
|---|
|  | 177 | stream >> Z >> ws; | 
|---|
|  | 178 | elemental = World::getInstance().getPeriode()->FindElement(Z); | 
|---|
|  | 179 | if (elemental != NULL) | 
|---|
|  | 180 | _T.push_back(elemental); | 
|---|
|  | 181 | Z = -1; | 
|---|
|  | 182 | } | 
|---|
|  | 183 | } else | 
|---|
|  | 184 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
| [4885f85] | 185 | } | 
|---|
|  | 186 |  | 
|---|
| [e4afb4] | 187 | void ValueStorage::queryCurrentValue(const char * name, std::vector<const molecule *>&_T) | 
|---|
|  | 188 | { | 
|---|
|  | 189 | int molID = -1; | 
|---|
|  | 190 | molecule *mol = NULL; | 
|---|
|  | 191 | if (typeid( std::vector<const molecule *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 192 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 193 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 194 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
|  | 195 | CurrentValueMap.erase(name); | 
|---|
|  | 196 | while (!stream.fail()) { | 
|---|
|  | 197 | stream >> molID >> ws; | 
|---|
|  | 198 | mol = World::getInstance().getMolecule(MoleculeById(molID)); | 
|---|
|  | 199 | if (mol != NULL) | 
|---|
|  | 200 | _T.push_back(mol); | 
|---|
|  | 201 | molID = -1; | 
|---|
|  | 202 | mol = NULL; | 
|---|
|  | 203 | } | 
|---|
|  | 204 | } else | 
|---|
|  | 205 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
| [9d33ba] | 206 | } | 
|---|
|  | 207 |  | 
|---|
| [e4afb4] | 208 | void ValueStorage::queryCurrentValue(const char * name, boost::filesystem::path&_T) | 
|---|
|  | 209 | { | 
|---|
|  | 210 | std::string tmp; | 
|---|
|  | 211 | if (typeid( boost::filesystem::path ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 212 | if (CurrentValueMap.find(name) == CurrentValueMap.end()) | 
|---|
|  | 213 | throw MissingValueException(__FILE__, __LINE__); | 
|---|
|  | 214 | std::istringstream stream(CurrentValueMap[name]); | 
|---|
|  | 215 | CurrentValueMap.erase(name); | 
|---|
|  | 216 | if (!stream.fail()) { | 
|---|
|  | 217 | stream >> tmp >> ws; | 
|---|
|  | 218 | _T = tmp; | 
|---|
|  | 219 | } | 
|---|
|  | 220 | } else | 
|---|
|  | 221 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
| [4885f85] | 222 | } | 
|---|
|  | 223 |  | 
|---|
| [e4afb4] | 224 | void ValueStorage::setCurrentValue(const char * name, const atom * &_T) | 
|---|
|  | 225 | { | 
|---|
|  | 226 | if (typeid( const atom *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 227 | std::ostringstream stream; | 
|---|
|  | 228 | stream << _T->getId(); | 
|---|
|  | 229 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 230 | } else | 
|---|
|  | 231 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 232 | } | 
|---|
|  | 233 |  | 
|---|
|  | 234 | void ValueStorage::setCurrentValue(const char * name, const element * &_T) | 
|---|
|  | 235 | { | 
|---|
|  | 236 | if (typeid( const element *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 237 | std::ostringstream stream; | 
|---|
|  | 238 | stream << _T->getAtomicNumber(); | 
|---|
|  | 239 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 240 | } else | 
|---|
|  | 241 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 242 | } | 
|---|
|  | 243 |  | 
|---|
|  | 244 | void ValueStorage::setCurrentValue(const char * name, const molecule * &_T) | 
|---|
|  | 245 | { | 
|---|
|  | 246 | if (typeid( const molecule *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 247 | std::ostringstream stream; | 
|---|
|  | 248 | stream << _T->getId(); | 
|---|
|  | 249 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 250 | } else | 
|---|
|  | 251 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 252 | } | 
|---|
|  | 253 |  | 
|---|
|  | 254 | void ValueStorage::setCurrentValue(const char * name, class Box &_T) | 
|---|
|  | 255 | { | 
|---|
| [cca9ef] | 256 | const RealSpaceMatrix &M = _T.getM(); | 
|---|
| [e4afb4] | 257 | if (typeid( Box ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 258 | std::ostringstream stream; | 
|---|
|  | 259 | stream << M.at(0,0) << " "; | 
|---|
|  | 260 | stream << M.at(0,1) << " "; | 
|---|
|  | 261 | stream << M.at(0,2) << " "; | 
|---|
|  | 262 | stream << M.at(1,1) << " "; | 
|---|
|  | 263 | stream << M.at(1,2) << " "; | 
|---|
|  | 264 | stream << M.at(2,2) << " "; | 
|---|
|  | 265 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 266 | } else | 
|---|
|  | 267 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 268 | } | 
|---|
|  | 269 |  | 
|---|
|  | 270 | void ValueStorage::setCurrentValue(const char * name, class Vector &_T) | 
|---|
|  | 271 | { | 
|---|
|  | 272 | if (typeid( Vector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())){ | 
|---|
|  | 273 | std::ostringstream stream; | 
|---|
|  | 274 | stream << _T[0] << " "; | 
|---|
|  | 275 | stream << _T[1] << " "; | 
|---|
|  | 276 | stream << _T[2] << " "; | 
|---|
|  | 277 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 278 | } else  if (typeid( BoxVector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())){ | 
|---|
|  | 279 | std::ostringstream stream; | 
|---|
|  | 280 | stream << _T[0] << " "; | 
|---|
|  | 281 | stream << _T[1] << " "; | 
|---|
|  | 282 | stream << _T[2] << " "; | 
|---|
|  | 283 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 284 | } else | 
|---|
|  | 285 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 286 | } | 
|---|
|  | 287 |  | 
|---|
|  | 288 | void ValueStorage::setCurrentValue(const char * name, std::vector<const atom *>&_T) | 
|---|
|  | 289 | { | 
|---|
|  | 290 | if (typeid( std::vector<const atom *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 291 | std::ostringstream stream; | 
|---|
|  | 292 | for (std::vector<const atom *>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) { | 
|---|
|  | 293 | stream << (*iter)->getId() << " "; | 
|---|
|  | 294 | } | 
|---|
|  | 295 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 296 | } else | 
|---|
|  | 297 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 298 | } | 
|---|
|  | 299 |  | 
|---|
|  | 300 | void ValueStorage::setCurrentValue(const char * name, std::vector<const element *>&_T) | 
|---|
|  | 301 | { | 
|---|
|  | 302 | if (typeid( std::vector<const element *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 303 | std::ostringstream stream; | 
|---|
|  | 304 | for (std::vector<const element *>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) { | 
|---|
|  | 305 | stream << (*iter)->getAtomicNumber() << " "; | 
|---|
|  | 306 | } | 
|---|
|  | 307 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 308 | } else | 
|---|
|  | 309 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 310 | } | 
|---|
|  | 311 |  | 
|---|
|  | 312 | void ValueStorage::setCurrentValue(const char * name, std::vector<const molecule *>&_T) | 
|---|
|  | 313 | { | 
|---|
|  | 314 | if (typeid( std::vector<const molecule *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 315 | std::ostringstream stream; | 
|---|
|  | 316 | for (std::vector<const molecule *>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) { | 
|---|
|  | 317 | stream << (*iter)->getId() << " "; | 
|---|
|  | 318 | } | 
|---|
|  | 319 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 320 | } else | 
|---|
|  | 321 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 322 | } | 
|---|
|  | 323 |  | 
|---|
|  | 324 | void ValueStorage::setCurrentValue(const char * name, boost::filesystem::path &_T) | 
|---|
|  | 325 | { | 
|---|
|  | 326 | if (typeid( boost::filesystem::path ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { | 
|---|
|  | 327 | std::ostringstream stream; | 
|---|
|  | 328 | stream << _T.string(); | 
|---|
|  | 329 | CurrentValueMap[name] = stream.str(); | 
|---|
|  | 330 | } else | 
|---|
|  | 331 | throw IllegalTypeException(__FILE__,__LINE__); | 
|---|
|  | 332 | } | 
|---|
|  | 333 |  | 
|---|
|  | 334 | const std::string ValueStorage::getCurrentValue(std::string actionname) { | 
|---|
|  | 335 | return CurrentValueMap[actionname]; | 
|---|
| [4885f85] | 336 | } | 
|---|
|  | 337 |  | 
|---|
| [75dc28] | 338 | CONSTRUCT_SINGLETON(ValueStorage) | 
|---|