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