tags:

views:

68

answers:

1

I am using the C++ interface to Prolog (the classes and methods of SWI-cpp.h). For working out a simple backtracking that john likes mary and emma and sara:

likes(john, mary).
likes(john, emma).
likes(john, ashley).

I can just do:

{
  PlFrame fr;
  PlTermv av(2);
  av[0] = PlCompound("john");
  PlQuery q("likes", av);
  while (q.next_solution())
  {
   cout << (char*)av[1] << endl;
  }
}

This works in a separate code, so the syntax is correct. But I am also trying to get this simple backtracking to work within a class:

class UserTaskProlog
{
  public:
                UserTaskProlog(ArRobot* r);
                ~UserTaskProlog();
  protected:
                int cycles;
                char* argv[1];
                ArRobot* robot;
                void logTask();
};

This class works fine, with my cycles variable incrementing every robot cycle. However, when I run my main code, I get an Unhandled Exception error message:

UserTaskProlog::UserTaskProlog(ArRobot* r) : robotTaskFunc(this, &UserTaskProlog::logTask)
{
  cycles = 0;
  PlEngine e(argv[0]);
  PlCall("consult('myFile.pl')");
  robot->addSensorInterpTask("UserTaskProlog", 50, &robotTaskFunc);
}

UserTaskProlog::~UserTaskProlog()
{
  robot->remSensorInterpTask(&robotTaskFunc);
  // Do I need a destructor here for pl?
}

void UserTaskProlog::logTask()
{
  cycles++;
  cout << cycles;
  {
    PlFrame fr;
    PlTermv av(2);
    av[0] = PlCompound("john");
    PlQuery q("likes", av);
    while (q.next_solution())
    {
     cout << (char*)av[1] << endl;
    }
  }
}

I have my opening and closing brackets for PlFrame. I have my frame, my query, etc... The exact same code that backtracks and prints out mary and emma and sara. What am I missing here that I get an error message?

Here is what I think the code should do: I expect mary and emma and sara to be printed out once, every time cycles increments. However, it opens SWI-cpp.h file automatically and points to class PlFrame. What is it trying to tell me? I don't see anything wrong with my PlFrame class declaration.

Thanks,

A: 

You cannot pass pointers to instance methods like this, you probably have to create C function wrapper for passing to addSensorInterpTask(). This seems to be the root of your problem, as robot calls the method with incorrect parameters. Also, you should catch PlException and check what it tells you. Anyway, your example cannot be compiled as it is, since it is incomplete (what is ArRobot?) and imprecise (ashley v.s. sara). Please try to imagine how other people could reproduce your problem without much a of a hassle before submitting a question.

Mart Oruaas
Thanks Mart. That was a typo that I am correcting now. ArRobot is referring to ARIA's robot object (http://robots.mobilerobots.com/wiki/Main_Page). I have only submitted the code snippets that I believe contain the error. It might put the viewers off if I submitted the entire code (so that my problem could be reproduced) although you have an absolutely valid point. I will check your other comments and see what I can do. Thank you,
HosseinJoshua MIRI