00001 #define TRACE_NAME "XSBQuery"
00002
00003 #include "XSBQuery.h"
00004
00005 #include "XSBAgent.h"
00006 #include "FlowManager.h"
00007 #include "XSBPool.h"
00008
00010
00011
00012
00014
00015 XSBQuery::XSBQuery(XSBAgent& agent, const XSBPool& dataset,
00016 const TripleSource& pattern)
00017 : Query(pattern),
00018 pattern(pattern),
00019 dataset(dataset),
00020 agent(agent),
00021 open(true),
00022 running(false),
00023 varsMatchOnlyVars(true)
00024 {
00025 assert(agent.currentQuery == 0);
00026 agent.currentQuery = this;
00027 file = agent.getFile(&fileName);
00028 runPredicate = agent.getNewPredicate();
00029 }
00030
00031 #if 0
00032
00033 XSBQuery::XSBQuery(const XSBQuery& other)
00034 {
00035 NOT_IMPLEMENTED
00036 }
00037
00038 const XSBQuery& XSBQuery::operator=(const XSBQuery& other)
00039 {
00040 NOT_IMPLEMENTED
00041 }
00042
00043 bool XSBQuery::operator==(const XSBQuery& other) const
00044 {
00045 NOT_IMPLEMENTED
00046 }
00047
00048 bool XSBQuery::operator<(const XSBQuery& other) const
00049 {
00050 NOT_IMPLEMENTED
00051 }
00052
00053 size_t XSBQuery::hash() const
00054 {
00055 NOT_IMPLEMENTED
00056 }
00057
00058 std::ostream& XSBQuery::print_to(std::ostream& stream) const
00059 {
00060 NOT_IMPLEMENTED
00061 }
00062
00063 #endif
00064
00065 XSBQuery::~XSBQuery()
00066 {
00067 close();
00068 }
00069
00071
00072
00073
00075
00076 void XSBQuery::close()
00077 {
00078 if (open) {
00079 if (running) *(agent.out) << "done.\n";
00080 agent.currentQuery = 0;
00081 open = false;
00082 }
00083 }
00084
00085 Triple XSBQuery::getFailurePoint()
00086 {
00087 return failurePoint;
00088 }
00089
00090 bool XSBQuery::advanceToFirst()
00091 {
00092 running = true;
00093 agent.sync();
00094 TRACE "** Sync'd" << endl;
00095 writeFile();
00096 TRACE "** Written" << endl;
00097 *(agent.out) << "['" << fileName << "'].\n";
00098 readIndex = 0;
00099 state = SEARCHING;
00100 *(agent.out) << runPredicate << ".\n";
00101 return true;
00102 }
00103
00104 bool XSBQuery::advanceToNext()
00105 {
00106 *(agent.out) << "more.\n";
00107 return true;
00108 }
00109
00110 bool XSBQuery::getResults()
00111 {
00112 state = SEARCHING;
00113 FlowManager::main.run();
00114 TRACE "returned from run(), state=" << state << endl;
00115
00116
00117 if (!moreResultsPossible) running = false;
00118 return moreResultsPossible;
00119
00120 }
00121
00122
00124
00125
00126
00128
00129 void XSBQuery::printTriple(std::ostream& out, Triple t, int* maxVar)
00130 {
00131 for (int i=0; i<3; i++) {
00132 Symbol s = t.get(i);
00133 if (s.isVar()) {
00134 int index = s.index();
00135 if (varsMatchOnlyVars) out << "var(";
00136 out << "V" << index;
00137 if (maxVar && index > *maxVar) *maxVar = index;
00138 if (varsMatchOnlyVars) out << ")";
00139 } else {
00140 out << s.index();
00141 }
00142 if (i<2) out << ",";
00143 }
00144 }
00145
00146 void XSBQuery::writeFile()
00147 {
00148 *file << "% This is prolog file written by Blindfold::XSBQuery.\n\n";
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 LoopState l;
00166
00167 *file << runPredicate << " :- (\n";
00168
00169 size_t pcount=0;
00170 maxVar=-1;
00171 while (Triple t = pattern.fetch(Triple::null, l)) {
00172 pv.push_back(t);
00173 *file << "set_status(" << pcount << "), ";
00174 *file << dataset.predicate << "(";
00175 printTriple(*file, t, &maxVar);
00176
00177 *file << "),\n";
00178 pcount++;
00179 }
00180
00181 *file << "put(1), writeln(" << MATCH_FOLLOWS << "),\n";
00182 size_t position = 0;
00183 for (int i=0; i<=maxVar; i++) {
00184 *file << "writeln(V" << i << "),\n";
00185
00186 position++;
00187 }
00188 #if 0 // we're not doing triple-numbers at the moment -- no need
00189 for (unsigned int i=0; i<pcount; i++) {
00190 *file << "writeln(T" << i << "),\n";
00191
00192 position++;
00193 }
00194 #endif
00195 *file << "put(4),\n";
00196
00197
00198 *file << "read(DoneFlag), DoneFlag=done\n";
00199
00200
00201 *file << "); put(1), writeln(" << FAILPOINT_FOLLOWS <<
00202 "), get_status(Status), writeln(Status), put(4), put(10).\n\n";
00203
00204
00205 file->flush();
00206 TRACE "Wrote file " << fileName << std::endl;
00207
00208 }
00209
00210 void XSBQuery::handleValue(int value)
00211 {
00212 TRACE " state=" << state << ", value=" << value << ". ";
00213 Triple pt;
00214 switch (state) {
00215 case SEARCHING:
00216 switch (value) {
00217 case MATCH_FOLLOWS:
00218 state = READING_MATCH;
00219 success = true;
00220 TRACE "match follows!";
00221 break;
00222 case FAILPOINT_FOLLOWS:
00223 state = READING_FAILURE;
00224 TRACE "failure follows!";
00225 success = false;
00226 break;
00227 default:
00228 throw(new std::string("huh"));
00229 }
00230 break;
00231 case READING_MATCH:
00232 if (readIndex <= maxVar) {
00233 Symbol v = pattern.getScope()->getVar(readIndex);
00234 TRACE "read pos[" << readIndex << "] = variable:" << v;
00235 if (value == -1) {
00236 TRACE " *unbound*" << std::endl;
00237 } else {
00238 Symbol b = Symbol(&Symbol::global, value);
00239 v.bind(b);
00240 TRACE " bind(" << b << ")" << std::endl;
00241 }
00242 } else {
00243 state = EXPECTING_END;
00244 #if 0
00245 int pos = (readIndex - maxVar) - 1;
00246
00247 Triple pt = pv[value];
00248
00249
00250
00251
00252
00253
00254 #endif
00255 }
00256 readIndex++;
00257 break;
00258 case READING_FAILURE:
00259 pt = pv[value];
00260 TRACE "\n\n############\n\nfailed on " << value << ", t=" << pt << std::endl;
00261 TRACE "NOT IN " << dataset << "\n\n";
00262 failurePoint = pt;
00263 moreResultsPossible = false;
00264 state = EXPECTING_END;
00265 break;
00266 case EXPECTING_END:
00267 if (value == -1) {
00268 FlowManager::main.returnFromRun();
00269 TRACE "\nQuery fetch is done!" << endl;
00270 state = ENDED;
00271 return;
00272 } else {
00273 TRACE "XSB protocol error: didn't get end when expecting it!" << endl;
00274 }
00275 break;
00276 case ENDED:
00277 assert(false);
00278 break;
00279 }
00280 TRACE std::endl;
00281 }
00282
00283 #undef TRACE_NAME