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

ForceAnnealing_oldresults IndependentFragmentGrids_IntegrationTest
Last change on this file since ef8667 was 2eded3e, 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.
  • TESTS: Marked dryrun store-session again as XFAIL, removed XFAIL from load- session python.
  • Property mode set to 100644
File size: 7.1 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 converted_value = parseAsVector(value);
193 if (!ValueSet) throw ParameterValueException();
194 if (!isValid(converted_value)) throw ParameterValueException();
195 return value;
196}
197
198const std::string Value<Vector>::getAsStringUnvalidated() const throw(ParameterValueException)
199{
200 if (!ValueSet) throw ParameterValueException();
201 return value;
202}
203
204void Value<Vector>::setAsString(const std::string &_value) throw(ParameterException)
205{
206 if (!ValueSet)
207 ValueSet = true;
208 value = _value;
209// LOG(0, "STATUS: Value is now set to " << value << ".");
210}
211
212
213const Validator<Vector> &Value<Vector>::getValidator() const
214{
215 if (validator == NULL) throw ParameterValidatorException();
216 return *validator;
217}
218
219
220Validator<Vector> &Value<Vector>::getValidator()
221{
222 if (validator == NULL) throw ParameterValidatorException();
223 return *validator;
224}
225
226
227const range<Vector> & Value<Vector>::getValidRange() const throw(ParameterValidatorException)
228{
229 return dynamic_cast<const RangeValidator<Vector>&>(getValidator()).getValidRange();
230}
231
232
233void Value<Vector>::setValidRange(const range<Vector> &_range) throw(ParameterValueException)
234{
235 dynamic_cast<RangeValidator<Vector>&>(getValidator()).setValidRange(_range);
236 if (ValueSet) {
237 //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
238 if (!isValid(converted_value)){
239 //std::cout << "ValueSet to false." << std::endl;
240 ValueSet = false;
241 // have full check again in assert such that it appears in output, too
242 throw ParameterValueException() << ParameterValidValues(toString(_range));
243 }
244 }
245 // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
246}
247
248void Value<Vector>::appendValidValue(const Vector &_value) throw(ParameterValidatorException)
249{
250 dynamic_cast<DiscreteValidator<Vector>&>(getValidator()).appendValidValue(_value);
251}
252
253const std::vector<Vector> &Value<Vector>::getValidValues() const throw(ParameterValidatorException)
254{
255 return dynamic_cast<const DiscreteValidator<Vector>&>(getValidator()).getValidValues();
256}
257
Note: See TracBrowser for help on using the repository browser.