Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

XSBQuery.cpp

00001 #define TRACE_NAME "XSBQuery"
00002 
00003 #include "XSBQuery.h"
00004 // $Id: XSBQuery_8cpp-source.html,v 1.2 2001/10/10 20:40:58 sandro Exp $
00005 #include "XSBAgent.h"
00006 #include "FlowManager.h"
00007 #include "XSBPool.h"
00008 
00010 //
00011 //  Standard Member Functions
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  /* omit definitions until they are implemented */
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 /* omit definitions until they are implemented */
00064     
00065 XSBQuery::~XSBQuery()
00066 {
00067     close();
00068 }
00069 
00071 //
00072 //  Additional Public Member Functions
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     // ...?   stuff with .bindings?
00117     if (!moreResultsPossible) running = false;
00118     return moreResultsPossible;
00119     
00120 }
00121 
00122 
00124 //
00125 //  Additional Private Member Functions
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     // write the dataset
00152     /*
00153       Theory Break...
00154 
00155       I think we need to keep the variables in the dataset as real variables!
00156       
00157       But then how can we get their values?  I don't really know how.
00158 
00159       But we could get triple-id-->triple-id matching, I suppose which
00160       would do most of the work for us.  And if we had a match, we
00161       could ask what was left over, too.
00162 
00163     */
00164 
00165     LoopState l;
00166     // write the pattern rule
00167     *file << runPredicate << " :- (\n";
00168     // first, the main pattern body
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     // *file << ",T" << pcount << "),\n";
00177     *file << "),\n";
00178     pcount++;
00179     }
00180     // then flag success & print the variable's values
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     // TRACE "pos[" << position << "] = variable:" << pattern->getScope()->getVar(i) << std::endl;
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     // TRACE "pos[" << position << "] = triple: " << dv[i] << std::endl;
00192     position++;
00193     }
00194 #endif
00195     *file << "put(4),\n";
00196 
00197     // then prompt for continue or not
00198     *file << "read(DoneFlag), DoneFlag=done\n";
00199 
00200     // then give an alternative (for failure) to signal completion
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         //Triple dt = dv[pos];
00247         Triple pt = pv[value];
00248         //TRACE "read pos=" << pos << ", " << dt << " == " << pt << std::endl;
00249 
00250             // set the variables
00251 
00252             // remove this one from some set to find out what's
00253             // remaining.
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

Home to blindfold. This page generated via doxygen 1.2.11.1 Wed Oct 10 16:40:33 2001.