/Expertbot/regles/generateurRegles.cpp
C++ | 455 lines | 342 code | 65 blank | 48 comment | 86 complexity | d6141c513a0522342c99e588d064fbcb MD5 | raw file
- //g++ generateurRegles.cpp -o genRegle
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- using namespace std;
- struct var {
- string nom;
- string type;
- };
- int main() {
- ifstream Reglestxt("regles.txt", ios::in); // on ouvre le fichier en lecture
- ofstream Reglescpp("regles.cpp", ios::out | ios::trunc); //déclaration du flux et ouverture du fichier
- /*
- if(!fichierRegles) // si l'ouverture n'a pas réussi
- { cout << "Erreur ŕ l'ouverture !" << endl;}
- // instructions
- else{
- fichier.close(); // on referme le fichier
- }*/
- Reglescpp<<"#include <vector>"<<endl<<"#include \"regles.h\" "<<endl<<endl<<"#include \"predicat.cpp\" "<<"//l'implémentation des fonctions utiles aux regles"<<endl<<endl<<endl;
- Reglescpp<<"using namespace std;"<<endl<<endl<<endl;
- // Reglescpp<<"regles::regles(Goban * gb)\n{\n g=gb;\n}"<<endl<<endl;
- Reglescpp<<"bool regles::executerRegles(){ \n"<<"Conclusions.resize(0); \n"<<endl;
- bool conclusion;
- string ligne;
- string courant;
- string regle= "";
- string tampon;
- vector<var> varTemp;
- int i;
- int a;
- vector<struct var> variables;
- bool vectorDeclare;
- bool init = false;
- string nomFait;
- while (getline(Reglestxt,ligne)) {
- vectorDeclare=false;
- conclusion=false;
- courant = "";
- tampon="";
- nomFait="";
- i = 0;
- a=0;
- while (ligne[i]==' ') {
- i++;
- }
- if(!(ligne[i]=='/'&&ligne[i+1]=='/')) { // si la ligne n'est pas commentée
- cout<<ligne<<endl;
- while (i<ligne.size()) {
- if (ligne[i]==' ') {
- i++;
- } else {
- if (!init) {
- regle+="if(";
- init = true;
- }
- var vide;
- varTemp.push_back(vide);
- //cout<<"caractere: "<<ligne[i]<<endl;
- if (ligne[i]=='(') { //Parenthese ouvrante
- if (conclusion && !vectorDeclare) {
- vectorDeclare = true;
- int j =i;
- while(ligne[j]==' ') {
- j++;
- }
- //cout<<"ligne j: "<<ligne[j]<<ligne[j+1]<<ligne[j+2]<<endl;
- if ((ligne[j+1]=='J')&&(ligne[j+2]=='_')) {
- regle+=" vector<vector<int> > listeParam; ";
- }
- }
- if (courant!="non") {
- if (conclusion) {
- tampon+=courant;
- } else {
- regle+=courant;
- }
- }
- if ((!conclusion)&&courant=="testerFait") {
- string varFait="";
- //while (!(ligne[i]==')'&&ligne[i-1]==')')){i++;}
- regle+="(";
- i++;
- //regle+="\"";
- while(ligne[i]!='(') { //le nom du fait
- regle+=ligne[i];
- i++;
- }
- i++;
- bool yaargs=false;
- while(ligne[i]!=')') {
- if(ligne[i]!=' ') {
- yaargs=true;
- }
- }
- if(yaargs) {
- regle+=", ";
- }
- while(ligne[i]!=')') { //ses arguments
- if ((ligne[i]==',')&&(varFait.size()>0)) {
- regle+="&"+varFait+", ";
- varFait="";
- } else {
- varFait+=ligne[i];
- }
- i++;
- }
- if (varFait.size()>0) {
- regle+="&"+varFait+")";
- } else {
- regle+=')';
- }
- i+=1;
- }
- //Cas délicat du NON
- else if ((!conclusion)&&courant=="non") {
- //test si c'est un fait:
- int k=i;
- k++;
- string nomfct;
- while(ligne[k]!='(') {
- nomfct+=ligne[k];
- k++;
- }
- if (nomfct=="testerFait") {
- i=k;
- regle+="!"+nomfct;
- string varFait="";
- //while (!(ligne[i]==')'&&ligne[i-1]==')')){i++;}
- regle+="(";
- i++;
- // cout<<"ligne: "<<ligne[i]<<endl;
- //regle+="\"";
- while(ligne[i]!='(') { //le nom du fait
- regle+=ligne[i];
- i++;
- }
- i++;
- bool yaargs=false;
- while(ligne[i]!=')') {
- if(ligne[i]!=' ') {
- yaargs=true;
- }
- i++;
- }
- if(yaargs) {
- regle+=", ";
- i--;
- }
- while(ligne[i]!=')') { //ses arguments
- cout<<"ligne: "<<ligne[i]<<endl;
- if ((ligne[i]==',')&&(varFait.size()>0)) {
- regle+="&"+varFait+", ";
- varFait="";
- } else {
- varFait+=ligne[i];
- cout<<"ON A TROUVE UN ARGUMENT:" <<varFait<<endl;
- }
- i++;
- }
- if (varFait.size()>0) {
- regle+="&"+varFait+")";
- } else {
- regle+=')';
- }
- i+=2;
- // varFait=="";
- }
- else { // si le non ne contient pas de fait
- string machiaTemp;
- machiaTemp+="(";
- vector<string> variables;
- string varT="";
- i++;
- while(ligne[i]!='(') {
- machiaTemp+=ligne[i];
- i++;
- }
- machiaTemp+=ligne[i];
- i++;
- while(ligne[i]!=')') {
- if ( (!(ligne[i]>='0'&&ligne[i]<='9')) && (ligne[i-1]=='(' ||ligne[i-1]==',' )) {
- machiaTemp+='&';
- }
- machiaTemp+=ligne[i];
- if((ligne[i]==',')&&(!(varT[0]>='0'&&varT[0]<='9'))) {
- variables.push_back(varT);
- varT="";
- } else if (ligne[i]==' ') {} else {
- varT+=ligne[i];
- }
- i++;
- }
- machiaTemp+=ligne[i];
- /* for (int k=0;k<variables.size();k++){
- regle+='&';
- regle+=variables[k];
- if(k<variables.size()-1){regle+=',';}
- }
- regle+=')';*/
- i++;
- if((varT.size()>0)&&(!(varT[0]>='0'&&varT[0]<='9'))) {
- variables.push_back(varT);
- varT="";
- }
- machiaTemp+="|| true) && inverser(";
- for (int k=0; k<variables.size(); k++) {
- machiaTemp+='&';
- machiaTemp+=variables[k];
- if(k<variables.size()-1) {
- machiaTemp+=',';
- }
- }
- machiaTemp+=')';
- regle+="initNon(";
- for (int k=0; k<variables.size(); k++) {
- // regle+='&';
- regle+=variables[k];
- if(k<variables.size()-1) {
- regle+=',';
- }
- }
- regle+=") && ";
- regle+=machiaTemp;
- }
- } else if (!conclusion) {
- while (i<ligne.size()&&(ligne[i-1]!=')')) {
- if (ligne[i]!=','&&ligne[i]!='('&&ligne[i]!=')'&&ligne[i]!=' '&&(!(ligne[i]>='0'&&ligne[i]<='9'))&&(courant!="testerFait")) {
- regle+='&';
- }
- regle+=ligne[i];
- if ((ligne[i]!='(')&&(ligne[i]!=' ')&&(!(ligne[i]>='0'&&ligne[i]<='9'))&&(courant!="testerFait")/*&&varTemp[a].nom.size()>0*/) {
- if ((ligne[i]==',')||(ligne[i]==')')) {
- int j;
- // cout<<"tentative d'ajout de "<< varTemp[a].nom<< " de taille: "<<varTemp[a].nom.size()<<endl;
- if (varTemp[a].nom.size()>0) {
- for (j = 0; j <variables.size(); j++) {
- if (varTemp[a].nom==variables[j].nom) break;
- }
- if (j==variables.size()) {
- varTemp[a].type="int";
- //cout<<"on ajoute la variable: "<<varTemp[a].nom<<endl;
- variables.push_back(varTemp[a]);
- }
- }
- var vide;
- varTemp.push_back(vide);
- a++;
- } else if (courant!="testerFait") {
- // cout<<" ajout d'un truc dans varTemp: "<<ligne[i]<<" "<<(ligne[i]==' ')<<endl;
- varTemp[a].nom += ligne[i];
- }
- }
- i++;
- }
- int j;
- for (j = 0; j <variables.size(); j++) {
- if (varTemp[a].nom==variables[j].nom) break;
- }
- if ((j==variables.size())&&(!(varTemp[a].nom[0]>='0'&&varTemp[a].nom[0]<='9'))) {
- }
- varTemp.clear();
- a=0;
- // regle+=')';
- } else { //si on est dans la conclusion
- if ((courant=="ajouterFait")||(courant=="retirerFait")) {
- // tampon+=courant;//+"(";
- i++;
- tampon+='(';
- string faitOriginal;
- nomFait+="F_";
- tampon+="F_";
- while (i<ligne.size()&&ligne[i]!='(') {
- tampon+=ligne[i];
- nomFait+=ligne[i];
- faitOriginal+=ligne[i];
- i++;
- }
- //test si le fait n'a pas déja été déclaré
- bool dejaDeclare = false;
- for (int j = 0; j<variables.size(); j++) {
- if (nomFait==variables[j].nom) {
- dejaDeclare=true;
- }
- }
- if(!dejaDeclare) {
- var fait;
- fait.nom=nomFait;
- variables.push_back(fait);
- }
- // cout<<"OUKONESTLA: "<<ligne[i]<<ligne[i+1]<<ligne[i+2]<<ligne[i+3]<<" FAIT: "<<nomFait<<endl;
- regle+=nomFait+".id = "+faitOriginal+";";
- // varTemp[a].type="int";
- // variables.push_back(varTemp[a]);
- while (i<ligne.size()&&ligne[i]!=')') {
- if ((ligne[i]!='(')&&(ligne[i]!=',')) {
- regle+=nomFait+".args = ";
- // regle+='&';
- while (ligne[i]!=')'&&(ligne[i]!=',')) {
- regle+=ligne[i];
- i++;
- }
- regle+="; ";
- // regle+=ligne[i];
- }
- i++;
- }
- tampon+="); ";
- regle+=tampon;
- i+=2;
- } else if (courant!="jouer") {
- // regle+=",\""+courant+"\"";
- tampon+=",\""+courant+"\"";
- // regle+=",{";
- while (i<ligne.size()&&ligne[i]!=')') {
- if ((ligne[i]!='(')&&(ligne[i]!=',')) {
- regle+="listeParam.push_back(";
- // regle+='&';
- while (ligne[i]!=')'&&(ligne[i]!=',')) {
- regle+=ligne[i];
- if (ligne[i+1]!=')'&&ligne[i+1]!=',') {
- i++;
- } else {
- break;
- }
- }
- regle+="); ";
- // regle+=ligne[i];
- }
- i++;
- }
- // bidule.push_back(x);
- regle+=tampon;
- regle+=",listeParam,";
- i+=2;
- // regle+="},";
- while ((ligne[i]==' ')||ligne[i]==',') {
- i++;
- }
- //cout<<regle[i]<<" - "<<(regle[i]==' ')<<endl;
- while(ligne[i]!=')') {
- regle+=ligne[i];
- i++;
- }
- regle+=");";
- //i++;
- } else {
- // regle+='(';
- tampon+='(';
- }
- }
- courant = "";
- } else if (ligne[i]=='^') { // ET
- regle+=" && ";
- i++;
- }
- else if (ligne[i]=='-') { //Fleche vers conclusion
- conclusion = true;
- regle+=") \n {";
- i++;
- } else if (ligne[i]==' ') {} else {
- courant+=ligne[i];
- }
- i++;
- }
- }
- if (init) {
- regle+="} \n \n traceTemp.clear(); \n \n";
- //regle+="} \n \n";
- }
- init=false;
- }
- }
- for (int j =0; j<variables.size(); j++) {
- if (variables[j].nom.size()>1&&variables[j].nom[0]=='F'&&variables[j].nom[1]=='_') { //on a affaire a un fait
- Reglescpp<<"machiaFait "<<variables[j].nom<<";"<<endl;
- } else {
- cout<<"nom vecteur: "<<variables[j].nom<<endl;
- Reglescpp<<"vector<"<<variables[j].type<<"> "<<variables[j].nom<<";"<<endl;
- }
- }
- Reglescpp<<"\n"<<regle;
- Reglescpp<<"\n }"<<endl;
- Reglescpp.close();
- Reglestxt.close();
- cout<<"Fichier regles.cpp généré..."<<endl;
- return 0;
- }