RDKit
Open-source cheminformatics and machine learning.
Query.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2003-2006 Greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #ifndef __RD_QUERY_H__
11 #define __RD_QUERY_H__
12 
13 #ifdef _MSC_VER
14 #pragma warning (disable: 4800) // warning: converting things to bool
15 #endif
16 
17 #include <vector>
18 #include <string>
19 #include <boost/smart_ptr.hpp>
20 #include <RDGeneral/Invariant.h>
21 
22 namespace Queries {
23 
24  //! class to allow integer values to pick templates
25  template <int v>
26  class Int2Type
27  {
28  enum { value = v };
29  };
30 
31  //! Base class for all queries
32  /*!
33  Query objects have one or two functions associated with them:
34  - <tt>bool matchFunc(MatchFuncArgType other)</tt> returns true or false
35  to indicate whether this query matches \c other.
36  This is mandatory.
37 
38  - <tt>MatchFuncArgType dataFunc(DataFuncArgType other)</tt> converts
39  the argument \c other from \c DataFuncArgType to \c MatchFuncArgType.
40  This is optional if \c DataFuncArgType is the same as (or implicitly
41  convertible to) \c MatchFuncArgType.
42 
43  */
44  template <class MatchFuncArgType, class DataFuncArgType=MatchFuncArgType,
45  bool needsConversion=false>
46  class Query {
47  public:
48  typedef boost::shared_ptr< Query<MatchFuncArgType, DataFuncArgType, needsConversion> > CHILD_TYPE;
49  typedef std::vector< CHILD_TYPE > CHILD_VECT;
50  typedef typename CHILD_VECT::iterator CHILD_VECT_I;
51  typedef typename CHILD_VECT::const_iterator CHILD_VECT_CI;
52 
53  Query() : d_description(""),df_negate(false),d_matchFunc(NULL),d_dataFunc(NULL){};
54  virtual ~Query() { this->d_children.clear(); };
55 
56 
57  //! sets whether or not we are negated
58  void setNegation(bool what) { this->df_negate = what; };
59  //! returns whether or not we are negated
60  bool getNegation() const { return this->df_negate; };
61 
62  //! sets our text description
63  void setDescription(const std::string &descr) { this->d_description = descr; };
64  //! \overload
65  void setDescription(const char *descr) { this->d_description = std::string(descr); };
66  //! returns our text description
67  const std::string &getDescription() const { return this->d_description; };
68  //! returns a fuller text description
69  virtual std::string getFullDescription() const {
70  if(!getNegation())
71  return getDescription();
72  else
73  return "not "+getDescription();
74  }
75 
76  //! sets our match function
77  void setMatchFunc(bool (*what)(MatchFuncArgType)) { this->d_matchFunc = what; };
78  //! returns our match function:
79  bool (*getMatchFunc() const)(MatchFuncArgType) { return this->d_matchFunc; };
80  //! sets our data function
81  void setDataFunc(MatchFuncArgType (*what)(DataFuncArgType)) { this->d_dataFunc = what; };
82  //! returns our data function:
83  MatchFuncArgType (*getDataFunc() const)(DataFuncArgType) { return this->d_dataFunc; };
84 
85  //! adds a child to our list of children
86  void addChild(CHILD_TYPE child) { this->d_children.push_back(child); };
87  //! returns an iterator for the beginning of our child vector
88  CHILD_VECT_CI beginChildren() const { return this->d_children.begin(); }
89  //! returns an iterator for the end of our child vector
90  CHILD_VECT_CI endChildren() const { return this->d_children.end(); }
91 
92  //! returns whether or not we match the argument
93  virtual bool Match(const DataFuncArgType arg) const{
94  MatchFuncArgType mfArg = TypeConvert(arg,Int2Type<needsConversion>());
95  bool tRes;
96  if(this->d_matchFunc) tRes = this->d_matchFunc(mfArg);
97  else tRes = static_cast<bool>(mfArg);
98 
99  if( this->getNegation() ) return !tRes;
100  else return tRes;
101  };
102 
103  //! returns a copy of this Query
104  /*!
105  <b>Notes:</b>
106  - the caller is responsible for <tt>delete</tt>ing the result
107  */
109  copy( ) const {
113  for(iter=this->beginChildren();
114  iter!=this->endChildren();
115  ++iter){
116  res->addChild(CHILD_TYPE(iter->get()->copy()));
117  }
118  res->df_negate = this->df_negate;
119  res->d_matchFunc = this->d_matchFunc;
120  res->d_dataFunc = this->d_dataFunc;
121  res->d_description = this->d_description;
122  return res;
123  };
124 
125  protected :
126  std::string d_description;
127  CHILD_VECT d_children;
128  bool df_negate;
129  bool (*d_matchFunc) (MatchFuncArgType);
130  MatchFuncArgType (*d_dataFunc)(DataFuncArgType);
131 
132  //! \brief calls our \c dataFunc (if it's set) on \c what and returns
133  //! the result, otherwise returns \c what
134  MatchFuncArgType TypeConvert(MatchFuncArgType what,Int2Type<false> /*d*/) const{
135  MatchFuncArgType mfArg;
136  if( this->d_dataFunc != NULL ){
137  mfArg = this->d_dataFunc(what);
138  } else {
139  mfArg = what;
140  }
141  return mfArg;
142  }
143  //! calls our \c dataFunc (which must be set) on \c what and returns the result
144  MatchFuncArgType TypeConvert(DataFuncArgType what,Int2Type<true> /*d*/) const{
145  PRECONDITION(this->d_dataFunc,"no data function");
146  MatchFuncArgType mfArg;
147  mfArg = this->d_dataFunc(what);
148  return mfArg;
149  }
150 
151 
152  };
153 
154 
155  //----------------------------
156  //
157  // Used within query functions to compare values
158  //
159  //----------------------------
160  template <class T1, class T2>
161  int queryCmp(const T1 v1, const T2 v2, const T1 tol) {
162  T1 diff = v1 - v2;
163  if( diff <= tol ){
164  if( diff >= -tol ){
165  return 0;
166  } else {
167  return -1;
168  }
169  } else {
170  return 1;
171  }
172  };
173 
174 
175 }
176 #endif
bool getNegation() const
returns whether or not we are negated
Definition: Query.h:60
int queryCmp(const T1 v1, const T2 v2, const T1 tol)
Definition: Query.h:161
CHILD_VECT_CI beginChildren() const
returns an iterator for the beginning of our child vector
Definition: Query.h:88
const std::string & getDescription() const
returns our text description
Definition: Query.h:67
void setNegation(bool what)
sets whether or not we are negated
Definition: Query.h:58
virtual Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const
returns a copy of this Query
Definition: Query.h:109
CHILD_VECT_CI endChildren() const
returns an iterator for the end of our child vector
Definition: Query.h:90
boost::shared_ptr< Query< MatchFuncArgType, DataFuncArgType, needsConversion > > CHILD_TYPE
Definition: Query.h:48
MatchFuncArgType TypeConvert(MatchFuncArgType what, Int2Type< false >) const
calls our dataFunc (if it&#39;s set) on what and returns the result, otherwise returns what ...
Definition: Query.h:134
CHILD_VECT::iterator CHILD_VECT_I
Definition: Query.h:50
MatchFuncArgType(*)(DataFuncArgType) getDataFunc() const
returns our data function:
Definition: Query.h:83
void setMatchFunc(bool(*what)(MatchFuncArgType))
sets our match function
Definition: Query.h:77
virtual bool Match(const DataFuncArgType arg) const
returns whether or not we match the argument
Definition: Query.h:93
bool(* d_matchFunc)(MatchFuncArgType)
Definition: Query.h:129
class to allow integer values to pick templates
Definition: Query.h:26
CHILD_VECT d_children
Definition: Query.h:127
void setDescription(const char *descr)
Definition: Query.h:65
void addChild(CHILD_TYPE child)
adds a child to our list of children
Definition: Query.h:86
MatchFuncArgType(* d_dataFunc)(DataFuncArgType)
Definition: Query.h:130
std::vector< CHILD_TYPE > CHILD_VECT
Definition: Query.h:49
bool df_negate
Definition: Query.h:128
virtual ~Query()
Definition: Query.h:54
bool(*)(MatchFuncArgType) getMatchFunc() const
returns our match function:
Definition: Query.h:79
void setDataFunc(MatchFuncArgType(*what)(DataFuncArgType))
sets our data function
Definition: Query.h:81
CHILD_VECT::const_iterator CHILD_VECT_CI
Definition: Query.h:51
#define PRECONDITION(expr, mess)
Definition: Invariant.h:119
virtual std::string getFullDescription() const
returns a fuller text description
Definition: Query.h:69
std::string d_description
Definition: Query.h:123
MatchFuncArgType TypeConvert(DataFuncArgType what, Int2Type< true >) const
calls our dataFunc (which must be set) on what and returns the result
Definition: Query.h:144
Base class for all queries.
Definition: Query.h:46
void setDescription(const std::string &descr)
sets our text description
Definition: Query.h:63