SWIProlog.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef LEARN_SWIPROLOG_C_DEFINED
00039 #define LEARN_SWIPROLOG_C_DEFINED
00040
00041 #include "Learn/SWIProlog.H"
00042 #include "Util/Assert.H"
00043 #include "Util/log.H"
00044 #include <math.h>
00045 #include <fcntl.h>
00046 #include <limits>
00047 #include <string>
00048
00049
00050 SWIProlog::SWIProlog(int argc, char **argv)
00051 {
00052 const char *av[10];
00053 int ac = 0;
00054
00055
00056 av[ac++] = "DefaultProg";
00057 av[ac++] = "-q";
00058 av[ac++] = "-nosignals";
00059 av[ac] = NULL;
00060
00061 #ifdef HAVE_SWI_PROLOG_H
00062 if (!PL_initialise(ac, av))
00063 {
00064 PL_halt(1);
00065 LFATAL("Failed to init prolog");
00066 }
00067 #else
00068 LINFO("SWI prolog not found");
00069 #endif
00070
00071 }
00072
00073
00074 SWIProlog::~SWIProlog()
00075 {
00076
00077 }
00078
00079
00080 bool SWIProlog::consult(const char *filename)
00081 {
00082
00083 bool ret = false;
00084 #ifdef HAVE_SWI_PROLOG_H
00085 term_t a0 = PL_new_term_refs(1);
00086 predicate_t p = NULL;
00087
00088 p = PL_predicate("consult", 1, NULL);
00089
00090 PL_put_atom_chars(a0, filename);
00091
00092 qid_t query_id= PL_open_query(NULL, (PL_Q_NORMAL|PL_Q_CATCH_EXCEPTION), p, a0);
00093
00094 ret = PL_next_solution(query_id);
00095 PL_close_query(query_id);
00096 #else
00097 LINFO("SWI prolog not found");
00098 #endif
00099
00100 return ret;
00101 }
00102
00103
00104 bool SWIProlog::query(const char *predicate, std::vector<std::string> &args)
00105 {
00106 bool ret=false;
00107 #ifdef HAVE_SWI_PROLOG_H
00108 term_t a0 = PL_new_term_refs(args.size());
00109 predicate_t p = NULL;
00110
00111 p = PL_predicate(predicate, args.size(), NULL);
00112
00113 for(uint i=0; i<args.size(); i++)
00114 {
00115 if (args[i].size() != 0)
00116 PL_put_atom_chars(a0+i, args[i].c_str());
00117 }
00118
00119 qid_t query_id= PL_open_query(NULL, (PL_Q_NORMAL|PL_Q_CATCH_EXCEPTION), p, a0);
00120
00121 ret = PL_next_solution(query_id);
00122
00123 if (ret)
00124 {
00125
00126 for(uint i=0; i<args.size(); i++)
00127 {
00128 if (args[i].size() == 0)
00129 {
00130 char *data;
00131 PL_get_atom_chars(a0+i, &data);
00132 args[i] = std::string(data);
00133 }
00134 }
00135 }
00136
00137 PL_close_query(query_id);
00138 #else
00139 LINFO("SWI prolog not found");
00140 #endif
00141
00142 return ret;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 #endif // LEARN_SWIPROLOG_C_DEFINED