/Expertbot/regles/generateurRegles2.cpp
C++ | 612 lines | 453 code | 92 blank | 67 comment | 122 complexity | 6a6bc762f604090c0e6dc208d24d1486 MD5 | raw file
- //g++ generateurRegles2.cpp -o genRegle
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- using namespace std;
- string regle= "";
- int compteur;
- struct var {
- string nom;
- string type;
- };
- vector<struct var> variables;
- // int i;
- void parserLigne(string ligne,int i);
- void trucDifferent(string ligne, int i) {
- //cout<<"welcome dans truc différent: "<<i<<endl;
- string var1,var2= "";
- //prédicat 1
- regle+="\n \n";
- while ((ligne[i]!='^')&&(ligne[i]!='-')) {
- // cout<<"ligne[i]: "<<ligne[i]<<endl;
- if (ligne[i]!=' ') {
- regle+=ligne[i];
- if (ligne[i]=='(') {
- regle+='&';
- int k = i+1;
- while (ligne[k]!=')') {
- var1+= ligne[k];
- k++;
- }
- }
- }
- i++;
- }
- // cout<<"var1: "<<var1<<endl;
- regle+="; ";
- i++;
- //prédicat 2
- while ((ligne[i]!='^')&&(ligne[i]!='-')) {
- if (ligne[i]!=' ') {
- regle+=ligne[i];
- if (ligne[i]=='(') {
- regle+='&';
- int k = i+1;
- while (ligne[k]!=')') {
- var2+= ligne[k];
- k++;
- }
- }
- }
- i++;
- }
- //cout<<"var2: "<<var2<<endl;
- i++;
- //cout<<ligne[i]<<endl;
- while (ligne[i]!='^') {
- i++;
- }
- i++;
- //sleep(1000);
- regle+="; ";
- //ajout des variables en dĂŠclaration
- var v1;
- v1.nom = var1;
- var v2;
- v2.nom = var2;
- int l;
- for ( l = 0; l <variables.size(); l++) {
- if (v1.nom==variables[l].nom) break;
- }
- if (l==variables.size()) {
- v1.type="int";
- //cout<<"on ajoute la variable: "<<varTemp[a].nom<<endl;
- variables.push_back(v1);
- }
- for ( l = 0; l <variables.size(); l++) {
- if (v2.nom==variables[l].nom) break;
- }
- if (l==variables.size()) {
- v2.type="int";
- //cout<<"on ajoute la variable: "<<varTemp[a].nom<<endl;
- variables.push_back(v2);
- }
- regle+="\n vector <int> A";
- regle+=(char)compteur+65;
- regle+=" = "+var1+";"+"\n vector <int> B";
- regle+=(char)compteur+65;
- regle+=" = "+var2+"; \n";
- regle+="for (int i = 0; i< (int)A";
- regle+=(char)compteur+65;
- regle+=".size(); i++){ \n "+var1+".resize(0); "+var1+".push_back(A";
- regle+=(char)compteur+65;
- regle+="[i]); \n for (int j = 0; j<(int) B";
- regle+=(char)compteur+65;
- regle+=".size(); j++){ \n ";
- regle+="if (i!=j) {";
- regle+=var2+".resize(0); "+var2+".push_back(B";
- regle+=(char)compteur+65;
- regle+="[j]); \n";
- compteur++;
- //cout<<"on va rappeller parser ligne sur "<<i<<" - "<<ligne[i]<<endl;
- parserLigne(ligne,i);
- regle+="} \n } \n} ";
- }
- void trucNormal(string ligne, int i) {
- bool conclusion;
- // string ligne;
- string courant;
- vector<var> varTemp;
- string tampon;
- // int i;
- int a;
- bool vectorDeclare;
- bool init = false;
- string nomFait;
- vectorDeclare=false;
- conclusion=false;
- courant = "";
- tampon="";
- nomFait="";
- // i = 0;
- a=0;
- while (i<ligne.size()) {
- if (ligne[i]==' ') {
- i++;
- } else {
- if (!init) {
- regle+="if(";
- init = true;
- }
- var vide;
- varTemp.push_back(vide);
- if (ligne[i]=='(') { //Parenthese ouvrante
- if (conclusion && !vectorDeclare) {
- vectorDeclare = true;
- int j =i;
- while (ligne[j]==' ') {
- j++;
- }
- 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="";
- regle+="(";
- i++;
- 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;
- } else if ((!conclusion)&&courant=="non") { //Cas dĂŠlicat du 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+="(regles::";
- }
- }
- 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";
- }
- init=false;
- }
- void parserLigne(string ligne, int j) {
- //cout<<"parser ligne : "<<ligne<<" : "<<j<<endl;
- int i=j;
- bool fini = false;
- string nomTemp="";
- //recherche d'un "diffĂŠrent"
- while (i<ligne.size() && !fini && nomTemp!="different") {
- // cout<<"in da 1er while: "<<i<<" - "<<ligne[i]<<endl;
- nomTemp="";
- while ((ligne[i]!='(')&&(ligne[i]!='>')) {
- //cout<<"in da 2eme while: "<<i<<" - "<<ligne[i]<<endl;
- if (ligne[i]!=' ') {
- nomTemp+=ligne[i];
- }
- i++;
- }
- // cout<<"sortie du while: "<<ligne[i]<<endl;
- if (ligne[i]=='>') {
- fini = true; //si on est vers la conclusion
- } else {
- while (ligne[i]!='^'&&ligne[i]!='-') {
- i++;
- }
- i++;
- }
- }
- //cout<<"fin recherche diffĂŠrent: "<<nomTemp<<endl;
- if (nomTemp=="different") {
- // cout<<"on lance truc différent a la ligne "<<j<<endl;
- trucDifferent(ligne,j);
- }
- else {
- trucNormal(ligne,j);
- }
- // cout<<"fin parser"<<endl;
- }
- int main() {
- ifstream Reglestxt("regles.txt", ios::in); // on ouvre le fichier en lecture
- ofstream Reglescpp("predicat.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;
- Reglescpp<<"//l'implementation 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;
- string ligne= "";
- compteur=0;
- while (getline(Reglestxt,ligne)) {
- cout<<ligne<<endl;
- int i = 0;
- while (ligne[i]==' ') {
- i++;
- }
- if (!(ligne[i]=='/'&&ligne[i+1]=='/')) { // si la ligne n'est pas commentée
- regle+="sTrace = \" ";
- regle+=ligne;
- regle+="\";";
- regle+="\n \n";
- parserLigne(ligne, i);
- }
- }
- 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; //C'est la qu'on écrit les regles!
- Reglescpp<<"\n return 1; \n }"<<endl;
- Reglescpp.close();
- Reglestxt.close();
- cout<<"Fichier regles.cpp généré..."<<endl;
- return 0;
- }