IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Prolog Discussion :

[PL2CPP] Problèmes avec PlQuery


Sujet :

Prolog

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4
    Points : 4
    Points
    4
    Par défaut [PL2CPP] Problèmes avec PlQuery
    Bonjour,
    Je réalise actuellement un projet en C++ (avec la librairie QT) avec une IA en SWI-Prolog, j'utilise donc PL2CPP pour faire l'interface entre les deux langages.

    J'ai crée une fonction pour executer un predicat en utilisant les types QT:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
     
    QVector<QVector<QString> > PlInterface::plExecute(QString predicat, const QVector<QString>& args, int nbLinesMax){
          QVector<QVector<QString> > res;
          int argc=args.size();
          int result,nbLines=0;
          bool is_there_null=false,stop;//if false the predicate will return yes or no
          QString arg;
          QStringList tabarg;
     
          //convert the args list into prolog type
          PlTermv argspl(argc);
          for (int i=0 ; i<argc ; i++){
                  if(!args[i].isNull()){
                     if(args[i].contains("[")){
                         arg = args[i];
                         //We may check if it not contain list
                         arg.replace("[","");
                         arg.replace("]","");
                         tabarg = arg.split(",", QString::SkipEmptyParts);
                         PlTail tab(argspl[i]);
     
                         for ( int j=0 ; j<tabarg.size() ; j++)
                              tab.append((const char*)tabarg[j].toLatin1());
                         tab.close();
                     }else
                        argspl[i]=(const char*)args[i].toLatin1();
                  }else{
                     is_there_null=true;
                     //QString arg=QString("X%1").arg(i);
                     //argspl[i]=NULL;//(const char*)arg.toLatin1();
                  }
          }
     
     
          try
          {
              PlQuery myQuery((const char*) predicat.toLatin1(), argspl);//Query prolog
     
              //store the solution(s) found if any
              result = myQuery.next_solution();
              QVector<QString> tmprep;
     
              stop=false;
              while (result && !stop){//for each solutions
                  tmprep.clear();
                  //convert the vector of result into QT-type
                  for(int i=0; i<argc ; i++){
                         try{//to deal with tab result
                             PlTail l(argspl[i]);
                             PlTerm e;
                             QString tab("[");
                             while(l.next(e)){
                                 tab.append(QString((char *)e) +",");//we may check if it's an array
     
                             }
                             l.close();
                             if(tab.size()!=1)
                                 tab.replace(tab.size()-1,1,"]");
                             else
                                 tab.append("]");
                             tmprep.append(tab);
                         }catch(...){
                             tmprep.append(QString((char *)argspl[i]));//standard conv
                         }
                  }
                  if(tmprep.size()>0)//we only append non-empy lines
                      res.append(tmprep);
                  result = myQuery.next_solution();
                  nbLines++;
                  stop=(nbLines==nbLinesMax);
              }
     
              //Yes/No answers
              if(nbLines==1 && (!is_there_null || res.size()==0)){//it's mean that the predicate has returned yes
                   tmprep.clear();
                   res.clear();
                   tmprep.append("yes");
                   res.append(tmprep);
              }else if(nbLines==0){//the predicate has returned no
                   tmprep.clear();
                   tmprep.append("no");
                   res.append(tmprep);
              }
          } catch ( PlException &exception )
          {
           //TODO Handle the exception
          }
     
          return res;
     }
    J'ai a coté un programme prolog avec le prédicat suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    % Lance la recherche de la meilleure combinaison
    meilleur(P,J,M) :- findall(C,coup(P,J,C),R), est_meilleur(P,R,J,-1001,_,_,M).
    Celui ci fonctionne parfaitement dans la console
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    ?- meilleur([0,0,0,0,-1,0,0,0,0],1,M).
    M = [-1, 0, 0] ;
    false.
    mais lorsque je fait dans mon programme (j'ai auparavant fait un PlEngine() et un PlCall("consult","IA.pl") )
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
        QVector<QVector<QString> > res;
     
        QVector<QString> arguments(3);
        arguments[0]="[0,0,0,0,-1,0,0,0,0]";
        arguments[1]="1";
        res = pl->plExecute("meilleur",arguments);
    Le 3ème paramètres n'est pas instancié, est-ce que quelqu'un pourrait éclairer ma lanterne

  2. #2
    Rédacteur/Modérateur
    Avatar de Trap D
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    4 942
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2003
    Messages : 4 942
    Points : 6 498
    Points
    6 498
    Par défaut
    Salut
    Je ne connaits pas PL2CPP mais j'ai eu l'occasion d'écrire une liaison Prolog <-> C, aussi peut-être peux-tu essayer de t'y retrouver en regardant ce que j'avais fait.

    Le 3ème paramètres n'est pas instancié, est-ce que quelqu'un pourrait éclairer ma lanterne
    A mon avis c'est normal, c'est dans le troisième argument que tu récupères la réponse de Prolog.
    "La haine seule fait des choix" - Koan Zen
    "Il ne faut pas être meilleur que les autres, il faut être meilleur que soi." Albert Jacquard
    "Ceux qui savent où ils ont posé leur parapluie ne sont pas alcooliques." - pgibonne.
    Faites du Prolog, ça vous changera les idées !
    Ma page Prolog
    Mes codes sources commentés

    Mon avatar : La Madeleine à la veilleuse de Georges de La Tour

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4
    Points : 4
    Points
    4
    Par défaut
    Merci pour ta réponse Trap
    Citation Envoyé par Trap D Voir le message
    A mon avis c'est normal, c'est dans le troisième argument que tu récupères la réponse de Prolog.
    Ce que j'entends par la c'est que le troisième argument devient _GA23 ou un truc du genre au lieu de la réponse souhaité
    Le problème serait lié a l'utilisation du findall rendant le prédicat non-déterministe et la gestion de ce type de prédicat est un peu particulière, cf
    http://gollem.science.uva.nl/SWI-Pro...ml#sec:9.6.1.1 mais j'ai vraiment du mal a comprendre le fonctionnement (notement la notion de control_t...)

  4. #4
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4
    Points : 4
    Points
    4
    Par défaut
    Je me répond car j'ai finalement résolu le problème en n'appelant pas directement le prédicat mais en l'appelant a l'aide du prédicat call...
    Concrètement la fonction du premier post est devenu
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
     
     QVector<QVector<QString> > PlInterface::plExecute(QString predicat, const QVector<QString>& args, int nbLinesMax){
          QVector<QVector<QString> > res;
          QString argv;
          int argc=args.size();
          int result,nbLines=0;
          bool is_there_null=false,stop;//if false the predicate will return yes or no
          QString arg;
          QStringList tabarg;
     
          //convert the args list into prolog type
          PlTermv argspl(argc);
          for (int i=0 ; i<argc ; i++){
                  if(!args[i].isNull()){
                     if (i==0) 
                        argv+=args[i];
                     else
                        argv+="," + args[i];
                  }else{
                     is_there_null=true;
                     if (i==0) 
                        argv+=QString("X%1").arg(i);
                     else
                        argv+="," + QString("X%1").arg(i);;
                  }
          }
     
     
          try 
          {
              QString query = predicat + "(" + argv + ")";
     
              argspl = PlTermv(PlCompound((const char*) query.toLatin1()));
              PlQuery myQuery( "call", argspl);
     
              //store the solution(s) found if any
              result = myQuery.next_solution();
              QVector<QString> tmprep;
     
              stop=false;
              while (result && !stop){//for each solutions
                  tmprep.clear();
                  //convert the vector of result into QT-type
     
                  QString tmpres=QString((char *)argspl[0]).mid(predicat.length()+1);
                  tmpres = tmpres.mid(0,tmpres.length()-1);
     
                  QVector<QString> tabarg;
                  int in_list=0;
                  QString tmpstr="";
                  for (int i=0 ; i<tmpres.size() ; i++){
                      if (tmpres[i]=='['){
                         tmpstr[i]=tmpres[i];
                         in_list++;
                      }else if (tmpres[i]==']'){
                         tmpstr[i]=tmpres[i];
                         in_list--;
                      }else if(tmpres[i]==',' and in_list==0){
                          tabarg.append(tmpstr);
                          tmpstr="";
                      }else{
                         tmpstr[i]=tmpres[i];
                      }
                  }
                  tabarg.append(tmpstr);
                  if(tabarg.size()>0)//we only append non-empy lines
                      res.append(tabarg);
                  result = myQuery.next_solution();
                  nbLines++;
                  stop=(nbLines==nbLinesMax);
              }
     
              //Yes/No answers
              if(nbLines==1 && (!is_there_null || res.size()==0)){//it's mean that the predicate has returned yes
                   tmprep.clear();
                   res.clear();
                   tmprep.append("yes");
                   res.append(tmprep);
              }else if(nbLines==0){//the predicate has returned no
                   tmprep.clear();
                   tmprep.append("no");
                   res.append(tmprep);
              }
          } catch ( PlException &exception )
          {
           //TODO Handle the exception
          }
     
          return res;
     }
    Je prétend pas que ma solution soit optimale mais au moins elle marche

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. VC++ Direct3D8, problème avec LPD3DXFONT et LPD3DTEXTURE8
    Par Magus (Dave) dans le forum DirectX
    Réponses: 3
    Dernier message: 03/08/2002, 12h10
  2. Problème avec [b]struct[/b]
    Par Bouziane Abderraouf dans le forum CORBA
    Réponses: 2
    Dernier message: 17/07/2002, 11h25
  3. Problème avec le type 'Corba::Any_out'
    Par Steven dans le forum CORBA
    Réponses: 2
    Dernier message: 14/07/2002, 19h48
  4. Problème avec la mémoire virtuelle
    Par Anonymous dans le forum CORBA
    Réponses: 13
    Dernier message: 16/04/2002, 17h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo