domain.h

00001 /*
00002  * All of the documentation and software included in the
00003  * Alchemy Software is copyrighted by Stanley Kok, Parag
00004  * Singla, Matthew Richardson, Pedro Domingos, Marc
00005  * Sumner, Hoifung Poon, Daniel Lowd, and Jue Wang.
00006  * 
00007  * Copyright [2004-09] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00009  * Poon, Daniel Lowd, and Jue Wang. All rights reserved.
00010  * 
00011  * Contact: Pedro Domingos, University of Washington
00012  * (pedrod@cs.washington.edu).
00013  * 
00014  * Redistribution and use in source and binary forms, with
00015  * or without modification, are permitted provided that
00016  * the following conditions are met:
00017  * 
00018  * 1. Redistributions of source code must retain the above
00019  * copyright notice, this list of conditions and the
00020  * following disclaimer.
00021  * 
00022  * 2. Redistributions in binary form must reproduce the
00023  * above copyright notice, this list of conditions and the
00024  * following disclaimer in the documentation and/or other
00025  * materials provided with the distribution.
00026  * 
00027  * 3. All advertising materials mentioning features or use
00028  * of this software must display the following
00029  * acknowledgment: "This product includes software
00030  * developed by Stanley Kok, Parag Singla, Matthew
00031  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00032  * Poon, Daniel Lowd, and Jue Wang in the Department of
00033  * Computer Science and Engineering at the University of
00034  * Washington".
00035  * 
00036  * 4. Your publications acknowledge the use or
00037  * contribution made by the Software to your research
00038  * using the following citation(s): 
00039  * Stanley Kok, Parag Singla, Matthew Richardson and
00040  * Pedro Domingos (2005). "The Alchemy System for
00041  * Statistical Relational AI", Technical Report,
00042  * Department of Computer Science and Engineering,
00043  * University of Washington, Seattle, WA.
00044  * http://alchemy.cs.washington.edu.
00045  * 
00046  * 5. Neither the name of the University of Washington nor
00047  * the names of its contributors may be used to endorse or
00048  * promote products derived from this software without
00049  * specific prior written permission.
00050  * 
00051  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00052  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00053  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00054  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00055  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00056  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00057  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00058  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00059  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00060  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00061  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00062  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00063  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00064  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     // Name of type used in a unary predicate to simulate propositional constant
00096   static const char* PROPOSITIONAL_TYPE;
00097     // Name of constant used in a unary predicate to simulate propositional
00098     // constant
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              //externalConstant_(new Array<bool>),
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; //delete PredTemplate*;
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; //delete FuncTemplate*;
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     // common
00322   void updatePerOldToNewIds(MLN* const & mln,
00323                             hash_map<int,int> & oldToNewConstIds);
00324 
00325     // for parser
00326   void reorderConstants(MLN* const & mln,
00327                         hash_map<int, PredicateHashArray*>& predIdToPredsMap);
00328 
00329     // for domain0 - postprocess
00330   void reorderConstants(MLN* const & mln);
00331 
00332     // for domain1-N - postprocess
00333   void reorderConstants(ConstDualMap* const & map,
00334                         Array<Array<int>*>* const & cbt,  
00335                         Array<Array<int>*>* const & ecbt, MLN* const & mln);
00336 
00337 
00338     //Caller is responsible for deleting returned pointer
00339   Predicate* createPredicate(const int& predId, 
00340                              const bool& includeEqualPreds) const;
00341 
00342     //Caller is responsible for deleting Array and its contents
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     // Returns id of type or -1 if type has been added before.
00352     // Type id increases by one each time addType() is called.
00353     // Caller is responsible for deleting name if required. 
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     // Caller should NOT delete the returned array.
00376   const Array<const char*>* getTypeNames() const  
00377   { return typeDualMap_->getIntToStrArr(); }
00378 
00379 
00380     // Caller is responsible for deleting name if required.
00381     // Returns -1 if type does not exist.
00382   int getTypeId(const char* const & name) const 
00383   { return typeDualMap_->getInt(name); }
00384   
00385     // Caller should not delete returned const char*
00386     // Returns NULL if type id does not exist.
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     // Caller must not delete returned pointer.
00396   const Array<int>* getConstantsByType(const int& typeId) const
00397   {
00398     return (*constantsByType_)[typeId];
00399   }
00400 
00401     // Caller must not delete returned pointer.
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     // Caller must delete returned pointer.
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     // Caller must delete returned pointer.
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         // Returns the index of the given constant of all constants of this type
00432 /*
00433   int getConstantIndexInType(const int& constId) const
00434   {
00435         int typeId = getConstantTypeId(constId);
00436         const Array<int>* constArray = getConstantsByType(typeId);
00437         for (int i = 0; i < constArray->size(); i++)
00438           if (constId == (*constArray)[i])
00439         return i;
00440         return -1;
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     // Returns id of constant or -1 if constant has been added before.
00529     // Constant id increases by one each time addConstant() is called.
00530     // Caller is responsible for deleting name and typeName if needed.
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       // If constant already exists, then add it to new type
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             // New type
00553           newType = true;
00554           break;
00555         }
00556       }
00557       
00558       if (!newType)
00559       {
00560           // Constant already belongs to this type
00561         return -1;
00562       }
00563     }
00564       // Insert constant into constDualMap_
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       // Insert id into by type index
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   int replaceTypeOfConstant(const char* const & constName,
00596                                                         const char* const & oldTypeName,
00597                                                         const char* const & newTypeName)
00598   {
00599     int oldTypeId = typeDualMap_->getInt(oldTypeName);
00600     if (oldTypeId < 0)
00601     {
00602       cout << "Warning: failed to replace type of constant " << constName 
00603            << " because its type of " << oldTypeName << " doesn't exist" << endl;
00604       return oldTypeId;
00605     }
00606     int newTypeId = typeDualMap_->getInt(newTypeName);
00607     if (newTypeId < 0)
00608     {
00609       cout << "Warning: failed to replace type of constant " << constName 
00610            << " because its type of " << newTypeName << " doesn't exist" << endl;
00611       return newTypeId;
00612     }
00613     
00614     int constId = constDualMap_->insert(constName, newTypeId);
00615     if (constId < 0)
00616     {
00617       cout << "Warning: failed to remove constant " << constName << " of type " 
00618            << oldTypeName << endl;
00619       return constId;
00620     }
00621     
00622     (*constantsByType_)[oldTypeId]->removeItem(constId);
00623     (*constantsByType_)[newTypeId]->append(constId);
00624     return constId;
00625   }
00626 */
00627   
00631 /*
00632   int getNumConstants() const
00633   {
00634     int count = 0;
00635     for (int i = 0; i < constantsByType_->size(); i++)
00636     {
00637       for (int j = 0; j < (*constantsByType_)[i]->size(); j++)
00638         count++;
00639     }
00640     return count;
00641   }
00642 */
00643 
00647 /*
00648   int getNumConstantsWithExt() const
00649   {
00650     return constDualMap_->getNumInt();
00651   }
00652 */
00653 
00663   const char* getConstantName(const int& id) const
00664   {
00665     return constDualMap_->getStr(id);
00666   }
00667 
00668 
00669     // Caller is responsible for deleting name if required.
00670     // Returns -1 if the constant isn't found
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     // Caller should delete the constName argument if required
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     // Caller should not delete returned const char*
00695     // Returns NULL if id does not exist.
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     // Returns id of predicate or -1 if PredicateTemplate has been added before.
00715     // predTemplate should have its name_, termTypes_, and domain_
00716     // assigned before this function is called.
00717     // Predicate id increases by one each time addPredicateTemplate() is called.
00718     // Caller should not delete predTemplate. 
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      //strToPredTemplateMap_ shares the predName object with predDualMap_
00731     (*strToPredTemplateMap_)[predName] = predTemplate; 
00732     return predId;
00733   }
00734 
00735 
00736   int getNumPredicates() const { return predDualMap_->getNumInt(); }
00737 
00738 
00739     // Caller should NOT delete the returned array and its contents.
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     // Caller is responsible for deleting name if required.
00757     // Returns id of predicate if it exist, otherwise returns -1
00758   int getPredicateId(const char* const & name) const
00759   { return predDualMap_->getInt(name); }
00760 
00761     // Caller should not delete returned const char*
00762     // Returns name of predicate if it exists, otherwise returns NULL
00763   const char* getPredicateName(const int& id) const
00764   { return predDualMap_->getStr(id); }
00765 
00766     //Returns the term type ids of the predicate with the specified name, or
00767     //NULL if there is no predicate with specified name.
00768     //Caller should delete name if required.
00769     //Caller should not delete the returned const Array<int>*
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     //Returns the term type ids of the predicate with the specified id, or
00780     //NULL if there is no predicate with specified id.
00781     //Caller should not delete the returned const Array<int>*
00782   const Array<int>* getPredicateTermTypesAsInt(const int& id) const
00783   { return getPredicateTermTypesAsInt(getPredicateName(id)); }
00784 
00785 
00786     //Returns the term type ids of the predicate with the specified name, or
00787     //NULL if there is no predicate with specified name.
00788     //Caller should delete name if required.
00789     //Caller should not delete the returned const Array<const char*>*
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     //Returns the term type ids of the predicate with the specified id, or
00801     //NULL if there is no predicate with specified id.
00802     //Caller should not delete the returned const Array<const char*>*
00803   const Array<const char*>* getPredicateTermTypesAsStr(const int& id)
00804   { return getPredicateTermTypesAsStr(getPredicateName(id)); }
00805 
00806 
00807     // Returns the PredicateTemplate* with the given name or NULL if there is no
00808     // PredicateTemplate with the specified name.
00809     // Caller is responsible for deleting name if required.
00810     // Caller should not delete the returned PredicateTemplate* nor modify it.
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     // Returns the PredicateTemplate* with the given id.
00821     // Caller should not delete the returned PredicateTemplate* nor modify it.
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     // Returns id of function or -1 if function has been added before.
00875     // functionTemplate should have its name_, termTypes_, domain_, 
00876     // and retTypeName_ assigned before this function is called.
00877     // Function id increases by one each time addFunctionTemplate() is called.
00878     // Caller should not delete funcTemplate. 
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      //strToFuncTemplateMap_ shares the funcName object with funcDualMap_
00891     (*strToFuncTemplateMap_)[funcName] = funcTemplate; 
00892     return funcId;
00893   }
00894 
00895 
00896   int getNumFunctions() const { return funcDualMap_->getNumInt(); }
00897 
00898 
00899     // Caller should NOT delete the returned array but not its contents.
00900   const Array<const char*>* getFunctionNames() const 
00901   { return funcDualMap_->getIntToStrArr(); }
00902 
00903 
00904     // Caller is responsible for deleting name if required.
00905   int getFunctionId(const char* const & name) const
00906   { return funcDualMap_->getInt(name); }
00907 
00908     // Caller should not delete returned const char*
00909   const char* getFunctionName(const int& id) const
00910   { return funcDualMap_->getStr(id); }
00911 
00912     //Returns the term type ids of the function with the specified name, or
00913     //NULL if there is no function with specified name.
00914     //Caller should delete name if required.
00915     //Caller should not delete the returned const Array<int>*
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     //Returns the term type ids of the function with the specified name, or
00926     //NULL if there is no function with specified name.
00927     //Caller should delete name if required.
00928     //Caller should not delete the returned const Array<const char*>*
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     // Returns the FunctionTemplate with the given name 
00940     // Caller is responsible for deleting name if required.
00941     // Caller should not delete the returned FunctionTemplate* nor modify it.
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     // Returns the FunctionTemplate* with the given id or NULL if there is no
00951     // FunctionTemplate with the specified id.
00952     // Caller should not delete the returned FunctionTemplate* nor modify it.
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     // Caller shouldn't delete the returned function set nor modify its contents
00976   const FunctionSet* getFunctionMappings() const { return funcSet_; }
00977 
00978   
00979     // f is owned by Domain and caller should not delete it
00980     // returns true if f is added, and false if f already exists
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 //      if (!(*it).second->isEqualPredicateTemplate() &&
01007 //                !(*it).second->isInternalPredicateTemplate() &&
01008 //                !(*it).second->isPredicateTemplateFromFunction())
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     //get the predicate corresponding to the tuple of constants
01187     //Note: caller is responsible for deleting it
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_;  //maps type id to name and vice versa
01209   ConstDualMap* constDualMap_; //maps constant id to name and vice versa
01210   DualMap* predDualMap_;  //maps predicate id to name and vice versa
01211   DualMap* funcDualMap_;  //maps func id to name and vice versa
01212     //maps predicate name to PredicateTemplate*. shares key with predDualMap_
01213   StrToPredTemplateMap* strToPredTemplateMap_;
01214     //maps function name to FunctionTemplate*. shares its key with funcDualMap_
01215   StrToFuncTemplateMap* strToFuncTemplateMap_;
01216 
01217     // corresponds to the predicate EQUAL_NAME(SK_ANY, SK_ANY)
01218   PredicateTemplate* equalPredTemplate_;
01219   
01220     // empty binary predicate template used when infix operators are used
01221   PredicateTemplate* emptyPredTemplate_;
01222   
01223     // empty unary function template used when infix operators are used
01224   FunctionTemplate* emptyFuncUnaryTemplate_;
01225 
01226     // empty binary function template used when infix operators are used
01227   FunctionTemplate* emptyFuncBinaryTemplate_;
01228   
01229     // constantsByType_[0] is an Array<int> of constants of type 0
01230   Array<Array<int>*>* constantsByType_;
01231     // externalConstantsByType_[0] is an Array<int> of ext. constants of type 0
01232   Array<Array<int>*>* externalConstantsByType_;
01233   Database* db_;
01234   TrueFalseGroundingsStore* trueFalseGroundingsStore_; //for sampling clauses
01235   FunctionSet* funcSet_;
01236     // (Partially grounded) predicates which represent one block. Terms which
01237     // are still variables are the vars with !
01238   Array<Predicate*>* predBlocks_;
01239     // Ground predicate in each block which is true
01240   Array<Predicate*>* truePredsInBlock_;
01241     // Grounding index in each block which is true (i-th combination of
01242     // constants)
01243   Array<int>* trueGroundingsInBlock_;
01244     // Size of each block
01245   Array<int>* blockSizes_;
01246     // Flags indicating if block is fulfilled by evidence
01247   Array<bool>* blockEvidence_;
01248   
01249     // numNonEvidAtomsPerPred_[p] holds the number of non-evidence groundings
01250     // of the first-order predicate with index p
01251   Array<int>* numNonEvidAtomsPerPred_;
01252 
01253     // numTrueNonEvidGndingsPerClause_[c] holds the total number of true
01254     // groundings of the first-order clause not satisfied by evidence
01255   Array<double>* numTrueNonEvidGndingsPerClause_;
01256   Array<double>* numFalseNonEvidGndingsPerClause_;
01257 };
01258 
01259 
01260 #endif

Generated on Sun Jun 7 11:55:16 2009 for Alchemy by  doxygen 1.5.1