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
00067 #ifndef DOMAIN_H_JUN_21_2005
00068 #define DOMAIN_H_JUN_21_2005
00069
00070 #include <ext/hash_set>
00071 #include "equalstr.h"
00072 #include "dualmap.h"
00073 #include "constdualmap.h"
00074 #include "predicatetemplate.h"
00075 #include "functiontemplate.h"
00076 #include "predicate.h"
00077 #include "function.h"
00078
00079 class Clause;
00080 class Database;
00081 class TrueFalseGroundingsStore;
00082 class MLN;
00083
00084
00085 typedef hash_map<const char*, const PredicateTemplate*, hash<const char*>,
00086 EqualStr> StrToPredTemplateMap;
00087
00088 typedef hash_map<const char*, const FunctionTemplate*, hash<const char*>,
00089 EqualStr> StrToFuncTemplateMap;
00090
00091
00092 class Domain
00093 {
00094 public:
00095
00096 static const char* PROPOSITIONAL_TYPE;
00097
00098
00099 static const char* PROPOSITIONAL_CONSTANT;
00100
00101 public:
00102 Domain() : typeDualMap_(new DualMap), constDualMap_(new ConstDualMap),
00103 predDualMap_(new DualMap), funcDualMap_(new DualMap),
00104 strToPredTemplateMap_(new StrToPredTemplateMap),
00105 strToFuncTemplateMap_(new StrToFuncTemplateMap),
00106 constantsByType_(new Array<Array<int>*>),
00107 externalConstantsByType_(new Array<Array<int>*>), db_(NULL),
00108 trueFalseGroundingsStore_(NULL), funcSet_(new FunctionSet),
00109 predBlocks_(new Array<Predicate*>),
00110 truePredsInBlock_(new Array<Predicate*>),
00111 blockSizes_(new Array<int>),
00112 blockEvidence_(new Array<bool>),
00113
00114 numNonEvidAtomsPerPred_(new Array<int>),
00115 numTrueNonEvidGndingsPerClause_(new Array<double>),
00116 numFalseNonEvidGndingsPerClause_(new Array<double>)
00117 {
00118 equalPredTemplate_ = new PredicateTemplate();
00119 equalPredTemplate_->setName(PredicateTemplate::EQUAL_NAME);
00120 emptyPredTemplate_ = new PredicateTemplate();
00121 emptyPredTemplate_->setName(PredicateTemplate::EMPTY_NAME);
00122 emptyFuncUnaryTemplate_ = new FunctionTemplate();
00123 emptyFuncUnaryTemplate_->setName(FunctionTemplate::EMPTY_FTEMPLATE_NAME);
00124 emptyFuncBinaryTemplate_ = new FunctionTemplate();
00125 emptyFuncBinaryTemplate_->setName(FunctionTemplate::EMPTY_FTEMPLATE_NAME);
00126
00127 int typeId = addType(PredicateTemplate::ANY_TYPE_NAME);
00128 if (typeId != 0)
00129 {
00130 cout << "ERROR in domain.h: typeId of EQUAL predicate is not 0." << endl;
00131 exit(-1);
00132 }
00133 equalPredTemplate_->appendTermType(PredicateTemplate::ANY_TYPE_NAME,
00134 false, this);
00135 equalPredTemplate_->appendTermType(PredicateTemplate::ANY_TYPE_NAME,
00136 false, this);
00137 emptyPredTemplate_->appendTermType(PredicateTemplate::ANY_TYPE_NAME,
00138 false, this);
00139 emptyPredTemplate_->appendTermType(PredicateTemplate::ANY_TYPE_NAME,
00140 false, this);
00141 emptyFuncUnaryTemplate_->appendTermType(FunctionTemplate::ANY_TYPE_NAME,
00142 false, this);
00143 emptyFuncBinaryTemplate_->appendTermType(FunctionTemplate::ANY_TYPE_NAME,
00144 false, this);
00145 emptyFuncBinaryTemplate_->appendTermType(FunctionTemplate::ANY_TYPE_NAME,
00146 false, this);
00147 }
00148
00149
00150 ~Domain();
00151
00152
00153 void replaceTypeDualMap(DualMap* const & map)
00154 {
00155 if (typeDualMap_) delete typeDualMap_;
00156 typeDualMap_ = map;
00157 }
00158
00159 void setTypeDualMap(DualMap* const & map) { typeDualMap_ = map; }
00160
00161
00162 const DualMap* getTypeDualMap() const { return typeDualMap_; }
00163
00164
00165 void replaceStrToPredTemplateMapAndPredDualMap(StrToPredTemplateMap* const& m,
00166 DualMap* const & predDualMap)
00167 {
00168 if (strToPredTemplateMap_)
00169 {
00170 StrToPredTemplateMap::iterator it = strToPredTemplateMap_->begin();
00171 for (; it != strToPredTemplateMap_->end(); it++)
00172 delete (*it).second;
00173 delete strToPredTemplateMap_;
00174 }
00175 if (predDualMap_) delete predDualMap_;
00176 strToPredTemplateMap_ = m;
00177 predDualMap_ = predDualMap;
00178 }
00179
00180
00181 void setStrToPredTemplateMapAndPredDualMap(StrToPredTemplateMap* const & map,
00182 DualMap* const & predDualMap)
00183 { strToPredTemplateMap_ = map; predDualMap_ = predDualMap; }
00184
00185
00186 const StrToPredTemplateMap* getStrToPredTemplateMap() const
00187 { return strToPredTemplateMap_; }
00188
00189
00190 const DualMap* getPredDualMap() const { return predDualMap_; }
00191
00192
00193 void replaceStrToFuncTemplateMapAndFuncDualMap(StrToFuncTemplateMap* const& m,
00194 DualMap* const & funcDualMap)
00195 {
00196 if (strToFuncTemplateMap_)
00197 {
00198 StrToFuncTemplateMap::iterator it2 = strToFuncTemplateMap_->begin();
00199 for (; it2 != strToFuncTemplateMap_->end(); it2++)
00200 delete (*it2).second;
00201 delete strToFuncTemplateMap_;
00202 }
00203 if (funcDualMap_) delete funcDualMap_;
00204 strToFuncTemplateMap_ = m;
00205 funcDualMap_ = funcDualMap;
00206 }
00207
00208
00209 void setStrToFuncTemplateMapAndFuncDualMap(StrToFuncTemplateMap* const& m,
00210 DualMap* const & funcDualMap)
00211 { strToFuncTemplateMap_ = m; funcDualMap_ = funcDualMap; }
00212
00213
00214 const StrToFuncTemplateMap* getStrToFuncTemplateMap() const
00215 { return strToFuncTemplateMap_; }
00216
00217
00218 const DualMap* getFuncDualMap() const { return funcDualMap_; }
00219
00220
00221 void replaceEqualPredTemplate(PredicateTemplate* const & t)
00222 {
00223 if (equalPredTemplate_) delete equalPredTemplate_;
00224 equalPredTemplate_ = t;
00225 }
00226
00227 void setEqualPredTemplate(PredicateTemplate* const & t)
00228 { equalPredTemplate_ = t; }
00229
00230
00231 const PredicateTemplate* getEqualPredTemplate() const
00232 { return equalPredTemplate_; }
00233
00234 void replaceEmptyPredTemplate(PredicateTemplate* const & t)
00235 {
00236 if (emptyPredTemplate_) delete emptyPredTemplate_;
00237 emptyPredTemplate_ = t;
00238 }
00239
00240 void setEmptyPredTemplate(PredicateTemplate* const & t)
00241 { emptyPredTemplate_ = t; }
00242
00243
00244 const PredicateTemplate* getEmptyPredTemplate() const
00245 { return emptyPredTemplate_; }
00246
00247 void replaceEmptyFuncUnaryTemplate(FunctionTemplate* const & t)
00248 {
00249 if (emptyFuncUnaryTemplate_) delete emptyFuncUnaryTemplate_;
00250 emptyFuncUnaryTemplate_ = t;
00251 }
00252
00253 void replaceEmptyFuncBinaryTemplate(FunctionTemplate* const & t)
00254 {
00255 if (emptyFuncBinaryTemplate_) delete emptyFuncBinaryTemplate_;
00256 emptyFuncBinaryTemplate_ = t;
00257 }
00258
00259 void setEmptyFuncUnaryTemplate(FunctionTemplate* const & t)
00260 { emptyFuncUnaryTemplate_ = t; }
00261
00262 void setEmptyFuncBinaryTemplate(FunctionTemplate* const & t)
00263 { emptyFuncBinaryTemplate_ = t; }
00264
00265
00266 const FunctionTemplate* getEmptyFuncUnaryTemplate() const
00267 { return emptyFuncUnaryTemplate_; }
00268
00269 const FunctionTemplate* getEmptyFuncBinaryTemplate() const
00270 { return emptyFuncBinaryTemplate_; }
00271
00272 void setConstDualMap(ConstDualMap* const & map) { constDualMap_ = map; }
00273
00274 const ConstDualMap* getConstDualMap() const { return constDualMap_; }
00275
00276 void setConstantsByType(Array<Array<int>*>* const & cbt)
00277 { constantsByType_ = cbt; }
00278
00279
00280 const Array<Array<int>*>* getConstantsByType() const
00281 { return constantsByType_; }
00282
00283 const Array<Array<int>*>* getExternalConstantsByType() const
00284 { return externalConstantsByType_; }
00285
00286 void replaceFuncSet(FunctionSet* const & funcSet)
00287 {
00288 if (funcSet_)
00289 {
00290 FunctionSet::iterator fit;
00291 while (!funcSet_->empty())
00292 {
00293 fit = funcSet_->begin();
00294 funcSet_->erase(fit);
00295 delete *fit;
00296 }
00297 delete funcSet_;
00298 }
00299 funcSet_ = funcSet;
00300 }
00301
00302
00303 void setFuncSet(FunctionSet* const & funcSet) { funcSet_ = funcSet; }
00304
00305
00306 const FunctionSet* getFuncSet() const { return funcSet_; }
00307
00308 void deleteDB();
00309 Database* getDB() const { return db_; }
00310 void setDB(Database* const & db) { db_ = db; }
00311
00312 void newTrueFalseGroundingsStore();
00313 TrueFalseGroundingsStore* getTrueFalseGroundingsStore() const
00314 { return trueFalseGroundingsStore_; }
00315 void setTrueFalseGroundingsStore(TrueFalseGroundingsStore* const & tfgs)
00316 { trueFalseGroundingsStore_ = tfgs; }
00317
00318
00319 void compress();
00320
00321
00322 void updatePerOldToNewIds(MLN* const & mln,
00323 hash_map<int,int> & oldToNewConstIds);
00324
00325
00326 void reorderConstants(MLN* const & mln,
00327 hash_map<int, PredicateHashArray*>& predIdToPredsMap);
00328
00329
00330 void reorderConstants(MLN* const & mln);
00331
00332
00333 void reorderConstants(ConstDualMap* const & map,
00334 Array<Array<int>*>* const & cbt,
00335 Array<Array<int>*>* const & ecbt, MLN* const & mln);
00336
00337
00338
00339 Predicate* createPredicate(const int& predId,
00340 const bool& includeEqualPreds) const;
00341
00342
00343 void createPredicates(Array<Predicate*>* const & preds,
00344 const bool& includeEqualPreds) const;
00345
00346 void createPredicates(Array<Predicate*>* const & preds,
00347 const Array<string>* const & predNames);
00348
00350
00351
00352
00353
00354 int addType(const char* const& name)
00355 {
00356 int typeId = typeDualMap_->insert(name);
00357 if (typeId < 0) return -1;
00358
00359 if (typeId != constantsByType_->size())
00360 {
00361 cout << "Error: In Domain::addType(). Expected typeId " << typeId
00362 << " to be equal to " << constantsByType_->size() << endl;
00363 exit(-1);
00364 }
00365
00366 constantsByType_->append(new Array<int>);
00367 externalConstantsByType_->append(new Array<int>);
00368 return typeId;
00369 }
00370
00371
00372 int getNumTypes() const { return typeDualMap_->getNumInt(); }
00373
00374
00375
00376 const Array<const char*>* getTypeNames() const
00377 { return typeDualMap_->getIntToStrArr(); }
00378
00379
00380
00381
00382 int getTypeId(const char* const & name) const
00383 { return typeDualMap_->getInt(name); }
00384
00385
00386
00387 const char* getTypeName(const int typeId) const
00388 { return typeDualMap_->getStr(typeId); }
00389
00390 bool isType(const char* const & name) const { return (getTypeId(name) >= 0); }
00391 bool isType(const int& id) const { return (getTypeName(id) != NULL); }
00392
00393
00395
00396 const Array<int>* getConstantsByType(const int& typeId) const
00397 {
00398 return (*constantsByType_)[typeId];
00399 }
00400
00401
00402 const Array<int>* getConstantsByType(const char* const & typeName) const
00403 {
00404 int typeId = getTypeId(typeName);
00405 if (typeId < 0) return NULL;
00406 return getConstantsByType(typeId);
00407 }
00408
00409
00410 const Array<int>* getConstantsByTypeWithExt(const int& typeId) const
00411 {
00412 Array<int>* cbt = (*constantsByType_)[typeId];
00413 Array<int>* retArr = new Array<int>;
00414 for (int i = 0; i < cbt->size(); i++)
00415 retArr->append((*cbt)[i]);
00416 Array<int>* ecbt = (*externalConstantsByType_)[typeId];
00417 for (int i = 0; i < ecbt->size(); i++)
00418 retArr->append((*ecbt)[i]);
00419 retArr->quicksort();
00420 return retArr;
00421 }
00422
00423
00424 const Array<int>* getConstantsByTypeWithExt(const char* const & typeName) const
00425 {
00426 int typeId = getTypeId(typeName);
00427 if (typeId < 0) return NULL;
00428 return getConstantsByTypeWithExt(typeId);
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00452 int getNumConstantsByType(const int& typeId) const
00453 {
00454 if (!(*constantsByType_)[typeId]) return 0;
00455 return (*constantsByType_)[typeId]->size();
00456 }
00457
00467 int getNumConstantsByType(const char* const & typeName) const
00468 {
00469 int typeId = getTypeId(typeName);
00470 if (typeId < 0) return 0;
00471 return getNumConstantsByType(typeId);
00472 }
00473
00482 int getNumConstantsByTypeWithExt(const int& typeId) const
00483 {
00484 return ((*constantsByType_)[typeId])->size() +
00485 ((*externalConstantsByType_)[typeId])->size();
00486 }
00487
00488
00497 int getNumConstantsByTypeWithExt(const char* const & typeName) const
00498 {
00499 int typeId = getTypeId(typeName);
00500 if (typeId < 0) return 0;
00501 return getNumConstantsByTypeWithExt(typeId);
00502 }
00503
00510 int addConstant(const char* const & constName, const char* const & typeName)
00511 {
00512 return addConstant(constName, typeName, false);
00513 }
00514
00522 int addExternalConstant(const char* const & constName,
00523 const char* const & typeName)
00524 {
00525 return addConstant(constName, typeName, true);
00526 }
00527
00528
00529
00530
00531 int addConstant(const char* const & constName, const char* const & typeName,
00532 const bool& external)
00533 {
00534 int typeId = typeDualMap_->getInt(typeName);
00535 if (typeId < 0)
00536 {
00537 cout << "Warning: failed to add constant " << constName
00538 << " because its type of " << typeName << " doesn't exist" << endl;
00539 return typeId;
00540 }
00541
00542
00543 int constId = getConstantId(constName);
00544 if (constId >= 0)
00545 {
00546 bool newType = false;
00547 Array<const char*>* prevTypes = getConstantTypeNames(constId);
00548 for (int i = 0; i < prevTypes->size(); i++)
00549 {
00550 if (strcmp((*prevTypes)[i], typeName) != 0)
00551 {
00552
00553 newType = true;
00554 break;
00555 }
00556 }
00557
00558 if (!newType)
00559 {
00560
00561 return -1;
00562 }
00563 }
00564
00565 constId = constDualMap_->insert(constName, typeId);
00566 if (constId < 0)
00567 {
00568 cout << "Warning: failed to add constant " << constName << " of type "
00569 << typeName << endl;
00570 return constId;
00571 }
00572
00573
00574 if (external)
00575 {
00576 (*externalConstantsByType_)[typeId]->append(constId);
00577 }
00578 else
00579 {
00580 (*constantsByType_)[typeId]->append(constId);
00581 }
00582 return constId;
00583 }
00584
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00647
00648
00649
00650
00651
00652
00653
00663 const char* getConstantName(const int& id) const
00664 {
00665 return constDualMap_->getStr(id);
00666 }
00667
00668
00669
00670
00671 int getConstantId(const char* const & name) const
00672 {
00673 return constDualMap_->getInt(name);
00674 }
00675
00676
00677 bool isConstant(const char* const & name) const
00678 { return (getConstantId(name) >= 0); }
00679 bool isConstant(const int& id) const { return (getConstantName(id) != NULL); }
00680
00681
00682 Array<int>* getConstantTypeIds(const char* const & constName) const
00683 {
00684 return constDualMap_->getInt2(constName);
00685 }
00686
00687
00688 Array<int>* getConstantTypeIds(const int& constId) const
00689 {
00690 return constDualMap_->getInt2(constId);
00691 }
00692
00693
00694
00695
00696 Array<const char*>* getConstantTypeNames(const int& constId) const
00697 {
00698 Array<int>* typeIds = getConstantTypeIds(constId);
00699 if (typeIds == NULL) return NULL;
00700 else
00701 {
00702 Array<const char*>* typeNames = new Array<const char*>;
00703 for (int i = 0; i < typeIds->size(); i++)
00704 typeNames->append(getTypeName((*typeIds)[i]));
00705 return typeNames;
00706 }
00707 }
00708
00709
00710
00711
00713
00714
00715
00716
00717
00718
00719 int addPredicateTemplate(const PredicateTemplate* const & predTemplate)
00720 {
00721 int predId = predDualMap_->insert(predTemplate->getName());
00722 if (predId < 0)
00723 {
00724 cout << "Warning: failed to add predicate template "
00725 << predTemplate->getName() << endl;
00726 return predId;
00727 }
00728
00729 const char* predName = predDualMap_->getStr(predId);
00730
00731 (*strToPredTemplateMap_)[predName] = predTemplate;
00732 return predId;
00733 }
00734
00735
00736 int getNumPredicates() const { return predDualMap_->getNumInt(); }
00737
00738
00739
00740 const Array<const char*>* getPredicateNames() const
00741 { return predDualMap_->getIntToStrArr(); }
00742
00743
00744 void getNonEqualPredicateNames(Array<string>& predNames) const
00745 {
00746 for (int i = 0; i < getNumPredicates(); i++)
00747 {
00748 if (getPredicateTemplate(i)->isEqualPredicateTemplate()) continue;
00749 if (getPredicateTemplate(i)->isInternalPredicateTemplate()) continue;
00750 if (getPredicateTemplate(i)->isPredicateTemplateFromFunction()) continue;
00751 predNames.append(getPredicateName(i));
00752 }
00753 }
00754
00755
00756
00757
00758 int getPredicateId(const char* const & name) const
00759 { return predDualMap_->getInt(name); }
00760
00761
00762
00763 const char* getPredicateName(const int& id) const
00764 { return predDualMap_->getStr(id); }
00765
00766
00767
00768
00769
00770 const Array<int>* getPredicateTermTypesAsInt(const char* const & name) const
00771 {
00772 StrToPredTemplateMap::iterator it;
00773 if ((it=strToPredTemplateMap_->find(name)) == strToPredTemplateMap_->end())
00774 return NULL;
00775 return (*it).second->getTermTypesAsInt();
00776 }
00777
00778
00779
00780
00781
00782 const Array<int>* getPredicateTermTypesAsInt(const int& id) const
00783 { return getPredicateTermTypesAsInt(getPredicateName(id)); }
00784
00785
00786
00787
00788
00789
00790 const Array<const char*>* getPredicateTermTypesAsStr(const char* const& name)
00791 const
00792 {
00793 StrToPredTemplateMap::iterator it;
00794 if ((it=strToPredTemplateMap_->find(name)) == strToPredTemplateMap_->end())
00795 return NULL;
00796 return (*it).second->getTermTypesAsStr();
00797 }
00798
00799
00800
00801
00802
00803 const Array<const char*>* getPredicateTermTypesAsStr(const int& id)
00804 { return getPredicateTermTypesAsStr(getPredicateName(id)); }
00805
00806
00807
00808
00809
00810
00811 const PredicateTemplate* getPredicateTemplate(const char* const & name) const
00812 {
00813 StrToPredTemplateMap::iterator it;
00814 if ((it=strToPredTemplateMap_->find(name)) == strToPredTemplateMap_->end())
00815 return NULL;
00816 return (*it).second;
00817 }
00818
00819
00820
00821
00822 const PredicateTemplate* getPredicateTemplate(const int& id) const
00823 {
00824 const char* predName = ((Domain*)this)->getPredicateName(id);
00825 return getPredicateTemplate(predName);
00826 }
00827
00833 const int getHighestPredicateArity() const
00834 {
00835 int highestArity = 1;
00836 int arity;
00837 for (int i = 0; i < getNumPredicates(); i++)
00838 {
00839 arity = getPredicateTemplate(i)->getNumTerms();
00840 if (arity > highestArity) highestArity = arity;
00841 }
00842
00843 return highestArity;
00844 }
00845
00846 const PredicateTemplate* getEqualPredicateTemplate() const
00847 { return equalPredTemplate_; }
00848
00849 const PredicateTemplate* getEmptyPredicateTemplate() const
00850 { return emptyPredTemplate_; }
00851
00852 const FunctionTemplate* getEmptyFunctionUnaryTemplate() const
00853 { return emptyFuncUnaryTemplate_; }
00854
00855 const FunctionTemplate* getEmptyFunctionBinaryTemplate() const
00856 { return emptyFuncBinaryTemplate_; }
00857
00858 bool isPredicate(const char* const & name) const
00859 {
00860 return (getPredicateId(name)>=0 ||
00861 strcmp(name, PredicateTemplate::GT_NAME) == 0 ||
00862 strcmp(name, PredicateTemplate::LT_NAME) == 0 ||
00863 strcmp(name, PredicateTemplate::GTEQ_NAME) == 0 ||
00864 strcmp(name, PredicateTemplate::LTEQ_NAME) == 0 ||
00865 strcmp(name, PredicateTemplate::SUBSTR_NAME) == 0);
00866
00867 }
00868
00869 bool isPredicate(const int& id) const {return (getPredicateName(id) != NULL);}
00870
00871
00873
00874
00875
00876
00877
00878
00879 int addFunctionTemplate(const FunctionTemplate* const & funcTemplate)
00880 {
00881 int funcId = funcDualMap_->insert(funcTemplate->getName());
00882 if (funcId < 0)
00883 {
00884 cout << "Warning: failed to add function template "
00885 << funcTemplate->getName() << endl;
00886 return funcId;
00887 }
00888
00889 const char* funcName = funcDualMap_->getStr(funcId);
00890
00891 (*strToFuncTemplateMap_)[funcName] = funcTemplate;
00892 return funcId;
00893 }
00894
00895
00896 int getNumFunctions() const { return funcDualMap_->getNumInt(); }
00897
00898
00899
00900 const Array<const char*>* getFunctionNames() const
00901 { return funcDualMap_->getIntToStrArr(); }
00902
00903
00904
00905 int getFunctionId(const char* const & name) const
00906 { return funcDualMap_->getInt(name); }
00907
00908
00909 const char* getFunctionName(const int& id) const
00910 { return funcDualMap_->getStr(id); }
00911
00912
00913
00914
00915
00916 const Array<int>* getFunctionTermTypesAsInt(const char* const & name) const
00917 {
00918 StrToFuncTemplateMap::iterator it;
00919 if ((it=strToFuncTemplateMap_->find(name)) == strToFuncTemplateMap_->end())
00920 return NULL;
00921 return (*it).second->getTermTypesAsInt();
00922 }
00923
00924
00925
00926
00927
00928
00929 const Array<const char*>* getFunctionTermTypesAsStr(const char* const & name)
00930 const
00931 {
00932 StrToFuncTemplateMap::iterator it;
00933 if ((it=strToFuncTemplateMap_->find(name)) == strToFuncTemplateMap_->end())
00934 return NULL;
00935 return (*it).second->getTermTypesAsStr();
00936 }
00937
00938
00939
00940
00941
00942 const FunctionTemplate* getFunctionTemplate(const char* const & name) const
00943 {
00944 StrToFuncTemplateMap::iterator it;
00945 if ((it=strToFuncTemplateMap_->find(name)) == strToFuncTemplateMap_->end())
00946 return NULL;
00947 return (*it).second;
00948 }
00949
00950
00951
00952
00953 const FunctionTemplate* getFunctionTemplate(const int& id) const
00954 {
00955 const char* funcName = ((Domain*)this)->getFunctionName(id);
00956 return getFunctionTemplate(funcName);
00957 }
00958
00959
00960 bool isFunction(const char* const & name) const
00961 {
00962 return (getFunctionId(name)>=0 ||
00963 strcmp(name, FunctionTemplate::SUCC_NAME) == 0 ||
00964 strcmp(name, FunctionTemplate::PLUS_NAME) == 0 ||
00965 strcmp(name, FunctionTemplate::MINUS_NAME) == 0 ||
00966 strcmp(name, FunctionTemplate::TIMES_NAME) == 0 ||
00967 strcmp(name, FunctionTemplate::DIVIDEDBY_NAME) == 0 ||
00968 strcmp(name, FunctionTemplate::MOD_NAME) == 0 ||
00969 strcmp(name, FunctionTemplate::CONCAT_NAME) == 0);
00970 }
00971
00972 bool isFunction(const int& id) const { return (getFunctionName(id) != NULL); }
00973
00974
00975
00976 const FunctionSet* getFunctionMappings() const { return funcSet_; }
00977
00978
00979
00980
00981 bool addFunctionMapping(Function* f)
00982 {
00983 assert(f->getRetConstId() >= 0);
00984 if (funcSet_->find(f) == funcSet_->end())
00985 {
00986 funcSet_->insert(f);
00987 return true;
00988 }
00989 return false;
00990 }
00991
00992
00993 int getFunctionRetConstId(Function* const & f) const
00994 {
00995 FunctionSet::iterator it;
00996 if ((it = funcSet_->find(f)) == funcSet_->end()) return -1;
00997 return (*it)->getRetConstId();
00998 }
00999
01000
01001 void printPredicateTemplates(ostream& out) const
01002 {
01003 StrToPredTemplateMap::iterator it = strToPredTemplateMap_->begin();
01004 for (; it != strToPredTemplateMap_->end(); it++)
01005 {
01006
01007
01008
01009 if (!(*it).second->isEqualPredicateTemplate() &&
01010 !(*it).second->isInternalPredicateTemplate())
01011 out << *((*it).second) << endl;
01012 }
01013 }
01014
01020 void printFunctionTemplates(ostream& out) const
01021 {
01022 StrToFuncTemplateMap::iterator it = strToFuncTemplateMap_->begin();
01023 for (; it != strToFuncTemplateMap_->end(); it++)
01024 {
01025 if (!(*it).second->isInternalFunctionTemplate())
01026 out << *((*it).second) << endl;
01027 }
01028 }
01029
01030 int getNumNonEvidenceAtoms() const;
01031
01032 Predicate* getNonEvidenceAtom(const int& index) const;
01033
01034 int addPredBlock(Predicate* const & predBlock) const;
01035
01036 int getBlock(const Predicate* const & pred) const;
01037
01038 int getBlock(const GroundPredicate* const & pred) const;
01039
01040 int getNumPredBlocks() const;
01041
01042 const Array<Predicate*>* getPredBlocks() const;
01043
01044 const Predicate* getPredBlock(const int& index) const;
01045
01051 const Predicate* getTruePredInBlock(const int& index) const
01052 {
01053 assert(index <= truePredsInBlock_->size());
01054 return (*truePredsInBlock_)[index];
01055 }
01056
01064 void setTruePredInBlock(const int& index, Predicate* const & pred) const
01065 {
01066 assert(index <= truePredsInBlock_->size());
01067 if ((*truePredsInBlock_)[index]) delete (*truePredsInBlock_)[index];
01068 (*truePredsInBlock_)[index] = pred;
01069 }
01070
01071 const int getBlockSize(const int& index) const
01072 {
01073 return (*blockSizes_)[index];
01074 }
01075
01076 const Array<bool>* getBlockEvidenceArray() const;
01077
01078 const bool getBlockEvidence(const int& index) const;
01079
01080 void setBlockEvidence(const int& index, const bool& value) const;
01081
01088 const Predicate* getRandomPredInBlock(const int& index) const
01089 {
01090 const int chosen = random() % getBlockSize(index);
01091 return getPredInBlock(chosen, index);
01092 }
01093
01101 const Predicate* getPredInBlock(const int& grounding, const int& index) const
01102 {
01103 Predicate* pred = (Predicate*)getPredBlock(index);
01104 assert(grounding <= getBlockSize(index));
01105 Array<Predicate*> predArr;
01106 pred->getGroundingNumber(this, predArr, grounding);
01107 assert(predArr.size() == 1);
01108 const Predicate* gndPred = new Predicate(*predArr[0]);
01109 predArr.deleteItemsAndClear();
01110 return gndPred;
01111 }
01112
01121 const int getIndexOfPredInBlock(const Predicate* const & pred,
01122 const int& blockIdx) const
01123 {
01124 assert(((Predicate*)pred)->isGrounded());
01125
01126 for (int i = 0; i < getBlockSize(blockIdx); i++)
01127 {
01128 const Predicate* predInBlock = getPredInBlock(i, blockIdx);
01129 if (((Predicate*)pred)->same((Predicate*)predInBlock))
01130 {
01131 delete predInBlock;
01132 return i;
01133 }
01134 delete predInBlock;
01135 }
01136 return -1;
01137 }
01138
01142 void computeNumNonEvidAtoms();
01143
01144 void setNumTrueNonEvidGndingsPerClause(Array<double>*
01145 numTrueNonEvidGndingsPerClause)
01146 {
01147 numTrueNonEvidGndingsPerClause_ = numTrueNonEvidGndingsPerClause;
01148 }
01149
01150 void setNumFalseNonEvidGndingsPerClause(Array<double>*
01151 numFalseNonEvidGndingsPerClause)
01152 {
01153 numFalseNonEvidGndingsPerClause_ = numFalseNonEvidGndingsPerClause;
01154 }
01155
01156 void setNumTrueNonEvidGndings(const int & clauseIdx, const double & count)
01157 {
01158 if (numTrueNonEvidGndingsPerClause_->size() <= clauseIdx)
01159 numTrueNonEvidGndingsPerClause_->growToSize(clauseIdx + 1);
01160 (*numTrueNonEvidGndingsPerClause_)[clauseIdx] = count;
01161 }
01162
01163 void setNumFalseNonEvidGndings(const int & clauseIdx, const double & count)
01164 {
01165 if (numFalseNonEvidGndingsPerClause_->size() <= clauseIdx)
01166 numFalseNonEvidGndingsPerClause_->growToSize(clauseIdx + 1);
01167 (*numFalseNonEvidGndingsPerClause_)[clauseIdx] = count;
01168 }
01169
01170 const double getNumTrueNonEvidGroundings(const int & clauseIdx) const
01171 {
01172 return (*numTrueNonEvidGndingsPerClause_)[clauseIdx];
01173 }
01174
01175 const double getNumFalseNonEvidGroundings(const int & clauseIdx) const
01176 {
01177 return (*numFalseNonEvidGndingsPerClause_)[clauseIdx];
01178 }
01179
01180 const double getNumNonEvidGroundings(const int & clauseIdx) const
01181 {
01182 return ((*numTrueNonEvidGndingsPerClause_)[clauseIdx] +
01183 (*numFalseNonEvidGndingsPerClause_)[clauseIdx]);
01184 }
01185
01186
01187
01188 Predicate * getPredicate(Array<int> * const & constants, int predId)
01189 {
01190 Term *term;
01191 const PredicateTemplate *pt = getPredicateTemplate(predId);
01192 Predicate *pred = new Predicate(pt);
01193 for (int i = 0; i < constants->size(); i++)
01194 {
01195 term = new Term((*constants)[i]);
01196 pred->appendTerm(term);
01197 }
01198 return pred;
01199 }
01200
01201 private:
01202
01203 void changePredTermsToNewIds(Predicate* const & p,
01204 hash_map<int,int>& oldToNewConstIds);
01205
01206
01207 private:
01208 DualMap* typeDualMap_;
01209 ConstDualMap* constDualMap_;
01210 DualMap* predDualMap_;
01211 DualMap* funcDualMap_;
01212
01213 StrToPredTemplateMap* strToPredTemplateMap_;
01214
01215 StrToFuncTemplateMap* strToFuncTemplateMap_;
01216
01217
01218 PredicateTemplate* equalPredTemplate_;
01219
01220
01221 PredicateTemplate* emptyPredTemplate_;
01222
01223
01224 FunctionTemplate* emptyFuncUnaryTemplate_;
01225
01226
01227 FunctionTemplate* emptyFuncBinaryTemplate_;
01228
01229
01230 Array<Array<int>*>* constantsByType_;
01231
01232 Array<Array<int>*>* externalConstantsByType_;
01233 Database* db_;
01234 TrueFalseGroundingsStore* trueFalseGroundingsStore_;
01235 FunctionSet* funcSet_;
01236
01237
01238 Array<Predicate*>* predBlocks_;
01239
01240 Array<Predicate*>* truePredsInBlock_;
01241
01242
01243 Array<int>* trueGroundingsInBlock_;
01244
01245 Array<int>* blockSizes_;
01246
01247 Array<bool>* blockEvidence_;
01248
01249
01250
01251 Array<int>* numNonEvidAtomsPerPred_;
01252
01253
01254
01255 Array<double>* numTrueNonEvidGndingsPerClause_;
01256 Array<double>* numFalseNonEvidGndingsPerClause_;
01257 };
01258
01259
01260 #endif