I am trying to process a list of numbers i get from a GUI interface made in Qt. In specific, I want to return to the user, in a label, a list of all the numbers that are even in his input list. Thanks to a user CapelliC i managed to include all the files and libraries in order to make SWI-Prolog work from inside C++ and i am now trying to develop the program.
It is required that i use a C++ interface to Prolog in such a way that i receive the list from the user through a QLineEdit object, send the list to the Prolog engine, and then Prolog should return to the C++ program the resulting list of pairs.
I have already made the rule that extracts the pairs from a list and returns it as another list. These are the contents of my manejoListas.pl file, the SWI-Prolog knowledgebase for my program:
pares(Lista,ListaPares):-findall(Numero,(member(Numero,Lista),mod(Numero,2)=:=0),ListaPares).
This works correctly in SWI-Prolog. Now, i want to load this .pl file on my C++ and Qt based program, send the input list to this rule and obtain the resulting list, pointed by the argument ListaPares to my C++ interface so that i can show it to the user.
I have read the Foreign Language Interface documentation and i haven't been able to locate what i need in order to solve this particular scenario. This is what i have so far based blindly on an incomplete "tutorial" i found of somebody on youtube who didn't bother to explain what he was doing for another unrelated problem, mainly, finding the factorial of a number using Qt, C++ and Prolog.
void MainWindow::on_btnPares_clicked()
{
QString listaEntrada = ui->txtListaEntrada->text();
term_t listaEntrada, listaPares, term;
functor_t paresFunc;
listaEntrada = PL_new_term_ref();
listaPares = PL_new_term_ref();
term = PL_new_term_ref();
PlCall("consult('manejoListas.pl')"); //I assume this opens my knowledgebase file so that i can work with it
paresFunc = PL_new_functor(PL_new_atom("pares"), 2);
PL_cons_functor(term, paresFunc, listaEntrada, listaPares);
if(PL_call(term, NULL)) {
PL_get_string()
}
}
I am unable to advance, i don't know how to pass the list to SWI-Prolog or how to even construct the correct consult i should send to Prolog or how to receive the answer. This program should behave exactly as if i ran the following consult to SWI-Prolog:
?- pares([2,4,8,9],X).
X = [2, 4, 8].
Thank you in advance for any help you can provide.