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 and Hoifung Poon.
00006  * 
00007  * Copyright [2004-07] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner and Hoifung
00009  * Poon. 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 and Hoifung
00032  * Poon in the Department of Computer Science and
00033  * Engineering at the University of Washington".
00034  * 
00035  * 4. Your publications acknowledge the use or
00036  * contribution made by the Software to your research
00037  * using the following citation(s): 
00038  * Stanley Kok, Parag Singla, Matthew Richardson and
00039  * Pedro Domingos (2005). "The Alchemy System for
00040  * Statistical Relational AI", Technical Report,
00041  * Department of Computer Science and Engineering,
00042  * University of Washington, Seattle, WA.
00043  * http://www.cs.washington.edu/ai/alchemy.
00044  * 
00045  * 5. Neither the name of the University of Washington nor
00046  * the names of its contributors may be used to endorse or
00047  * promote products derived from this software without
00048  * specific prior written permission.
00049  * 
00050  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00051  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00052  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00053  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00054  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00055  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00056  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00057  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00058  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00059  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00060  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00061  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00062  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00063  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00064  * 
00065  */
00066 #ifndef DOMAIN_H_JUN_21_2005
00067 #define DOMAIN_H_JUN_21_2005
00068 
00069 #include <ext/hash_set>
00070 #include "equalstr.h"
00071 #include "dualmap.h"
00072 #include "constdualmap.h"
00073 #include "predicatetemplate.h"
00074 #include "functiontemplate.h"
00075 #include "predicate.h"
00076 #include "function.h"
00077 
00078 class Clause;
00079 class Database;
00080 class TrueFalseGroundingsStore;
00081 class MLN;
00082 
00083 
00084 typedef hash_map<const char*, const PredicateTemplate*, hash<const char*>, EqualStr>  StrToPredTemplateMap;
00085 
00086 typedef hash_map<const char*, const FunctionTemplate*, hash<const char*>, EqualStr>  StrToFuncTemplateMap;
00087 
00088 
00089 class Domain
00090 {
00091  public:
00092   Domain() : typeDualMap_(new DualMap), constDualMap_(new ConstDualMap),
00093              predDualMap_(new DualMap), funcDualMap_(new DualMap),
00094              strToPredTemplateMap_(new StrToPredTemplateMap),
00095              strToFuncTemplateMap_(new StrToFuncTemplateMap),
00096              constantsByType_(new Array<Array<int>*>), db_(NULL),
00097              trueFalseGroundingsStore_(NULL), funcSet_(new FunctionSet),
00098              predBlocks_(new Array<Array<Predicate*>*>),
00099              blockEvidence_(new Array<bool>)
00100   {
00101     equalPredTemplate_ = new PredicateTemplate();
00102     equalPredTemplate_->setName(PredicateTemplate::EQUAL_NAME);
00103     emptyPredTemplate_ = new PredicateTemplate();
00104     emptyPredTemplate_->setName(PredicateTemplate::EMPTY_NAME);
00105     emptyFuncUnaryTemplate_ = new FunctionTemplate();
00106     emptyFuncUnaryTemplate_->setName(FunctionTemplate::EMPTY_FTEMPLATE_NAME);
00107     emptyFuncBinaryTemplate_ = new FunctionTemplate();
00108     emptyFuncBinaryTemplate_->setName(FunctionTemplate::EMPTY_FTEMPLATE_NAME);
00109     
00110     int typeId = addType(PredicateTemplate::ANY_TYPE_NAME);
00111     if (typeId != 0)
00112     {
00113       cout << "ERROR in domain.h: typeId of EQUAL predicate is not 0." << endl;
00114       exit(-1);
00115     }
00116     equalPredTemplate_->appendTermType(PredicateTemplate::ANY_TYPE_NAME, 
00117                                        false, this);
00118     equalPredTemplate_->appendTermType(PredicateTemplate::ANY_TYPE_NAME, 
00119                                        false, this);
00120     emptyPredTemplate_->appendTermType(PredicateTemplate::ANY_TYPE_NAME, 
00121                                        false, this);
00122     emptyPredTemplate_->appendTermType(PredicateTemplate::ANY_TYPE_NAME, 
00123                                        false, this);
00124     emptyFuncUnaryTemplate_->appendTermType(FunctionTemplate::ANY_TYPE_NAME, 
00125                                        false, this);
00126     emptyFuncBinaryTemplate_->appendTermType(FunctionTemplate::ANY_TYPE_NAME, 
00127                                        false, this);
00128     emptyFuncBinaryTemplate_->appendTermType(FunctionTemplate::ANY_TYPE_NAME, 
00129                                        false, this);
00130                                        
00131   }
00132 
00133 
00134   ~Domain();
00135 
00136 
00137   void replaceTypeDualMap(DualMap* const & map)
00138   {
00139     if (typeDualMap_) delete typeDualMap_;
00140     typeDualMap_ =  map;
00141   }
00142 
00143   void setTypeDualMap(DualMap* const & map) { typeDualMap_ =  map; }
00144   
00145 
00146   const DualMap* getTypeDualMap() const { return typeDualMap_; }
00147 
00148 
00149   void replaceStrToPredTemplateMapAndPredDualMap(StrToPredTemplateMap* const& m,
00150                                                  DualMap* const & predDualMap)
00151   {
00152     if (strToPredTemplateMap_)
00153     {
00154       StrToPredTemplateMap::iterator it = strToPredTemplateMap_->begin(); 
00155       for (; it != strToPredTemplateMap_->end(); it++)
00156         delete (*it).second; //delete PredTemplate*;
00157       delete strToPredTemplateMap_;
00158     }
00159     if (predDualMap_) delete predDualMap_;
00160     strToPredTemplateMap_ = m;
00161     predDualMap_ = predDualMap;
00162   }
00163 
00164 
00165   void setStrToPredTemplateMapAndPredDualMap(StrToPredTemplateMap* const & map,
00166                                              DualMap* const & predDualMap)
00167   { strToPredTemplateMap_ = map; predDualMap_ = predDualMap; }
00168 
00169 
00170   const StrToPredTemplateMap* getStrToPredTemplateMap() const
00171   { return strToPredTemplateMap_; }
00172 
00173 
00174   const DualMap* getPredDualMap() const { return predDualMap_; }
00175 
00176 
00177   void replaceStrToFuncTemplateMapAndFuncDualMap(StrToFuncTemplateMap* const& m,
00178                                                  DualMap* const & funcDualMap)
00179   {
00180     if (strToFuncTemplateMap_)
00181     {
00182       StrToFuncTemplateMap::iterator it2 = strToFuncTemplateMap_->begin(); 
00183       for (; it2 != strToFuncTemplateMap_->end(); it2++)
00184         delete (*it2).second; //delete FuncTemplate*;
00185       delete strToFuncTemplateMap_;
00186     }
00187     if (funcDualMap_) delete funcDualMap_;
00188     strToFuncTemplateMap_ = m; 
00189     funcDualMap_ = funcDualMap;
00190   }
00191 
00192 
00193   void setStrToFuncTemplateMapAndFuncDualMap(StrToFuncTemplateMap* const& m,
00194                                              DualMap* const & funcDualMap)
00195   { strToFuncTemplateMap_ = m;  funcDualMap_ = funcDualMap; }
00196 
00197 
00198   const StrToFuncTemplateMap* getStrToFuncTemplateMap() const
00199   { return strToFuncTemplateMap_; }
00200 
00201 
00202   const DualMap* getFuncDualMap() const { return funcDualMap_; }
00203 
00204   
00205   void replaceEqualPredTemplate(PredicateTemplate* const & t)
00206   {
00207     if (equalPredTemplate_) delete equalPredTemplate_;
00208     equalPredTemplate_ = t;
00209   }
00210 
00211   void setEqualPredTemplate(PredicateTemplate* const & t) 
00212   { equalPredTemplate_ = t; }
00213 
00214   
00215   const PredicateTemplate* getEqualPredTemplate() const
00216   { return equalPredTemplate_; }
00217 
00218   void replaceEmptyPredTemplate(PredicateTemplate* const & t)
00219   {
00220     if (emptyPredTemplate_) delete emptyPredTemplate_;
00221     emptyPredTemplate_ = t;
00222   }
00223 
00224   void setEmptyPredTemplate(PredicateTemplate* const & t) 
00225   { emptyPredTemplate_ = t; }
00226 
00227   
00228   const PredicateTemplate* getEmptyPredTemplate() const
00229   { return emptyPredTemplate_; }
00230 
00231   void replaceEmptyFuncUnaryTemplate(FunctionTemplate* const & t)
00232   {
00233     if (emptyFuncUnaryTemplate_) delete emptyFuncUnaryTemplate_;
00234     emptyFuncUnaryTemplate_ = t;
00235   }
00236 
00237   void replaceEmptyFuncBinaryTemplate(FunctionTemplate* const & t)
00238   {
00239     if (emptyFuncBinaryTemplate_) delete emptyFuncBinaryTemplate_;
00240     emptyFuncBinaryTemplate_ = t;
00241   }
00242 
00243   void setEmptyFuncUnaryTemplate(FunctionTemplate* const & t) 
00244   { emptyFuncUnaryTemplate_ = t; }
00245 
00246   void setEmptyFuncBinaryTemplate(FunctionTemplate* const & t) 
00247   { emptyFuncBinaryTemplate_ = t; }
00248 
00249   
00250   const FunctionTemplate* getEmptyFuncUnaryTemplate() const
00251   { return emptyFuncUnaryTemplate_; }
00252 
00253   const FunctionTemplate* getEmptyFuncBinaryTemplate() const
00254   { return emptyFuncBinaryTemplate_; }
00255 
00256   void replaceConstDualMap(ConstDualMap* const & map)
00257   {
00258     if (constDualMap_) delete constDualMap_;
00259     constDualMap_ = map;
00260   }
00261 
00262   void setConstDualMap(ConstDualMap* const & map) { constDualMap_ = map; }
00263 
00264 
00265   const ConstDualMap* getConstDualMap() const { return constDualMap_; }
00266 
00267 
00268   void replaceConstantsByType(Array<Array<int>*>* const & cbt)
00269   {
00270     if (constantsByType_)
00271     {
00272       for (int i = 0; i < constantsByType_->size(); i++)
00273         delete (*constantsByType_)[i];
00274       delete constantsByType_;
00275     }
00276     constantsByType_ = cbt;
00277   }
00278 
00279 
00280   void setConstantsByType(Array<Array<int>*>* const & cbt) 
00281   { constantsByType_ = cbt; }
00282 
00283   
00284   const Array<Array<int>*>* getConstantsByType() const 
00285   { return constantsByType_; }
00286 
00287 
00288   void replaceFuncSet(FunctionSet* const & funcSet)
00289   {
00290     if (funcSet_)
00291     {
00292       FunctionSet::iterator fit;
00293       while (!funcSet_->empty())
00294       { 
00295         fit = funcSet_->begin();
00296         funcSet_->erase(fit);
00297         delete *fit;
00298       }
00299       delete funcSet_;
00300     }
00301     funcSet_ = funcSet;
00302   }
00303 
00304 
00305   void setFuncSet(FunctionSet* const & funcSet)  { funcSet_ = funcSet; }
00306 
00307 
00308   const FunctionSet* getFuncSet() const { return funcSet_; }
00309 
00310   void deleteDB();
00311   Database* getDB() const { return db_; }
00312   void setDB(Database* const & db) { db_ = db; }
00313 
00314   void newTrueFalseGroundingsStore();
00315   TrueFalseGroundingsStore* getTrueFalseGroundingsStore() const
00316   { return trueFalseGroundingsStore_; }
00317   void setTrueFalseGroundingsStore(TrueFalseGroundingsStore* const & tfgs)
00318   { trueFalseGroundingsStore_ = tfgs; }
00319 
00320 
00321   void compress();
00322 
00323   void reorderConstants(MLN* const & mln, 
00324                         hash_map<int, PredicateHashArray*>& predIdToPredsMap);
00325 
00326     //Caller is responsible for deleting returned pointer
00327   Predicate* createPredicate(const int& predId, 
00328                              const bool& includeEqualPreds) const;
00329 
00330     //Caller is responsible for deleting Array and its contents
00331   void createPredicates(Array<Predicate*>* const & preds,
00332                         const bool& includeEqualPreds) const;
00333 
00334   void createPredicates(Array<Predicate*>* const & preds,
00335                         const Array<string>* const & predNames);
00336 
00338 
00339     // Returns id of type or -1 if type has been added before.
00340     // Type id increases by one each time addType() is called.
00341     // Caller is responsible for deleting name if required. 
00342   int addType(const char* const& name) 
00343   { 
00344     int typeId = typeDualMap_->insert(name); 
00345     if (typeId < 0) return -1;
00346 
00347     if (typeId != constantsByType_->size())
00348     {
00349       cout << "Error: In Domain::addType(). Expected typeId " << typeId 
00350            << " to be equal to " << constantsByType_->size() << endl;
00351       exit(-1);
00352     }
00353       
00354     constantsByType_->append(new Array<int>);
00355     return typeId;
00356   }
00357 
00358 
00359   int getNumTypes() const { return typeDualMap_->getNumInt(); }
00360 
00361 
00362     // Caller should NOT delete the returned array.
00363   const Array<const char*>* getTypeNames() const  
00364   { return typeDualMap_->getIntToStrArr(); }
00365 
00366 
00367     // Caller is responsible for deleting name if required.
00368     // Returns -1 if type does not exist.
00369   int getTypeId(const char* const & name) const 
00370   { return typeDualMap_->getInt(name); }
00371   
00372     // Caller should not delete returned const char*
00373     // Returns NULL if type id does not exist.
00374   const char* getTypeName(const int typeId) const
00375   { return typeDualMap_->getStr(typeId); }
00376 
00377   bool isType(const char* const & name) const { return (getTypeId(name) >= 0); }
00378   bool isType(const int& id) const { return (getTypeName(id) != NULL); }
00379 
00380 
00382 
00383     // Caller must not delete returned pointer.
00384   const Array<int>* getConstantsByType(const int& typeId) const
00385   { return (*constantsByType_)[typeId]; }
00386 
00387   const Array<int>* getConstantsByType(const char* const & typeName) const
00388   { 
00389     int typeId = getTypeId(typeName);
00390     if (typeId < 0) return NULL;
00391     return (*constantsByType_)[typeId]; 
00392   }
00393 
00394         // Returns the index of the given constant of all constants of this type
00395   int getConstantIndexInType(const int& constId) const
00396   {
00397         int typeId = getConstantTypeId(constId);
00398         const Array<int>* constArray = getConstantsByType(typeId);
00399         for (int i = 0; i < constArray->size(); i++)
00400           if (constId == (*constArray)[i]) return i;
00401         return -1;
00402   }
00403   
00404   int getNumConstantsByType(const int& typeId) const
00405   { return ((*constantsByType_)[typeId])->size(); }
00406 
00407 
00408   int getNumConstantsByType(const char* const & typeName) const
00409   {
00410     int typeId = getTypeId(typeName);
00411     if (typeId < 0) return 0;
00412     return ((*constantsByType_)[typeId])->size(); 
00413   }
00414 
00415 
00416     // Returns id of constant or -1 if constant has been added before.
00417     // Constant id increases by one each time addConstant() is called.
00418     // Caller is responsible for deleting name and typeName if needed.
00419   int addConstant(const char* const & constName, const char* const & typeName)
00420   {
00421     int typeId =  typeDualMap_->getInt(typeName);
00422     if (typeId < 0)
00423     {
00424       cout << "Warning: failed to add constant " << constName 
00425            << " because its type of " << typeName << " doesn't exist" << endl;
00426       return typeId;
00427     }
00428     
00429     int constId 
00430       = constDualMap_->insert(constName, typeId);
00431     if (constId < 0)
00432     {
00433       cout << "Warning: failed to add constant " << constName << " of type " 
00434            << typeName << endl;
00435       return constId;
00436     }
00437     
00438     (*constantsByType_)[typeId]->append(constId);
00439     return constId;
00440   }
00441 
00451   int replaceTypeOfConstant(const char* const & constName,
00452                                                         const char* const & oldTypeName,
00453                                                         const char* const & newTypeName)
00454   {
00455     int oldTypeId = typeDualMap_->getInt(oldTypeName);
00456     if (oldTypeId < 0)
00457     {
00458       cout << "Warning: failed to replace type of constant " << constName 
00459            << " because its type of " << oldTypeName << " doesn't exist" << endl;
00460       return oldTypeId;
00461     }
00462     int newTypeId = typeDualMap_->getInt(newTypeName);
00463     if (newTypeId < 0)
00464     {
00465       cout << "Warning: failed to replace type of constant " << constName 
00466            << " because its type of " << newTypeName << " doesn't exist" << endl;
00467       return newTypeId;
00468     }
00469     
00470     int constId 
00471       = constDualMap_->insert(constName, newTypeId);
00472     if (constId < 0)
00473     {
00474       cout << "Warning: failed to remove constant " << constName << " of type " 
00475            << oldTypeName << endl;
00476       return constId;
00477     }
00478     
00479     (*constantsByType_)[oldTypeId]->append(constId);    
00480     (*constantsByType_)[newTypeId]->append(constId);
00481     return constId;
00482   }
00483   
00484   int getNumConstants() const { return constDualMap_->getNumInt(); }
00485 
00486     // Caller SHOULD delete the returned array but not its contents.
00487   const Array<const char*>* getConstantNames()
00488   { return constDualMap_->getIntToStrArr(); }
00489 
00490 
00491     // Caller should not delete returned const char*
00492     // Returns NULL if id does not exist.
00493   const char* getConstantName(const int& id) const
00494   { return constDualMap_->getStr(id); }
00495 
00496 
00497     // Caller is responsible for deleting name if required.
00498     // Returns -1 if the constant isn't found
00499   int getConstantId(const char* const & name) const
00500   { return constDualMap_->getInt(name); }
00501 
00502 
00503   bool isConstant(const char* const & name) const {return (getConstantId(name) >= 0);}
00504   bool isConstant(const int& id) const { return (getConstantName(id) != NULL); }
00505 
00506     // Caller should delete the constName argument if required
00507   int getConstantTypeId(const char* const & constName) const 
00508   { return constDualMap_->getInt2(constName); }
00509 
00510 
00511   int getConstantTypeId(const int& constId) const
00512   { return constDualMap_->getInt2(constId); }
00513 
00514 
00515     // Caller should not delete returned const char*
00516     // Returns NULL if id does not exist.
00517   const char* getConstantTypeName(const int& constId) const
00518   { 
00519     int typeId = getConstantTypeId(constId);
00520     return getTypeName(typeId);
00521   }
00522 
00523 
00524 
00525 
00527 
00528     // Returns id of predicate or -1 if PredicateTemplate has been added before.
00529     // predTemplate should have its name_, termTypes_, and domain_
00530     // assigned before this function is called.
00531     // Predicate id increases by one each time addPredicateTemplate() is called.
00532     // Caller should not delete predTemplate. 
00533   int addPredicateTemplate(const PredicateTemplate* const & predTemplate)
00534   {
00535     int predId = predDualMap_->insert(predTemplate->getName());
00536     if (predId < 0)
00537     {
00538       cout << "Warning: failed to add predicate template " 
00539            << predTemplate->getName() << endl;
00540       return predId;
00541     }
00542 
00543     const char* predName = predDualMap_->getStr(predId);
00544      //strToPredTemplateMap_ shares the predName object with predDualMap_
00545     (*strToPredTemplateMap_)[predName] = predTemplate; 
00546     return predId;
00547   }
00548 
00549 
00550   int getNumPredicates() const { return predDualMap_->getNumInt(); }
00551 
00552 
00553     // Caller should NOT delete the returned array and its contents.
00554   const Array<const char*>* getPredicateNames() const 
00555   { return predDualMap_->getIntToStrArr(); }
00556 
00557 
00558   void getNonEqualPredicateNames(Array<string>& predNames) const
00559   {
00560     for (int i = 0; i < getNumPredicates(); i++)
00561     {
00562       if (getPredicateTemplate(i)->isEqualPredicateTemplate()) continue;
00563       if (getPredicateTemplate(i)->isInternalPredicateTemplate()) continue;
00564       if (getPredicateTemplate(i)->isPredicateTemplateFromFunction()) continue;
00565       predNames.append(getPredicateName(i));
00566     }
00567   }
00568 
00569 
00570     // Caller is responsible for deleting name if required.
00571     // Returns id of predicate if it exist, otherwise returns -1
00572   int getPredicateId(const char* const & name) const
00573   { return predDualMap_->getInt(name); }
00574 
00575     // Caller should not delete returned const char*
00576     // Returns name of predicate if it exists, otherwise returns NULL
00577   const char* getPredicateName(const int& id) const
00578   { return predDualMap_->getStr(id); }
00579 
00580     //Returns the term type ids of the predicate with the specified name, or
00581     //NULL if there is no predicate with specified name.
00582     //Caller should delete name if required.
00583     //Caller should not delete the returned const Array<int>*
00584   const Array<int>* getPredicateTermTypesAsInt(const char* const & name) const
00585   {
00586     StrToPredTemplateMap::iterator it;
00587     if ((it=strToPredTemplateMap_->find(name)) == strToPredTemplateMap_->end())
00588       return NULL;
00589     return (*it).second->getTermTypesAsInt();
00590   }
00591 
00592 
00593     //Returns the term type ids of the predicate with the specified id, or
00594     //NULL if there is no predicate with specified id.
00595     //Caller should not delete the returned const Array<int>*
00596   const Array<int>* getPredicateTermTypesAsInt(const int& id) const
00597   { return getPredicateTermTypesAsInt(getPredicateName(id)); }
00598 
00599 
00600     //Returns the term type ids of the predicate with the specified name, or
00601     //NULL if there is no predicate with specified name.
00602     //Caller should delete name if required.
00603     //Caller should not delete the returned const Array<const char*>*
00604   const Array<const char*>* getPredicateTermTypesAsStr(const char* const& name)
00605     const
00606   {
00607     StrToPredTemplateMap::iterator it;
00608     if ((it=strToPredTemplateMap_->find(name)) == strToPredTemplateMap_->end())
00609       return NULL;
00610     return (*it).second->getTermTypesAsStr();
00611   }
00612 
00613 
00614     //Returns the term type ids of the predicate with the specified id, or
00615     //NULL if there is no predicate with specified id.
00616     //Caller should not delete the returned const Array<const char*>*
00617   const Array<const char*>* getPredicateTermTypesAsStr(const int& id)
00618   { return getPredicateTermTypesAsStr(getPredicateName(id)); }
00619 
00620 
00621     // Returns the PredicateTemplate* with the given name or NULL if there is no
00622     // PredicateTemplate with the specified name.
00623     // Caller is responsible for deleting name if required.
00624     // Caller should not delete the returned PredicateTemplate* nor modify it.
00625   const PredicateTemplate* getPredicateTemplate(const char* const & name) const
00626   {
00627     StrToPredTemplateMap::iterator it;
00628     if ((it=strToPredTemplateMap_->find(name)) == strToPredTemplateMap_->end())
00629       return NULL;
00630     return (*it).second;
00631   }
00632 
00633 
00634     // Returns the PredicateTemplate* with the given id.
00635     // Caller should not delete the returned PredicateTemplate* nor modify it.
00636   const PredicateTemplate* getPredicateTemplate(const int& id) const
00637   {
00638     const char* predName = ((Domain*)this)->getPredicateName(id);
00639     return getPredicateTemplate(predName);
00640   }
00641 
00647   const int getHighestPredicateArity() const
00648   {
00649         int highestArity = 1;
00650         int arity;
00651         for (int i = 0; i < getNumPredicates(); i++)
00652     {
00653       arity = getPredicateTemplate(i)->getNumTerms();
00654       if (arity > highestArity) highestArity = arity;
00655     }
00656     
00657         return highestArity;
00658   }
00659 
00660   const PredicateTemplate* getEqualPredicateTemplate() const
00661   { return equalPredTemplate_; }
00662   
00663   const PredicateTemplate* getEmptyPredicateTemplate() const
00664   { return emptyPredTemplate_; }
00665  
00666   const FunctionTemplate* getEmptyFunctionUnaryTemplate() const
00667   { return emptyFuncUnaryTemplate_; }
00668 
00669   const FunctionTemplate* getEmptyFunctionBinaryTemplate() const
00670   { return emptyFuncBinaryTemplate_; }
00671 
00672   bool isPredicate(const char* const & name) const 
00673   {
00674         return (getPredicateId(name)>=0 ||
00675                         strcmp(name, PredicateTemplate::GT_NAME) == 0 || 
00676                         strcmp(name, PredicateTemplate::LT_NAME) == 0 ||
00677                         strcmp(name, PredicateTemplate::GTEQ_NAME) == 0 ||
00678                         strcmp(name, PredicateTemplate::LTEQ_NAME) == 0 ||
00679                         strcmp(name, PredicateTemplate::SUBSTR_NAME) == 0);
00680         
00681   }
00682 
00683   bool isPredicate(const int& id) const {return (getPredicateName(id) != NULL);}
00684 
00685 
00687 
00688     // Returns id of function or -1 if function has been added before.
00689     // functionTemplate should have its name_, termTypes_, domain_, 
00690     // and retTypeName_ assigned before this function is called.
00691     // Function id increases by one each time addFunctionTemplate() is called.
00692     // Caller should not delete funcTemplate. 
00693   int addFunctionTemplate(const FunctionTemplate* const & funcTemplate)
00694   {
00695     int funcId = funcDualMap_->insert(funcTemplate->getName());
00696     if (funcId < 0)
00697     {
00698       cout << "Warning: failed to add function template " 
00699            << funcTemplate->getName() << endl;
00700       return funcId;
00701     }
00702 
00703     const char* funcName = funcDualMap_->getStr(funcId);
00704      //strToFuncTemplateMap_ shares the funcName object with funcDualMap_
00705     (*strToFuncTemplateMap_)[funcName] = funcTemplate; 
00706     return funcId;
00707   }
00708 
00709 
00710   int getNumFunctions() const { return funcDualMap_->getNumInt(); }
00711 
00712 
00713     // Caller should NOT delete the returned array but not its contents.
00714   const Array<const char*>* getFunctionNames() const 
00715   { return funcDualMap_->getIntToStrArr(); }
00716 
00717 
00718     // Caller is responsible for deleting name if required.
00719   int getFunctionId(const char* const & name) const
00720   { return funcDualMap_->getInt(name); }
00721 
00722     // Caller should not delete returned const char*
00723   const char* getFunctionName(const int& id) const
00724   { return funcDualMap_->getStr(id); }
00725 
00726     //Returns the term type ids of the function with the specified name, or
00727     //NULL if there is no function with specified name.
00728     //Caller should delete name if required.
00729     //Caller should not delete the returned const Array<int>*
00730   const Array<int>* getFunctionTermTypesAsInt(const char* const & name) const
00731   {
00732     StrToFuncTemplateMap::iterator it;
00733     if ((it=strToFuncTemplateMap_->find(name)) == strToFuncTemplateMap_->end())
00734       return NULL;
00735     return (*it).second->getTermTypesAsInt();
00736   }
00737 
00738 
00739     //Returns the term type ids of the function with the specified name, or
00740     //NULL if there is no function with specified name.
00741     //Caller should delete name if required.
00742     //Caller should not delete the returned const Array<const char*>*
00743   const Array<const char*>* getFunctionTermTypesAsStr(const char* const & name)
00744     const
00745   {
00746     StrToFuncTemplateMap::iterator it;
00747     if ((it=strToFuncTemplateMap_->find(name)) == strToFuncTemplateMap_->end())
00748       return NULL;
00749     return (*it).second->getTermTypesAsStr();
00750   }
00751 
00752 
00753     // Returns the FunctionTemplate with the given name 
00754     // Caller is responsible for deleting name if required.
00755     // Caller should not delete the returned FunctionTemplate* nor modify it.
00756   const FunctionTemplate* getFunctionTemplate(const char* const & name) const
00757   {
00758     StrToFuncTemplateMap::iterator it;
00759     if ((it=strToFuncTemplateMap_->find(name)) == strToFuncTemplateMap_->end())
00760       return NULL;
00761     return (*it).second;
00762   }
00763 
00764     // Returns the PredicateTemplate* with the given id or NULL if there is no
00765     // PredicateTemplate with the specified id.
00766     // Caller should not delete the returned PredicateTemplate* nor modify it.
00767   const FunctionTemplate* getFunctionTemplate(const int& id) const
00768   {
00769     const char* funcName = ((Domain*)this)->getFunctionName(id);
00770     return getFunctionTemplate(funcName);
00771   }
00772   
00773   
00774   bool isFunction(const char* const & name) const 
00775   {
00776         return (getFunctionId(name)>=0 ||
00777                         strcmp(name, FunctionTemplate::SUCC_NAME) == 0 || 
00778                         strcmp(name, FunctionTemplate::PLUS_NAME) == 0 ||
00779                         strcmp(name, FunctionTemplate::MINUS_NAME) == 0 ||
00780                         strcmp(name, FunctionTemplate::TIMES_NAME) == 0 ||
00781                         strcmp(name, FunctionTemplate::DIVIDEDBY_NAME) == 0 ||
00782                         strcmp(name, FunctionTemplate::MOD_NAME) == 0 ||
00783                         strcmp(name, FunctionTemplate::CONCAT_NAME) == 0);
00784   }
00785   
00786   bool isFunction(const int& id) const { return (getFunctionName(id) != NULL); }
00787 
00788 
00789     // Caller shouldn't delete the returned function set nor modify its contents
00790   const FunctionSet* getFunctionMappings() const { return funcSet_; }
00791 
00792   
00793     // f is owned by Domain and caller should not delete it
00794     // returns true if f is added, and false if f already exists
00795   bool addFunctionMapping(Function* f) 
00796   {
00797         assert(f->getRetConstId() >= 0);
00798     if (funcSet_->find(f) ==  funcSet_->end())
00799     {
00800       funcSet_->insert(f);
00801       return true;
00802     }
00803     return false;
00804   }
00805 
00806   
00807   int getFunctionRetConstId(Function* const & f) const
00808   {
00809     FunctionSet::iterator it;
00810     if ((it = funcSet_->find(f)) == funcSet_->end()) return -1;
00811     return (*it)->getRetConstId();
00812   }
00813 
00814 
00815   void printPredicateTemplates(ostream& out) const
00816   {
00817     StrToPredTemplateMap::iterator it = strToPredTemplateMap_->begin();
00818     for (; it !=  strToPredTemplateMap_->end(); it++)
00819     {
00820 //      if (!(*it).second->isEqualPredicateTemplate() &&
00821 //                !(*it).second->isInternalPredicateTemplate() &&
00822 //                !(*it).second->isPredicateTemplateFromFunction())
00823       if (!(*it).second->isEqualPredicateTemplate() &&
00824           !(*it).second->isInternalPredicateTemplate())
00825         out << *((*it).second) << endl;
00826     }
00827   }
00828 
00834   void printFunctionTemplates(ostream& out) const
00835   {
00836     StrToFuncTemplateMap::iterator it = strToFuncTemplateMap_->begin();
00837     for (; it !=  strToFuncTemplateMap_->end(); it++)
00838     {
00839       if (!(*it).second->isInternalFunctionTemplate())
00840         out << *((*it).second) << endl;
00841     }
00842   }
00843 
00844   int getNumNonEvidenceAtoms() const;
00845   
00846   Predicate* getNonEvidenceAtom(int index) const;
00847   
00848   int addPredBlock(Array<Predicate*>* const & predBlock) const;
00849   
00850   int getBlock(Predicate* pred) const;
00851   
00852   int getNumPredBlocks() const;
00853   
00854   const Array<Array<Predicate*>*>* getPredBlocks() const;
00855 
00856   const Array<Predicate*>* getPredBlock(const int index) const;
00857   
00858   const Array<bool>* getBlockEvidenceArray() const;
00859   
00860   const bool getBlockEvidence(const int index) const;
00861 
00862   void setBlockEvidence(const int index, const bool value) const;
00863 
00864   int getEvidenceIdxInBlock(const int index) const;
00865  
00866  private:
00867  
00868   void changePredTermsToNewIds(Predicate* const & p,
00869                                hash_map<int,int>& oldToNewConstIds);
00870 
00871 
00872  private:
00873   DualMap* typeDualMap_;  //maps type id to name and vice versa
00874   ConstDualMap* constDualMap_; //maps constant id to name and vice versa
00875   DualMap* predDualMap_;  //maps predicate id to name and vice versa
00876   DualMap* funcDualMap_;  //maps func id to name and vice versa
00877     //maps predicate name to PredicateTemplatex*. shares key with predDualMap_
00878   StrToPredTemplateMap* strToPredTemplateMap_;
00879     //maps function name to FunctionTemplate*. shares its key with funcDualMap_
00880   StrToFuncTemplateMap* strToFuncTemplateMap_;
00881 
00882     // corresponds to the predicate EQUAL_NAME(SK_ANY, SK_ANY)
00883   PredicateTemplate* equalPredTemplate_;
00884   
00885     // empty binary predicate template used when infix operators are used
00886   PredicateTemplate* emptyPredTemplate_;
00887   
00888     // empty unary function template used when infix operators are used
00889   FunctionTemplate* emptyFuncUnaryTemplate_;
00890 
00891     // empty binary function template used when infix operators are used
00892   FunctionTemplate* emptyFuncBinaryTemplate_;
00893   
00894     // constantsByTypeArr_[0] is an Array<int> of constants of type 0
00895   Array<Array<int>*>* constantsByType_;
00896   Database* db_;
00897   TrueFalseGroundingsStore* trueFalseGroundingsStore_; //for sampling clauses
00898   FunctionSet* funcSet_;
00899     // Array storing blocks of preds which are mutually exclusive and exhaustive
00900   Array<Array<Predicate*>*>* predBlocks_;
00901     // Flags indicating if block is fulfilled by evidence
00902   Array<bool >* blockEvidence_;
00903 };
00904 
00905 
00906 #endif

Generated on Tue Jan 16 05:30:04 2007 for Alchemy by  doxygen 1.5.1