Re: Accessing PowerLoom from Haskell
Hans Chalupsky <[email protected]> Wed, 02 Sep 2015 13:54:35 -0700
| Newsgroups | gmane.comp.ai.powerloom |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--------------060403030004090608070405
Content-Type: text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by gamma.isi.edu id t82KtkEe011808
On 08/31/2015 06:16 AM, Anders N=E5sell wrote:
> Den 2015-08-28 19:31, Hans Chalupsky skrev:
>> ... There could be some problems with all the C++ constructor initiali=
zation code being run when your C or Haskell program starts up, but that =
should be solvable.
> I saw in the PowerLoom Manual "6.2.1.6 CLOS Objects versus Structs" tha=
t it is possible to get an API with
> structs instead of CLOS objects. Would this be a way to get some OO out=
of the API?
>
> If so I suppose a tool like e.g. http://www.swig.org/ could then be use=
d to generate the C API?
>
> Anders
Anders,
I just tested it and the C++ initialization code is called just fine=20
when linked into a C program. I thought it would, I just wasn't=20
completely sure. I don't think there is a good reason to get OO into=20
the C API. Almost all of the PLI functions are functions, and the very=20
few methods can be wrapped via functions (they are only used for PLI=20
Iterators, I think). There is no need to access the slots of any=20
objects directly, if it turns out that there is, we should add accessors=20
for them in the API. Objects can be passed around by their pointers=20
which in C simply have to be declared as void*. I didn't know about=20
SWIG which looks interesting but seems overkill for this.
Below is a a working example on how to call PowerLoom from C. In the=20
longer term, the PLI C entry points should be generated automatically by=20
the STELLA translator (which won't involve much code) and we'll have a C=20
include file that you can use to get access to the API in C. For now,=20
you just have to write that yourself for the PLI functions you want to us=
e.
I am attaching three little source files which you can compile and run=20
like this (provided you have built the PowerLoom C++ version once to get=20
access to the compiled libraries):
% setenv PL_HOME .../powerloom-4.x.y
% g++ -I$PL_HOME/native/cpp=20
-I$PL_HOME/native/cpp/stella/cpp-lib/gc/include -c pli-c-api.cc=20
-DSTELLA_USE_GC
% gcc -c powerloom-from-c.c
% gcc pli-c-api.o powerloom-from-c.o -o powerloom-from-c=20
-L$PL_HOME/native/cpp/lib -lpowerloom -lpowerloom-extensions -lutilities=20
-llogic -lstella -lgc -lm -lstdc++
% setenv LD_LIBRARY_PATH $PL_HOME/native/cpp/lib
% ./powerloom-from-c
Initializing...
(defconcept person)
/PL-KERNEL-KB/PL-USER/PERSON
(defrelation friend-of (?x ?y)))
FRIEND-OF
(assert (and (person fred) (person susi))))
((PERSON FRED) (PERSON SUSI))
(assert (friend-of fred susi))
(FRIEND-OF FRED SUSI)
(retrieve all (person ?x))
[2015-SEP-02 13:24:04.000 PL] Processing check-types agenda...
(SUSI FRED)
(retrieve all (friend-of ?x ?y))
((FRED SUSI))
Hans
--------------060403030004090608070405
Content-Type: text/x-chdr;
name="pli-c-api.h"
Content-Disposition: attachment;
filename="pli-c-api.h"
Content-Transfer-Encoding: 7bit
void pli_initialize();
void* pli_s_evaluate(char* command, char* moduleName);
char* pli_s_evaluate_to_string(char* command, char* moduleName);
char* pli_object_to_string(void* object);
void* pli_s_change_module(char* name);
--------------060403030004090608070405
Content-Type: text/x-c++src;
name="pli-c-api.cc"
Content-Disposition: attachment;
filename="pli-c-api.cc"
Content-Transfer-Encoding: 7bit
// g++ compilation: g++ -o globals globals.cc
// Compile like this:
// setenv PL_HOME /tmp/build-kagg
// % g++ -I$PL_HOME/native/cpp -I$PL_HOME/native/cpp/stella/cpp-lib/gc/include -c pli-c-api.cc -DSTELLA_USE_GC
#include "powerloom/powerloom-system.hh"
namespace pli {
using namespace stella;
using namespace logic;
extern "C" void pli_initialize() {
initialize();
}
extern "C" void* pli_s_evaluate(char* command, char* moduleName) {
return sEvaluate(command, moduleName, NULL);
}
extern "C" char* pli_s_evaluate_to_string(char* command, char* moduleName) {
Object* result;
result = sEvaluate(command, moduleName, NULL);
if (result != NULL)
return objectToString(result);
else
return (char*) ""; // for simplicity
}
extern "C" char* pli_object_to_string(Object* object) {
if (object != NULL)
return objectToString(object);
else
return NULL;
}
extern "C" void* pli_s_change_module(char* name) {
return sChangeModule(name, NULL);
}
}
--------------060403030004090608070405
Content-Type: text/x-csrc;
name="powerloom-from-c.c"
Content-Disposition: attachment;
filename="powerloom-from-c.c"
Content-Transfer-Encoding: 7bit
// Compile and run like this:
//
// % setenv PL_HOME .../powerloom-4.x.y
//
// % g++ -I$PL_HOME/native/cpp -I$PL_HOME/native/cpp/stella/cpp-lib/gc/include -c pli-c-api.cc -DSTELLA_USE_GC
// % gcc -c powerloom-from-c.c
// % gcc pli-c-api.o powerloom-from-c.o -o powerloom-from-c -L$PL_HOME/native/cpp/lib -lpowerloom -lpowerloom-extensions -lutilities -llogic -lstella -lgc -lm -lstdc++
// % setenv LD_LIBRARY_PATH $PL_HOME/native/cpp/lib
// % ./powerloom-from-c
/*
Initializing...
(defconcept person)
/PL-KERNEL-KB/PL-USER/PERSON
(defrelation friend-of (?x ?y)))
FRIEND-OF
(assert (and (person fred) (person susi))))
((PERSON FRED) (PERSON SUSI))
(assert (friend-of fred susi))
(FRIEND-OF FRED SUSI)
(retrieve all (person ?x))
[2015-SEP-02 13:24:04.000 PL] Processing check-types agenda...
(SUSI FRED)
(retrieve all (friend-of ?x ?y))
((FRED SUSI))
*/
#include "pli-c-api.h"
#include <stdio.h>
main ()
{
char* command;
char* module="PL-USER";
printf("Initializing...\n");
pli_initialize();
printf("%s\n", command = "(defconcept person)");
printf("%s\n", pli_s_evaluate_to_string(command, module));
// change module for nicer result printing:
pli_s_change_module(module);
printf("%s\n", command = "(defrelation friend-of (?x ?y)))");
printf("%s\n", pli_s_evaluate_to_string(command, module));
printf("%s\n", command = "(assert (and (person fred) (person susi))))");
printf("%s\n", pli_s_evaluate_to_string(command, module));
printf("%s\n", command = "(assert (friend-of fred susi))");
printf("%s\n", pli_s_evaluate_to_string(command, module));
printf("%s\n", command = "(retrieve all (person ?x))");
printf("%s\n", pli_s_evaluate_to_string(command, module));
// passing objects:
command = "(retrieve all (friend-of ?x ?y))";
void* result = pli_s_evaluate(command, module);
printf("%s\n", command);
printf("%s\n", pli_object_to_string(result));
}
--------------060403030004090608070405
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
_______________________________________________
powerloom-forum mailing list
[email protected]
http://mailman.isi.edu/mailman/listinfo/powerloom-forum
--------------060403030004090608070405--