RDKit
Open-source cheminformatics and machine learning.
Rules.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright (C) 2020 Schrödinger, LLC
4 //
5 // @@ All Rights Reserved @@
6 // This file is part of the RDKit.
7 // The contents are covered by the terms of the BSD license
8 // which is included in the file license.txt, found at the root
9 // of the RDKit source tree.
10 //
11 #pragma once
12 
13 #include <initializer_list>
14 #include <vector>
15 
16 #include "Rule5.h"
17 #include "SequenceRule.h"
18 
19 namespace RDKit {
20 namespace CIPLabeler {
21 
22 class Rules : public SequenceRule {
23 
24 public:
25  Rules() = delete;
26 
27  Rules(std::initializer_list<SequenceRule *> rules) {
28  for (auto &rule : rules) {
29  add(rule);
30  }
31  }
32 
33  ~Rules() override {
34  for (auto &rule : d_rules) {
35  delete rule;
36  }
37  }
38 
39  void add(SequenceRule *rule) {
40  if (rule == nullptr) {
41  throw std::runtime_error("No sequence rule provided");
42  }
43  d_rules.push_back(rule);
44  rule->setSorter(new Sort(d_rules));
45  }
46 
47  int getNumSubRules() const { return d_rules.size(); }
48 
49  const Sort *getSorter() const override {
50  if (dp_sorter == nullptr) {
51  const_cast<Rules *>(this)->setSorter(new Sort(this));
52  }
53  return dp_sorter.get();
54  }
55 
56  int compare(const Edge *o1, const Edge *o2) const override {
57  // Try using each rules. The rules will expand the search exhaustively
58  // to all child substituents
59  for (const auto &rule : d_rules) {
60  // compare expands exhaustively across the whole graph
61  int value = rule->recursiveCompare(o1, o2);
62  if (value != 0) {
63  return value;
64  }
65  }
66  return 0;
67  }
68 
69  int getComparision(const Edge *a, const Edge *b, bool deep) const override {
70  (void)deep;
71 
72  // Try using each rules. The rules will expand the search exhaustively
73  // to all child substituents
74  for (const auto &rule : d_rules) {
75  // compare expands exhaustively across the whole graph
76  int value = rule->recursiveCompare(a, b);
77 
78  if (value != 0) {
79  return value;
80  }
81  }
82 
83  return 0;
84  }
85 
86 private:
87  std::vector<const SequenceRule *> d_rules;
88 };
89 
90 } // namespace CIPLabeler
91 } // namespace RDKit
~Rules() override
Definition: Rules.h:33
Rules(std::initializer_list< SequenceRule * > rules)
Definition: Rules.h:27
int getComparision(const Edge *a, const Edge *b, bool deep) const override
Definition: Rules.h:69
const Sort * getSorter() const override
Definition: Rules.h:49
void add(SequenceRule *rule)
Definition: Rules.h:39
int getNumSubRules() const
Definition: Rules.h:47
int compare(const Edge *o1, const Edge *o2) const override
Definition: Rules.h:56
std::unique_ptr< const Sort > dp_sorter
Definition: SequenceRule.h:60
void setSorter(const Sort *sorter)
Std stuff.
Definition: Abbreviations.h:17