LydiaSyft
GR1.h
1 //
2 // Created by shuzhu on 21/01/24.
3 //
4 
5 #ifndef LYDIASYFT_GR1_H
6 #define LYDIASYFT_GR1_H
7 
8 #include <fstream>
9 #include "string_utilities.h"
10 #include "VarMgr.h"
11 
12 namespace Syft {
52  struct GR1 {
53  std::vector<CUDD::BDD> env_justices;
54  std::vector<CUDD::BDD> agn_justices;
55 
59  static CUDD::BDD read_gr1_justice(const std::shared_ptr<VarMgr> &var_mgr,
60  std::istream &in,
61  std::size_t &line_number) {
62  std::string line;
63  CUDD::BDD justice = var_mgr->cudd_mgr()->bddOne();
64 
65  while (std::getline(in, line) && (line.find("Justice-End") == std::string::npos)) {
66 
67  CUDD::BDD clause = var_mgr->cudd_mgr()->bddZero();
68  if (line == "true") {
69  clause = var_mgr->cudd_mgr()->bddOne();
70  } else if (line == "false") {
71  clause = var_mgr->cudd_mgr()->bddZero();
72  } else {
73  std::vector<std::string> tokens;
74  trim(line); // remove leading and trailing whitespace
75  tokens = split(line, " ");
76  std::vector<std::string> variable_names;
77 
78  for (std::string token: tokens) {
79  std::string variable_name;
80  variable_name = (line.find("!") == std::string::npos) ? token : token.substr(2,
81  token.size() - 3);
82 // variable_name = to_upper_copy(variable_name); // turn variable names into uppercase to match the variable names in InputOutputPartition
83  variable_names.push_back(variable_name);
84  var_mgr->create_named_variables(variable_names);
85  CUDD::BDD variable_bdd = var_mgr->name_to_variable(variable_name);
86  clause = (line.find("!") == std::string::npos) ? (clause | variable_bdd) : (clause |
87  !variable_bdd);
88  }
89  }
90  justice = justice & clause;
91  ++line_number;
92  }
93  return justice;
94  }
95 
96  static std::runtime_error bad_file_format_exception(
97  std::size_t line_number) {
98  return std::runtime_error("Incorrect format in line " +
99  std::to_string(line_number) +
100  " of the GR(1) file.");
101  }
102 
109  static GR1 read_from_gr1_file(const std::shared_ptr<VarMgr> &var_mgr,
110  const std::string &gr1_filename) {
111  GR1 gr1;
112  std::ifstream in(gr1_filename);
113  std::size_t line_number = 0;
114  std::string line;
115  while (std::getline(in, line) && line != "End") {
116  if (line.find("Justice") != std::string::npos) {
117  if (line.find("Env") == std::string::npos && line.find("Agn") == std::string::npos) {
118  throw bad_file_format_exception(line_number);
119  }
120  ++line_number;
121  CUDD::BDD justice = read_gr1_justice(var_mgr, in, line_number);
122  if (line.find("Env") != std::string::npos) {
123  gr1.env_justices.push_back(justice);
124  } else {
125  gr1.agn_justices.push_back(justice);
126  }
127  }
128  ++line_number;
129  }
130  return gr1;
131  }
132  };
133 
134 
135 }
136 
137 #endif //LYDIASYFT_GR1_H
Definition: GR1.h:52
static GR1 read_from_gr1_file(const std::shared_ptr< VarMgr > &var_mgr, const std::string &gr1_filename)
Stores a GR(1) condition.
Definition: GR1.h:109
static CUDD::BDD read_gr1_justice(const std::shared_ptr< VarMgr > &var_mgr, std::istream &in, std::size_t &line_number)
Stores a player justice.
Definition: GR1.h:59