| [2eded3e] | 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 |  | 
|---|
|  | 45 | const 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 |  | 
|---|
|  | 80 | const 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 |  | 
|---|
|  | 95 | const 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 |  | 
|---|
|  | 103 | const 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 |  | 
|---|
|  | 114 | Value<Vector>::Value() : | 
|---|
|  | 115 | ValueSet(false), | 
|---|
|  | 116 | validator(new DummyValidator<Vector>) | 
|---|
|  | 117 | {} | 
|---|
|  | 118 |  | 
|---|
|  | 119 | Value<Vector>::Value(const Validator<Vector> &_validator) : | 
|---|
|  | 120 | ValueSet(false), | 
|---|
|  | 121 | validator(_validator.clone()) | 
|---|
|  | 122 | {} | 
|---|
|  | 123 |  | 
|---|
|  | 124 | Value<Vector>::Value(const std::vector<Vector> &_ValidValues) : | 
|---|
|  | 125 | ValueSet(false), | 
|---|
|  | 126 | validator(NULL) | 
|---|
|  | 127 | { | 
|---|
|  | 128 | validator = new DiscreteValidator<Vector>(_ValidValues); | 
|---|
|  | 129 | } | 
|---|
|  | 130 |  | 
|---|
|  | 131 | Value<Vector>::Value(const range<Vector> &_ValidRange) : | 
|---|
|  | 132 | ValueSet(false), | 
|---|
|  | 133 | validator(NULL) | 
|---|
|  | 134 | { | 
|---|
|  | 135 | validator = new RangeValidator<Vector>(_ValidRange); | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
|  | 138 | Value<Vector>::~Value() | 
|---|
|  | 139 | { | 
|---|
|  | 140 | ASSERT(validator, | 
|---|
|  | 141 | "Value<Vector>::~Value() - validator missing."); | 
|---|
|  | 142 | delete(validator); | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
|  | 145 | bool Value<Vector>::isValid(const Vector & _value) const throw(ParameterValidatorException) | 
|---|
|  | 146 | { | 
|---|
|  | 147 | if (validator == NULL) throw ParameterValidatorException(); | 
|---|
|  | 148 | return (*validator)(_value); | 
|---|
|  | 149 | } | 
|---|
|  | 150 |  | 
|---|
|  | 151 |  | 
|---|
|  | 152 | bool 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 |  | 
|---|
|  | 164 | void 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 |  | 
|---|
|  | 176 | bool Value<Vector>::isSet() const | 
|---|
|  | 177 | { | 
|---|
|  | 178 | return ValueSet; | 
|---|
|  | 179 | } | 
|---|
|  | 180 |  | 
|---|
|  | 181 |  | 
|---|
|  | 182 | bool 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 |  | 
|---|
|  | 190 | const 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 |  | 
|---|
|  | 198 | const std::string Value<Vector>::getAsStringUnvalidated() const throw(ParameterValueException) | 
|---|
|  | 199 | { | 
|---|
|  | 200 | if (!ValueSet) throw ParameterValueException(); | 
|---|
|  | 201 | return value; | 
|---|
|  | 202 | } | 
|---|
|  | 203 |  | 
|---|
|  | 204 | void 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 |  | 
|---|
|  | 213 | const Validator<Vector> &Value<Vector>::getValidator() const | 
|---|
|  | 214 | { | 
|---|
|  | 215 | if (validator == NULL) throw ParameterValidatorException(); | 
|---|
|  | 216 | return *validator; | 
|---|
|  | 217 | } | 
|---|
|  | 218 |  | 
|---|
|  | 219 |  | 
|---|
|  | 220 | Validator<Vector> &Value<Vector>::getValidator() | 
|---|
|  | 221 | { | 
|---|
|  | 222 | if (validator == NULL) throw ParameterValidatorException(); | 
|---|
|  | 223 | return *validator; | 
|---|
|  | 224 | } | 
|---|
|  | 225 |  | 
|---|
|  | 226 |  | 
|---|
|  | 227 | const range<Vector> & Value<Vector>::getValidRange() const throw(ParameterValidatorException) | 
|---|
|  | 228 | { | 
|---|
|  | 229 | return dynamic_cast<const RangeValidator<Vector>&>(getValidator()).getValidRange(); | 
|---|
|  | 230 | } | 
|---|
|  | 231 |  | 
|---|
|  | 232 |  | 
|---|
|  | 233 | void 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 |  | 
|---|
|  | 248 | void Value<Vector>::appendValidValue(const Vector &_value) throw(ParameterValidatorException) | 
|---|
|  | 249 | { | 
|---|
|  | 250 | dynamic_cast<DiscreteValidator<Vector>&>(getValidator()).appendValidValue(_value); | 
|---|
|  | 251 | } | 
|---|
|  | 252 |  | 
|---|
|  | 253 | const std::vector<Vector> &Value<Vector>::getValidValues() const throw(ParameterValidatorException) | 
|---|
|  | 254 | { | 
|---|
|  | 255 | return dynamic_cast<const DiscreteValidator<Vector>&>(getValidator()).getValidValues(); | 
|---|
|  | 256 | } | 
|---|
|  | 257 |  | 
|---|