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
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef DATABASE_H_JUN_28_2005
00067 #define DATABASE_H_JUN_28_2005
00068
00069 #include <set>
00070 #include "hash.h"
00071 #include "groundpreds.h"
00072 #include "domain.h"
00073 #include "arraysaccessor.h"
00074 #include "groundpredicate.h"
00075
00076
00077
00078 typedef pair<unsigned long long, unsigned int> MultAndType;
00079
00080 const int dbdebug = 0;
00081
00082
00083
00084 enum IndexType { T_INDEX = 0, F_INDEX = 1, A_INDEX = 2, INDEX_TYPE_COUNT = 3 };
00085
00086 struct NumTrueFalse
00087 {
00088 NumTrueFalse(): numTrue(0), numFalse(0) {}
00089 int numTrue;
00090 int numFalse;
00091 };
00092
00093
00095 class HashLongLong
00096 {
00097 public:
00098 size_t operator()(const unsigned long long ld) const
00099 {
00100 return (size_t)ld;
00101 }
00102 };
00103
00104
00105 class EqualLongLong
00106 {
00107 public:
00108 bool operator()(const unsigned long long ld1, const unsigned long long ld2)
00109 const
00110 { return ld1 == ld2; }
00111 };
00112
00114
00115 typedef hash_set<unsigned long long, HashLongLong, EqualLongLong>
00116 LongLongHashSet;
00117
00118 typedef hash_map<pair<unsigned int, unsigned int>, set<unsigned long long>,
00119 IntPairHash>
00120 LongLongHashMap;
00121
00122
00123 class Database
00124 {
00125 public:
00126
00127
00128 Database(Domain* const & d, const Array<bool>& closedWorld,
00129 const bool &storeGndPreds) : domain_(d)
00130 {
00131 lazyFlag_ = false;
00132 performingInference_ = false;
00133 assert(closedWorld.size() == domain_->getNumPredicates());
00134 int numFOPreds = domain_->getNumPredicates();
00135
00136 int numTypes = domain_->getNumTypes();
00137 firstConstIdByType_.growToSize(numTypes);
00138 for (int i = 0; i < numTypes; i++)
00139 {
00140 assert(domain_->isType(i));
00141 const Array<int>* constIds = domain_->getConstantsByTypeWithExt(i);
00142 if (constIds->empty()) firstConstIdByType_[i] = (unsigned int)0;
00143 else firstConstIdByType_[i] = (*constIds)[0];
00144 delete constIds;
00145 }
00146
00147 closedWorld_.growToSize(numFOPreds);
00148 memcpy((void*)closedWorld_.getItems(), closedWorld.getItems(),
00149 closedWorld.size()*sizeof(bool));
00150
00151 termMultByPred_.growToSize(numFOPreds, NULL);
00152
00153 truePredIdxSet_ = new Array<LongLongHashSet>(numFOPreds);
00154 falsePredIdxSet_ = new Array<LongLongHashSet>(numFOPreds);
00155 numberOfGroundings_.growToSize(numFOPreds);
00156 truePredIdxSet_->growToSize(numFOPreds);
00157 falsePredIdxSet_->growToSize(numFOPreds);
00158
00159
00160 trueEvIndex_ = new Array<LongLongHashMap>(numFOPreds);
00161 trueEvIndex_->growToSize(numFOPreds);
00162 falseEvIndex_ = new Array<LongLongHashMap>(numFOPreds);
00163 falseEvIndex_->growToSize(numFOPreds);
00164 activeIndex_ = new Array<LongLongHashMap>(numFOPreds);
00165 activeIndex_->growToSize(numFOPreds);
00166
00167 for (int i = 0; i < numFOPreds; i++)
00168 {
00169
00170
00171 const PredicateTemplate* t = domain_->getPredicateTemplate(i);
00172 if (t->isEqualPredWithType()) continue;
00173
00174
00175 const Array<int>* termTypes = domain_->getPredicateTermTypesAsInt(i);
00176 Array<MultAndType>* matArr = new Array<MultAndType>;
00177 int numTermTypes = termTypes->size();
00178 matArr->growToSize(numTermTypes);
00179 unsigned long long curMult = 1;
00180 for (int j = numTermTypes-1; j >= 0; j--)
00181 {
00182 int typeId = (*termTypes)[j];
00183 (*matArr)[j] = MultAndType(curMult,typeId);
00184 curMult *= domain_->getNumConstantsByTypeWithExt(typeId);
00185 }
00186 termMultByPred_[i] = matArr;
00187
00188 int numGnd = 1;
00189 for (int j = 0; j < t->getNumTerms(); j++)
00190 {
00191 int numConstByType =
00192 domain_->getNumConstantsByType(t->getTermTypeAsInt(j));
00193 numGnd *= numConstByType;
00194 }
00195 numberOfGroundings_[i] = numGnd;
00196 }
00197
00198 predIdToNumTF_ = new Array<NumTrueFalse>(numFOPreds);
00199 predIdToNumTF_->growToSize(numFOPreds);
00200
00201 activePredIdxSet_ = NULL;
00202 evidencePredIdxSet_ = NULL;
00203 deactivatedPredIdxSet_ = NULL;
00204 }
00205
00206
00207 Database(const Database& db)
00208 {
00209 domain_ = db.domain_;
00210 closedWorld_.growToSize(db.closedWorld_.size());
00211 memcpy((void*)closedWorld_.getItems(), db.closedWorld_.getItems(),
00212 db.closedWorld_.size()*sizeof(bool));
00213
00214 lazyFlag_ = db.lazyFlag_;
00215 performingInference_ = db.performingInference_;
00216 firstConstIdByType_ = db.firstConstIdByType_;
00217 numberOfGroundings_ = db.numberOfGroundings_;
00218
00219 oppEqGndPreds_ = db.oppEqGndPreds_;
00220
00221 termMultByPred_.growToSize(db.termMultByPred_.size(), NULL);
00222 for (int i = 0; i < db.termMultByPred_.size(); i++)
00223 {
00224 if (db.termMultByPred_[i])
00225 {
00226 Array<MultAndType>* matArr = new Array<MultAndType>;
00227 matArr->growToSize(db.termMultByPred_[i]->size());
00228 for (int j = 0; j < db.termMultByPred_[i]->size(); j++)
00229 {
00230 MultAndType mat;
00231 mat.first = (*db.termMultByPred_[i])[j].first;
00232 mat.second = (*db.termMultByPred_[i])[j].second;
00233 (*matArr)[j] = mat;
00234 }
00235 termMultByPred_[i] = matArr;
00236 }
00237 }
00238
00239 if (db.trueEvIndex_)
00240 {
00241 trueEvIndex_ = new Array<LongLongHashMap>(db.trueEvIndex_->size());
00242 trueEvIndex_->growToSize(db.trueEvIndex_->size());
00243 for (int i = 0; i < db.trueEvIndex_->size(); i++)
00244 {
00245 (*trueEvIndex_)[i] = (*db.trueEvIndex_)[i];
00246 }
00247 }
00248
00249 if (db.falseEvIndex_)
00250 {
00251 falseEvIndex_ = new Array<LongLongHashMap>(db.falseEvIndex_->size());
00252 falseEvIndex_->growToSize(db.falseEvIndex_->size());
00253 for (int i = 0; i < db.falseEvIndex_->size(); i++)
00254 {
00255 (*falseEvIndex_)[i] = (*db.falseEvIndex_)[i];
00256 }
00257 }
00258
00259 if (db.activeIndex_)
00260 {
00261 activeIndex_ = new Array<LongLongHashMap>(db.activeIndex_->size());
00262 activeIndex_->growToSize(db.activeIndex_->size());
00263 for (int i = 0; i < db.activeIndex_->size(); i++)
00264 {
00265 (*activeIndex_)[i] = (*db.activeIndex_)[i];
00266 }
00267 }
00268
00269 if (db.predIdToNumTF_)
00270 {
00271 predIdToNumTF_ = new Array<NumTrueFalse>(db.predIdToNumTF_->size());
00272 predIdToNumTF_->growToSize(db.predIdToNumTF_->size());
00273 for (int i = 0; i < db.predIdToNumTF_->size(); i++)
00274 {
00275 (*predIdToNumTF_)[i] = (*db.predIdToNumTF_)[i];
00276 }
00277 }
00278
00279 if (db.truePredIdxSet_)
00280 {
00281 truePredIdxSet_ = new Array<LongLongHashSet>(db.truePredIdxSet_->size());
00282 truePredIdxSet_->growToSize(db.truePredIdxSet_->size());
00283 for (int i = 0; i < db.truePredIdxSet_->size(); i++)
00284 {
00285 (*truePredIdxSet_)[i] = (*db.truePredIdxSet_)[i];
00286 }
00287 }
00288
00289 if (db.falsePredIdxSet_)
00290 {
00291 falsePredIdxSet_ = new Array<LongLongHashSet>();
00292 falsePredIdxSet_->growToSize(db.falsePredIdxSet_->size());
00293 for (int i = 0; i < db.falsePredIdxSet_->size(); i++)
00294 (*falsePredIdxSet_)[i] = (*db.falsePredIdxSet_)[i];
00295 }
00296
00297 if (db.activePredIdxSet_)
00298 {
00299 activePredIdxSet_ = new Array<LongLongHashSet>();
00300 activePredIdxSet_->growToSize(db.activePredIdxSet_->size());
00301 for (int i = 0; i < db.activePredIdxSet_->size(); i++)
00302 (*activePredIdxSet_)[i] = (*db.activePredIdxSet_)[i];
00303 }
00304
00305 if (db.evidencePredIdxSet_)
00306 {
00307 evidencePredIdxSet_ = new Array<LongLongHashSet>();
00308 evidencePredIdxSet_->growToSize(db.evidencePredIdxSet_->size());
00309 for (int i = 0; i < db.evidencePredIdxSet_->size(); i++)
00310 (*evidencePredIdxSet_)[i] = (*db.evidencePredIdxSet_)[i];
00311 }
00312
00313 if (db.deactivatedPredIdxSet_)
00314 {
00315 deactivatedPredIdxSet_ = new Array<LongLongHashSet>();
00316 deactivatedPredIdxSet_->growToSize(db.deactivatedPredIdxSet_->size());
00317 for (int i = 0; i < db.deactivatedPredIdxSet_->size(); i++)
00318 (*deactivatedPredIdxSet_)[i] = (*db.deactivatedPredIdxSet_)[i];
00319 }
00320 }
00321
00322 ~Database()
00323 {
00324 for (int i = 0; i < termMultByPred_.size(); i++)
00325 if (termMultByPred_[i]) delete termMultByPred_[i];
00326 termMultByPred_.clearAndCompress();
00327
00328 for (int i = 0; i < truePredIdxSet_->size(); i++)
00329 (*truePredIdxSet_)[i].clear();
00330 delete truePredIdxSet_;
00331
00332 for (int i = 0; i < falsePredIdxSet_->size(); i++)
00333 (*falsePredIdxSet_)[i].clear();
00334 delete falsePredIdxSet_;
00335
00336 if (lazyFlag_)
00337 {
00338 for (int i = 0; i < activePredIdxSet_->size(); i++)
00339 (*activePredIdxSet_)[i].clear();
00340 delete activePredIdxSet_;
00341
00342 for (int i = 0; i < evidencePredIdxSet_->size(); i++)
00343 (*evidencePredIdxSet_)[i].clear();
00344 delete evidencePredIdxSet_;
00345
00346 for (int i = 0; i < deactivatedPredIdxSet_->size(); i++)
00347 (*deactivatedPredIdxSet_)[i].clear();
00348 delete deactivatedPredIdxSet_;
00349 }
00350
00351 if (predIdToNumTF_) { delete predIdToNumTF_; predIdToNumTF_ = NULL; }
00352
00353 for (int i = 0; i < trueEvIndex_->size(); i++)
00354 (*trueEvIndex_)[i].clear();
00355 delete trueEvIndex_;
00356
00357 for (int i = 0; i < falseEvIndex_->size(); i++)
00358 (*falseEvIndex_)[i].clear();
00359 delete falseEvIndex_;
00360
00361 for (int i = 0; i < activeIndex_->size(); i++)
00362 (*activeIndex_)[i].clear();
00363 delete activeIndex_;
00364
00365 }
00366
00367 void compress()
00368 {
00369 for (int i = 0; i < termMultByPred_.size(); i++)
00370 if (termMultByPred_[i]) termMultByPred_[i]->compress();
00371 termMultByPred_.compress();
00372
00373 firstConstIdByType_.compress();
00374 }
00375
00376 void printInfo()
00377 {
00378 cout << "GNDINGS " << endl;
00379 for (int i = 0; i < numberOfGroundings_.size(); i++)
00380 cout << i << ": " << numberOfGroundings_[i] << endl;
00381
00382 cout << "TRUE " << truePredIdxSet_->size() << endl;
00383 for (int i = 0; i < truePredIdxSet_->size(); i++)
00384 {
00385 LongLongHashSet hs = (*truePredIdxSet_)[i];
00386 cout << i << ": " << hs.size() << endl;
00387 }
00388 cout << "FALSE " << falsePredIdxSet_->size() << endl;
00389 for (int i = 0; i < falsePredIdxSet_->size(); i++)
00390 {
00391 LongLongHashSet hs = (*falsePredIdxSet_)[i];
00392 cout << i << ": " << hs.size() << endl;
00393 }
00394 cout << "ACTIVE " << activePredIdxSet_->size() << endl;
00395 cout << "EVIDENCE " << evidencePredIdxSet_->size() << endl;
00396 cout << "DEACTIVE " << deactivatedPredIdxSet_->size() << endl;
00397 }
00398
00399
00400 void setLazyFlag()
00401 {
00402 setLazyFlag(true);
00403 }
00404
00405 void setLazyFlag(const bool& lf)
00406 {
00407
00408 if ((lf && lazyFlag_) || (!lf && !lazyFlag_)) return;
00409
00410
00411 if (activePredIdxSet_)
00412 {
00413 for (int i = 0; i < activePredIdxSet_->size(); i++)
00414 (*activePredIdxSet_)[i].clear();
00415 delete activePredIdxSet_;
00416 activePredIdxSet_ = NULL;
00417 }
00418 if (evidencePredIdxSet_)
00419 {
00420 for (int i = 0; i < evidencePredIdxSet_->size(); i++)
00421 (*evidencePredIdxSet_)[i].clear();
00422 delete evidencePredIdxSet_;
00423 evidencePredIdxSet_ = NULL;
00424 }
00425 if (deactivatedPredIdxSet_)
00426 {
00427 for (int i = 0; i < deactivatedPredIdxSet_->size(); i++)
00428 (*deactivatedPredIdxSet_)[i].clear();
00429 delete deactivatedPredIdxSet_;
00430 deactivatedPredIdxSet_ = NULL;
00431 }
00432 if (lf)
00433 {
00434 lazyFlag_ = true;
00435 int numFOPreds = domain_->getNumPredicates();
00436
00437 activePredIdxSet_ = new Array<LongLongHashSet>(numFOPreds);
00438 evidencePredIdxSet_ = new Array<LongLongHashSet>(numFOPreds);
00439 deactivatedPredIdxSet_ = new Array<LongLongHashSet>(numFOPreds);
00440 activePredIdxSet_->growToSize(numFOPreds);
00441 evidencePredIdxSet_->growToSize(numFOPreds);
00442 deactivatedPredIdxSet_->growToSize(numFOPreds);
00443 }
00444 if (!lf)
00445 {
00446 lazyFlag_ = false;
00447 }
00448 }
00449
00450
00451 bool setPerformingInference(bool pi)
00452 {
00453 bool previous = performingInference_;
00454 performingInference_ = pi;
00455 return previous;
00456 }
00457
00458 const Domain* getDomain() const { return domain_; }
00459
00460 static bool sameTruthValueAndSense(const TruthValue& tv, const bool& sense)
00461 { return (tv==TRUE && sense) || (tv==FALSE && !sense); }
00462
00466 TruthValue getValue(const Predicate* const& pred) const
00467 {
00468 if (dbdebug >= 1) cout << "Calling database::getValue" << endl;
00469 assert(((Predicate*)pred)->isGrounded());
00470 Predicate* ppred = (Predicate*) pred;
00471 int predId = pred->getId();
00472
00473
00474 if (pred->isEqualPredWithType())
00475 {
00476
00477
00478 TruthValue actual
00479 = (pred->getTerm(0)->getId()==pred->getTerm(1)->getId()) ? TRUE : FALSE;
00480
00481
00482 for (int i = 0; i < oppEqGndPreds_.size(); i++)
00483 if (ppred->same(oppEqGndPreds_[i]))
00484 {
00485 if (actual == TRUE)
00486 {
00487 if (dbdebug >= 1) cout << "Returning FALSE" << endl;
00488 return FALSE;
00489 }
00490 if (dbdebug >= 1) cout << "Returning TRUE" << endl;
00491 return TRUE;
00492 }
00493
00494 if (dbdebug >= 1) cout << "Returning " << actual << endl;
00495 return actual;
00496 }
00497
00498 unsigned long long idx = getIdxOfGndPredValues(pred);
00499 return getValue(idx, predId);
00500 }
00501
00506 TruthValue getValue(const GroundPredicate* const& pred) const
00507 {
00508 if (dbdebug >= 1) cout << "Calling database::getValue" << endl;
00509 int predId = pred->getId();
00510
00511 unsigned long long idx = getIdxOfGndPredValues(pred);
00512 return getValue(idx, predId);
00513 }
00514
00518 bool getActiveStatus(const Predicate* const& pred) const
00519 {
00520 if (dbdebug >= 1) cout << "Calling database::getActiveStatus" << endl;
00521 if (pred->isEqualPredWithType()) return false;
00522 assert(lazyFlag_);
00523 assert(((Predicate*)pred)->isGrounded());
00524
00525 unsigned long long idx = getIdxOfGndPredValues(pred);
00526 return getActiveStatus(idx, pred->getId());
00527 }
00528
00533 bool getActiveStatus(const GroundPredicate* const& pred) const
00534 {
00535 if (dbdebug >= 1) cout << "Calling database::getActiveStatus" << endl;
00536 assert(lazyFlag_);
00537
00538 unsigned long long idx = getIdxOfGndPredValues(pred);
00539 return getActiveStatus(idx, pred->getId());
00540 }
00541
00542
00546 bool getDeactivatedStatus(const Predicate* const& pred) const
00547 {
00548 if (dbdebug >= 1) cout << "Calling database::getDeactivatedStatus" << endl;
00549 if (pred->isEqualPredWithType()) return false;
00550 assert(lazyFlag_);
00551 assert(((Predicate*)pred)->isGrounded());
00552
00553 unsigned long long idx = getIdxOfGndPredValues(pred);
00554 return getDeactivatedStatus(idx, pred->getId());
00555 }
00556
00561 bool getDeactivatedStatus(const GroundPredicate* const& pred) const
00562 {
00563 if (dbdebug >= 1) cout << "Calling database::getDeactivatedStatus" << endl;
00564 assert(lazyFlag_);
00565
00566 unsigned long long idx = getIdxOfGndPredValues(pred);
00567 return getDeactivatedStatus(idx, pred->getId());
00568 }
00569
00573 bool getEvidenceStatus(const Predicate* const& pred) const
00574 {
00575 if (dbdebug >= 1) cout << "Calling database::getEvidenceStatus" << endl;
00576 if (pred->isEqualPredWithType()) return true;
00577 assert(lazyFlag_);
00578 assert(((Predicate*)pred)->isGrounded());
00579
00580 unsigned long long idx = getIdxOfGndPredValues(pred);
00581 return getEvidenceStatus(idx, pred->getId());
00582 }
00583
00588 bool getEvidenceStatus(const GroundPredicate* const& pred) const
00589 {
00590 if (dbdebug >= 1) cout << "Calling database::getEvidenceStatus" << endl;
00591 assert(lazyFlag_);
00592
00593 unsigned long long idx = getIdxOfGndPredValues(pred);
00594 return getEvidenceStatus(idx, pred->getId());
00595 }
00596
00597 string getValueAsString(const Predicate* const& pred) const
00598 {
00599 TruthValue tv = getValue(pred);
00600 if (tv == TRUE) return "TRUE";
00601 if (tv == FALSE) return "FALSE";
00602 if (tv == UNKNOWN) return "UNKNOWN";
00603 assert(false);
00604 return "UNKNOWN";
00605 }
00606
00607
00618 TruthValue setValue(const Predicate* const & pred, const TruthValue& tv)
00619 { return setValueHelper(pred, false, tv); }
00620
00631 TruthValue setValue(const GroundPredicate* const & pred, const TruthValue& tv)
00632 {
00633 return setValueHelper(pred, false, tv);
00634 }
00635
00642 TruthValue flipValue(const Predicate* const & pred)
00643 { return setValueHelper(pred, true, UNKNOWN); }
00644
00651 TruthValue flipValue(const GroundPredicate* const & pred)
00652 { return setValueHelper(pred, true, UNKNOWN); }
00653
00660 bool setActiveStatus(const Predicate* const & pred, const bool& as)
00661 {
00662 if (dbdebug >= 1) cout << "Calling database::setActiveStatus" << endl;
00663 if (pred->isEqualPredWithType())
00664 {
00665 if (dbdebug >= 1) cout << "Returning false" << endl;
00666 return false;
00667 }
00668
00669 if (as) assert(!getEvidenceStatus(pred));
00670
00671 assert(lazyFlag_);
00672 assert(((Predicate*)pred)->isGrounded());
00673
00674 int predId = pred->getId();
00675 unsigned long long idx = getIdxOfGndPredValues(pred);
00676 return setActiveStatus(idx, predId, as, pred, NULL);
00677 }
00678
00685 bool setActiveStatus(const GroundPredicate* const & pred, const bool& as)
00686 {
00687 if (dbdebug >= 1) cout << "Calling database::setActiveStatus" << endl;
00688
00689 if (as) assert(!getEvidenceStatus(pred));
00690 assert(lazyFlag_);
00691
00692 int predId = pred->getId();
00693 unsigned long long idx = getIdxOfGndPredValues(pred);
00694 return setActiveStatus(idx, predId, as, NULL, pred);
00695 }
00696
00697 void resetActiveStatus()
00698 {
00699 assert(lazyFlag_);
00700 for (int i = 0; i < activePredIdxSet_->size(); i++)
00701 (*activePredIdxSet_)[i].clear();
00702 }
00703
00710 bool setDeactivatedStatus(const Predicate* const & pred, const bool& das)
00711 {
00712 if (dbdebug >= 1) cout << "Calling database::setDeactivatedStatus" << endl;
00713 if (pred->isEqualPredWithType())
00714 {
00715 if (dbdebug >= 1) cout << "Returning false" << endl;
00716 return false;
00717 }
00718
00719 if (das) assert(!getEvidenceStatus(pred));
00720
00721 assert(lazyFlag_);
00722 assert(((Predicate*)pred)->isGrounded());
00723
00724 int predId = pred->getId();
00725 unsigned long long idx = getIdxOfGndPredValues(pred);
00726 return setDeactivatedStatus(idx, predId, das);
00727 }
00728
00735 bool setDeactivatedStatus(const GroundPredicate* const & pred,
00736 const bool& das)
00737 {
00738 if (dbdebug >= 1) cout << "Calling database::setDeactivatedStatus" << endl;
00739
00740 if (das) assert(!getEvidenceStatus(pred));
00741 assert(lazyFlag_);
00742
00743 int predId = pred->getId();
00744 unsigned long long idx = getIdxOfGndPredValues(pred);
00745 return setDeactivatedStatus(idx, predId, das);
00746 }
00747
00748 void resetDeactivatedStatus()
00749 {
00750 assert(lazyFlag_);
00751 for (int i = 0; i < deactivatedPredIdxSet_->size(); i++)
00752 (*deactivatedPredIdxSet_)[i].clear();
00753 }
00754
00761 bool setEvidenceStatus(const Predicate* const & pred, const bool& es)
00762 {
00763 if (dbdebug >= 1) cout << "Calling database::setEvidenceStatus" << endl;
00764 if (pred->isEqualPredWithType())
00765 {
00766 if (dbdebug >= 1) cout << "Returning true" << endl;
00767 return true;
00768 }
00769
00770 if (es) assert(!getActiveStatus(pred));
00771
00772 assert(lazyFlag_);
00773 assert(((Predicate*)pred)->isGrounded());
00774
00775 int predId = pred->getId();
00776 unsigned long long idx = getIdxOfGndPredValues(pred);
00777 return setEvidenceStatus(idx, predId, es);
00778 }
00779
00786 bool setEvidenceStatus(const GroundPredicate* const & pred, const bool& es)
00787 {
00788 if (dbdebug >= 1) cout << "Calling database::setEvidenceStatus" << endl;
00789
00790 if (es) assert(!getActiveStatus(pred));
00791 assert(lazyFlag_);
00792
00793 int predId = pred->getId();
00794 unsigned long long idx = getIdxOfGndPredValues(pred);
00795 return setEvidenceStatus(idx, predId, es);
00796 }
00797
00798 void setValuesToUnknown(const Array<Predicate*>* const & gndPreds,
00799 Array<TruthValue>* const & prevValues)
00800 {
00801 if (prevValues) prevValues->clear();
00802 for (int i = 0; i < gndPreds->size(); i++)
00803 {
00804 TruthValue prev = setValue((*gndPreds)[i], UNKNOWN);
00805 if (prevValues) prevValues->append(prev);
00806 }
00807 }
00808
00809 void setValuesToUnknown(const GroundPredicateHashArray* const & gndPreds,
00810 Array<TruthValue>* const & prevValues)
00811 {
00812 if (prevValues) prevValues->clear();
00813 for (int i = 0; i < gndPreds->size(); i++)
00814 {
00815 TruthValue prev = setValue((*gndPreds)[i], UNKNOWN);
00816 if (prevValues) prevValues->append(prev);
00817 }
00818 }
00819
00820
00821 void setValuesToGivenValues(const Array<Predicate*>* const & gndPreds,
00822 const Array<TruthValue>* const & values)
00823 {
00824 assert(values);
00825 assert(values->size() == gndPreds->size());
00826 for (int i = 0; i < gndPreds->size(); i++)
00827 setValue((*gndPreds)[i], (*values)[i]);
00828 }
00829
00830 void setValuesToGivenValues(const GroundPredicateHashArray* const & gndPreds,
00831 const Array<TruthValue>* const & values)
00832 {
00833 assert(values);
00834 assert(values->size() == gndPreds->size());
00835 for (int i = 0; i < gndPreds->size(); i++)
00836 setValue((*gndPreds)[i], (*values)[i]);
00837 }
00838
00839
00840
00841 void alterTruthValue(const Array<Predicate*>* const & gndPreds,
00842 const TruthValue& tobeAlteredVal,
00843 const TruthValue& newVal,
00844 Array<TruthValue>* const & gndPredValues)
00845 {
00846 for (int i = 0; i < gndPreds->size(); i++)
00847 {
00848 TruthValue val = getValue((*gndPreds)[i]);
00849 gndPredValues->append(val);
00850 if (val == tobeAlteredVal)
00851 {
00852 setValue((*gndPreds)[i], newVal);
00853 }
00854 }
00855 }
00856
00857
00858
00859
00860 void addEvidenceGroundPredicate(Predicate* const & pred)
00861 {
00862 if (dbdebug >= 1)
00863 cout << "Calling database::addEvidenceGroundPredicate" << endl;
00864 setValue(pred, pred->getTruthValue());
00865
00866 if (!closedWorld_[pred->getId()])
00867 {
00868 setEvidenceStatus(pred, true);
00869 }
00870 unsigned long long idx = getIdxOfGndPredValues(pred);
00871
00872 if (pred->getTruthValue() == TRUE)
00873 addToInvertedIndex(pred, idx, T_INDEX);
00874
00875
00876 if (dbdebug >= 1) cout << "Returning" << endl;
00877 }
00878
00888 void addPredToEvidenceIndex(Predicate* const& pred, const bool& sense)
00889 {
00890 TruthValue tv = getValue(pred);
00891 unsigned long long idx = getIdxOfGndPredValues(pred);
00892
00893 if (sense && tv == FALSE)
00894 addToInvertedIndex(pred, idx, F_INDEX);
00895
00896 else if (!sense && tv == TRUE)
00897 addToInvertedIndex(pred, idx, T_INDEX);
00898 }
00899
00900 int getNumGroundings(const int& predId) const
00901 {
00902
00903 const PredicateTemplate* t = domain_->getPredicateTemplate(predId);
00904 if (t->isEqualPredWithType())
00905 {
00906 int nc = domain_->getNumConstantsByType(t->getTermTypeAsInt(0));
00907 return nc*nc;
00908 }
00909
00910 return numberOfGroundings_[predId];
00911 }
00912
00913 int getNumEvidenceGndPreds(const int& predId) const
00914 {
00915
00916 const PredicateTemplate* t = domain_->getPredicateTemplate(predId);
00917 if (t->isEqualPredWithType())
00918 {
00919 int nc = domain_->getNumConstantsByType(t->getTermTypeAsInt(0));
00920 return nc*nc;
00921 }
00922
00923
00924 if (closedWorld_[predId])
00925 {
00926 return numberOfGroundings_[predId];
00927 }
00928 return (*evidencePredIdxSet_)[predId].size();
00929 }
00930
00931 int getNumTrueGndPreds(const int& predId) const
00932 {
00933
00934 const PredicateTemplate* t = domain_->getPredicateTemplate(predId);
00935 if (t->isEqualPredWithType())
00936 {
00937 int nc = domain_->getNumConstantsByType(t->getTermTypeAsInt(0));
00938 int minus = 0;
00939 for (int i = 0; i < oppEqGndPreds_.size(); i++)
00940 {
00941 Predicate* pred = oppEqGndPreds_[i];
00942 if (pred->getId() == predId &&
00943 pred->getTerm(0)->getId() == pred->getTerm(1)->getId())
00944 minus++;
00945 }
00946
00947 return nc-minus;
00948 }
00949
00950 return (*predIdToNumTF_)[predId].numTrue;
00951 }
00952
00953
00954 int getNumFalseGndPreds(const int& predId) const
00955 {
00956
00957 const PredicateTemplate* t = domain_->getPredicateTemplate(predId);
00958 if (t->isEqualPredWithType())
00959 {
00960 int nc = domain_->getNumConstantsByType(t->getTermTypeAsInt(0));
00961 int minus = 0;
00962 for (int i = 0; i < oppEqGndPreds_.size(); i++)
00963 {
00964 Predicate* pred = oppEqGndPreds_[i];
00965 if (pred->getId() == predId &&
00966 pred->getTerm(0)->getId() != pred->getTerm(1)->getId())
00967 minus++;
00968 }
00969
00970 return nc*(nc-1)-minus;
00971 }
00972
00973 if (closedWorld_[predId])
00974 return numberOfGroundings_[predId]-(*predIdToNumTF_)[predId].numTrue;
00975 else
00976 return (*predIdToNumTF_)[predId].numFalse;
00977 }
00978
00979
00980 int getNumUnknownGndPreds(const int& predId) const
00981 {
00982
00983 const PredicateTemplate* t = domain_->getPredicateTemplate(predId);
00984 if (t->isEqualPredWithType()) return 0;
00985
00986
00987 if (closedWorld_[predId]) return 0;
00988
00989 return numberOfGroundings_[predId] -(*predIdToNumTF_)[predId].numTrue
00990 -(*predIdToNumTF_)[predId].numFalse;
00991 }
00992
00993
00994 int getNumPredIds() const
00995 {
00996 return predIdToNumTF_->size();
00997 }
00998
00999 bool isClosedWorld(const int& predId) const { return closedWorld_[predId]; }
01000 void setClosedWorld(const int& predId, const bool& b)
01001 { closedWorld_[predId] = b; }
01002 const Array<bool>& getClosedWorld() const { return closedWorld_; }
01003
01010 void getTrueGndings(int predId, Array<Predicate*>* const & indexedGndings)
01011 {
01012
01013 LongLongHashSet::const_iterator it = (*truePredIdxSet_)[predId].begin();
01014 for (; it != (*truePredIdxSet_)[predId].end(); it++) {
01015 Predicate* p = getPredFromIdx((*it), domain_->getPredicateTemplate(predId));
01016 indexedGndings->append(p);
01017 }
01018 }
01019
01033 void getIndexedGndings(Array<Predicate*>* const & indexedGndings,
01034 Predicate* const & pred,
01035 bool const & ignoreActivePreds,
01036 bool const & trueGndings)
01037 {
01038 int predId = pred->getId();
01039
01040 if (pred->isGrounded())
01041 {
01042
01043 TruthValue tv = getValue(pred);
01044 if ((trueGndings && tv == TRUE) ||
01045 (!trueGndings && tv == FALSE) ||
01046 (!ignoreActivePreds && getActiveStatus(pred)))
01047 {
01048 Predicate* p = new Predicate(*pred);
01049 indexedGndings->append(p);
01050 }
01051 return;
01052 }
01053
01054
01055
01056 if (!pred->containsConstants())
01057 {
01058 if (trueGndings)
01059 {
01060
01061 LongLongHashSet::const_iterator it = (*truePredIdxSet_)[predId].begin();
01062 for (; it != (*truePredIdxSet_)[predId].end(); it++)
01063 {
01064 Predicate* p = getPredFromIdx((*it), pred->getTemplate());
01065 indexedGndings->append(p);
01066 }
01067 }
01068 else
01069 {
01070 if (closedWorld_[predId])
01071 {
01072
01073
01074 Array<Predicate*> predArr;
01075 Predicate::createAllGroundings(predId, domain_, predArr);
01076 int numPreds = predArr.size();
01077 for (int i = 0; i < numPreds; i++)
01078 {
01079 Predicate* newPred = predArr[i];
01080
01081 LongLongHashSet::const_iterator it;
01082 if ((it = (*truePredIdxSet_)[predId].find(i)) !=
01083 (*truePredIdxSet_)[predId].end())
01084 delete newPred;
01085 else
01086 indexedGndings->append(newPred);
01087 }
01088 }
01089 else
01090 {
01091 LongLongHashSet::const_iterator it =
01092 (*falsePredIdxSet_)[predId].begin();
01093 for (; it != (*falsePredIdxSet_)[predId].end(); it++)
01094 {
01095 Predicate* p = getPredFromIdx((*it), pred->getTemplate());
01096 indexedGndings->append(p);
01097 }
01098 }
01099 }
01100
01101 if (!ignoreActivePreds)
01102 {
01103
01104 LongLongHashSet::const_iterator ait =
01105 (*activePredIdxSet_)[predId].begin();
01106 for (; ait != (*activePredIdxSet_)[predId].end(); ait++)
01107 {
01108 Predicate* p = getPredFromIdx((*ait), pred->getTemplate());
01109 indexedGndings->append(p);
01110 }
01111 }
01112 return;
01113 }
01114
01115
01116 assert(predId < trueEvIndex_->size());
01117 assert(predId < falseEvIndex_->size());
01118
01119 set<unsigned long long> trueOrFalseIntersection;
01120 set<unsigned long long> activeIntersection;
01121 bool initial = true;
01122
01123 for (int term = 0; term < pred->getNumTerms(); term++)
01124 {
01125 int constantId = pred->getTerm(term)->getId();
01126
01127 if (constantId < 0) continue;
01128 pair<unsigned int, unsigned int> placeAndConstant(term, constantId);
01129
01130 if (initial)
01131 {
01132 if (trueGndings)
01133 trueOrFalseIntersection = ((*trueEvIndex_)[predId])[placeAndConstant];
01134 else
01135 trueOrFalseIntersection =
01136 ((*falseEvIndex_)[predId])[placeAndConstant];
01137 }
01138 else
01139 {
01140 set<unsigned long long> tmpTrueOrFalseIntersection;
01141 if (trueGndings)
01142 set_intersection(trueOrFalseIntersection.begin(),
01143 trueOrFalseIntersection.end(),
01144 ((*trueEvIndex_)[predId])[placeAndConstant].begin(),
01145 ((*trueEvIndex_)[predId])[placeAndConstant].end(),
01146 inserter(tmpTrueOrFalseIntersection,
01147 tmpTrueOrFalseIntersection.begin()));
01148 else
01149 set_intersection(trueOrFalseIntersection.begin(),
01150 trueOrFalseIntersection.end(),
01151 ((*falseEvIndex_)[predId])[placeAndConstant].begin(),
01152 ((*falseEvIndex_)[predId])[placeAndConstant].end(),
01153 inserter(tmpTrueOrFalseIntersection,
01154 tmpTrueOrFalseIntersection.begin()));
01155 trueOrFalseIntersection = tmpTrueOrFalseIntersection;
01156
01157 }
01158
01159 if (!ignoreActivePreds)
01160 {
01161 if (initial)
01162 activeIntersection = ((*activeIndex_)[predId])[placeAndConstant];
01163 else
01164 {
01165 set<unsigned long long> tmpActiveIntersection;
01166 set_intersection(activeIntersection.begin(),
01167 activeIntersection.end(),
01168 ((*activeIndex_)[predId])[placeAndConstant].begin(),
01169 ((*activeIndex_)[predId])[placeAndConstant].end(),
01170 inserter(tmpActiveIntersection,
01171 tmpActiveIntersection.begin()));
01172 activeIntersection = tmpActiveIntersection;
01173 }
01174 }
01175 initial = false;
01176 }
01177
01178
01179
01180
01181 set<unsigned long long>::const_iterator it =
01182 trueOrFalseIntersection.begin();
01183 for (; it != trueOrFalseIntersection.end(); it++)
01184 {
01185 Predicate* p = getPredFromIdx((*it), pred->getTemplate());
01186 indexedGndings->append(p);
01187 }
01188
01189 if (!ignoreActivePreds)
01190 {
01191 it = activeIntersection.begin();
01192 for (; it != activeIntersection.end(); it++)
01193 {
01194 Predicate* p = getPredFromIdx((*it), pred->getTemplate());
01195 indexedGndings->append(p);
01196 }
01197 }
01198 return;
01199 }
01200
01201
01208 void changeConstantsToNewIds(hash_map<int,int>& oldToNewConstIds)
01209 {
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219 int numTypes = domain_->getNumTypes();
01220 Array<unsigned int> newfirstConstIdByType;
01221 newfirstConstIdByType.growToSize(numTypes);
01222 for (int i = 0; i < numTypes; i++)
01223 {
01224 assert(domain_->isType(i));
01225 const Array<int>* constIds = domain_->getConstantsByTypeWithExt(i);
01226
01227 if (constIds->empty()) newfirstConstIdByType[i] = (unsigned int)0;
01228 else newfirstConstIdByType[i] = (*constIds)[0];
01229 }
01230
01231
01232 int numFOPreds = domain_->getNumPredicates();
01233 Array<Array<MultAndType>*> newtermMultByPred;
01234 newtermMultByPred.growToSize(numFOPreds, NULL);
01235 for (int i = 0; i < numFOPreds; i++)
01236 {
01237
01238 const PredicateTemplate* t = domain_->getPredicateTemplate(i);
01239 if (t->isEqualPredWithType()) continue;
01240
01241 const Array<int>* termTypes = domain_->getPredicateTermTypesAsInt(i);
01242 Array<MultAndType>* matArr = new Array<MultAndType>;
01243 int numTermTypes = termTypes->size();
01244 matArr->growToSize(numTermTypes);
01245 unsigned long long curMult = 1;
01246 for (int j = numTermTypes - 1; j >= 0; j--)
01247 {
01248 int typeId = (*termTypes)[j];
01249 (*matArr)[j] = MultAndType(curMult, typeId);
01250 curMult *= domain_->getNumConstantsByTypeWithExt(typeId);
01251
01252 }
01253
01254 newtermMultByPred[i] = matArr;
01255 }
01256
01257
01258 changeConstantsToNewIds(truePredIdxSet_, oldToNewConstIds,
01259 newfirstConstIdByType, newtermMultByPred);
01260 changeConstantsToNewIds(falsePredIdxSet_, oldToNewConstIds,
01261 newfirstConstIdByType, newtermMultByPred);
01262 changeConstantsToNewIds(activePredIdxSet_, oldToNewConstIds,
01263 newfirstConstIdByType, newtermMultByPred);
01264 changeConstantsToNewIds(evidencePredIdxSet_, oldToNewConstIds,
01265 newfirstConstIdByType, newtermMultByPred);
01266 changeConstantsToNewIds(deactivatedPredIdxSet_, oldToNewConstIds,
01267 newfirstConstIdByType, newtermMultByPred);
01268 changeConstantsToNewIds(trueEvIndex_, oldToNewConstIds,
01269 newfirstConstIdByType, newtermMultByPred);
01270 changeConstantsToNewIds(falseEvIndex_, oldToNewConstIds,
01271 newfirstConstIdByType, newtermMultByPred);
01272 changeConstantsToNewIds(activeIndex_, oldToNewConstIds,
01273 newfirstConstIdByType, newtermMultByPred);
01274
01275
01276 firstConstIdByType_ = newfirstConstIdByType;
01277 for (int i = 0; i < numFOPreds; i++)
01278 {
01279 delete termMultByPred_[i];
01280 }
01281 termMultByPred_ = newtermMultByPred;
01282 }
01283
01284
01285
01286 private:
01287
01297 const PredicateHashArray* getGndPreds(const int& predId,
01298 const bool& truthValue,
01299 const bool& withEvidence) const
01300 {
01301 PredicateHashArray* preds = new PredicateHashArray();
01302 LongLongHashSet predIdxSet;
01303
01304 if (truthValue) predIdxSet = (*truePredIdxSet_)[predId];
01305 else predIdxSet = (*falsePredIdxSet_)[predId];
01306
01307 LongLongHashSet::const_iterator it = predIdxSet.begin();
01308 for (; it != predIdxSet.end(); it++)
01309 {
01310
01311 if (withEvidence ||
01312 (*evidencePredIdxSet_)[predId].find(*it) ==
01313 (*evidencePredIdxSet_)[predId].end())
01314 {
01315 Predicate* p = getPredFromIdx((*it),
01316 domain_->getPredicateTemplate(predId));
01317 preds->append(p);
01318 }
01319 }
01320 return preds;
01321 }
01322
01336 unsigned long long getIdxOfGndPredValues(const Predicate* const & pred,
01337 const Array<unsigned int> & currfirstConstIdByType,
01338 const Array<Array<MultAndType>*> & currtermMultByPred) const
01339 {
01340 int numTerms = pred->getNumTerms();
01341 Array<MultAndType>* multAndTypes = currtermMultByPred[pred->getId()];
01342 unsigned long long idx = 0;
01343 for (int i = 0; i < numTerms; i++)
01344 {
01345 int constId = pred->getTerm(i)->getId();
01346 assert(constId >= 0);
01347
01348 idx += (*multAndTypes)[i].first
01349 * (constId - currfirstConstIdByType[(*multAndTypes)[i].second]);
01350 }
01351 return idx;
01352 }
01353
01354
01355 unsigned long long getIdxOfGndPredValues(const Predicate* const & pred) const
01356 {
01357 return getIdxOfGndPredValues(pred, firstConstIdByType_, termMultByPred_);
01358 }
01359
01373 unsigned long long getIdxOfGndPredValues(const GroundPredicate* const & pred,
01374 const Array<unsigned int> & currfirstConstIdByType,
01375 const Array<Array<MultAndType>*> & currtermMultByPred) const
01376 {
01377 int numTerms = pred->getNumTerms();
01378 Array<MultAndType>* multAndTypes = currtermMultByPred[pred->getId()];
01379 unsigned long long idx = 0;
01380 for (int i = 0; i < numTerms; i++)
01381 {
01382 int constId = pred->getTermId(i);
01383 assert(constId >= 0);
01384
01385 idx += (*multAndTypes)[i].first
01386 * (constId - currfirstConstIdByType[(*multAndTypes)[i].second]);
01387 }
01388 return idx;
01389 }
01390
01391
01392 unsigned long long getIdxOfGndPredValues(const GroundPredicate* const & pred)
01393 const
01394 {
01395 return getIdxOfGndPredValues(pred, firstConstIdByType_, termMultByPred_);
01396 }
01397
01406 Predicate* getPredFromIdx(unsigned long long idx,
01407 const PredicateTemplate* const & predTemplate,
01408 const Array<unsigned int> & currfirstConstIdByType,
01409 const Array<Array<MultAndType>*> & currtermMultByPred) const
01410 {
01411 Predicate* p = new Predicate(predTemplate);
01412 Array<unsigned long long>* auxIdx =
01413 new Array<unsigned long long>(predTemplate->getNumTerms());
01414 auxIdx->growToSize(predTemplate->getNumTerms());
01415 Array<MultAndType>* multAndTypes = currtermMultByPred[p->getId()];
01416 for (int i = 0; i < predTemplate->getNumTerms(); i++)
01417 {
01418 unsigned long long aux = 0;
01419 for (int j = 0; j < i; j++)
01420 {
01421 aux += (*auxIdx)[j] * ((*multAndTypes)[j].first);
01422 }
01423 (*auxIdx)[i] = (idx - aux) / (*multAndTypes)[i].first;
01424 int constId =
01425 (int)(*auxIdx)[i] + currfirstConstIdByType[(*multAndTypes)[i].second];
01426 p->setTermToConstant(i, constId);
01427 }
01428 delete auxIdx;
01429 return p;
01430 }
01431
01432 Predicate* getPredFromIdx(unsigned long long idx,
01433 const PredicateTemplate* const & predTemplate) const
01434 {
01435 return getPredFromIdx(idx, predTemplate, firstConstIdByType_,
01436 termMultByPred_);
01437 }
01438
01446 void changeConstantsToNewIds(Array<LongLongHashSet >* predIdxSets,
01447 hash_map<int,int>& oldToNewConstIds,
01448 const Array<unsigned int> & currfirstConstIdByType,
01449 const Array<Array<MultAndType>*> & currtermMultByPred)
01450 {
01451 for (int i = 0; i < predIdxSets->size(); i++)
01452 {
01453 LongLongHashSet newHashSet;
01454 LongLongHashSet& predIdxSet = (*predIdxSets)[i];
01455 LongLongHashSet::const_iterator it = predIdxSet.begin();
01456 for (; it != predIdxSet.end(); it++)
01457 {
01458 Predicate* p = getPredFromIdx((*it), domain_->getPredicateTemplate(i));
01459
01460 for (int j = 0; j < p->getNumTerms(); j++)
01461 {
01462 Term* t = (Term*) p->getTerm(j);
01463 if (t->getType() == Term::CONSTANT)
01464 {
01465 int oldId = t->getId();
01466 assert(oldToNewConstIds.find(oldId) != oldToNewConstIds.end());
01467 t->setId(oldToNewConstIds[oldId]);
01468 }
01469 }
01470
01471 unsigned long long d = getIdxOfGndPredValues(p, currfirstConstIdByType,
01472 currtermMultByPred);
01473 newHashSet.insert(d);
01474 delete p;
01475 }
01476 assert(predIdxSet.size() == newHashSet.size());
01477 (*predIdxSets)[i] = newHashSet;
01478 }
01479 }
01480
01488 void changeConstantsToNewIds(Array<LongLongHashMap>* predIdxMaps,
01489 hash_map<int,int>& oldToNewConstIds,
01490 const Array<unsigned int> & currfirstConstIdByType,
01491 const Array<Array<MultAndType>*> & currtermMultByPred)
01492 {
01493 for (int i = 0; i < predIdxMaps->size(); i++)
01494 {
01495 LongLongHashMap newHashMap;
01496 LongLongHashMap& predIdxMap = (*predIdxMaps)[i];
01497 LongLongHashMap::iterator it = predIdxMap.begin();
01498 for (; it != predIdxMap.end(); it++)
01499 {
01500 set<unsigned long long> newSet;
01501
01502 set<unsigned long long> gndings = (*it).second;
01503 set<unsigned long long>::const_iterator setit = gndings.begin();
01504
01505 for (; setit != gndings.end(); setit++)
01506 {
01507 Predicate* p = getPredFromIdx((*setit),
01508 domain_->getPredicateTemplate(i));
01509
01510 for (int j = 0; j < p->getNumTerms(); j++)
01511 {
01512 Term* t = (Term*) p->getTerm(j);
01513 if (t->getType() == Term::CONSTANT)
01514 {
01515 int oldId = t->getId();
01516 assert(oldToNewConstIds.find(oldId) != oldToNewConstIds.end());
01517 t->setId(oldToNewConstIds[oldId]);
01518 }
01519 }
01520
01521 unsigned long long d = getIdxOfGndPredValues(p, currfirstConstIdByType,
01522 currtermMultByPred);
01523 delete p;
01524 newSet.insert(d);
01525 }
01526
01527
01528 pair<unsigned int, unsigned int> placeAndConstant = (*it).first;
01529 pair<unsigned int, unsigned int>
01530 newPlaceAndConstant(placeAndConstant.first,
01531 oldToNewConstIds[placeAndConstant.second]);
01532 newHashMap[newPlaceAndConstant] = newSet;
01533 }
01534 assert(predIdxMap.size() == newHashMap.size());
01535 (*predIdxMaps)[i] = newHashMap;
01536 }
01537 }
01538
01539
01546 TruthValue getValue(const unsigned long long& idx, const int& predId) const
01547 {
01548 LongLongHashSet::const_iterator it = (*truePredIdxSet_)[predId].find(idx);
01549 if (it != (*truePredIdxSet_)[predId].end())
01550 {
01551 if (dbdebug >= 1) cout << "returning TRUE" << endl;
01552 return TRUE;
01553 }
01554
01555 if (!closedWorld_[predId] && !performingInference_)
01556 {
01557 if((*falsePredIdxSet_)[predId].find(idx) !=
01558 (*falsePredIdxSet_)[predId].end())
01559 {
01560 if (dbdebug >= 1) cout << "returning FALSE" << endl;
01561 return FALSE;
01562 }
01563 else
01564 {
01565 if (dbdebug >= 1) cout << "returning UNKNOWN" << endl;
01566 return UNKNOWN;
01567 }
01568 }
01569 if (dbdebug >= 1) cout << "returning FALSE" << endl;
01570 return FALSE;
01571 }
01572
01579 bool getActiveStatus(const unsigned long long& idx, const int& predId) const
01580 {
01581
01582 if (getEvidenceStatus(idx, predId))
01583 {
01584 if (dbdebug >= 1) cout << "Returning false(1)" << endl;
01585 return false;
01586 }
01587
01588 if ((*activePredIdxSet_)[predId].find(idx) !=
01589 (*activePredIdxSet_)[predId].end())
01590 {
01591 if (dbdebug >= 1) cout << "Returning true" << endl;
01592 return true;
01593 }
01594 if (dbdebug >= 1) cout << "Returning false(2)" << endl;
01595 return false;
01596 }
01597
01604 bool getDeactivatedStatus(const unsigned long long& idx, const int& predId) const
01605 {
01606
01607 if (getEvidenceStatus(idx, predId))
01608 {
01609 if (dbdebug >= 1) cout << "Returning false(1)" << endl;
01610 return false;
01611 }
01612
01613 if ((*deactivatedPredIdxSet_)[predId].find(idx) !=
01614 (*deactivatedPredIdxSet_)[predId].end())
01615 {
01616 if (dbdebug >= 1) cout << "Returning true" << endl;
01617 return true;
01618 }
01619 if (dbdebug >= 1) cout << "Returning false(2)" << endl;
01620 return false;
01621 }
01622
01629 bool getEvidenceStatus(const unsigned long long& idx, const int& predId) const
01630 {
01631
01632 if (closedWorld_[predId])
01633 {
01634 if (dbdebug >= 1) cout << "Returning true(1)" << endl;
01635 return true;
01636 }
01637 if ((*evidencePredIdxSet_)[predId].find(idx) !=
01638 (*evidencePredIdxSet_)[predId].end())
01639 {
01640 if (dbdebug >= 1) cout << "Returning true(2)" << endl;
01641 return true;
01642 }
01643 if (dbdebug >= 1) cout << "Returning false" << endl;
01644 return false;
01645 }
01646
01656 TruthValue setValueHelper(const Predicate* const & pred, const bool& flip,
01657 const TruthValue& ttv)
01658 {
01659 if (dbdebug >= 1) cout << "Calling database::setValueHelper" << endl;
01660 Predicate* ppred = (Predicate *)pred;
01661 assert(ppred->isGrounded());
01662
01663
01664 if (pred->isEqualPredWithType())
01665 {
01666
01667
01668 TruthValue actual
01669 = (pred->getTerm(0)->getId() == pred->getTerm(1)->getId()) ?
01670 TRUE : FALSE;
01671 TruthValue opposite = (actual==TRUE) ? FALSE : TRUE;
01672
01673
01674 for (int i = 0; i < oppEqGndPreds_.size(); i++)
01675 if (ppred->same(oppEqGndPreds_[i]))
01676 {
01677 if (flip || ttv == actual)
01678 {
01679 Predicate* pr = oppEqGndPreds_[i];
01680 oppEqGndPreds_.removeItemFastDisorder(i);
01681 delete pr;
01682 }
01683 if (dbdebug >= 1) cout << "returning " << opposite << endl;
01684 return opposite;
01685 }
01686
01687 if (flip || ttv == opposite) oppEqGndPreds_.append(new Predicate(*pred));
01688 if (dbdebug >= 1) cout << "returning " << actual << endl;
01689 return actual;
01690 }
01691
01692 unsigned long long idx = getIdxOfGndPredValues(pred);
01693 int predId = pred->getId();
01694 return setValueHelper(idx, predId, flip, ttv, pred, NULL);
01695 }
01696
01707 TruthValue setValueHelper(const GroundPredicate* const & pred,
01708 const bool& flip, const TruthValue& ttv)
01709 {
01710 if (dbdebug >= 1) cout << "Calling database::setValueHelper" << endl;
01711 unsigned long long idx = getIdxOfGndPredValues(pred);
01712 int predId = pred->getId();
01713 return setValueHelper(idx, predId, flip, ttv, NULL, pred);
01714 }
01715
01731 TruthValue setValueHelper(const unsigned long long& idx, const int& predId,
01732 const bool& flip, const TruthValue& ttv,
01733 const Predicate* const & pred,
01734 const GroundPredicate* const & gndPred)
01735 {
01736 assert((pred && !gndPred) || (!pred && gndPred));
01737 TruthValue oldtv = getValue(idx, predId);
01738
01739 TruthValue tv = ttv;
01740
01741 if (flip)
01742 {
01743 assert(oldtv == TRUE || oldtv == FALSE);
01744 if (oldtv == FALSE) tv = TRUE;
01745 else tv = FALSE;
01746 }
01747
01748 if (oldtv != tv)
01749 {
01750 if (closedWorld_[predId])
01751 {
01752 if (oldtv == TRUE)
01753 {
01754 assert(tv == FALSE);
01755 (*predIdToNumTF_)[predId].numTrue--;
01756 (*truePredIdxSet_)[predId].erase(idx);
01757 }
01758 else
01759 {
01760 assert(oldtv == FALSE && tv == TRUE);
01761 (*predIdToNumTF_)[predId].numTrue++;
01762 (*truePredIdxSet_)[predId].insert(idx);
01763 }
01764 }
01765 else
01766 {
01767 if (oldtv == UNKNOWN)
01768 {
01769 if (tv == TRUE)
01770 {
01771 (*predIdToNumTF_)[predId].numTrue++;
01772 (*truePredIdxSet_)[predId].insert(idx);
01773 }
01774 else
01775 {
01776 (*predIdToNumTF_)[predId].numFalse++;
01777 if (!performingInference_) (*falsePredIdxSet_)[predId].insert(idx);
01778 }
01779 }
01780 else
01781 if (oldtv == TRUE)
01782 {
01783 (*predIdToNumTF_)[predId].numTrue--;
01784 (*truePredIdxSet_)[predId].erase(idx);
01785 if (tv == FALSE)
01786 {
01787 (*predIdToNumTF_)[predId].numFalse++;
01788 if (!performingInference_) (*falsePredIdxSet_)[predId].insert(idx);
01789 }
01790 }
01791 else
01792 {
01793 assert(oldtv == FALSE);
01794 (*predIdToNumTF_)[predId].numFalse--;
01795 if (!performingInference_) (*falsePredIdxSet_)[predId].erase(idx);
01796 if (tv == TRUE)
01797 {
01798 (*predIdToNumTF_)[predId].numTrue++;
01799 (*truePredIdxSet_)[predId].insert(idx);
01800 }
01801 }
01802 }
01803 }
01804 if (dbdebug >= 1) cout << "returning " << oldtv << endl;
01805 return oldtv;
01806 }
01807
01818 bool setActiveStatus(const unsigned long long& idx, const int& predId,
01819 const bool& as, const Predicate* const & pred,
01820 const GroundPredicate* const & gndPred)
01821 {
01822 assert((pred && !gndPred) || (!pred && gndPred));
01823 bool oldas = getActiveStatus(idx, predId);
01824 if (oldas != as)
01825 {
01826 if (as)
01827 {
01828 (*activePredIdxSet_)[predId].insert(idx);
01829 }
01830 else
01831 {
01832 (*activePredIdxSet_)[predId].erase(idx);
01833 }
01834
01835
01836 if (lazyFlag_)
01837 {
01838 if (as)
01839 {
01840 if (pred) addToInvertedIndex(pred, idx, A_INDEX);
01841 else if (gndPred) addToInvertedIndex(gndPred, idx, A_INDEX);
01842 }
01843 else
01844 {
01845 if (pred) removeFromInvertedIndex(pred, idx, A_INDEX);
01846 if (gndPred) removeFromInvertedIndex(gndPred, idx, A_INDEX);
01847 }
01848 }
01849 }
01850 if (dbdebug >= 1) cout << "Returning " << oldas << endl;
01851 return oldas;
01852 }
01853
01860 bool setDeactivatedStatus(const unsigned long long& idx, const int& predId,
01861 const bool& das)
01862 {
01863 bool olddas = getDeactivatedStatus(idx, predId);
01864 if (olddas != das)
01865 {
01866 if (das)
01867 {
01868 (*deactivatedPredIdxSet_)[predId].insert(idx);
01869 }
01870 else
01871 {
01872 (*deactivatedPredIdxSet_)[predId].erase(idx);
01873 }
01874 }
01875 if (dbdebug >= 1) cout << "Returning " << olddas << endl;
01876 return olddas;
01877 }
01878
01885 bool setEvidenceStatus(const unsigned long long& idx, const int& predId, const bool& es)
01886 {
01887 bool oldes = getEvidenceStatus(idx, predId);
01888 if (oldes != es)
01889 {
01890 if (es)
01891 {
01892 (*evidencePredIdxSet_)[predId].insert(idx);
01893 }
01894 else
01895 {
01896 (*evidencePredIdxSet_)[predId].erase(idx);
01897 }
01898 }
01899 if (dbdebug >= 1) cout << "Returning " << oldes << endl;
01900 return oldes;
01901 }
01902
01906 void addToInvertedIndex(const Predicate* const & pred,
01907 const unsigned long long& predIdx, IndexType idxType)
01908 {
01909
01910 assert(((Predicate*)pred)->isGrounded());
01911 int predId = pred->getId();
01912 assert(predId < trueEvIndex_->size());
01913 for (int term = 0; term < pred->getNumTerms(); term++)
01914 {
01915 int constantId = pred->getTerm(term)->getId();
01916 assert(constantId >= 0);
01917
01918 pair<unsigned int, unsigned int> placeAndConstant(term, constantId);
01919 pair<set<unsigned long long>::iterator, bool> prevElement;
01920
01921 if (idxType == T_INDEX)
01922 {
01923 prevElement =
01924 ((*trueEvIndex_)[predId])[placeAndConstant].insert(predIdx);
01925
01926
01927 if (!prevElement.second)
01928 assert(predIdx == *(prevElement.first));
01929 }
01930
01931 else if (idxType == F_INDEX)
01932 {
01933 prevElement =
01934 ((*falseEvIndex_)[predId])[placeAndConstant].insert(predIdx);
01935
01936
01937 if (!prevElement.second)
01938 assert(predIdx == *(prevElement.first));
01939 }
01940
01941 else if (idxType == A_INDEX)
01942 {
01943 prevElement =
01944 ((*activeIndex_)[predId])[placeAndConstant].insert(predIdx);
01945
01946
01947 if (!prevElement.second)
01948 assert(predIdx == *(prevElement.first));
01949 }
01950 }
01951 }
01952
01956 void addToInvertedIndex(const GroundPredicate* const & pred,
01957 const unsigned long long& predIdx, IndexType idxType)
01958 {
01959 int predId = pred->getId();
01960 assert(predId < trueEvIndex_->size());
01961 for (unsigned int term = 0; term < pred->getNumTerms(); term++)
01962 {
01963 int constantId = pred->getTermId(term);
01964 assert(constantId >= 0);
01965
01966 pair<unsigned int, unsigned int> placeAndConstant(term, constantId);
01967 pair<set<unsigned long long>::iterator, bool> prevElement;
01968
01969 if (idxType == T_INDEX)
01970 {
01971 prevElement =
01972 ((*trueEvIndex_)[predId])[placeAndConstant].insert(predIdx);
01973
01974
01975 if (!prevElement.second)
01976 assert(predIdx == *(prevElement.first));
01977 }
01978
01979 else if (idxType == F_INDEX)
01980 {
01981 prevElement =
01982 ((*falseEvIndex_)[predId])[placeAndConstant].insert(predIdx);
01983
01984
01985 if (!prevElement.second)
01986 assert(predIdx == *(prevElement.first));
01987 }
01988
01989 else if (idxType == A_INDEX)
01990 {
01991 prevElement =
01992 ((*activeIndex_)[predId])[placeAndConstant].insert(predIdx);
01993
01994
01995 if (!prevElement.second)
01996 assert(predIdx == *(prevElement.first));
01997 }
01998 }
01999 }
02000
02004 void removeFromInvertedIndex(const Predicate* const & pred,
02005 const unsigned long long& predIdx,
02006 IndexType idxType)
02007 {
02008
02009 assert(((Predicate*)pred)->isGrounded());
02010 int predId = pred->getId();
02011 assert(predId < trueEvIndex_->size());
02012 for (int term = 0; term < pred->getNumTerms(); term++)
02013 {
02014 int constantId = pred->getTerm(term)->getId();
02015 assert(constantId >= 0);
02016
02017 pair<unsigned int, unsigned int> placeAndConstant(term, constantId);
02018 pair<set<unsigned long long>::iterator, bool> prevElement;
02019 int numErased = 0;
02020
02021 if (idxType == T_INDEX)
02022 {
02023 numErased = ((*trueEvIndex_)[predId])[placeAndConstant].erase(predIdx);
02024 assert(numErased <= 1);
02025 }
02026
02027 else if (idxType == F_INDEX)
02028 {
02029 numErased = ((*falseEvIndex_)[predId])[placeAndConstant].erase(predIdx);
02030 assert(numErased <= 1);
02031 }
02032
02033 else if (idxType == A_INDEX)
02034 {
02035 numErased =
02036 ((*activeIndex_)[predId])[placeAndConstant].erase(predIdx);
02037 assert(numErased <= 1);
02038 }
02039 }
02040 }
02041
02045 void removeFromInvertedIndex(const GroundPredicate* const & pred,
02046 const unsigned long long& predIdx,
02047 IndexType idxType)
02048 {
02049 int predId = pred->getId();
02050 assert(predId < trueEvIndex_->size());
02051 for (unsigned int term = 0; term < pred->getNumTerms(); term++)
02052 {
02053 int constantId = pred->getTermId(term);
02054 assert(constantId >= 0);
02055
02056 pair<unsigned int, unsigned int> placeAndConstant(term, constantId);
02057 pair<set<unsigned long long>::iterator, bool> prevElement;
02058 int numErased = 0;
02059
02060 if (idxType == T_INDEX)
02061 {
02062 numErased = ((*trueEvIndex_)[predId])[placeAndConstant].erase(predIdx);
02063 assert(numErased <= 1);
02064 }
02065
02066 else if (idxType == F_INDEX)
02067 {
02068 numErased = ((*falseEvIndex_)[predId])[placeAndConstant].erase(predIdx);
02069 assert(numErased <= 1);
02070 }
02071
02072 else if (idxType == A_INDEX)
02073 {
02074 numErased =
02075 ((*activeIndex_)[predId])[placeAndConstant].erase(predIdx);
02076 assert(numErased <= 1);
02077 }
02078 }
02079 }
02080
02081 private:
02082 const Domain* domain_;
02083
02084
02085 Array<bool> closedWorld_;
02086
02087
02088 bool lazyFlag_;
02089
02090
02091
02092 bool performingInference_;
02093
02094
02095 Array<unsigned int> firstConstIdByType_;
02096
02097
02098
02099
02100
02101
02102
02103 Array<Array<MultAndType>*> termMultByPred_;
02104
02105
02106
02107 Array<NumTrueFalse>* predIdToNumTF_;
02108
02109
02110
02111
02112
02113
02114 Array<Predicate*> oppEqGndPreds_;
02115
02116
02117
02118
02119 Array<LongLongHashMap>* trueEvIndex_;
02120
02121
02122
02123
02124 Array<LongLongHashMap>* falseEvIndex_;
02125
02126
02127
02128
02129 Array<LongLongHashMap>* activeIndex_;
02130
02131
02132 Array<LongLongHashSet>* truePredIdxSet_;
02133
02134
02135 Array<LongLongHashSet>* falsePredIdxSet_;
02136
02137
02138 Array<LongLongHashSet>* activePredIdxSet_;
02139
02140
02141 Array<LongLongHashSet>* evidencePredIdxSet_;
02142
02143
02144 Array<LongLongHashSet>* deactivatedPredIdxSet_;
02145
02146
02147 Array<unsigned long long> numberOfGroundings_;
02148 };
02149
02150
02151
02152 #endif