Version: 1.0
rule_set.h
Go to the documentation of this file.
1 // Copyright(c) 2019
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // *Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and / or other materials provided with the distribution.
13 //
14 // * Neither the name of GRAPHGEN nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifndef GRAPHGEN_RULE_SET_H_
30 #define GRAPHGEN_RULE_SET_H_
31 
32 #include <bitset>
33 #include <fstream>
34 #include <iterator>
35 #include <ostream>
36 #include <unordered_map>
37 
38 #include "pixel_set.h"
39 #include "utilities.h"
40 
41 struct rule {
42  unsigned long long frequency = 1;
43  std::bitset<131/*CTBE needs 131 bits*/> actions; // bitmapped
44 };
45 
46 
47 struct rule_set {
48  std::vector<std::string> conditions;
49  std::unordered_map<std::string, size_t> conditions_pos;
50  std::vector<std::string> actions;
51  std::unordered_map<std::string, size_t> actions_pos;
52  std::vector<rule> rules;
54 
55  rule_set() {}
56  rule_set(YAML::Node& node) {
57  Deserialize(node);
58  }
59 
60  static std::string binary(size_t u, size_t nbits, const std::string& separator = "") {
61  std::string s;
62  while (nbits-- > 0) {
63  s += ((u >> nbits) & 1) + 48;
64  s += separator;
65  }
66  return s;
67  }
68 
69  void AddCondition(const std::string& name) {
70  conditions.emplace_back(name);
71  conditions_pos[name] = conditions.size() - 1;
72  }
73 
74  void ClearConditions() {
75  conditions.clear();
76  conditions_pos.clear();
77  }
78 
79  void InitConditions(const pixel_set& ps) {
80  ps_ = ps;
82  for (uint i = 0; i < ps.pixels_.size(); ++i) {
83  AddCondition(ps.pixels_[i].name_);
84  }
85  }
86 
87  void AddAction(const std::string& action) {
88  actions.emplace_back(action);
89  actions_pos[action] = actions.size(); // Action 0 doesn't exist
90  }
91 
92  void ClearActions() {
93  actions.clear();
94  actions_pos.clear();
95  }
96 
97  void InitActions(std::vector<std::string> actions_) {
98  actions = std::move(actions_);
99  actions_pos.clear();
100  for (uint i = 0; i < actions.size(); ++i)
101  actions_pos[actions[i]] = i + 1; // Action 0 doesn't exist
102  }
103 
105  return 1 << conditions.size();
106  };
107 
108  template<typename T>
109  void generate_rules(T fn) {
110  uint nrules = 1 << conditions.size();
111  rules.resize(nrules);
112  for (uint i = 0; i < nrules; ++i) {
113  fn(*this, i);
114  }
115  }
116 
117  void print_rules(std::ostream& os) const {
118  copy(std::rbegin(conditions), std::rend(conditions), std::ostream_iterator<std::string>(os, "\t"));
119  os << "\n";
120  for (size_t i = 0; i < rules.size(); ++i) {
121  os << binary(i, conditions.size(), "\t") << ": ";
122  for (uint j = 0; j < actions.size(); ++j)
123  if (rules[i].actions[j])
124  os << actions[j] << "(" << actions_pos.at(actions[j]) << ")" << ", ";
125  os << "\n";
126  }
127  }
128 
129  uint get_condition(const std::string& s, uint rule) const {
130  return (rule >> conditions_pos.at(s)) & 1;
131  }
132 
133  void set_action(const std::string& s, uint rule) {
134  rules[rule].actions.set(actions_pos.at(s) - 1);
135  }
136  void SetFrequency(uint rule, uint frequency) {
137  // To improve: who ensures that there is correspondence in the rules representation
138  // used externally and those implemented by the rule_set?
139  rules[rule].frequency = frequency;
140  }
141 
142  void StoreFrequenciesOnFile(const std::string &output_file) const {
143  std::ofstream os(output_file);
144  if (!os.is_open()) {
145  return;
146  }
147 
148  for (size_t i = 0; i < rules.size(); ++i) {
149  os << rules[i].frequency << "\n";
150  }
151  }
152 
153  bool LoadFrequenciesFromFile(const std::string &file_path) {
154  std::ifstream is(file_path);
155  if (!is.is_open()) {
156  return false;
157  }
158 
159  int i = 0;
160  unsigned long long freq;
161  while (is >> freq) {
162  rules[i].frequency = freq;
163  ++i;
164  }
165 
166  return true;
167  }
168 
169  YAML::Node Serialize() const {
170  YAML::Node rs_node;
171  rs_node["pixel_set"] = ps_.Serialize();
172 
173  for (const auto& c : conditions) {
174  rs_node["conditions"].push_back(c);
175  }
176 
177  for (const auto& a : actions) {
178  rs_node["actions"].push_back(a);
179  }
180 
181  bool with_freq = false;
182  for (unsigned i = 0; i < rules.size(); ++i) {
183  for (uint j = 0; j < actions.size(); ++j) {
184  if (rules[i].actions[j]) {
185  rs_node["rules"][i].push_back(actions_pos.at(actions[j]));
186  }
187  }
188  rs_node["frequencies"].push_back(rules[i].frequency);
189  if (rules[i].frequency != 1) {
190  with_freq = true;
191  }
192  }
193 
194  if (!with_freq) {
195  rs_node.remove("frequencies");
196  }
197 
198  return rs_node;
199  }
200 
201  void Deserialize(const YAML::Node& rs_node) {
202  /*std::vector<rule> rules;*/
203 
204  ps_ = pixel_set(rs_node["pixel_set"]);
205 
206  for (unsigned i = 0; i < rs_node["conditions"].size(); ++i) {
207  AddCondition(rs_node["conditions"][i].as<std::string>());
208  }
209 
210  for (unsigned i = 0; i < rs_node["actions"].size(); ++i) {
211  AddAction(rs_node["actions"][i].as<std::string>());
212  }
213 
214  rules.resize(rs_node["rules"].size());
215  for (unsigned i = 0; i < rs_node["rules"].size(); ++i) {
216  for (unsigned j = 0; j < rs_node["rules"][i].size(); ++j) {
217  rules[i].actions.set(rs_node["rules"][i][j].as<int>() - 1);
218  if (auto& frequencies = rs_node["frequencies"]) {
219  rules[i].frequency = frequencies[i].as<unsigned long long>();
220  }
221  }
222  }
223 
224  }
225 
226 };
227 
228 struct rule_wrapper {
231  rule_wrapper(rule_set& rs, uint i) : rs_{ rs }, i_{ i } {}
232 
233  bool operator[](const std::string& s) const {
234  return rs_.get_condition(s, i_) != 0;
235  }
236  void operator<<(const std::string& s) {
237  rs_.set_action(s, i_);
238  }
239  bool has_actions() {
240  return rs_.rules[i_].actions != 0;
241  }
242 };
243 
244 #endif // !GRAPHGEN_RULE_SET_H_
uint32_t uint
std::vector< pixel > pixels_
Definition: pixel_set.h:88
YAML::Node Serialize() const
Definition: pixel_set.h:136
void generate_rules(T fn)
Definition: rule_set.h:109
void Deserialize(const YAML::Node &rs_node)
Definition: rule_set.h:201
uint get_condition(const std::string &s, uint rule) const
Definition: rule_set.h:129
pixel_set ps_
Definition: rule_set.h:53
uint GetNumberOfRules() const
Definition: rule_set.h:104
void AddAction(const std::string &action)
Definition: rule_set.h:87
YAML::Node Serialize() const
Definition: rule_set.h:169
void StoreFrequenciesOnFile(const std::string &output_file) const
Definition: rule_set.h:142
void ClearConditions()
Definition: rule_set.h:74
void print_rules(std::ostream &os) const
Definition: rule_set.h:117
static std::string binary(size_t u, size_t nbits, const std::string &separator="")
Definition: rule_set.h:60
bool LoadFrequenciesFromFile(const std::string &file_path)
Definition: rule_set.h:153
void set_action(const std::string &s, uint rule)
Definition: rule_set.h:133
std::vector< rule > rules
Definition: rule_set.h:52
std::unordered_map< std::string, size_t > conditions_pos
Definition: rule_set.h:49
void ClearActions()
Definition: rule_set.h:92
rule_set()
Definition: rule_set.h:55
void SetFrequency(uint rule, uint frequency)
Definition: rule_set.h:136
std::vector< std::string > actions
Definition: rule_set.h:50
void InitConditions(const pixel_set &ps)
Definition: rule_set.h:79
rule_set(YAML::Node &node)
Definition: rule_set.h:56
std::unordered_map< std::string, size_t > actions_pos
Definition: rule_set.h:51
void InitActions(std::vector< std::string > actions_)
Definition: rule_set.h:97
std::vector< std::string > conditions
Definition: rule_set.h:48
void AddCondition(const std::string &name)
Definition: rule_set.h:69
bool has_actions()
Definition: rule_set.h:239
rule_set & rs_
Definition: rule_set.h:229
rule_wrapper(rule_set &rs, uint i)
Definition: rule_set.h:231
bool operator[](const std::string &s) const
Definition: rule_set.h:233
void operator<<(const std::string &s)
Definition: rule_set.h:236
Definition: rule_set.h:41
std::bitset< 131 > actions
Definition: rule_set.h:43
unsigned long long frequency
Definition: rule_set.h:42