| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#ifndef BALL_MOLMEC_AMBER_GAFFCESPARSER_H |
| 5 |
#define BALL_MOLMEC_AMBER_GAFFCESPARSER_H |
| 6 |
|
| 7 |
#ifndef BALL_KERNEL_ATOM_H |
| 8 |
#include <BALL/KERNEL/atom.h> |
| 9 |
#endif |
| 10 |
|
| 11 |
#ifndef BALL_DATATYPE_STRING_H |
| 12 |
#include <BALL/DATATYPE/string.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <set> |
| 16 |
#include <map> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
namespace BALL |
| 20 |
{ |
| 21 |
|
| 22 |
class GAFFCESParser |
| 23 |
{ |
| 24 |
public: |
| 25 |
|
| 26 |
|
| 27 |
class APSMatcher |
| 28 |
{ |
| 29 |
public: |
| 30 |
|
| 31 |
enum APSType |
| 32 |
{ |
| 33 |
IS_RING_ATOM, |
| 34 |
IS_NON_RING_ATOM, |
| 35 |
IS_PLANAR, |
| 36 |
IS_PLANAR_WITH_DB_TO_NR, |
| 37 |
IS_PURELY_AROMATIC, |
| 38 |
IS_PURELY_ALIPHATIC, |
| 39 |
IS_OTHER_RING, |
| 40 |
IS_3_RING_ATOM, |
| 41 |
IS_4_RING_ATOM, |
| 42 |
IS_5_RING_ATOM, |
| 43 |
IS_6_RING_ATOM, |
| 44 |
IS_7_RING_ATOM, |
| 45 |
IS_8_RING_ATOM, |
| 46 |
IS_9_RING_ATOM, |
| 47 |
PURE_SINGLE_BOND, |
| 48 |
PURE_SINGLE_BOND_TO_PARENT, |
| 49 |
NO_PURE_SINGLE_BOND_TO_PARENT, |
| 50 |
SINGLE_BOND, |
| 51 |
SINGLE_BOND_TO_PARENT, |
| 52 |
NO_SINGLE_BOND_TO_PARENT, |
| 53 |
PURE_DOUBLE_BOND, |
| 54 |
PURE_DOUBLE_BOND_TO_PARENT, |
| 55 |
NO_PURE_DOUBLE_BOND_TO_PARENT, |
| 56 |
DOUBLE_BOND, |
| 57 |
DOUBLE_BOND_TO_PARENT, |
| 58 |
NO_DOUBLE_BOND_TO_PARENT, |
| 59 |
TRIPLE_BOND, |
| 60 |
TRIPLE_BOND_TO_PARENT, |
| 61 |
NO_TRIPLE_BOND_TO_PARENT, |
| 62 |
DELOCALIZED_BOND, |
| 63 |
DELOCALIZED_BOND_TO_PARENT, |
| 64 |
NO_DELOCALIZED_BOND_TO_PARENT, |
| 65 |
AROMATIC_BOND, |
| 66 |
AROMATIC_BOND_TO_PARENT, |
| 67 |
NO_AROMATIC_BOND_TO_PARENT, |
| 68 |
APS_TRUE |
| 69 |
}; |
| 70 |
|
| 71 |
class APSTerm |
| 72 |
{ |
| 73 |
public: |
| 74 |
APSTerm(APSType new_type, int new_feature_number) |
| 75 |
: type(new_type), |
| 76 |
feature_number(new_feature_number) |
| 77 |
{} |
| 78 |
|
| 79 |
APSType type; |
| 80 |
|
| 81 |
int feature_number; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
bool isRingAtom(Atom& atom); |
| 86 |
|
| 87 |
|
| 88 |
int isNRingAtom(Size size, Atom& atom); |
| 89 |
|
| 90 |
|
| 91 |
bool isNonRingAtom(Atom& atom); |
| 92 |
|
| 93 |
bool checkGAFFProperties(Atom& atom, Atom& predecessor, APSTerm aps); |
| 94 |
|
| 95 |
|
| 96 |
APSMatcher(); |
| 97 |
~APSMatcher(); |
| 98 |
|
| 99 |
String printAPS(); |
| 100 |
|
| 101 |
|
| 102 |
bool operator() (Atom& atom, Atom& predecessor); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
std::vector < std::vector< APSTerm> > aps_terms; |
| 108 |
|
| 109 |
protected: |
| 110 |
bool hasBond_(Atom* atom, Atom* predecessor, int bond_type, int feature_number); |
| 111 |
}; |
| 112 |
|
| 113 |
struct State |
| 114 |
{ |
| 115 |
|
| 116 |
GAFFCESParser* current_parser; |
| 117 |
|
| 118 |
APSMatcher::APSType current_aps_type; |
| 119 |
int feature_number; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
class CESPredicate |
| 126 |
{ |
| 127 |
public: |
| 128 |
|
| 129 |
enum CESwildcards |
| 130 |
{ |
| 131 |
XA, XB, XC, XD, XX |
| 132 |
}; |
| 133 |
|
| 134 |
CESPredicate(GAFFCESParser* parser) |
| 135 |
: parent(0), |
| 136 |
atom_to_test(0), |
| 137 |
parser_(parser) |
| 138 |
{ |
| 139 |
initStringToWildcard(); |
| 140 |
}; |
| 141 |
|
| 142 |
virtual ~CESPredicate(); |
| 143 |
|
| 144 |
|
| 145 |
void initStringToWildcard(); |
| 146 |
std::map<String, CESwildcards> getStringToWildcard(); |
| 147 |
|
| 148 |
|
| 149 |
void addCESwildcardsConnectionPredicate(String wildcard, Size partners); |
| 150 |
|
| 151 |
void addCESwildcardsPredicate(String wildcard); |
| 152 |
|
| 153 |
void addCESelementPredicate(String name); |
| 154 |
|
| 155 |
void addCESelementConnectionPredicate(Size partners, String name); |
| 156 |
|
| 157 |
void addTruePredicate() const; |
| 158 |
|
| 159 |
|
| 160 |
virtual bool operator () (Atom& atom); |
| 161 |
|
| 162 |
virtual bool match(Atom&){return false;}; |
| 163 |
|
| 164 |
void clear(); |
| 165 |
|
| 166 |
|
| 167 |
bool alreadySeenThisAtom(Atom* atom); |
| 168 |
|
| 169 |
|
| 170 |
void addNewAND(); |
| 171 |
void addNewOR(APSMatcher::APSType aps, int feature_number); |
| 172 |
|
| 173 |
|
| 174 |
APSMatcher aps_matcher; |
| 175 |
|
| 176 |
|
| 177 |
std::vector<CESPredicate*> children; |
| 178 |
|
| 179 |
CESPredicate* parent; |
| 180 |
|
| 181 |
|
| 182 |
Atom* atom_to_test; |
| 183 |
protected: |
| 184 |
|
| 185 |
std::map<String, CESwildcards > stringToWildcard_; |
| 186 |
GAFFCESParser* parser_; |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
class CESelementConnectionPredicate : public CESPredicate |
| 191 |
{ |
| 192 |
public: |
| 193 |
CESelementConnectionPredicate(GAFFCESParser* parser) |
| 194 |
: CESPredicate(parser), |
| 195 |
numberOfPartners_(0), |
| 196 |
elementName_("NoName") |
| 197 |
{}; |
| 198 |
|
| 199 |
~CESelementConnectionPredicate(); |
| 200 |
|
| 201 |
void setNumberOfPartners(Size number); |
| 202 |
void setElementName(String name); |
| 203 |
Size getNumberOfPartners(); |
| 204 |
String getElementName(); |
| 205 |
|
| 206 |
bool match(Atom& atom); |
| 207 |
protected: |
| 208 |
Size numberOfPartners_; |
| 209 |
String elementName_; |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
class CESelementPredicate : public CESPredicate |
| 214 |
{ |
| 215 |
public: |
| 216 |
CESelementPredicate(GAFFCESParser* parser) |
| 217 |
: CESPredicate(parser), |
| 218 |
elementName_("NoName") |
| 219 |
{ |
| 220 |
}; |
| 221 |
|
| 222 |
~CESelementPredicate(); |
| 223 |
|
| 224 |
void setElementName(String name); |
| 225 |
String getElementName(); |
| 226 |
|
| 227 |
bool match(Atom& atom); |
| 228 |
protected: |
| 229 |
String elementName_; |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
class CESwildcardsPredicate : public CESPredicate |
| 234 |
{ |
| 235 |
public: |
| 236 |
CESwildcardsPredicate(GAFFCESParser* parser) |
| 237 |
: CESPredicate(parser) |
| 238 |
{}; |
| 239 |
|
| 240 |
~CESwildcardsPredicate(); |
| 241 |
|
| 242 |
void setWildcards(String new_wildcard); |
| 243 |
CESwildcards getWildcards(); |
| 244 |
|
| 245 |
bool matchWildcards(Atom& atom); |
| 246 |
|
| 247 |
bool match(Atom& atom); |
| 248 |
protected: |
| 249 |
CESwildcards wildcards_; |
| 250 |
}; |
| 251 |
|
| 252 |
|
| 253 |
class CESwildcardsConnectionPredicate : public CESPredicate |
| 254 |
{ |
| 255 |
public: |
| 256 |
CESwildcardsConnectionPredicate(GAFFCESParser* parser) |
| 257 |
: CESPredicate(parser), |
| 258 |
numberOfPartners_(0) |
| 259 |
{}; |
| 260 |
~CESwildcardsConnectionPredicate(); |
| 261 |
|
| 262 |
void setNumberOfPartners(Size number); |
| 263 |
void setWildcards(String new_wildcard); |
| 264 |
Size getNumberOfPartners(); |
| 265 |
CESwildcards getWildcards(); |
| 266 |
|
| 267 |
bool matchWildcards(Atom& atom); |
| 268 |
|
| 269 |
bool match(Atom& atom); |
| 270 |
protected: |
| 271 |
CESwildcards wildcards_; |
| 272 |
Size numberOfPartners_; |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
class TruePredicate : public CESPredicate |
| 277 |
{ |
| 278 |
public: |
| 279 |
TruePredicate(GAFFCESParser* parser) |
| 280 |
: CESPredicate(parser) |
| 281 |
{}; |
| 282 |
~TruePredicate() {}; |
| 283 |
|
| 284 |
bool match(Atom&) { return true; } |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
bool match(Atom& atom) const; |
| 289 |
|
| 290 |
|
| 291 |
void initElementSymbols(); |
| 292 |
const std::set<String>& getElementSymbols(); |
| 293 |
|
| 294 |
GAFFCESParser(); |
| 295 |
GAFFCESParser(const String& cesstring); |
| 296 |
~GAFFCESParser(); |
| 297 |
|
| 298 |
|
| 299 |
Size read(char* buf, Size max_size); |
| 300 |
|
| 301 |
static State state; |
| 302 |
|
| 303 |
|
| 304 |
TruePredicate root; |
| 305 |
|
| 306 |
CESPredicate* root_predicate; |
| 307 |
|
| 308 |
CESPredicate* current_root_predicate; |
| 309 |
|
| 310 |
CESPredicate* current_predicate; |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
bool parse(const String& cesstring); |
| 315 |
|
| 316 |
bool GAFFCESatomMatcher(Atom& atom, const String& cesstring); |
| 317 |
|
| 318 |
void startChildPredicates(); |
| 319 |
|
| 320 |
void endChildPredicates(); |
| 321 |
|
| 322 |
protected: |
| 323 |
|
| 324 |
String cesstring_; |
| 325 |
|
| 326 |
std::set<String> element_symbols_; |
| 327 |
|
| 328 |
Position read_start_; |
| 329 |
}; |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
#endif |