ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/io/ParamConstraint.hpp
Revision: 1465
Committed: Fri Jul 9 23:08:25 2010 UTC (14 years, 9 months ago) by chuckv
File size: 11666 byte(s)
Log Message:
Creating busticated version of OpenMD

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date