ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/io/ParamConstraint.hpp
Revision: 1746
Committed: Wed Jun 6 02:18:54 2012 UTC (12 years, 10 months ago) by gezelter
File size: 11708 byte(s)
Log Message:
added a minimizer parsing block

File Contents

# Content
1 /*
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 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * 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 *
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 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).
39 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 */
42
43 #ifndef IO_PARAMCONSTRAINT_HPP
44 #define IO_PARAMCONSTRAINT_HPP
45 #include <sstream>
46 #include "utils/CaseConversion.hpp"
47 #include "utils/StringTokenizer.hpp"
48 namespace OpenMD {
49 /**
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 std::string getConstraintDescription() {return description_;}
56 protected:
57 std::string description_;
58 };
59
60 struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint>{
61
62 NotEmptyConstraint() { description_= "nonempty";}
63 bool operator()( const std::string data ) const {
64 return !data.empty();
65 }
66 };
67
68 struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint>{
69
70 ZeroConstraint() {this->description_ = "zero";}
71 template<typename DataType>
72 bool operator()( DataType data ) const {
73 return data == 0;
74 }
75 };
76
77 struct NonZeroConstraint : public ParamConstraintFacade<NonZeroConstraint>{
78 NonZeroConstraint() {this->description_ = "nonzero";}
79
80 template<typename DataType>
81 bool operator()( DataType data ) const {
82 return data != 0;
83 }
84 };
85
86 struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint>{
87 PositiveConstraint() {this->description_ = "positive";}
88 template<typename DataType>
89 bool operator()( DataType data ) const
90 {
91 return data > 0;
92 }
93 };
94
95 struct NonPositiveConstraint : public ParamConstraintFacade<NonPositiveConstraint>{
96 NonPositiveConstraint() {this->description_ = "nonpositive";}
97 template<typename DataType>
98 bool operator()( DataType data ) const
99 {
100 return data <= 0;
101 }
102 };
103
104 struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint>{
105 NegativeConstraint() {this->description_ = "negative";}
106 template<typename DataType>
107 bool operator()( DataType data ) const
108 {
109 return data < 0;
110 }
111 };
112
113 struct NonNegativeConstraint : public ParamConstraintFacade<NonNegativeConstraint>{
114 NonNegativeConstraint() {this->description_ = "nonnegative";}
115 template<typename DataType>
116 bool operator()( DataType data ) const
117 {
118 return data >= 0;
119 }
120 };
121
122
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
135 LessThanConstraint(T rhs) : rhs_(rhs){
136 std::stringstream iss;
137 iss << "less than " << rhs;
138 this->description_ = iss.str();
139 }
140 template<typename DataType>
141 bool operator()( DataType data ) const {
142 return data < rhs_;
143 }
144 private:
145 T rhs_;
146 };
147
148 template<typename T>
149 struct LessThanOrEqualToConstraint : public ParamConstraintFacade<LessThanOrEqualToConstraint<T> > {
150
151 LessThanOrEqualToConstraint(T rhs) : rhs_(rhs) {
152 std::stringstream iss;
153 iss << "less than or equal to" << rhs;
154 this->description_ = iss.str();
155 }
156
157 template<typename DataType>
158 bool operator()( DataType data ) const {
159 return data <= rhs_;
160 }
161
162 private:
163 T rhs_;
164 };
165
166 template<typename T>
167 struct EqualConstraint : public ParamConstraintFacade<EqualConstraint<T> > {
168
169 EqualConstraint(T rhs) : rhs_(rhs){
170 std::stringstream iss;
171 iss << "equal to" << rhs;
172 this->description_ = iss.str();
173
174 }
175 template<typename DataType>
176 bool operator()( DataType data ) const {
177 return data == rhs_;
178 }
179 private:
180 T rhs_;
181 };
182
183 struct EqualIgnoreCaseConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> {
184
185 EqualIgnoreCaseConstraint(std::string rhs) : rhs_(OpenMD::toUpperCopy(rhs)){
186 std::stringstream iss;
187 iss << "equal to (case insensitive) " << rhs;
188 this->description_ = iss.str();
189 }
190
191 bool operator()( std::string data ) const {
192 return OpenMD::toUpperCopy(data) == rhs_;
193 }
194
195 private:
196 std::string rhs_;
197 };
198
199 struct ContainsConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> {
200 ContainsConstraint(std::string rhs) : rhs_(OpenMD::toUpperCopy(rhs)){
201 std::stringstream iss;
202 iss << "contains " << rhs;
203 this->description_ = iss.str();
204 }
205
206 bool operator()( std::string data ) const {
207 OpenMD::StringTokenizer tokenizer(OpenMD::toUpperCopy(data), " ,;|\t\n\r");
208 while (tokenizer.hasMoreTokens()) {
209 if (tokenizer.nextToken() == rhs_) {
210 return true;
211 }
212 }
213
214 return false;
215 }
216
217 private:
218 std::string rhs_;
219
220 };
221
222 template<typename T>
223 struct GreaterThanConstraint : public ParamConstraintFacade<GreaterThanConstraint<T> > {
224
225 GreaterThanConstraint(T rhs) : rhs_(rhs){
226 std::stringstream iss;
227 iss << "greater than" << rhs;
228 this->description_ = iss.str();
229 }
230 template<typename DataType>
231 bool operator()( DataType data ) const {
232 return data > rhs_;
233 }
234 private:
235 T rhs_;
236 };
237
238 template<typename T>
239 struct GreaterThanOrEqualTo : public ParamConstraintFacade<GreaterThanOrEqualTo<T> > {
240
241 GreaterThanOrEqualTo(T rhs) : rhs_(rhs){
242 std::stringstream iss;
243 iss << "greater than or equal to" << rhs;
244 this->description_ = iss.str();
245 }
246 template<typename DataType>
247 bool operator()( DataType data ) const {
248 return data >= rhs_;
249 }
250 private:
251 T rhs_;
252 };
253
254 // class_and composition predicate
255 template<typename Cons1T, typename Cons2T>
256 struct AndParamConstraint: public ParamConstraintFacade< AndParamConstraint<Cons1T,Cons2T> > {
257 public:
258
259 AndParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) {
260 std::stringstream iss;
261 iss << "(" << cons1_.getConstraintDescription() << " and " << cons2_.getConstraintDescription() << ")";
262 this->description_ = iss.str();
263 }
264
265 template<typename DataType>
266 bool operator()( DataType data ) const {
267 return cons1_(data) && cons2_(data);
268 }
269
270 private:
271 Cons1T cons1_;
272 Cons2T cons2_;
273 };
274
275
276
277 template<typename Cons1T, typename Cons2T>
278 struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > {
279 public:
280
281 OrParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) {
282 std::stringstream iss;
283 iss << cons1_.getConstraintDescription() << " or " << cons2_.getConstraintDescription() << "";
284 this->description_ = iss.str();
285 }
286
287 template<typename DataType>
288 bool operator()( DataType data ) const {
289 return cons1_(data) || cons2_(data);
290 }
291
292 private:
293 Cons1T cons1_;
294 Cons2T cons2_;
295 };
296
297 template<typename ConsT>
298 struct NotParamConstraint: public ParamConstraintFacade< NotParamConstraint<ConsT> > {
299 public:
300
301
302 NotParamConstraint( ConsT cons) : cons_(cons) {
303 std::stringstream iss;
304 iss << "(not" << cons_.getConstraintDescription() << ")";
305 this->description_ = iss.str();
306 }
307
308 template<typename DataType>
309 bool operator()( DataType data ) const {
310 return !cons_(data);
311 }
312
313 private:
314 ConsT cons_;
315 };
316
317
318 template<typename Cons1T, typename Cons2T>
319 inline AndParamConstraint<Cons1T, Cons2T>
320 operator &&(const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) {
321
322 return AndParamConstraint<Cons1T,Cons2T>(
323 *static_cast<const Cons1T*>(&cons1),
324 *static_cast<const Cons2T*>(&cons2) );
325 }
326
327 template<typename Cons1T, typename Cons2T>
328 inline OrParamConstraint<Cons1T, Cons2T>
329 operator ||( const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) {
330
331 return OrParamConstraint<Cons1T,Cons2T>(
332 *static_cast<const Cons1T*>(&cons1),
333 *static_cast<const Cons2T*>(&cons2) );
334 }
335
336
337 template<typename ConsT>
338 inline NotParamConstraint<ConsT>
339 operator !( const ParamConstraintFacade<ConsT>& cons ) {
340
341 return NotParamConstraint<ConsT>(*static_cast<const ConsT*>(&cons));
342 }
343
344
345 NotEmptyConstraint isNotEmpty();
346 ZeroConstraint isZero();
347
348 ParamConstraintFacade<NonZeroConstraint> isNonZero();
349 PositiveConstraint isPositive();
350 NonPositiveConstraint isNonPositive();
351
352 NegativeConstraint isNegative();
353
354 NonNegativeConstraint isNonNegative();
355 EvenConstraint isEven();
356
357 template<typename T>
358 inline LessThanConstraint<T>isLessThan(T& v ) {
359 return LessThanConstraint<T>(v);
360 }
361
362 template<typename T>
363 inline LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) {
364 return LessThanOrEqualToConstraint<T>(v);
365 }
366
367 template<typename T>
368 inline EqualConstraint<T> isEqual(T& v ) {
369 return EqualConstraint<T>(v);
370 }
371
372 template<typename T>
373 inline GreaterThanConstraint<T> isGreaterThan(T& v ) {
374 return GreaterThanConstraint<T>(v);
375 }
376
377 template<typename T>
378 inline GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) {
379 return GreaterThanOrEqualTo<T>(v);
380 }
381
382 EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str);
383 }
384 #endif

Properties

Name Value
svn:keywords Author Id Revision Date