-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXSBPrologEngine.cpp
298 lines (252 loc) · 7.94 KB
/
XSBPrologEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/***************************************************************************
* Copyright (C) 2009 by Mushthofa *
* unintendedchoice@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/**
* @file XSBPrologEngine.cpp
* @author Mushthofa
* @date Tue 24 06 2009
*
* @brief XSB Prolog Engine interface class
*
*
*/
#include "XSBPrologEngine.h"
#include <sstream>
#include <cstring>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string/trim.hpp>
typedef boost::tokenizer<boost::char_separator<char> > dotTokenizer_t;
/* Initialize static member */
th_context* XSBPrologEngine::main_ctxt = 0;
XSBPrologThread::XSBPrologThread(th_context* ctxt)
:PrologThread(), th_ctxt(ctxt)
{
}
XSBPrologThread::~XSBPrologThread()
{
//std::cout<<"Killing my self " << std::endl;
xsb_kill_thread(th_ctxt);
}
void XSBPrologThread::doAssert(const std::string& argsterm)
{
std::string term(argsterm);
boost::trim(term);
if(term.size()==0)
throw FatalError("Trying to assert an empty term into Prolog");
std::string assert_command = "assert((" + term + ")).";
//std::cout<<assert_command<<std::endl;
char* command = new char[assert_command.size() + 1];
strcpy(command, assert_command.c_str());
int retQ = xsb_command_string(th_ctxt, command );
if(retQ == XSB_ERROR || retQ == XSB_FAILURE)
{
std::ostringstream os;
os << "XSB Error in asserting: " <<term<<":"<<xsb_get_error_message(th_ctxt)<<std::endl;
throw FatalError(os.str());
}
delete [] command;
}
void XSBPrologThread::doRetract(const std::string& term)
{
if(term.size()==0)
throw FatalError("Trying to retract an empty term into Prolog");
std::string assert_command = "retract((" + term + ")).";
//std::cout<<assert_command<<std::endl;
char* command = new char[assert_command.size() + 1];
strcpy(command, assert_command.c_str());
int retQ = xsb_command_string(th_ctxt, command );
if(retQ == XSB_ERROR ) //|| retQ == XSB_FAILURE)
{
std::ostringstream os;
os << "XSB Error in retracting: " <<term<<":"<<xsb_get_error_message(th_ctxt)<<std::endl;
throw FatalError(os.str());
}
delete [] command;
}
void XSBPrologThread::consult(const std::string& clauses)
{
//std::cout<<"Consulting "<<std::endl<<clauses<<std::endl;
boost::char_separator<char> dotSep (".\n");
dotTokenizer_t tokens(clauses, dotSep);
dotTokenizer_t::iterator t_it;
for (t_it = tokens.begin(); t_it != tokens.end(); ++t_it)
{
doAssert(*t_it);
}
//std::cout<<"Done"<<std::endl;
}
void XSBPrologThread::addFacts(const std::string& facts)
{
consult(facts);
}
void XSBPrologThread::delFacts(const std::string& facts)
{
boost::char_separator<char> dotSep (".\n");
dotTokenizer_t tokens(facts, dotSep);
dotTokenizer_t::iterator t_it;
for (t_it = tokens.begin(); t_it != tokens.end(); ++t_it)
{
doRetract(*t_it);
}
}
void XSBPrologThread::declareDynamic(const std::set<PredicateSign>& ps)
{
//std::cout<<"Declaring dynamics "<<std::endl;
std::set<PredicateSign>::const_iterator p_it;
for(p_it = ps.begin(); p_it != ps.end(); ++p_it)
{
std::ostringstream dynamic_cmd;
dynamic_cmd <<"dynamic(" << p_it->name << "/" << p_it->arity << "), ";
dynamic_cmd <<"dynamic(m_NEG_"<<p_it->name<<"/"<<p_it->arity<<"). ";
// dynamic_cmd <<"dynamic(" << p_it->name << "_m_PRIME " << "/" <<p_it->arity <<"), ";
// dynamic_cmd <<"dynamic(m_NEG_" << p_it->name << "_m_PRIME " << "/" <<p_it->arity <<"). ";
char* command = new char[dynamic_cmd.str().size() + 1];
strcpy(command, dynamic_cmd.str().c_str());
//std::cout<<p_it->name<<"/"<<p_it->arity<<", " <<std::endl;
if(xsb_command_string(th_ctxt, command) == XSB_ERROR)
{
std::ostringstream os;
os << "XSB Error in declaring dynamic: " <<p_it->name<<"/"<<p_it->arity
<<":"<<xsb_get_error_message(th_ctxt)<<std::endl;
throw FatalError(os.str());
}
delete [] command;
}
//std::cout<<" "<<std::endl;
}
bool XSBPrologThread::checkQuery(const std::string& qstr)
{
if(query_opened)
xsb_close_query(th_ctxt);
XSB_StrDefine(return_str);
int retQuery;
char *queryStr = new char[qstr.size()+1];
strcpy(queryStr, qstr.c_str());
retQuery = xsb_query_string_string(th_ctxt, queryStr, &return_str, (char*)"|");
delete[] queryStr;
bool result = false;
if(retQuery == XSB_SUCCESS)
{
result = true;
xsb_close_query(th_ctxt);
query_opened = false;
}
else if(retQuery == XSB_FAILURE)
{
result = false;
}
else if(retQuery == XSB_ERROR)
{
std::ostringstream os;
os<<"XSB Error: cannot send query "<<qstr<<std::endl<<": "<<xsb_get_error_message(th_ctxt)<<std::endl;
throw FatalError(os.str());
}
return result;
}
void XSBPrologThread::openQuery(const std::string& qstr)
{
xsb_close_query(th_ctxt);
XSB_StrDefine(return_str);
int retQuery;
char *queryStr = new char[qstr.size()+1];
strcpy(queryStr, qstr.c_str());
retQuery = xsb_query_string_string(th_ctxt, queryStr, &return_str, (char*)"|");
delete[] queryStr;
if(retQuery == XSB_SUCCESS)
{
answersleft = true;
curr_answer = (std::string) return_str.string;
}
else if(retQuery == XSB_FAILURE)
{
answersleft = false;
}
else if(retQuery == XSB_ERROR)
{
answersleft = false;
std::ostringstream os;
os<<"XSB Error: cannot send query "<<qstr<<std::endl<<": "<<xsb_get_error_message(th_ctxt)<<std::endl;
throw FatalError(os.str());
}
query_opened = true;
}
void XSBPrologThread::getNextAnswer(std::string& answer)
{
if(!query_opened || !answersleft)
throw FatalError("XSB Error: no more answers available!");
XSB_StrDefine(return_str);
answer = curr_answer;
int retQuery;
retQuery = xsb_next_string(th_ctxt, &return_str, (char*)"|");
if(retQuery == XSB_SUCCESS)
{
curr_answer = (std::string) return_str.string;
answersleft = true;
}
else
{
answersleft = false;
query_opened = false;
}
}
void XSBPrologThread::closeQuery()
{
if(query_opened)
xsb_close_query(th_ctxt);
}
XSBPrologEngine::XSBPrologEngine(const std::string& xsbpath)
:PrologEngine()
{
int myargc = 4;
char* myargv[4];
myargv[0] = new char [xsbpath.size()+1];
strcpy(myargv[0], xsbpath.c_str());
myargv[1] = (char*)"-n";
myargv[2] = (char*)"--nobanner";
myargv[3] = (char*)"--quietload";
int retInit = xsb_init(myargc, myargv);
if(retInit == XSB_ERROR)
{
std::ostringstream os;
os<<"Error initialising XSB : "<<xsb_get_init_error_message()<<std::endl;
throw FatalError(os.str());
}
else
initialised = true;
delete [] myargv[0];
main_ctxt = xsb_get_main_thread();
}
XSBPrologEngine::~XSBPrologEngine()
{
//std::cout <<"Closing XSB ... " <<std::endl;
xsb_close(main_ctxt);
}
XSBPrologThread* XSBPrologEngine::createThread() const
{
if(main_ctxt)
{
th_context* new_ctxt;
xsb_ccall_thread_create(main_ctxt, &new_ctxt);
return new XSBPrologThread(new_ctxt);
//return new_thread;
}
else
throw FatalError("XSB Prolog Engine is not initialised yet, cannot create a new Prolog thread!");
}
//End