ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/io/ParamConstraint.hpp
(Generate patch)

Comparing:
trunk/src/io/ParamConstraint.hpp (file contents), Revision 672 by tim, Fri Oct 14 21:43:13 2005 UTC vs.
branches/development/src/io/ParamConstraint.hpp (file contents), Revision 1874 by gezelter, Wed May 15 15:09:35 2013 UTC

# Line 6 | Line 6
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
9 > * 1. Redistributions of source code must retain the above copyright
10   *    notice, this list of conditions and the following disclaimer.
11   *
12 < * 3. Redistributions in binary form must reproduce the above copyright
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.
# Line 37 | Line 28
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, 234107 (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 <
47 < /**
48 <  * This class allows to recognize constraint predicates, so that they can be combined using
49 <  * composition operators. Every constraint predicate must be derived from this class
50 <  */
51 < template<typename Derived>
52 < struct ParamConstraintFacade {
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 < };
56 >  protected:
57 >    std::string description_;
58 >  };
59  
60 < struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint>{
61 <
60 >  struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint>{
61 >    
62      NotEmptyConstraint() { description_= "nonempty";}
63 <    bool operator()( const std::string data ) const {
64 <        return !data.empty();
63 >    bool operator()( const std::string &data ) const {
64 >      return !data.empty();
65      }
66 < };
66 >  };
67  
68 < struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint>{
68 >  struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint>{
69      
70 <    ZeroConstraint() {description_ = "zero";}
70 >    ZeroConstraint() {this->description_ = "zero";}
71      template<typename DataType>
72      bool operator()( DataType data ) const {
73 <        return data == 0;
73 >      return data == 0;
74      }
75 < };
75 >  };
76  
77 < struct NonZeroConstraint : public ParamConstraintFacade<NonZeroConstraint>{
78 <    NonZeroConstraint() {description_ = "nonzero";}
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;
82 >      return data != 0;
83      }
84 < };
84 >  };
85  
86 < struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint>{
87 <    PositiveConstraint() {description_ = "positive";}
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;
91 >      return data > 0;
92      }
93 < };
93 >  };
94  
95 < struct NonPositiveConstraint : public ParamConstraintFacade<NonPositiveConstraint>{
96 <    NonPositiveConstraint() {description_ = "nonpositive";}
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;
100 >      return data <= 0;
101      }
102 < };
102 >  };
103  
104 < struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint>{
105 <    NegativeConstraint() {description_ = "negative";}
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;
109 >      return data < 0;
110      }
111 < };
111 >  };
112  
113 < struct NonNegativeConstraint : public ParamConstraintFacade<NonNegativeConstraint>{
114 <    NonNegativeConstraint() {description_ = "nonnegative";}
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;
118 >      return data >= 0;
119      }
120 < };
120 >  };
121  
122 < template<typename T>
123 < struct LessThanConstraint : public ParamConstraintFacade<LessThanConstraint<T> > {
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 <        description_ = iss.str();
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_;
142 >      return data < rhs_;
143      }
144 <    private:
145 <        T rhs_;        
146 < };
144 >  private:
145 >    T rhs_;        
146 >  };
147  
148 < template<typename T>
149 < struct LessThanOrEqualToConstraint : public ParamConstraintFacade<LessThanOrEqualToConstraint<T> > {
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 <        description_ = iss.str();
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_;
159 >      return data <= rhs_;
160      }
161      
162 <    private:
163 <        T rhs_;        
164 < };
162 >  private:
163 >    T rhs_;        
164 >  };
165  
166 < template<typename T>
167 < struct EqualConstraint : public ParamConstraintFacade<EqualConstraint<T> > {
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 <        description_ = iss.str();
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_;
177 >      return data == rhs_;
178      }
179 <    private:
180 <        T rhs_;        
181 < };
179 >  private:
180 >    T rhs_;        
181 >  };
182  
183 < struct EqualIgnoreCaseConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> {
183 >  struct EqualIgnoreCaseConstraint : public ParamConstraintFacade<EqualIgnoreCaseConstraint> {
184      
185 <    EqualIgnoreCaseConstraint(std::string rhs) : rhs_(rhs){
186 <        std::stringstream iss;
187 <        iss << "equal to (case insensitive) " << rhs;
188 <        description_ = iss.str();
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 data == rhs_;
192 >      return OpenMD::toUpperCopy(data) == rhs_;
193      }
194      
195 <    private:
196 <       std::string rhs_;        
197 < };
195 >  private:
196 >    std::string rhs_;        
197 >  };
198  
199 < template<typename T>
200 < struct GreaterThanConstraint : public ParamConstraintFacade<GreaterThanConstraint<T> > {
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 <        description_ = iss.str();
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_;
232 >      return data > rhs_;
233      }
234 <    private:
235 <        T rhs_;        
236 < };
234 >  private:
235 >    T rhs_;        
236 >  };
237  
238 < template<typename T>
239 < struct GreaterThanOrEqualTo : public ParamConstraintFacade<GreaterThanOrEqualTo<T> > {
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 <        description_ = iss.str();
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_;
248 >      return data >= rhs_;
249      }
250 <    private:
251 <        T rhs_;        
252 < };
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:
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 <        description_ = iss.str();        
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);
267 >      return cons1_(data) && cons2_(data);
268      }
269  
270 < private:
270 >  private:
271      Cons1T cons1_;
272      Cons2T cons2_;
273 < };
273 >  };
274  
275  
276  
277 < template<typename Cons1T, typename Cons2T>
278 < struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > {
279 < public:
277 >  template<typename Cons1T, typename Cons2T>
278 >  struct OrParamConstraint: public ParamConstraintFacade< OrParamConstraint<Cons1T,Cons2T> > {
279 >  public:
280  
245
281      OrParamConstraint( Cons1T cons1, Cons2T cons2 ) : cons1_(cons1), cons2_(cons2) {
282 <        std::stringstream iss;
283 <        iss << cons1_.getConstraintDescription() << " or " << cons2_.getConstraintDescription() << "";
284 <        description_ = iss.str();
285 <     }
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);
289 >      return cons1_(data) || cons2_(data);
290      }
291  
292 < private:
292 >  private:
293      Cons1T cons1_;
294      Cons2T cons2_;
295 < };
295 >  };
296  
297 < template<typename ConsT>
298 < struct NotParamConstraint: public ParamConstraintFacade< NotParamConstraint<ConsT> > {
299 < public:
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" << cons1_.getConstraintDescription() << ")";
305 <        description_ = iss.str();
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);
310 >      return !cons_(data);
311      }
312  
313 < private:
313 >  private:
314      ConsT cons_;
315 < };    
315 >  };    
316  
317  
318 < template<typename Cons1T, typename Cons2T>
319 < inline AndParamConstraint<Cons1T, Cons2T>
320 < operator &&(const ParamConstraintFacade<Cons1T>& cons1,  const ParamConstraintFacade<Cons2T>& cons2 ) {    
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 < }
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 ) {    
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 < }
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 ) {
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 < }
342 >  }
343  
344  
345 < NotEmptyConstraint isNotEmpty() {
346 <    return NotEmptyConstraint();
312 < }
345 >  NotEmptyConstraint isNotEmpty();
346 >  ZeroConstraint isZero();
347  
348 < ZeroConstraint isZero() {
349 <    return ZeroConstraint();
350 < }
348 >  ParamConstraintFacade<NonZeroConstraint> isNonZero();
349 >  PositiveConstraint isPositive();
350 >  NonPositiveConstraint isNonPositive();
351  
352 < ParamConstraintFacade<NonZeroConstraint> isNonZero() {
319 <    return ParamConstraintFacade<NonZeroConstraint>();
320 < }
352 >  NegativeConstraint isNegative();
353  
354 < PositiveConstraint isPositive() {
355 <    return PositiveConstraint();
324 < }
354 >  NonNegativeConstraint isNonNegative();
355 >  EvenConstraint isEven();
356  
357 < NonPositiveConstraint isNonPositive() {
358 <    return NonPositiveConstraint();
328 < }
329 <
330 < NegativeConstraint isNegative() {
331 <    return NegativeConstraint();
332 < }
333 <
334 < NonNegativeConstraint isNonNegative() {
335 <    return NonNegativeConstraint();
336 < }
337 <
338 < template<typename T>
339 < LessThanConstraint<T>isLessThan(T& v ) {
357 >  template<typename T>
358 >  inline LessThanConstraint<T>isLessThan(T& v ) {
359      return LessThanConstraint<T>(v);
360 < }
360 >  }
361  
362 < template<typename T>
363 < LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) {
364 <    return ParamConstraintFacade<LessThanOrEqualToConstraint<T> >(v);
365 < }
362 >  template<typename T>
363 >  inline LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v ) {
364 >    return LessThanOrEqualToConstraint<T>(v);
365 >  }
366  
367 < template<typename T>
368 < EqualConstraint<T> isEqual(T& v ) {
367 >  template<typename T>
368 >  inline EqualConstraint<T> isEqual(T& v ) {
369      return EqualConstraint<T>(v);
370 < }
370 >  }
371  
372 < template<typename T>
373 < GreaterThanConstraint<T> isGreaterThan(T& v ) {
372 >  template<typename T>
373 >  inline GreaterThanConstraint<T> isGreaterThan(T& v ) {
374      return GreaterThanConstraint<T>(v);
375 < }
375 >  }
376  
377 < template<typename T>
378 < GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) {
377 >  template<typename T>
378 >  inline GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v ) {
379      return GreaterThanOrEqualTo<T>(v);
380 < }
380 >  }
381  
382 < EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str) {
364 <    return EqualIgnoreCaseConstraint(str);
382 >  EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str);
383   }
366
384   #endif

Comparing:
trunk/src/io/ParamConstraint.hpp (property svn:keywords), Revision 672 by tim, Fri Oct 14 21:43:13 2005 UTC vs.
branches/development/src/io/ParamConstraint.hpp (property svn:keywords), Revision 1874 by gezelter, Wed May 15 15:09:35 2013 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines