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;
} |
Partager