ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/io/ParamConstraint.hpp
Revision: 3501
Committed: Fri May 8 19:47:05 2009 UTC (16 years, 2 months ago) by skuang
File size: 11619 byte(s)
Log Message:
Bug fixes and sanity checks for RNEMD (nBins should be even), check to make
sure we aren't selecting inappropriate numbers of integrable Objects

File Contents

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