tran.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // =====================================================================================
  2. //
  3. // Filename: tran.cc
  4. //
  5. // Description: transform input of acts to cnf
  6. //
  7. // Version: 1.0
  8. // Created: 2019年 01月 05日 星期六 07:43:30 CST
  9. // Revision: none
  10. // Compiler: g++
  11. //
  12. // Author: Jinkun Lin, jkunlin@gmail.com
  13. // Organization: School of EECS, Peking University
  14. //
  15. // =====================================================================================
  16. #include <fstream>
  17. #include <iostream>
  18. #include <map>
  19. #include <sstream>
  20. #include <string>
  21. #include <vector>
  22. using namespace std;
  23. struct Literal {
  24. char sign;
  25. int value;
  26. Literal(char s, int v) : sign(s), value(v) {}
  27. };
  28. struct Clause {
  29. vector<Literal> lits;
  30. };
  31. int main(int argc, char const *argv[]) {
  32. if (argc != 2) {
  33. cout << "usage: " << endl;
  34. return 1;
  35. }
  36. ifstream infile(argv[1]);
  37. if (!infile) {
  38. cout << "infile error" << endl;
  39. return 1;
  40. }
  41. string line;
  42. while (getline(infile, line)) {
  43. if (line.find("Parameter") != line.npos) {
  44. break;
  45. }
  46. }
  47. map<string, int> parameter;
  48. vector<map<string, int>> value;
  49. int cur_pid = 0;
  50. int cur_vid = 0;
  51. string tmp_str;
  52. while (getline(infile, line)) {
  53. if (line.find("Constraint") != line.npos) {
  54. break;
  55. }
  56. istringstream is(line);
  57. string p;
  58. is >> p >> tmp_str;
  59. if (!is) {
  60. if (line.size() != 1) {
  61. abort();
  62. }
  63. continue;
  64. }
  65. parameter.insert(make_pair(p, cur_pid++));
  66. value.push_back(map<string, int>());
  67. string v;
  68. while (is >> v) {
  69. if (v[v.size() - 1] == ',') {
  70. v.pop_back();
  71. }
  72. value[parameter[p]].insert(make_pair(v, cur_vid++));
  73. }
  74. }
  75. #ifndef NDEBUG
  76. // for (auto p_pair : parameter) {
  77. // cout << p_pair.first << ": ";
  78. // for (auto v_pair : value[p_pair.second]) {
  79. // cout << v_pair.first << ", ";
  80. // }
  81. // cout << endl;
  82. // }
  83. //
  84. // for (auto p_pair : parameter) {
  85. // cout << p_pair.second << ": ";
  86. // for (auto v_pair : value[p_pair.second]) {
  87. // cout << v_pair.second << ", ";
  88. // }
  89. // cout << endl;
  90. // }
  91. cout << 2 << endl;
  92. cout << parameter.size() << endl;
  93. auto iter = parameter.begin();
  94. cout << value[iter->second].size();
  95. for (++iter; iter != parameter.end(); ++iter) {
  96. cout << ' ' << value[iter->second].size();
  97. }
  98. cout << endl;
  99. #endif
  100. vector<Clause> formular;
  101. char tmp_ch;
  102. while (getline(infile, line)) {
  103. istringstream is(line);
  104. is >> tmp_str >> tmp_ch;
  105. string p, v;
  106. char sign;
  107. Clause c;
  108. do {
  109. is >> p >> tmp_str >> tmp_ch;
  110. // sign
  111. if (tmp_str == "!=") {
  112. sign = '-';
  113. } else if (tmp_str == "=") {
  114. sign = '+';
  115. } else {
  116. cout << "error operator" << endl;
  117. return 1;
  118. }
  119. // value
  120. is >> v;
  121. if (v[v.size() - 1] == '"') {
  122. v.pop_back();
  123. } else if (v[v.size() - 1] == ')' && v[v.size() - 2] == '"') {
  124. v.pop_back();
  125. v.pop_back();
  126. } else {
  127. cout << "format error: " << v << endl;
  128. return 1;
  129. }
  130. c.lits.push_back(Literal(sign, value[parameter[p]][v]));
  131. // operator
  132. is >> tmp_str;
  133. } while (tmp_str == "||");
  134. formular.push_back(c);
  135. }
  136. #ifndef NDEBUG
  137. cout << formular.size() << endl;
  138. for (auto c : formular) {
  139. cout << c.lits.size() << endl;
  140. cout << c.lits[0].sign << ' ' << c.lits[0].value;
  141. for (int i = 1; i < (int)c.lits.size(); ++i) {
  142. cout << ' ' << c.lits[i].sign << ' ' << c.lits[i].value;
  143. }
  144. cout << endl;
  145. }
  146. #endif
  147. return 0;
  148. }