00001 #define TRACE_NAME "XSBPool"
00002 #include "XSBPool.h"
00003
00004 #include "XSBQuery.h"
00005
00007
00008
00009
00011
00012
00013 XSBPool::XSBPool(XSBAgent& agent, VariableScope* scope)
00014 : agent(agent),
00015 cache(scope),
00016 maybeChanged(true)
00017 {
00018 file = agent.getFile(&fileName);
00019 predicate = agent.getNewPredicate();
00020 agent.pools.push_back(this);
00021 TRACE "XSBPool() @" << this << " using file " << fileName << endl;
00022 }
00023
00024 #if 0
00025
00026 XSBPool::XSBPool(const XSBPool& other)
00027 {
00028 NOT_IMPLEMENTED
00029 }
00030
00031 const XSBPool& XSBPool::operator=(const XSBPool& other)
00032 {
00033 NOT_IMPLEMENTED
00034 }
00035
00036 bool XSBPool::operator==(const XSBPool& other) const
00037 {
00038 NOT_IMPLEMENTED
00039 }
00040
00041 bool XSBPool::operator<(const XSBPool& other) const
00042 {
00043 NOT_IMPLEMENTED
00044 }
00045
00046 size_t XSBPool::hash() const
00047 {
00048 NOT_IMPLEMENTED
00049 }
00050
00051 #endif
00052
00053 std::ostream& XSBPool::print_to(std::ostream& stream) const
00054 {
00055 cache.print_to(stream);
00056 return stream;
00057 }
00058
00059
00060 XSBPool::~XSBPool()
00061 {
00062 agent.pools.remove(this);
00063 free(fileName);
00064 }
00065
00067
00068
00069
00071
00072
00073 static void printTriple(std::ostream& out, Triple t, int* maxVar)
00074 {
00075 for (int i=0; i<3; i++) {
00076 Symbol s = t.get(i);
00077 if (s.isVar()) {
00078 int index = s.index();
00079 out << "var(" << index;
00080 if (maxVar && index > *maxVar) *maxVar = index;
00081 out << ")";
00082 } else {
00083 out << s.index();
00084 }
00085 if (i<2) out << ",";
00086 }
00087 }
00088
00089 void XSBPool::sync()
00090 {
00091 if (maybeChanged) {
00092 maybeChanged = false;
00093
00094
00095 *file << "% This is prolog file written by Blindfold::XSBPool.\n\n";
00096
00097 LoopState l;
00098 size_t dcount=0;
00099 while (Triple t = cache.fetch(Triple::null, l)) {
00100 *file << predicate << "(";
00101 printTriple(*file, t, 0);
00102 *file << ").\n";
00103 dcount++;
00104 }
00105 if (dcount == 0) {
00106
00107
00108 *file << predicate << "(place_holder(0), place_holder(1), place_holder(2)).\n";
00109 }
00110 }
00111 file->flush();
00112 *(agent.out) << "['" << fileName << "'].\n";
00113
00114 }
00115
00116
00118
00119
00120
00122
00123 #undef TRACE_NAME