00001
00002
00003
00004 #ifndef BALL_MOLMEC_AMBER_GAFFCESPARSER_H
00005 #define BALL_MOLMEC_AMBER_GAFFCESPARSER_H
00006
00007 #ifndef BALL_KERNEL_ATOM_H
00008 #include <BALL/KERNEL/atom.h>
00009 #endif
00010
00011 #ifndef BALL_DATATYPE_STRING_H
00012 #include <BALL/DATATYPE/string.h>
00013 #endif
00014
00015 #include <set>
00016 #include <map>
00017 #include <vector>
00018
00019 namespace BALL
00020 {
00021
00022 class GAFFCESParser
00023 {
00024 public:
00025
00026
00027 class APSMatcher
00028 {
00029 public:
00030
00031 enum APSType
00032 {
00033 IS_RING_ATOM,
00034 IS_NON_RING_ATOM,
00035 IS_PLANAR,
00036 IS_PLANAR_WITH_DB_TO_NR,
00037 IS_PURELY_AROMATIC,
00038 IS_PURELY_ALIPHATIC,
00039 IS_OTHER_RING,
00040 IS_3_RING_ATOM,
00041 IS_4_RING_ATOM,
00042 IS_5_RING_ATOM,
00043 IS_6_RING_ATOM,
00044 IS_7_RING_ATOM,
00045 IS_8_RING_ATOM,
00046 IS_9_RING_ATOM,
00047 PURE_SINGLE_BOND,
00048 PURE_SINGLE_BOND_TO_PARENT,
00049 NO_PURE_SINGLE_BOND_TO_PARENT,
00050 SINGLE_BOND,
00051 SINGLE_BOND_TO_PARENT,
00052 NO_SINGLE_BOND_TO_PARENT,
00053 PURE_DOUBLE_BOND,
00054 PURE_DOUBLE_BOND_TO_PARENT,
00055 NO_PURE_DOUBLE_BOND_TO_PARENT,
00056 DOUBLE_BOND,
00057 DOUBLE_BOND_TO_PARENT,
00058 NO_DOUBLE_BOND_TO_PARENT,
00059 TRIPLE_BOND,
00060 TRIPLE_BOND_TO_PARENT,
00061 NO_TRIPLE_BOND_TO_PARENT,
00062 DELOCALIZED_BOND,
00063 DELOCALIZED_BOND_TO_PARENT,
00064 NO_DELOCALIZED_BOND_TO_PARENT,
00065 AROMATIC_BOND,
00066 AROMATIC_BOND_TO_PARENT,
00067 NO_AROMATIC_BOND_TO_PARENT,
00068 APS_TRUE
00069 };
00070
00071 class APSTerm
00072 {
00073 public:
00074 APSTerm(APSType new_type, int new_feature_number)
00075 : type(new_type),
00076 feature_number(new_feature_number)
00077 {}
00078
00079 APSType type;
00080
00081 int feature_number;
00082 };
00083
00084
00085 bool isRingAtom(Atom& atom);
00086
00087
00088 int isNRingAtom(Size size, Atom& atom);
00089
00090
00091 bool isNonRingAtom(Atom& atom);
00092
00093 bool checkGAFFProperties(Atom& atom, Atom& predecessor, APSTerm aps);
00094
00095
00096 APSMatcher();
00097 ~APSMatcher();
00098
00099 String printAPS();
00100
00101
00102 bool operator() (Atom& atom, Atom& predecessor);
00103
00104
00105
00106
00107 std::vector < std::vector< APSTerm> > aps_terms;
00108
00109 protected:
00110 bool hasBond_(Atom* atom, Atom* predecessor, int bond_type, int feature_number);
00111 };
00112
00113 struct State
00114 {
00115
00116 GAFFCESParser* current_parser;
00117
00118 APSMatcher::APSType current_aps_type;
00119 int feature_number;
00120 };
00121
00122
00123
00124
00125 class CESPredicate
00126 {
00127 public:
00128
00129 enum CESwildcards
00130 {
00131 XA, XB, XC, XD, XX
00132 };
00133
00134 CESPredicate(GAFFCESParser* parser)
00135 : parent(0),
00136 parser_(parser)
00137 {
00138 initStringToWildcard();
00139 };
00140
00141 virtual ~CESPredicate();
00142
00143
00144 void initStringToWildcard();
00145 std::map<String, CESwildcards> getStringToWildcard();
00146
00147
00148 void addCESwildcardsConnectionPredicate(String wildcard, Size partners);
00149
00150 void addCESwildcardsPredicate(String wildcard);
00151
00152 void addCESelementPredicate(String name);
00153
00154 void addCESelementConnectionPredicate(Size partners, String name);
00155
00156 void addTruePredicate();
00157
00158
00159 virtual bool operator () (Atom& atom);
00160
00161 virtual bool match(Atom&){return false;};
00162
00163 void clear();
00164
00165
00166 bool alreadySeenThisAtom(Atom* atom);
00167
00168
00169 void addNewAND();
00170 void addNewOR(APSMatcher::APSType aps, int feature_number);
00171
00172
00173 APSMatcher aps_matcher;
00174
00175
00176 std::vector<CESPredicate*> children;
00177
00178 CESPredicate* parent;
00179
00180
00181 Atom* atom_to_test;
00182 protected:
00183
00184 std::map<String, CESwildcards > stringToWildcard_;
00185 GAFFCESParser* parser_;
00186 };
00187
00188
00189 class CESelementConnectionPredicate : public CESPredicate
00190 {
00191 public:
00192 CESelementConnectionPredicate(GAFFCESParser* parser)
00193 : CESPredicate(parser),
00194 numberOfPartners_(0),
00195 elementName_("NoName")
00196 {};
00197
00198 ~CESelementConnectionPredicate();
00199
00200 void setNumberOfPartners(Size number);
00201 void setElementName(String name);
00202 Size getNumberOfPartners();
00203 String getElementName();
00204
00205 bool match(Atom& atom);
00206 protected:
00207 Size numberOfPartners_;
00208 String elementName_;
00209 };
00210
00211
00212 class CESelementPredicate : public CESPredicate
00213 {
00214 public:
00215 CESelementPredicate(GAFFCESParser* parser)
00216 : CESPredicate(parser),
00217 elementName_("NoName")
00218 {
00219 };
00220
00221 ~CESelementPredicate();
00222
00223 void setElementName(String name);
00224 String getElementName();
00225
00226 bool match(Atom& atom);
00227 protected:
00228 String elementName_;
00229 };
00230
00231
00232 class CESwildcardsPredicate : public CESPredicate
00233 {
00234 public:
00235 CESwildcardsPredicate(GAFFCESParser* parser)
00236 : CESPredicate(parser)
00237 {};
00238
00239 ~CESwildcardsPredicate();
00240
00241 void setWildcards(String new_wildcard);
00242 CESwildcards getWildcards();
00243
00244 bool matchWildcards(Atom& atom);
00245
00246 bool match(Atom& atom);
00247 protected:
00248 CESwildcards wildcards_;
00249 };
00250
00251
00252 class CESwildcardsConnectionPredicate : public CESPredicate
00253 {
00254 public:
00255 CESwildcardsConnectionPredicate(GAFFCESParser* parser)
00256 : CESPredicate(parser),
00257 numberOfPartners_(0)
00258 {};
00259 ~CESwildcardsConnectionPredicate();
00260
00261 void setNumberOfPartners(Size number);
00262 void setWildcards(String new_wildcard);
00263 Size getNumberOfPartners();
00264 CESwildcards getWildcards();
00265
00266 bool matchWildcards(Atom& atom);
00267
00268 bool match(Atom& atom);
00269 protected:
00270 CESwildcards wildcards_;
00271 Size numberOfPartners_;
00272 };
00273
00274
00275 class TruePredicate : public CESPredicate
00276 {
00277 public:
00278 TruePredicate(GAFFCESParser* parser)
00279 : CESPredicate(parser)
00280 {};
00281 ~TruePredicate() {};
00282
00283 bool match(Atom&) { return true; }
00284 };
00285
00286
00287 bool match(Atom& atom);
00288
00289
00290 void initElementSymbols();
00291 const std::set<String>& getElementSymbols();
00292
00293 GAFFCESParser();
00294 GAFFCESParser(const String& cesstring);
00295 ~GAFFCESParser();
00296
00297
00298 Size read(char* buf, Size max_size);
00299
00300 static State state;
00301
00303 TruePredicate root;
00304
00305 CESPredicate* root_predicate;
00306
00307 CESPredicate* current_root_predicate;
00308
00309 CESPredicate* current_predicate;
00310
00311
00312
00313 bool parse(const String& cesstring);
00314
00315 bool GAFFCESatomMatcher(Atom& atom, const String& cesstring);
00316
00317 void startChildPredicates();
00318
00319 void endChildPredicates();
00320
00321 protected:
00322
00323 String cesstring_;
00324
00325 std::set<String> element_symbols_;
00326
00327 Position read_start_;
00328 };
00329
00330 }
00331
00332 #endif