source: src/Parameters/Specifics/Value_vector.cpp@ cb5231

ForceAnnealing_goodresults ForceAnnealing_tocheck
Last change on this file since cb5231 was a443e1, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Added specific Value and Parameter implemenations for Vector.

  • for the CommandLineParser the values are get parsed immediately (prior to executing any Action). Hence, names of geometry objects that first need to be created by an action cannot yet be present in the registry and thus the Action will fail.
  • we need to postpone the replacement of the geometry name by its stored vector components until the Parameter<>::get() call. This is possible as the value is validated only on get(), not on set(), i.e. giving illegal values is ok, only the Action will fail.
  • therefore, we override the specialize the template Value for Vector to allow storing of a string instead of a Vector and to allow putting the actual parsing of the string in front.
  • the Parameter overriding becomes necessary in order to override clone() and copy cstor(), there using string setters.
  • ContinuousValueTest now needs lib..Parameters, lib..Geometry.
  • static functions parseAsVector and setFromVector for convenience, e.g. QtUI needs to convert from string and to Vector.
  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2017 Frederik Heber. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * Value_vector.cpp
25 *
26 * Created on: Mar 29, 2017
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35//#include "CodePatterns/MemDebug.hpp"
36
37#include "Value_vector.hpp"
38
39#include <boost/lexical_cast.hpp>
40#include <boost/tokenizer.hpp>
41
42#include "Geometry/GeometryRegistry.hpp"
43#include "World.hpp"
44
45const Vector Value<Vector>::parseAsVector(const std::string &_value)
46{
47 Vector temp;
48 if (GeometryRegistry::getInstance().isPresentByName(_value)) {
49 temp = GeometryRegistry::getInstance().getByName(_value)->getVector();
50 LOG(4, "DEBUG: Using stored vector '" << _value << "' from geometry registry.");
51 } else {
52 // dissect by ","
53 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
54 boost::char_separator<char> value_separator(",)( ");
55
56 bool status = true;
57 tokenizer tokens(_value, value_separator);
58 if (!_value.empty()) {
59 tokenizer::iterator tok_iter = tokens.begin();
60 for (size_t i=0;i<NDIM;++i) {
61 if (tok_iter == tokens.end()) {
62 status = false;
63 break;
64 }
65 try {
66 temp[i] = boost::lexical_cast<double>(*(tok_iter++));
67 } catch (boost::bad_lexical_cast &e) {
68 throw ParameterValueException();
69 }
70 }
71// if (tok_iter != tokens.end())
72// ELOG(2, "There are still components left: " << *tok_iter << "?");
73 }
74 if (!status)
75 temp.Zero();
76 }
77 return temp;
78}
79
80const std::string Value<Vector>::setFromVector(const Vector &_vec)
81{
82 std::stringstream output;
83 for (size_t i=0;i<NDIM;++i) {
84 if (i!=0)
85 output << ",";
86 output << _vec;
87 }
88 return output.str();
89}
90
91/** Here come the modified functions.
92 *
93 */
94
95const Vector & Value<Vector>::get() const throw(ParameterValueException)
96{
97 converted_value = parseAsVector(value);
98 if (!ValueSet) throw ParameterValueException();
99 if (!isValid(converted_value)) throw ParameterValueException();
100 return converted_value;
101}
102
103const Vector & Value<Vector>::getUnvalidated() const throw(ParameterValueException)
104{
105 converted_value = parseAsVector(value);
106 if (!ValueSet) throw ParameterValueException();
107 return converted_value;
108}
109
110/** Here are the original functions that we simply copied.
111 *
112 */
113
114Value<Vector>::Value() :
115 ValueSet(false),
116 validator(new DummyValidator<Vector>)
117{}
118
119Value<Vector>::Value(const Validator<Vector> &_validator) :
120 ValueSet(false),
121 validator(_validator.clone())
122{}
123
124Value<Vector>::Value(const std::vector<Vector> &_ValidValues) :
125 ValueSet(false),
126 validator(NULL)
127{
128 validator = new DiscreteValidator<Vector>(_ValidValues);
129}
130
131Value<Vector>::Value(const range<Vector> &_ValidRange) :
132 ValueSet(false),
133 validator(NULL)
134{
135 validator = new RangeValidator<Vector>(_ValidRange);
136}
137
138Value<Vector>::~Value()
139{
140 ASSERT(validator,
141 "Value<Vector>::~Value() - validator missing.");
142 delete(validator);
143}
144
145bool Value<Vector>::isValid(const Vector & _value) const throw(ParameterValidatorException)
146{
147 if (validator == NULL) throw ParameterValidatorException();
148 return (*validator)(_value);
149}
150
151
152bool Value<Vector>::operator==(const Value<Vector> &_instance) const throw(ParameterValidatorException)
153{
154 if (validator == NULL) throw ParameterValidatorException();
155 if (_instance.validator == NULL) throw ParameterValidatorException();
156 bool status = true;
157 status = status && (*validator == *_instance.validator);
158 status = status && (ValueSet == _instance.ValueSet);
159 if (ValueSet && _instance.ValueSet)
160 status = status && (value == _instance.value);
161 return status;
162}
163
164void Value<Vector>::set(const Vector & _value) throw(ParameterException)
165{
166 // any value may be set, this allows Actions to have invalid parameters
167 // (e.g. because the given atom id does not yet exist) that are checked
168 // on performCall()
169// if (!isValid(_value)) throw ParameterValueException();
170 if (!ValueSet)
171 ValueSet = true;
172 value = setFromVector(_value);
173}
174
175
176bool Value<Vector>::isSet() const
177{
178 return ValueSet;
179}
180
181
182bool Value<Vector>::isValidAsString(const std::string &_value) const throw(ParameterValidatorException)
183{
184 const Vector castvalue = parseAsVector(_value);
185// LOG(0, "Converted value reads " << castvalue <<".");
186 return isValid(castvalue);
187}
188
189
190const std::string Value<Vector>::getAsString() const throw(ParameterValueException)
191{
192 return value;
193}
194
195
196void Value<Vector>::setAsString(const std::string &_value) throw(ParameterException)
197{
198 if (!ValueSet)
199 ValueSet = true;
200 value = _value;
201// LOG(0, "STATUS: Value is now set to " << value << ".");
202}
203
204
205const Validator<Vector> &Value<Vector>::getValidator() const
206{
207 if (validator == NULL) throw ParameterValidatorException();
208 return *validator;
209}
210
211
212Validator<Vector> &Value<Vector>::getValidator()
213{
214 if (validator == NULL) throw ParameterValidatorException();
215 return *validator;
216}
217
218
219const range<Vector> & Value<Vector>::getValidRange() const throw(ParameterValidatorException)
220{
221 return dynamic_cast<const RangeValidator<Vector>&>(getValidator()).getValidRange();
222}
223
224
225void Value<Vector>::setValidRange(const range<Vector> &_range) throw(ParameterValueException)
226{
227 dynamic_cast<RangeValidator<Vector>&>(getValidator()).setValidRange(_range);
228 if (ValueSet) {
229 //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
230 if (!isValid(converted_value)){
231 //std::cout << "ValueSet to false." << std::endl;
232 ValueSet = false;
233 // have full check again in assert such that it appears in output, too
234 throw ParameterValueException() << ParameterValidValues(toString(_range));
235 }
236 }
237 // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
238}
239
240void Value<Vector>::appendValidValue(const Vector &_value) throw(ParameterValidatorException)
241{
242 dynamic_cast<DiscreteValidator<Vector>&>(getValidator()).appendValidValue(_value);
243}
244
245const std::vector<Vector> &Value<Vector>::getValidValues() const throw(ParameterValidatorException)
246{
247 return dynamic_cast<const DiscreteValidator<Vector>&>(getValidator()).getValidValues();
248}
249
Note: See TracBrowser for help on using the repository browser.