Linux g++ Embedding Prolog Logic Engine Within C++
Asked Answered
G

2

5

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?

Gooding answered 15/2, 2012 at 16:39 Comment(2)
This blog post has a small example of what you want to do wit swi-prolog: electricbacon.wordpress.com/2010/09/08/…Dacoity
Why not use CLIPS instead of prolog? clipsrules.sourceforge.net/WhatIsCLIPS.htmlMicrolith
J
4

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.

Jotun answered 15/2, 2012 at 17:17 Comment(0)
G
3

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/

Gawlas answered 15/2, 2012 at 17:9 Comment(2)
Castor it's interesting. Did you tried it? I fear it could be rather inefficient.Jotun
I figured it out. It's quite nice once you figure it out. Writing a predicate in C++ is easy too. Here's the core of it.Gooding

© 2022 - 2024 — McMap. All rights reserved.