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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines