RDKit
Open-source cheminformatics and machine learning.
QueryBond.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2017 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 #include <RDGeneral/export.h>
11 #ifndef _RD_QUERYBOND_H
12 #define _RD_QUERYBOND_H
13 
14 #include <Query/QueryObjects.h>
15 #include "Bond.h"
16 #include "QueryOps.h"
17 
18 namespace RDKit {
19 
20 //! Class for storing Bond queries
21 /*!
22  QueryBond objects are derived from Bond objects, so they can be
23  added to molecules and the like, but they have much fancier
24  querying capabilities.
25 
26  */
27 
29  public:
31 
32  QueryBond() : Bond(), dp_query(NULL){};
33  //! initialize with a particular bond order
34  explicit QueryBond(BondType bT);
35  //! initialize from a bond
36  explicit QueryBond(const Bond &other)
37  : Bond(other), dp_query(makeBondOrderEqualsQuery(other.getBondType())){};
38  QueryBond(const QueryBond &other)
39  : Bond(other), dp_query(other.dp_query->copy()){};
40 
41  ~QueryBond();
42 
43  //! returns a copy of this query, owned by the caller
44  virtual Bond *copy() const;
45 
46  QueryBond &operator=(const QueryBond &other);
47 
48  //! sets the BondType of this query:
49  void setBondType(BondType bT);
50  //! sets the BondDir of this query:
51  void setBondDir(BondDir bD);
52 
53  //! returns true if we match Bond \c what
54  bool Match(Bond const *what) const;
55 
56  //! returns true if our query details match those of QueryBond \c what
57  bool QueryMatch(QueryBond const *what) const;
58 
59  // This method can be used to distinguish query bonds from standard bonds
60  bool hasQuery() const { return dp_query != 0; };
61 
62  //! returns our current query
63  QUERYBOND_QUERY *getQuery() const { return dp_query; };
64  //! replaces our current query with the value passed in
65  void setQuery(QUERYBOND_QUERY *what) {
66  // free up any existing query (Issue255):
67  delete dp_query;
68  dp_query = what;
69  };
70 
71  //! expands our current query
72  /*!
73  \param what the Queries::Query to be added. The ownership of
74  the query is passed to the current object, where it
75  might be deleted, so that the pointer should not be
76  used again in the calling code.
77  \param how the operator to be used in the expansion
78  \param maintainOrder (optional) flags whether the relative order of
79  the queries needs to be maintained, if this is
80  false, the order is reversed
81 
82  <b>Notes:</b>
83  - \c what should probably be constructed using one of the functions
84  defined in QueryOps.h
85  - the \c maintainOrder option can be useful because the combination
86  operators short circuit when possible.
87 
88  */
89  void expandQuery(QUERYBOND_QUERY *what,
91  bool maintainOrder = true);
92 
93  protected:
95 };
96 
97 namespace detail {
98 inline std::string qhelper(Bond::QUERYBOND_QUERY *q, unsigned int depth) {
99  std::string res;
100  if (q) {
101  for (unsigned int i = 0; i < depth; ++i) res += " ";
102  res += q->getFullDescription() + "\n";
104  ci != q->endChildren(); ++ci) {
105  res += qhelper((*ci).get(), depth + 1);
106  }
107  }
108  return res;
109 }
110 } // namespace detail
111 inline std::string describeQuery(const Bond *bond) {
112  PRECONDITION(bond, "bad bond");
113  std::string res = "";
114  if (bond->hasQuery()) {
115  res = detail::qhelper(bond->getQuery(), 0);
116  }
117  return res;
118 }
119 }; // namespace RDKit
120 
121 #endif
RDKit::Bond::getQuery
virtual QUERYBOND_QUERY * getQuery() const
NOT CALLABLE.
Queries::Query::CHILD_VECT_CI
CHILD_VECT::const_iterator CHILD_VECT_CI
Definition: Query.h:53
RDKit::QueryBond::setQuery
void setQuery(QUERYBOND_QUERY *what)
replaces our current query with the value passed in
Definition: QueryBond.h:65
Queries::COMPOSITE_AND
@ COMPOSITE_AND
Definition: QueryObjects.h:36
RDKit::QueryBond::QUERYBOND_QUERY
Queries::Query< int, Bond const *, true > QUERYBOND_QUERY
Definition: QueryBond.h:30
QueryObjects.h
Pulls in all the query types.
Bond.h
RDKit::Bond
class for representing a bond
Definition: Bond.h:47
Queries::CompositeQueryType
CompositeQueryType
Definition: QueryObjects.h:36
RDKit::describeQuery
std::string describeQuery(const Atom *atom)
Definition: QueryAtom.h:107
RDKit::QueryBond::hasQuery
bool hasQuery() const
Definition: QueryBond.h:60
RDKit::QueryBond
Class for storing Bond queries.
Definition: QueryBond.h:28
RDKit::detail::qhelper
std::string qhelper(Atom::QUERYATOM_QUERY *q, unsigned int depth)
Definition: QueryAtom.h:94
RDKit::QueryBond::getQuery
QUERYBOND_QUERY * getQuery() const
returns our current query
Definition: QueryBond.h:63
RDKIT_GRAPHMOL_EXPORT
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:307
RDKit::makeBondOrderEqualsQuery
RDKIT_GRAPHMOL_EXPORT BOND_EQUALS_QUERY * makeBondOrderEqualsQuery(Bond::BondType what)
returns a Query for matching bond orders
QueryOps.h
Queries::Query::endChildren
CHILD_VECT_CI endChildren() const
returns an iterator for the end of our child vector
Definition: Query.h:105
RDKit
Std stuff.
Definition: Atom.h:30
Queries::Query::getFullDescription
virtual std::string getFullDescription() const
returns a fuller text description
Definition: Query.h:78
Queries::Query::beginChildren
CHILD_VECT_CI beginChildren() const
returns an iterator for the beginning of our child vector
Definition: Query.h:103
RDKit::Bond::hasQuery
virtual bool hasQuery() const
Definition: Bond.h:245
RDKit::QueryBond::QueryBond
QueryBond()
Definition: QueryBond.h:32
PRECONDITION
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
Queries::Query
Base class for all queries.
Definition: Query.h:46
RDKit::QueryBond::dp_query
QUERYBOND_QUERY * dp_query
Definition: QueryBond.h:94
RDKit::QueryBond::QueryBond
QueryBond(const Bond &other)
initialize from a bond
Definition: QueryBond.h:36
RDKit::QueryBond::QueryBond
QueryBond(const QueryBond &other)
Definition: QueryBond.h:38
export.h