PageRenderTime 22ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Expertbot/regles/generateurRegles2.cpp

http://machiabot.googlecode.com/
C++ | 612 lines | 453 code | 92 blank | 67 comment | 122 complexity | 6a6bc762f604090c0e6dc208d24d1486 MD5 | raw file
  1. //g++ generateurRegles2.cpp -o genRegle
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5. #include <string>
  6. using namespace std;
  7. string regle= "";
  8. int compteur;
  9. struct var {
  10. string nom;
  11. string type;
  12. };
  13. vector<struct var> variables;
  14. // int i;
  15. void parserLigne(string ligne,int i);
  16. void trucDifferent(string ligne, int i) {
  17. //cout<<"welcome dans truc différent: "<<i<<endl;
  18. string var1,var2= "";
  19. //prédicat 1
  20. regle+="\n \n";
  21. while ((ligne[i]!='^')&&(ligne[i]!='-')) {
  22. // cout<<"ligne[i]: "<<ligne[i]<<endl;
  23. if (ligne[i]!=' ') {
  24. regle+=ligne[i];
  25. if (ligne[i]=='(') {
  26. regle+='&';
  27. int k = i+1;
  28. while (ligne[k]!=')') {
  29. var1+= ligne[k];
  30. k++;
  31. }
  32. }
  33. }
  34. i++;
  35. }
  36. // cout<<"var1: "<<var1<<endl;
  37. regle+="; ";
  38. i++;
  39. //prédicat 2
  40. while ((ligne[i]!='^')&&(ligne[i]!='-')) {
  41. if (ligne[i]!=' ') {
  42. regle+=ligne[i];
  43. if (ligne[i]=='(') {
  44. regle+='&';
  45. int k = i+1;
  46. while (ligne[k]!=')') {
  47. var2+= ligne[k];
  48. k++;
  49. }
  50. }
  51. }
  52. i++;
  53. }
  54. //cout<<"var2: "<<var2<<endl;
  55. i++;
  56. //cout<<ligne[i]<<endl;
  57. while (ligne[i]!='^') {
  58. i++;
  59. }
  60. i++;
  61. //sleep(1000);
  62. regle+="; ";
  63. //ajout des variables en dĂŠclaration
  64. var v1;
  65. v1.nom = var1;
  66. var v2;
  67. v2.nom = var2;
  68. int l;
  69. for ( l = 0; l <variables.size(); l++) {
  70. if (v1.nom==variables[l].nom) break;
  71. }
  72. if (l==variables.size()) {
  73. v1.type="int";
  74. //cout<<"on ajoute la variable: "<<varTemp[a].nom<<endl;
  75. variables.push_back(v1);
  76. }
  77. for ( l = 0; l <variables.size(); l++) {
  78. if (v2.nom==variables[l].nom) break;
  79. }
  80. if (l==variables.size()) {
  81. v2.type="int";
  82. //cout<<"on ajoute la variable: "<<varTemp[a].nom<<endl;
  83. variables.push_back(v2);
  84. }
  85. regle+="\n vector <int> A";
  86. regle+=(char)compteur+65;
  87. regle+=" = "+var1+";"+"\n vector <int> B";
  88. regle+=(char)compteur+65;
  89. regle+=" = "+var2+"; \n";
  90. regle+="for (int i = 0; i< (int)A";
  91. regle+=(char)compteur+65;
  92. regle+=".size(); i++){ \n "+var1+".resize(0); "+var1+".push_back(A";
  93. regle+=(char)compteur+65;
  94. regle+="[i]); \n for (int j = 0; j<(int) B";
  95. regle+=(char)compteur+65;
  96. regle+=".size(); j++){ \n ";
  97. regle+="if (i!=j) {";
  98. regle+=var2+".resize(0); "+var2+".push_back(B";
  99. regle+=(char)compteur+65;
  100. regle+="[j]); \n";
  101. compteur++;
  102. //cout<<"on va rappeller parser ligne sur "<<i<<" - "<<ligne[i]<<endl;
  103. parserLigne(ligne,i);
  104. regle+="} \n } \n} ";
  105. }
  106. void trucNormal(string ligne, int i) {
  107. bool conclusion;
  108. // string ligne;
  109. string courant;
  110. vector<var> varTemp;
  111. string tampon;
  112. // int i;
  113. int a;
  114. bool vectorDeclare;
  115. bool init = false;
  116. string nomFait;
  117. vectorDeclare=false;
  118. conclusion=false;
  119. courant = "";
  120. tampon="";
  121. nomFait="";
  122. // i = 0;
  123. a=0;
  124. while (i<ligne.size()) {
  125. if (ligne[i]==' ') {
  126. i++;
  127. } else {
  128. if (!init) {
  129. regle+="if(";
  130. init = true;
  131. }
  132. var vide;
  133. varTemp.push_back(vide);
  134. if (ligne[i]=='(') { //Parenthese ouvrante
  135. if (conclusion && !vectorDeclare) {
  136. vectorDeclare = true;
  137. int j =i;
  138. while (ligne[j]==' ') {
  139. j++;
  140. }
  141. if ((ligne[j+1]=='J')&&(ligne[j+2]=='_')) {
  142. regle+=" vector<vector<int> > listeParam; ";
  143. }
  144. }
  145. if (courant!="non") {
  146. if (conclusion) {
  147. tampon+=courant;
  148. } else {
  149. regle+=courant;
  150. }
  151. }
  152. if ((!conclusion)&&courant=="testerFait") {
  153. string varFait="";
  154. regle+="(";
  155. i++;
  156. while (ligne[i]!='(') { //le nom du fait
  157. regle+=ligne[i];
  158. i++;
  159. }
  160. i++;
  161. bool yaargs=false;
  162. while (ligne[i]!=')') {
  163. if (ligne[i]!=' ') {
  164. yaargs=true;
  165. }
  166. }
  167. if (yaargs) {
  168. regle+=", ";
  169. }
  170. while (ligne[i]!=')') { //ses arguments
  171. if ((ligne[i]==',')&&(varFait.size()>0)) {
  172. regle+="&"+varFait+", ";
  173. varFait="";
  174. } else {
  175. varFait+=ligne[i];
  176. }
  177. i++;
  178. }
  179. if (varFait.size()>0) {
  180. regle+="&"+varFait+")";
  181. } else {
  182. regle+=')';
  183. }
  184. i+=1;
  185. } else if ((!conclusion)&&courant=="non") { //Cas dĂŠlicat du NON
  186. //test si c'est un fait:
  187. int k=i;
  188. k++;
  189. string nomfct;
  190. while (ligne[k]!='(') {
  191. nomfct+=ligne[k];
  192. k++;
  193. }
  194. if (nomfct=="testerFait") {
  195. i=k;
  196. regle+="!"+nomfct;
  197. string varFait="";
  198. //while (!(ligne[i]==')'&&ligne[i-1]==')')){i++;}
  199. regle+="(";
  200. i++;
  201. // cout<<"ligne: "<<ligne[i]<<endl;
  202. //regle+="\"";
  203. while (ligne[i]!='(') { //le nom du fait
  204. regle+=ligne[i];
  205. i++;
  206. }
  207. i++;
  208. bool yaargs=false;
  209. while (ligne[i]!=')') {
  210. if (ligne[i]!=' ') {
  211. yaargs=true;
  212. }
  213. i++;
  214. }
  215. if (yaargs) {
  216. regle+=", ";
  217. i--;
  218. }
  219. while (ligne[i]!=')') { //ses arguments
  220. // cout<<"ligne: "<<ligne[i]<<endl;
  221. if ((ligne[i]==',')&&(varFait.size()>0)) {
  222. regle+="&"+varFait+", ";
  223. varFait="";
  224. } else {
  225. varFait+=ligne[i]; /*cout<<"ON A TROUVE UN ARGUMENT:" <<varFait<<endl;*/
  226. }
  227. i++;
  228. }
  229. if (varFait.size()>0) {
  230. regle+="&"+varFait+")";
  231. } else {
  232. regle+=')';
  233. }
  234. i+=2;
  235. // varFait=="";
  236. } else { // si le non ne contient pas de fait
  237. string machiaTemp;
  238. machiaTemp+="(";
  239. vector<string> variables;
  240. string varT="";
  241. i++;
  242. while (ligne[i]!='(') {
  243. machiaTemp+=ligne[i];
  244. i++;
  245. }
  246. machiaTemp+=ligne[i];
  247. i++;
  248. while (ligne[i]!=')') {
  249. if ( (!(ligne[i]>='0'&&ligne[i]<='9')) && (ligne[i-1]=='(' ||ligne[i-1]==',' )) {
  250. machiaTemp+='&';
  251. }
  252. machiaTemp+=ligne[i];
  253. if ((ligne[i]==',')&&(!(varT[0]>='0'&&varT[0]<='9'))) {
  254. variables.push_back(varT);
  255. varT="";
  256. } else if (ligne[i]==' ') {} else {
  257. varT+=ligne[i];
  258. }
  259. i++;
  260. }
  261. machiaTemp+=ligne[i];
  262. /* for (int k=0;k<variables.size();k++){
  263. regle+='&';
  264. regle+=variables[k];
  265. if(k<variables.size()-1){regle+=',';}
  266. }
  267. regle+=')';*/
  268. i++;
  269. if ((varT.size()>0)&&(!(varT[0]>='0'&&varT[0]<='9'))) {
  270. variables.push_back(varT);
  271. varT="";
  272. }
  273. machiaTemp+="|| true) && inverser(";
  274. for (int k=0;k<variables.size();k++) {
  275. machiaTemp+='&';
  276. machiaTemp+=variables[k];
  277. if (k<variables.size()-1) {
  278. machiaTemp+=',';
  279. }
  280. }
  281. machiaTemp+=')';
  282. regle+="initNon(";
  283. for (int k=0;k<variables.size();k++) {
  284. // regle+='&';
  285. regle+=variables[k];
  286. if (k<variables.size()-1) {
  287. regle+=',';
  288. }
  289. }
  290. regle+=") && ";
  291. regle+=machiaTemp;
  292. }
  293. } else if (!conclusion) {
  294. while (i<ligne.size()&&(ligne[i-1]!=')')) {
  295. if (ligne[i]!=','&&ligne[i]!='('&&ligne[i]!=')'&&ligne[i]!=' '&&(!(ligne[i]>='0'&&ligne[i]<='9'))&&(courant!="testerFait")) {
  296. regle+='&';
  297. }
  298. regle+=ligne[i];
  299. if ((ligne[i]!='(')&&(ligne[i]!=' ')&&(!(ligne[i]>='0'&&ligne[i]<='9'))&&(courant!="testerFait")/*&&varTemp[a].nom.size()>0*/) {
  300. if ((ligne[i]==',')||(ligne[i]==')')) {
  301. int j;
  302. // cout<<"tentative d'ajout de "<< varTemp[a].nom<< " de taille: "<<varTemp[a].nom.size()<<endl;
  303. if (varTemp[a].nom.size()>0) {
  304. for (j = 0; j <variables.size(); j++) {
  305. if (varTemp[a].nom==variables[j].nom) break;
  306. }
  307. if (j==variables.size()) {
  308. varTemp[a].type="int";
  309. //cout<<"on ajoute la variable: "<<varTemp[a].nom<<endl;
  310. variables.push_back(varTemp[a]);
  311. }
  312. }
  313. var vide;
  314. varTemp.push_back(vide);
  315. a++;
  316. } else if (courant!="testerFait") {
  317. // cout<<" ajout d'un truc dans varTemp: "<<ligne[i]<<" "<<(ligne[i]==' ')<<endl;
  318. varTemp[a].nom += ligne[i];
  319. }
  320. }
  321. i++;
  322. }
  323. int j;
  324. for (j = 0; j <variables.size(); j++) {
  325. if (varTemp[a].nom==variables[j].nom) break;
  326. }
  327. if ((j==variables.size())&&(!(varTemp[a].nom[0]>='0'&&varTemp[a].nom[0]<='9'))) {
  328. }
  329. varTemp.clear();
  330. a=0;
  331. // regle+=')';
  332. } else { //si on est dans la conclusion
  333. if ((courant=="ajouterFait")||(courant=="retirerFait")) {
  334. // tampon+=courant;//+"(";
  335. i++;
  336. tampon+='(';
  337. string faitOriginal;
  338. nomFait+="F_";
  339. tampon+="F_";
  340. while (i<ligne.size()&&ligne[i]!='(') {
  341. tampon+=ligne[i];
  342. nomFait+=ligne[i];
  343. faitOriginal+=ligne[i];
  344. i++;
  345. }
  346. //test si le fait n'a pas dĂŠja ĂŠtĂŠ dĂŠclarĂŠ
  347. bool dejaDeclare = false;
  348. for (int j = 0; j<variables.size(); j++) {
  349. if (nomFait==variables[j].nom) {
  350. dejaDeclare=true;
  351. }
  352. }
  353. if (!dejaDeclare) {
  354. var fait;
  355. fait.nom=nomFait;
  356. variables.push_back(fait);
  357. }
  358. // cout<<"OUKONESTLA: "<<ligne[i]<<ligne[i+1]<<ligne[i+2]<<ligne[i+3]<<" FAIT: "<<nomFait<<endl;
  359. regle+=nomFait+".id = "+faitOriginal+";";
  360. // varTemp[a].type="int";
  361. // variables.push_back(varTemp[a]);
  362. while (i<ligne.size()&&ligne[i]!=')') {
  363. if ((ligne[i]!='(')&&(ligne[i]!=',')) {
  364. regle+=nomFait+".args = ";
  365. // regle+='&';
  366. while (ligne[i]!=')'&&(ligne[i]!=',')) {
  367. regle+=ligne[i];
  368. i++;
  369. }
  370. regle+="; ";
  371. // regle+=ligne[i];
  372. }
  373. i++;
  374. }
  375. tampon+="); ";
  376. regle+=tampon;
  377. i+=2;
  378. } else if (courant!="jouer") {
  379. // regle+=",\""+courant+"\"";
  380. tampon+=",\""+courant+"\"";
  381. // regle+=",{";
  382. while (i<ligne.size()&&ligne[i]!=')') {
  383. if ((ligne[i]!='(')&&(ligne[i]!=',')) {
  384. regle+="listeParam.push_back(";
  385. // regle+='&';
  386. while (ligne[i]!=')'&&(ligne[i]!=',')) {
  387. regle+=ligne[i];
  388. if (ligne[i+1]!=')'&&ligne[i+1]!=',') {
  389. i++;
  390. } else {
  391. break;
  392. }
  393. }
  394. regle+="); ";
  395. // regle+=ligne[i];
  396. }
  397. i++;
  398. }
  399. // bidule.push_back(x);
  400. regle+=tampon;
  401. regle+=",listeParam,";
  402. i+=2;
  403. // regle+="},";
  404. while ((ligne[i]==' ')||ligne[i]==',') {
  405. i++;
  406. }
  407. //cout<<regle[i]<<" - "<<(regle[i]==' ')<<endl;
  408. while (ligne[i]!=')') {
  409. regle+=ligne[i];
  410. i++;
  411. }
  412. regle+=");";
  413. //i++;
  414. } else {
  415. // regle+='(';
  416. tampon+="(regles::";
  417. }
  418. }
  419. courant = "";
  420. } else if (ligne[i]=='^') { // ET
  421. regle+=" && ";
  422. i++;
  423. } else if (ligne[i]=='-') { //Fleche vers conclusion
  424. conclusion = true;
  425. regle+=") \n {";
  426. i++;
  427. } else if (ligne[i]==' ') {
  428. } else {
  429. courant+=ligne[i];
  430. }
  431. i++;
  432. }
  433. }
  434. if (init) {
  435. regle+="} \n \n traceTemp.clear(); \n \n";
  436. }
  437. init=false;
  438. }
  439. void parserLigne(string ligne, int j) {
  440. //cout<<"parser ligne : "<<ligne<<" : "<<j<<endl;
  441. int i=j;
  442. bool fini = false;
  443. string nomTemp="";
  444. //recherche d'un "diffĂŠrent"
  445. while (i<ligne.size() && !fini && nomTemp!="different") {
  446. // cout<<"in da 1er while: "<<i<<" - "<<ligne[i]<<endl;
  447. nomTemp="";
  448. while ((ligne[i]!='(')&&(ligne[i]!='>')) {
  449. //cout<<"in da 2eme while: "<<i<<" - "<<ligne[i]<<endl;
  450. if (ligne[i]!=' ') {
  451. nomTemp+=ligne[i];
  452. }
  453. i++;
  454. }
  455. // cout<<"sortie du while: "<<ligne[i]<<endl;
  456. if (ligne[i]=='>') {
  457. fini = true; //si on est vers la conclusion
  458. } else {
  459. while (ligne[i]!='^'&&ligne[i]!='-') {
  460. i++;
  461. }
  462. i++;
  463. }
  464. }
  465. //cout<<"fin recherche diffĂŠrent: "<<nomTemp<<endl;
  466. if (nomTemp=="different") {
  467. // cout<<"on lance truc différent a la ligne "<<j<<endl;
  468. trucDifferent(ligne,j);
  469. }
  470. else {
  471. trucNormal(ligne,j);
  472. }
  473. // cout<<"fin parser"<<endl;
  474. }
  475. int main() {
  476. ifstream Reglestxt("regles.txt", ios::in); // on ouvre le fichier en lecture
  477. ofstream Reglescpp("predicat.cpp", ios::out | ios::trunc); //dĂŠclaration du flux et ouverture du fichier
  478. /*
  479. if(!fichierRegles) // si l'ouverture n'a pas rĂŠussi
  480. { cout << "Erreur Ă  l'ouverture !" << endl;}
  481. // instructions
  482. else{
  483. fichier.close(); // on referme le fichier
  484. }*/
  485. Reglescpp<<"#include <vector>"<<endl<<"#include \"regles.h\" "<<endl<<endl;
  486. Reglescpp<<"//l'implementation des fonctions utiles aux regles"<<endl<<endl<<endl;
  487. Reglescpp<<"using namespace std;"<<endl<<endl<<endl;
  488. // Reglescpp<<"regles::regles(Goban * gb)\n{\n g=gb;\n}"<<endl<<endl;
  489. Reglescpp<<"bool regles::executerRegles(){ \n"<<"Conclusions.resize(0); \n"<<endl;
  490. string ligne= "";
  491. compteur=0;
  492. while (getline(Reglestxt,ligne)) {
  493. cout<<ligne<<endl;
  494. int i = 0;
  495. while (ligne[i]==' ') {
  496. i++;
  497. }
  498. if (!(ligne[i]=='/'&&ligne[i+1]=='/')) { // si la ligne n'est pas commentée
  499. regle+="sTrace = \" ";
  500. regle+=ligne;
  501. regle+="\";";
  502. regle+="\n \n";
  503. parserLigne(ligne, i);
  504. }
  505. }
  506. for (int j =0; j<variables.size(); j++) {
  507. if (variables[j].nom.size()>1&&variables[j].nom[0]=='F'&&variables[j].nom[1]=='_') { //on a affaire a un fait
  508. Reglescpp<<"machiaFait "<<variables[j].nom<<";"<<endl;
  509. } else { //cout<<"nom vecteur: "<<variables[j].nom<<endl;
  510. Reglescpp<<"vector<"<<variables[j].type<<"> "<<variables[j].nom<<";"<<endl;
  511. }
  512. }
  513. Reglescpp<<"\n"<<regle; //C'est la qu'on écrit les regles!
  514. Reglescpp<<"\n return 1; \n }"<<endl;
  515. Reglescpp.close();
  516. Reglestxt.close();
  517. cout<<"Fichier regles.cpp généré..."<<endl;
  518. return 0;
  519. }