3

I'm trying to combine SWI-prolog with QProcess. So I already can interact with prolog and send questions to it, but I always getting only the first answer. So I tried to send such characters like "n" or "r" which should give me the next answer of my query like in the shell. However, it doesn't work with QProcess. Here is my code:

connect(&p,SIGNAL(readyReadStandardError()),this,SLOT(readStdError()));
connect(&p,SIGNAL(readyReadStandardOutput()),this,SLOT(readStdOut()));

p.setProcessChannelMode(QProcess::MergedChannels);
p.start("swipl",QIODevice::ReadWrite | QIODevice::Unbuffered);
if(!p.waitForStarted())
    return;

p.write("consult(ws).\r\n");      //prolog program
if(!p.waitForBytesWritten())
{
    p.close();
    return;
}

p.write("test(X,Y).\r\n");       //query
if(!p.waitForBytesWritten())
{
    p.close();
    return;
}

p.write("n",1);                  //give me the next solution -> nothing happens

I don't know what to do anymore. How can I get the next answers or how can I tell prolog to show me all answers?

Paul
  • 233
  • 2
  • 3
  • 14

1 Answers1

1

I would use something like

p.write("forall(test(X,Y), writeln(test(X,Y))).\r\n");

or something a bit more reusable

QString q("forall(%1, writeln(%1)).\r\n");
p.write(q.arg("test(X,Y)"));

you got the concept...

If you prefer, maybe to ease answer parsing, print separed variables

p.write("forall(test(X,Y), maplist(writeln, [X,Y])).\r\n");
CapelliC
  • 59,646
  • 5
  • 47
  • 90