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.
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 <
46 < /**
47 <  * This class allows to recognize constraint predicates, so that they can be combined using
48 <  * composition operators. Every constraint predicate must be derived from this class
49 <  */
50 < template<typename Derived>
51 < struct ParamConstraintFacade {
52 < };
53 <
54 <
55 < struct NonConstraint : public ParamConstraintFacade<NonConstraint>{
56 <    template<typename DataType>
57 <    bool operator()( DataType data ) const {
58 <        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 < };    
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 <
61 < template<typename Cons1T, typename Cons2T>
62 < inline AndParamConstraint<Cons1T, Cons2T>
63 < operator &&(const ParamConstraintFacade<Cons1T>& cons1,  const ParamConstraintFacade<Cons2T>& cons2 ) {    
64 <
65 <    return AndParamConstraint<Cons1T,Cons2T>(
66 <        *static_cast<const Cons1T*>(&cons1),
67 <        *static_cast<const Cons2T*>(&cons2) );
68 < }
69 <
70 < template<typename Cons1T, typename Cons2T>
71 < inline OrParamConstraint<Cons1T, Cons2T>
72 < operator ||( const ParamConstraintFacade<Cons1T>& cons1, const ParamConstraintFacade<Cons2T>& cons2 ) {    
73 <
74 <    return OrParamConstraint<Cons1T,Cons2T>(
75 <        *static_cast<const Cons1T*>(&cons1),
76 <        *static_cast<const Cons2T*>(&cons2) );
77 < }
78 <
79 <
80 < template<typename ConsT>
81 < inline NotParamConstraint<ConsT>
82 < operator !( const ParamConstraintFacade<ConsT>& cons ) {
83 <
84 <    return NotParamConstraint<ConsT>(*static_cast<const ConsT*>(&cons));
85 < }
86 <
87 <
88 <
89 < #endif
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

Comparing:
trunk/src/io/ParamConstraint.hpp (property svn:keywords), Revision 667 by tim, Fri Oct 14 14:48:10 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