Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux g++ Embedding Prolog Logic Engine Within C++

I have some logic in a C++ program that is not only insanely complex, it requires multiple solutions for which Prolog is ideal. It's sort of like a firewall config script, checking input for actions, but sometimes more that one action is required.

What I want is something like this:

class PrologEngine
{
    LoadLogic(const char* filename) throw PrologException; // Load a file of prolog rules, predicates facts etc in textual format. Must be callable multiple times to load AND COMPILE (for speed) prolog rule files.

    std::vector<std::string> Evaluate(const char* predicate_in_string_form = "execute(input, Result)") throw PrologException; Returns a vector of matching predicates in text form.

};

It needs no ability to call back into C++.

AMI Prolog seems to get it, but it's not available on Linux. I'm trying to use SWI-Prolog and can only find 2 examples and and incredibly byzantine API (my opinion)

Can anyone point me to an example that is close to what I'm looking for?

like image 863
Walt Howard Avatar asked Feb 15 '12 16:02

Walt Howard


2 Answers

There is A C++ interface to SWI-Prolog, that's high level.

I'm fighting with it, here an example of bridging to OpenGL:

PREDICATE(glEvalCoord1d, 1) {
 double u = A1;
 glEvalCoord1d( u );
 return TRUE;
}

This clean code hides many 'bizantinism', using implicit type conversion and some macro. The interface is well tought and bidirectional: to call Prolog from C++ there are PlCall ('run' a query, similar to Evaluate you expose in the answer) or a more structured PlQuery, for multiple results...

If you don't need to link to openGl, or can wait to ear about the answer that hopefully I'll get from SWI-Prolog mailing list, you should evaluate it.

like image 137
CapelliC Avatar answered Sep 20 '22 21:09

CapelliC


If you don't mind rewriting the prolog code for use in a native c++ header only library, I'd look into the castor library: http://www.mpprogramming.com/cpp/

like image 21
shuttle87 Avatar answered Sep 24 '22 21:09

shuttle87