1 |
tim |
667 |
/* |
2 |
|
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
tim |
667 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
tim |
667 |
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
gezelter |
1390 |
* |
32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
|
|
* research, please cite the appropriate papers when you publish your |
34 |
|
|
* work. Good starting points are: |
35 |
|
|
* |
36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
gezelter |
1850 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
gezelter |
1665 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
tim |
667 |
*/ |
42 |
|
|
|
43 |
tim |
672 |
#ifndef IO_PARAMCONSTRAINT_HPP |
44 |
|
|
#define IO_PARAMCONSTRAINT_HPP |
45 |
|
|
#include <sstream> |
46 |
tim |
673 |
#include "utils/CaseConversion.hpp" |
47 |
tim |
677 |
#include "utils/StringTokenizer.hpp" |
48 |
gezelter |
1390 |
namespace OpenMD { |
49 |
skuang |
1341 |
/** |
50 |
|
|
* This class allows to recognize constraint predicates, so that they can be combined using |
51 |
|
|
* composition operators. Every constraint predicate must be derived from this class |
52 |
|
|
*/ |
53 |
|
|
template<typename Derived> |
54 |
|
|
struct ParamConstraintFacade { |
55 |
tim |
672 |
std::string getConstraintDescription() {return description_;} |
56 |
skuang |
1341 |
protected: |
57 |
|
|
std::string description_; |
58 |
|
|
}; |
59 |
tim |
672 |
|
60 |
skuang |
1341 |
struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint>{ |
61 |
|
|
|
62 |
tim |
672 |
NotEmptyConstraint() { description_= "nonempty";} |
63 |
gezelter |
1874 |
bool operator()( const std::string &data ) const { |
64 |
skuang |
1341 |
return !data.empty(); |
65 |
tim |
672 |
} |
66 |
skuang |
1341 |
}; |
67 |
tim |
672 |
|
68 |
skuang |
1341 |
struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint>{ |
69 |
tim |
672 |
|
70 |
tim |
677 |
ZeroConstraint() {this->description_ = "zero";} |
71 |
tim |
672 |
template<typename DataType> |
72 |
|
|
bool operator()( DataType data ) const { |
73 |
skuang |
1341 |
return data == 0; |
74 |
tim |
672 |
} |
75 |
skuang |
1341 |
}; |
76 |
tim |
672 |
|
77 |
skuang |
1341 |
struct NonZeroConstraint : public ParamConstraintFacade<NonZeroConstraint>{ |
78 |
tim |
677 |
NonZeroConstraint() {this->description_ = "nonzero";} |
79 |
tim |
672 |
|
80 |
|
|
template<typename DataType> |
81 |
|
|
bool operator()( DataType data ) const { |
82 |
skuang |
1341 |
return data != 0; |
83 |
tim |
672 |
} |
84 |
skuang |
1341 |
}; |
85 |
tim |
672 |
|
86 |
skuang |
1341 |
struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint>{ |
87 |
tim |
677 |
PositiveConstraint() {this->description_ = "positive";} |
88 |
tim |
672 |
template<typename DataType> |
89 |
|
|
bool operator()( DataType data ) const |
90 |
|
|
{ |
91 |
skuang |
1341 |
return data > 0; |
92 |
tim |
672 |
} |
93 |
skuang |
1341 |
}; |
94 |
tim |
672 |
|
95 |
skuang |
1341 |
struct NonPositiveConstraint : public ParamConstraintFacade<NonPositiveConstraint>{ |
96 |
tim |
677 |
NonPositiveConstraint() {this->description_ = "nonpositive";} |
97 |
tim |
672 |
template<typename DataType> |
98 |
|
|
bool operator()( DataType data ) const |
99 |
|
|
{ |
100 |
skuang |
1341 |
return data <= 0; |
101 |
tim |
672 |
} |
102 |
skuang |
1341 |
}; |
103 |
tim |
672 |
|
104 |
skuang |
1341 |
struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint>{ |
105 |
tim |
677 |
NegativeConstraint() {this->description_ = "negative";} |
106 |
tim |
672 |
template<typename DataType> |
107 |
|
|
bool operator()( DataType data ) const |
108 |
|
|
{ |
109 |
skuang |
1341 |
return data < 0; |
110 |
tim |
672 |
} |
111 |
skuang |
1341 |
}; |
112 |
tim |
672 |
|
113 |
skuang |
1341 |
struct NonNegativeConstraint : public ParamConstraintFacade<NonNegativeConstraint>{ |
114 |
tim |
677 |
NonNegativeConstraint() {this->description_ = "nonnegative";} |
115 |
tim |
672 |
template<typename DataType> |
116 |
|
|
bool operator()( DataType data ) const |
117 |
|
|
{ |
118 |
skuang |
1341 |
return data >= 0; |
119 |
tim |
672 |
} |
120 |
skuang |
1341 |
}; |
121 |
tim |
672 |
|
122 |
skuang |
1341 |
|
123 |
|
|
struct EvenConstraint : public ParamConstraintFacade<EvenConstraint>{ |
124 |
|
|
EvenConstraint() {this->description_ = "even";} |
125 |
|
|
template<typename DataType> |
126 |
|
|
bool operator()( DataType data ) const |
127 |
|
|
{ |
128 |
|
|
return data % 2 == 0; |
129 |
|
|
} |
130 |
|
|
}; |
131 |
|
|
|
132 |
|
|
template<typename T> |
133 |
|
|
struct LessThanConstraint : public ParamConstraintFacade<LessThanConstraint<T> > { |
134 |
tim |
672 |
|
135 |
|
|
LessThanConstraint(T rhs) : rhs_(rhs){ |
136 |
skuang |
1341 |
std::stringstream iss; |
137 |
|
|
iss << "less than " << rhs; |
138 |
|
|
this->description_ = iss.str(); |
139 |
tim |
672 |
} |
140 |
|
|
template<typename DataType> |
141 |
|
|
bool operator()( DataType data ) const { |
142 |
skuang |
1341 |
return data < rhs_; |
143 |
tim |
672 |
} |
144 |
skuang |
1341 |
private: |
145 |
|
|
T rhs_; |
146 |
|
|
}; |
147 |
tim |
672 |
|
148 |
skuang |
1341 |
template<typename T> |
149 |
|
|
struct LessThanOrEqualToConstraint : public ParamConstraintFacade<LessThanOrEqualToConstraint<T> > { |
150 |
tim |
672 |
|
151 |
|
|
LessThanOrEqualToConstraint(T rhs) : rhs_(rhs) { |
152 |
skuang |
1341 |
std::stringstream iss; |
153 |
|
|
iss << "less than or equal to" << rhs; |
154 |
|
|
this->description_ = iss.str(); |
155 |
tim |
672 |
} |
156 |
|
|
|
157 |
|
|
template<typename DataType> |
158 |
|
|
bool operator()( DataType data ) const { |
159 |
skuang |
1341 |
return data <= rhs_; |
160 |
tim |
672 |
} |
161 |
|
|
|
162 |
skuang |
1341 |
private: |
163 |
|
|
T rhs_; |
164 |
|
|
}; |
165 |
tim |
672 |
|
166 |
skuang |
1341 |
template<typename T> |
167 |
|
|
struct EqualConstraint : public ParamConstraintFacade<EqualConstraint<T> > { |
168 |
tim |
672 |
|
169 |
|
|
EqualConstraint(T rhs) : rhs_(rhs){ |
170 |
skuang |
1341 |
std::stringstream iss; |
171 |
|
|
iss << "equal to" << rhs; |
172 |
|
|
this->description_ = iss.str(); |
173 |
tim |
672 |
|
174 |
|
|
} |
175 |
|
|
template<typename DataType> |
176 |
|
|
bool operator()( DataType data ) const { |
177 |
skuang |
1341 |
return data == rhs_; |
178 |
tim |
672 |
} |
179 |
skuang |
1341 |
private: |
180 |
|
|
T rhs_; |
181 |
|
|
}; |
182 |
tim |
672 |
|
183 |
skuang |
1341 |
struct EqualIgnoreCaseConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> { |
184 |
tim |
672 |
|
185 |
gezelter |
1390 |
EqualIgnoreCaseConstraint(std::string rhs) : rhs_(OpenMD::toUpperCopy(rhs)){ |
186 |
skuang |
1341 |
std::stringstream iss; |
187 |
|
|
iss << "equal to (case insensitive) " << rhs; |
188 |
|
|
this->description_ = iss.str(); |
189 |
tim |
672 |
} |
190 |
|
|
|
191 |
|
|
bool operator()( std::string data ) const { |
192 |
gezelter |
1390 |
return OpenMD::toUpperCopy(data) == rhs_; |
193 |
tim |
672 |
} |
194 |
|
|
|
195 |
skuang |
1341 |
private: |
196 |
|
|
std::string rhs_; |
197 |
|
|
}; |
198 |
tim |
672 |
|
199 |
skuang |
1341 |
struct ContainsConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> { |
200 |
gezelter |
1390 |
ContainsConstraint(std::string rhs) : rhs_(OpenMD::toUpperCopy(rhs)){ |
201 |
skuang |
1341 |
std::stringstream iss; |
202 |
|
|
iss << "contains " << rhs; |
203 |
|
|
this->description_ = iss.str(); |
204 |
tim |
677 |
} |
205 |
|
|
|
206 |
|
|
bool operator()( std::string data ) const { |
207 |
gezelter |
1390 |
OpenMD::StringTokenizer tokenizer(OpenMD::toUpperCopy(data), " ,;|\t\n\r"); |
208 |
skuang |
1341 |
while (tokenizer.hasMoreTokens()) { |
209 |
|
|
if (tokenizer.nextToken() == rhs_) { |
210 |
|
|
return true; |
211 |
tim |
677 |
} |
212 |
skuang |
1341 |
} |
213 |
tim |
677 |
|
214 |
skuang |
1341 |
return false; |
215 |
tim |
677 |
} |
216 |
|
|
|
217 |
skuang |
1341 |
private: |
218 |
|
|
std::string rhs_; |
219 |
tim |
677 |
|
220 |
skuang |
1341 |
}; |
221 |
tim |
677 |
|
222 |
skuang |
1341 |
template<typename T> |
223 |
|
|
struct GreaterThanConstraint : public ParamConstraintFacade<GreaterThanConstraint<T> > { |
224 |
tim |
672 |
|
225 |
|
|
GreaterThanConstraint(T rhs) : rhs_(rhs){ |
226 |
skuang |
1341 |
std::stringstream iss; |
227 |
|
|
iss << "greater than" << rhs; |
228 |
|
|
this->description_ = iss.str(); |
229 |
tim |
672 |
} |
230 |
|
|
template<typename DataType> |
231 |
|
|
bool operator()( DataType data ) const { |
232 |
skuang |
1341 |
return data > rhs_; |
233 |
tim |
672 |
} |
234 |
skuang |
1341 |
private: |
235 |
|
|
T rhs_; |
236 |
|
|
}; |
237 |
tim |
672 |
|
238 |
skuang |
1341 |
template<typename T> |
239 |
|
|
struct GreaterThanOrEqualTo : public ParamConstraintFacade<GreaterThanOrEqualTo<T> > { |
240 |
tim |
672 |
|
241 |
|
|
GreaterThanOrEqualTo(T rhs) : rhs_(rhs){ |
242 |
skuang |
1341 |
std::stringstream iss; |
243 |
|
|
iss << "greater than or equal to" << rhs; |
244 |
|
|
this->description_ = iss.str(); |
245 |
tim |
672 |
} |
246 |
|
|
template<typename DataType> |
247 |
|
|
bool operator()( DataType data ) const { |
248 |
skuang |
1341 |
return data >= rhs_; |
249 |
tim |
672 |
} |
250 |
skuang |
1341 |
private: |
251 |
|
|
T rhs_; |
252 |
|
|
}; |
253 |
tim |
672 |
|
254 |
skuang |
1341 |
// class_and composition predicate |
255 |
|
|
template<typename Cons1T, typename Cons2T> |
256 |
|
|
struct AndParamConstraint: public ParamConstraintFacade< AndParamConstraint<Cons1T,Cons2T> > { |
257 |
|
|
public: |
258 |
tim |
672 |
|
259 |
|
|
AndParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) { |
260 |
skuang |
1341 |
std::stringstream iss; |
261 |
|
|
iss << "(" << cons1_.getConstraintDescription() << " and " << cons2_.getConstraintDescription() << ")"; |
262 |
|
|
this->description_ = iss.str(); |
263 |
tim |
672 |
} |
264 |
|
|
|
265 |
|
|
template<typename DataType> |
266 |
|
|
bool operator()( DataType data ) const { |
267 |
skuang |
1341 |
return cons1_(data) && cons2_(data); |
268 |
tim |
672 |
} |
269 |
|
|
|
270 |
skuang |
1341 |
private: |
271 |
tim |
672 |
Cons1T cons1_; |
272 |
|
|
Cons2T cons2_; |
273 |
skuang |
1341 |
}; |
274 |
tim |
672 |
|
275 |
|
|
|
276 |
|
|
|
277 |
skuang |
1341 |
template<typename Cons1T, typename Cons2T> |
278 |
|
|
struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > { |
279 |
|
|
public: |
280 |
tim |
672 |
|
281 |
|
|
OrParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) { |
282 |
skuang |
1341 |
std::stringstream iss; |
283 |
|
|
iss << cons1_.getConstraintDescription() << " or " << cons2_.getConstraintDescription() << ""; |
284 |
|
|
this->description_ = iss.str(); |
285 |
|
|
} |
286 |
tim |
672 |
|
287 |
|
|
template<typename DataType> |
288 |
|
|
bool operator()( DataType data ) const { |
289 |
skuang |
1341 |
return cons1_(data) || cons2_(data); |
290 |
tim |
672 |
} |
291 |
|
|
|
292 |
skuang |
1341 |
private: |
293 |
tim |
672 |
Cons1T cons1_; |
294 |
|
|
Cons2T cons2_; |
295 |
skuang |
1341 |
}; |
296 |
tim |
672 |
|
297 |
skuang |
1341 |
template<typename ConsT> |
298 |
|
|
struct NotParamConstraint: public ParamConstraintFacade< NotParamConstraint<ConsT> > { |
299 |
|
|
public: |
300 |
tim |
672 |
|
301 |
|
|
|
302 |
|
|
NotParamConstraint( ConsT cons) : cons_(cons) { |
303 |
skuang |
1341 |
std::stringstream iss; |
304 |
|
|
iss << "(not" << cons_.getConstraintDescription() << ")"; |
305 |
|
|
this->description_ = iss.str(); |
306 |
tim |
672 |
} |
307 |
|
|
|
308 |
|
|
template<typename DataType> |
309 |
|
|
bool operator()( DataType data ) const { |
310 |
skuang |
1341 |
return !cons_(data); |
311 |
tim |
672 |
} |
312 |
|
|
|
313 |
skuang |
1341 |
private: |
314 |
tim |
672 |
ConsT cons_; |
315 |
skuang |
1341 |
}; |
316 |
tim |
672 |
|
317 |
|
|
|
318 |
skuang |
1341 |
template<typename Cons1T, typename Cons2T> |
319 |
|
|
inline AndParamConstraint<Cons1T, Cons2T> |
320 |
|
|
operator &&(const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) { |
321 |
tim |
672 |
|
322 |
|
|
return AndParamConstraint<Cons1T,Cons2T>( |
323 |
skuang |
1341 |
*static_cast<const Cons1T*>(&cons1), |
324 |
|
|
*static_cast<const Cons2T*>(&cons2) ); |
325 |
|
|
} |
326 |
tim |
672 |
|
327 |
skuang |
1341 |
template<typename Cons1T, typename Cons2T> |
328 |
|
|
inline OrParamConstraint<Cons1T, Cons2T> |
329 |
|
|
operator ||( const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) { |
330 |
tim |
672 |
|
331 |
|
|
return OrParamConstraint<Cons1T,Cons2T>( |
332 |
skuang |
1341 |
*static_cast<const Cons1T*>(&cons1), |
333 |
|
|
*static_cast<const Cons2T*>(&cons2) ); |
334 |
|
|
} |
335 |
tim |
672 |
|
336 |
|
|
|
337 |
skuang |
1341 |
template<typename ConsT> |
338 |
|
|
inline NotParamConstraint<ConsT> |
339 |
|
|
operator !( const ParamConstraintFacade<ConsT>& cons ) { |
340 |
tim |
672 |
|
341 |
|
|
return NotParamConstraint<ConsT>(*static_cast<const ConsT*>(&cons)); |
342 |
skuang |
1341 |
} |
343 |
tim |
672 |
|
344 |
|
|
|
345 |
skuang |
1341 |
NotEmptyConstraint isNotEmpty(); |
346 |
|
|
ZeroConstraint isZero(); |
347 |
tim |
672 |
|
348 |
skuang |
1341 |
ParamConstraintFacade<NonZeroConstraint> isNonZero(); |
349 |
|
|
PositiveConstraint isPositive(); |
350 |
|
|
NonPositiveConstraint isNonPositive(); |
351 |
tim |
672 |
|
352 |
skuang |
1341 |
NegativeConstraint isNegative(); |
353 |
tim |
672 |
|
354 |
skuang |
1341 |
NonNegativeConstraint isNonNegative(); |
355 |
|
|
EvenConstraint isEven(); |
356 |
tim |
672 |
|
357 |
skuang |
1341 |
template<typename T> |
358 |
|
|
inline LessThanConstraint<T>isLessThan(T& v ) { |
359 |
tim |
672 |
return LessThanConstraint<T>(v); |
360 |
skuang |
1341 |
} |
361 |
tim |
672 |
|
362 |
skuang |
1341 |
template<typename T> |
363 |
|
|
inline LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) { |
364 |
gezelter |
1746 |
return LessThanOrEqualToConstraint<T>(v); |
365 |
skuang |
1341 |
} |
366 |
tim |
672 |
|
367 |
skuang |
1341 |
template<typename T> |
368 |
|
|
inline EqualConstraint<T> isEqual(T& v ) { |
369 |
tim |
672 |
return EqualConstraint<T>(v); |
370 |
skuang |
1341 |
} |
371 |
tim |
672 |
|
372 |
skuang |
1341 |
template<typename T> |
373 |
|
|
inline GreaterThanConstraint<T> isGreaterThan(T& v ) { |
374 |
tim |
672 |
return GreaterThanConstraint<T>(v); |
375 |
skuang |
1341 |
} |
376 |
tim |
672 |
|
377 |
skuang |
1341 |
template<typename T> |
378 |
|
|
inline GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) { |
379 |
tim |
672 |
return GreaterThanOrEqualTo<T>(v); |
380 |
skuang |
1341 |
} |
381 |
tim |
672 |
|
382 |
skuang |
1341 |
EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str); |
383 |
tim |
672 |
} |
384 |
|
|
#endif |