domain.cpp

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 #include "clause.h"
00068 #include "domain.h"
00069 #include "database.h"
00070 #include "mln.h"
00071 #include "truefalsegroundingsstore.h"
00072 
00073 const char* Domain::PROPOSITIONAL_TYPE = "AlchemyPropositionalType";
00074 const char* Domain::PROPOSITIONAL_CONSTANT = "AlchemyPropositionalConstant";
00075 
00076 Domain::~Domain()
00077 {
00078   if (typeDualMap_) delete typeDualMap_;
00079   if (constDualMap_) delete constDualMap_;
00080   
00081   if (strToPredTemplateMap_)
00082   {
00083       // strToPredTemplateMap_'s keys (predicate names) are shared with 
00084       // predDualMap_. The latter will delete the keys.
00085     StrToPredTemplateMap::iterator it = strToPredTemplateMap_->begin(); 
00086     for (; it != strToPredTemplateMap_->end(); it++)
00087       delete (*it).second; //delete PredTemplate*;
00088     delete strToPredTemplateMap_;
00089   }
00090   if (predDualMap_) delete predDualMap_;
00091 
00092   if (strToFuncTemplateMap_)
00093   {
00094       // strToFuncTemplateMap_'s keys (function names) are shared with 
00095       // funcDualMap_. The latter will delete the keys.
00096     StrToFuncTemplateMap::iterator it2 = strToFuncTemplateMap_->begin(); 
00097     for (; it2 != strToFuncTemplateMap_->end(); it2++)
00098       delete (*it2).second; //delete FuncTemplate*;
00099     delete strToFuncTemplateMap_;
00100   }
00101   if (funcDualMap_) delete funcDualMap_;
00102   
00103   if (equalPredTemplate_) delete equalPredTemplate_;
00104   
00105   if (emptyPredTemplate_) delete emptyPredTemplate_;
00106   
00107   if (emptyFuncUnaryTemplate_) delete emptyFuncUnaryTemplate_;
00108   
00109   if (emptyFuncBinaryTemplate_) delete emptyFuncBinaryTemplate_;
00110   
00111   if (constantsByType_)
00112   {
00113     for (int i = 0; i < constantsByType_->size(); i++)
00114       delete (*constantsByType_)[i];
00115     delete constantsByType_;
00116   }
00117 
00118   if (externalConstantsByType_)
00119   {
00120     for (int i = 0; i < externalConstantsByType_->size(); i++)
00121       delete (*externalConstantsByType_)[i];
00122     delete externalConstantsByType_;
00123   }
00124 
00125   if (predBlocks_)
00126   {
00127     for (int i = 0; i < predBlocks_->size(); i++)
00128       delete (*predBlocks_)[i];
00129     delete predBlocks_;
00130   }
00131 
00132   if (truePredsInBlock_)
00133   {
00134     for (int i = 0; i < truePredsInBlock_->size(); i++)
00135       delete (*truePredsInBlock_)[i];
00136     delete truePredsInBlock_;
00137   }
00138 
00139   if (blockSizes_) delete blockSizes_;
00140 
00141   if (blockEvidence_) delete blockEvidence_;
00142 
00143   if (db_) delete db_;
00144 
00145   if (trueFalseGroundingsStore_) delete trueFalseGroundingsStore_;
00146   
00147   if (funcSet_)
00148   {
00149     FunctionSet::iterator fit;
00150     while (!funcSet_->empty())
00151     { 
00152       fit = funcSet_->begin();
00153       funcSet_->erase(fit);
00154       delete *fit;
00155     }
00156     delete funcSet_;
00157   }
00158 
00159   //if (externalConstant_) delete externalConstant_;
00160   
00161   if (numNonEvidAtomsPerPred_) delete numNonEvidAtomsPerPred_;
00162   if (numTrueNonEvidGndingsPerClause_) delete numTrueNonEvidGndingsPerClause_;
00163   if (numFalseNonEvidGndingsPerClause_) delete numFalseNonEvidGndingsPerClause_;
00164 }
00165 
00166 
00167 void Domain::deleteDB() { if (db_) delete db_; db_ = NULL; }
00168 
00169 
00170 void Domain::newTrueFalseGroundingsStore() 
00171 { trueFalseGroundingsStore_ = new TrueFalseGroundingsStore(this); }
00172 
00173   
00174 void Domain::compress() 
00175 {
00176   typeDualMap_->compress();
00177   constDualMap_->compress();
00178   predDualMap_->compress();
00179   funcDualMap_->compress();
00180   constantsByType_->compress();
00181   for (int i = 0; i < constantsByType_->size(); i++)
00182     (*constantsByType_)[i]->compress();
00183   externalConstantsByType_->compress();
00184   for (int i = 0; i < externalConstantsByType_->size(); i++)
00185     (*externalConstantsByType_)[i]->compress();
00186   db_->compress();
00187 }
00188 
00194 void Domain::updatePerOldToNewIds(MLN* const & mln,
00195                                   hash_map<int,int> & oldToNewConstIds)
00196 {
00197     //ensure that the constants in MLN clauses have the new ids 
00198   const ClauseHashArray* clauses = mln->getClauses();
00199   for (int i = 0; i < clauses->size(); i++)
00200   {
00201     Clause* c = (*clauses)[i];
00202     for (int i = 0; i < c->getNumPredicates(); i++)
00203       changePredTermsToNewIds(c->getPredicate(i), oldToNewConstIds);
00204   }
00205 
00206     // Change the const ids in the pred blocks
00207   for (int i = 0; i < predBlocks_->size(); i++)
00208   {
00209     changePredTermsToNewIds((*predBlocks_)[i], oldToNewConstIds);
00210   }
00211   for (int i = 0; i < truePredsInBlock_->size(); i++)
00212   {
00213     changePredTermsToNewIds((*truePredsInBlock_)[i], oldToNewConstIds);
00214   }
00215 
00216     // Change the const ids in the database
00217   if (db_)
00218   {
00219     db_->changeConstantsToNewIds(oldToNewConstIds);
00220   }
00221 }
00222 
00223 // for parser
00224 void Domain::reorderConstants(MLN* const & mln,
00225                            hash_map<int, PredicateHashArray*>& predIdToPredsMap)
00226 {
00227   hash_map<int,int> oldToNewConstIds;
00228 
00229   ConstDualMap* newConstDualMap = new ConstDualMap;
00230   Array<Array<int>*>* newConstantsByType = new Array<Array<int>*>;
00231   Array<Array<int>*>* newExternalConstantsByType = new Array<Array<int>*>;
00232 
00233   int prevNewConstId = -1;
00234   bool constChanged = false;
00235   for (int i = 0; i < constantsByType_->size(); i++)
00236   {
00237     newConstantsByType->append(new Array<int>);
00238     Array<int>* constIds = (*constantsByType_)[i];
00239     for (int j = 0; j < constIds->size(); j++)
00240     {
00241       int constId = (*constIds)[j];
00242       const char* constName = getConstantName(constId);
00243       int newConstId = newConstDualMap->insert(constName, i);
00244       assert(prevNewConstId + 1 == newConstId);
00245       prevNewConstId = newConstId;
00246       (*newConstantsByType)[i]->append(newConstId);
00247       oldToNewConstIds[constId] = newConstId;
00248       if (constId != newConstId) constChanged = true;
00249     }
00250   }
00251   for (int i = 0; i < externalConstantsByType_->size(); i++)
00252   {
00253     newExternalConstantsByType->append(new Array<int>);
00254     Array<int>* constIds = (*externalConstantsByType_)[i];
00255     for (int j = 0; j < constIds->size(); j++)
00256     {
00257       int constId = (*constIds)[j];
00258       const char* constName = getConstantName(constId);
00259       int newConstId = newConstDualMap->insert(constName, i);
00260       assert(prevNewConstId + 1 == newConstId);
00261       prevNewConstId = newConstId;
00262       (*newExternalConstantsByType)[i]->append(newConstId);
00263       oldToNewConstIds[constId] = newConstId;
00264       if (constId != newConstId) constChanged = true;
00265     }
00266   }
00267 
00268   if (!constChanged)
00269   {
00270     delete newConstDualMap;
00271     for (int i = 0; i < newConstantsByType->size(); i++)
00272       delete (*newConstantsByType)[i];
00273     delete newConstantsByType;
00274     for (int i = 0; i < newExternalConstantsByType->size(); i++)
00275       delete (*newExternalConstantsByType)[i];
00276     delete newExternalConstantsByType;
00277     return;
00278   }
00279 
00280   delete constDualMap_;
00281   for (int i = 0; i < constantsByType_->size(); i++)
00282     delete (*constantsByType_)[i];
00283   delete constantsByType_;
00284   for (int i = 0; i < externalConstantsByType_->size(); i++)
00285     delete (*externalConstantsByType_)[i];
00286   delete externalConstantsByType_;
00287 
00288   constDualMap_ = newConstDualMap;
00289   constantsByType_ = newConstantsByType;
00290   externalConstantsByType_ = newExternalConstantsByType;
00291 
00292   constantsByType_->compress();
00293   for (int i = 0; i < constantsByType_->size(); i++)
00294     (*constantsByType_)[i]->compress();
00295   externalConstantsByType_->compress();
00296   for (int i = 0; i < externalConstantsByType_->size(); i++)
00297     (*externalConstantsByType_)[i]->compress();
00298   constDualMap_->compress();
00299 
00300     // update
00301   updatePerOldToNewIds(mln, oldToNewConstIds);
00302 
00303         // predIdToPredMap
00304   hash_map<int, PredicateHashArray*>::iterator mit = predIdToPredsMap.begin();
00305   for (; mit != predIdToPredsMap.end(); mit++)
00306   {
00307     PredicateHashArray* pha = (*mit).second;
00308     for (int i = 0; i < pha->size(); i++ )
00309       changePredTermsToNewIds((*pha)[i], oldToNewConstIds);
00310   }
00311 
00312     //clauses and hence their hash values have changed, and they need to be 
00313     //inserted into the MLN
00314   mln->rehashClauses();
00315 }
00316 
00317   // for domain0 - postprocess
00318 void Domain::reorderConstants(MLN* const & mln)
00319 {
00320   hash_map<int,int> oldToNewConstIds;
00321 
00322   ConstDualMap* newConstDualMap = new ConstDualMap;
00323   Array<Array<int>*>* newConstantsByType = new Array<Array<int>*>;
00324   Array<Array<int>*>* newExternalConstantsByType = new Array<Array<int>*>;
00325 
00326   int prevNewConstId = -1;
00327   bool constChanged = false;
00328   assert(constantsByType_->size() == externalConstantsByType_->size());
00329   for (int i = 0; i < constantsByType_->size(); i++)
00330   {
00331     newConstantsByType->append(new Array<int>);
00332     Array<int>* constIds = (*constantsByType_)[i];
00333     for (int j = 0; j < constIds->size(); j++)
00334     {
00335       int constId = (*constIds)[j];
00336       const char* constName = getConstantName(constId);
00337       int newConstId = newConstDualMap->insert(constName, i);
00338       assert(prevNewConstId + 1 == newConstId);
00339       prevNewConstId = newConstId;
00340       (*newConstantsByType)[i]->append(newConstId);
00341       oldToNewConstIds[constId] = newConstId;
00342       if (constId != newConstId) constChanged = true;
00343     }
00344     newExternalConstantsByType->append(new Array<int>);
00345     Array<int>* extConstIds = (*externalConstantsByType_)[i];
00346     for (int j = 0; j < extConstIds->size(); j++)
00347     {
00348       int constId = (*extConstIds)[j];
00349       const char* constName = getConstantName(constId);
00350       int newConstId = newConstDualMap->insert(constName, i);
00351       assert(prevNewConstId + 1 == newConstId);
00352       prevNewConstId = newConstId;
00353       (*newExternalConstantsByType)[i]->append(newConstId);
00354       oldToNewConstIds[constId] = newConstId;
00355       if (constId != newConstId) constChanged = true;
00356     }
00357   }
00358 
00359   if (!constChanged)
00360   {
00361     delete newConstDualMap;
00362     for (int i = 0; i < newConstantsByType->size(); i++)
00363       delete (*newConstantsByType)[i];
00364     delete newConstantsByType;
00365     for (int i = 0; i < newExternalConstantsByType->size(); i++)
00366       delete (*newExternalConstantsByType)[i];
00367     delete newExternalConstantsByType;
00368     return;
00369   }
00370 
00371   delete constDualMap_;
00372   for (int i = 0; i < constantsByType_->size(); i++)
00373     delete (*constantsByType_)[i];
00374   delete constantsByType_;
00375   for (int i = 0; i < externalConstantsByType_->size(); i++)
00376     delete (*externalConstantsByType_)[i];
00377   delete externalConstantsByType_;
00378 
00379   constDualMap_ = newConstDualMap;
00380   constantsByType_ = newConstantsByType;
00381   externalConstantsByType_ = newExternalConstantsByType;
00382 
00383   constantsByType_->compress();
00384   for (int i = 0; i < constantsByType_->size(); i++)
00385     (*constantsByType_)[i]->compress();
00386   externalConstantsByType_->compress();
00387   for (int i = 0; i < externalConstantsByType_->size(); i++)
00388     (*externalConstantsByType_)[i]->compress();
00389   constDualMap_->compress();
00390 
00391     // update
00392   updatePerOldToNewIds(mln, oldToNewConstIds);
00393 
00394     //clauses and hence their hash values have changed, and they need to be 
00395     //inserted into the MLN
00396   mln->rehashClauses();
00397 }
00398 
00399   // for domain1-N - postprocess
00400   // Assumption is: map is a superset of this domain's ConstDualMap
00401 void Domain::reorderConstants(ConstDualMap* const & map,
00402                               Array<Array<int>*>* const & cbt,
00403                               Array<Array<int>*>* const & ecbt,
00404                               MLN* const & mln)
00405 {
00406     // Generate oldtoNew
00407   hash_map<int,int> oldToNewConstIds;
00408 
00409     // Mark constants not currently in domain as external
00410   for (int i = 0; i < externalConstantsByType_->size(); i++)
00411     delete (*externalConstantsByType_)[i];
00412   delete externalConstantsByType_;
00413   externalConstantsByType_ = new Array<Array<int>*>;
00414   for (int i = 0; i < cbt->size(); i++)
00415     externalConstantsByType_->append(new Array<int>);
00416 
00417   int numConstants = map->getNumInt();
00418   for (int i = 0; i < numConstants; i++)
00419   {
00420     const char* name = map->getStr(i);
00421     if (isConstant(name)) 
00422     {
00423         // Constant is present: note the new id to replace later
00424       oldToNewConstIds[constDualMap_->getInt(name)] = i;
00425     }
00426     else
00427     {
00428 /*
00429       bool continueOuter = false;
00430         // Add as external constant
00431         // Constant is in reference domain
00432       for (int j = 0; j < cbt->size(); j++)
00433       {
00434         bool breakOuter = false;
00435         for (int k = 0; k < (*cbt)[j]->size(); k++)
00436         {
00437           if (map->getInt(name) == (*(*cbt)[j])[k])
00438           {
00439               // Add as external constant
00440             (*externalConstantsByType_)[j]->append((*(*cbt)[j])[k]);
00441             breakOuter = true;
00442             continueOuter = true;
00443             break;
00444           }
00445         }
00446         if (breakOuter) break;
00447       }
00448       if (continueOuter) continue;
00449         // Constant is external in reference domain (comes from another domain)
00450       for (int j = 0; j < ecbt->size(); j++)
00451       {
00452         bool breakOuter = false;
00453         for (int k = 0; k < (*ecbt)[j]->size(); k++)
00454         {
00455           if (map->getInt(name) == (*(*ecbt)[j])[k])
00456               // Add as external constant
00457           {
00458             (*externalConstantsByType_)[j]->append((*(*ecbt)[j])[k]);
00459             breakOuter = true;
00460             break;
00461           }
00462         }
00463         if (breakOuter) break;
00464       }
00465 */
00466       int mi = map->getInt(name);
00467       Array<int>* ints = map->getInt2(mi);
00468       for (int i = 0; i < ints->size(); i++)
00469         (*externalConstantsByType_)[(*ints)[i]]->append(mi);
00470     }
00471   }
00472   
00473     // Replace old ids with new ids in constantsByType_
00474   for (int i = 0; i < constantsByType_->size(); i++)
00475   {
00476     for (int j = 0; j < (*constantsByType_)[i]->size(); j++)
00477     {
00478       int oldId = (*(*constantsByType_)[i])[j];
00479       assert(oldToNewConstIds.find(oldId) != oldToNewConstIds.end());
00480       (*(*constantsByType_)[i])[j] = oldToNewConstIds[oldId];
00481     }
00482     (*constantsByType_)[i]->quicksort();
00483   }
00484 
00485     // swap map/type
00486   delete constDualMap_;  
00487   constDualMap_ = map;
00488 
00489     // update
00490   updatePerOldToNewIds(mln, oldToNewConstIds);
00491 
00492     //clauses and hence their hash values have changed, and they need to be 
00493     //inserted into the MLN
00494   mln->rehashClauses();
00495 }
00496 
00497 
00498 void Domain::changePredTermsToNewIds(Predicate* const & p,
00499                                      hash_map<int,int>& oldToNewConstIds)
00500 {
00501     // p could be NULL
00502   if (p)
00503   {
00504     for (int j = 0; j < p->getNumTerms(); j++)
00505     {
00506       Term* t = (Term*) p->getTerm(j);
00507       if (t->getType() == Term::CONSTANT)
00508       {
00509         int oldId = t->getId();
00510         assert(oldToNewConstIds.find(oldId) != oldToNewConstIds.end());
00511         t->setId(oldToNewConstIds[oldId]);
00512       } 
00513     }
00514   }
00515 } 
00516 
00517 
00518   //Caller is responsible for deleting returned pointer
00519 Predicate* Domain::createPredicate(const int& predId, 
00520                                    const bool& includeEqualPreds) const
00521 {
00522   const PredicateTemplate* pt = getPredicateTemplate(predId);
00523   if (!includeEqualPreds && pt->isEqualPredicateTemplate()) return NULL;
00524   Predicate* pred = new Predicate(pt);
00525   pred->setSense(true);
00526   for (int j = 0; j < pt->getNumTerms(); j++)
00527     pred->appendTerm(new Term(-(j+1), (void*)pred, true));
00528   return pred;
00529 }
00530 
00531 
00532   //Caller is responsible for deleting Array and its contents
00533 void Domain::createPredicates(Array<Predicate*>* const & preds,
00534                               const bool& includeEqualPreds) const
00535 {
00536   for (int i = 0; i < getNumPredicates(); i++)
00537   {
00538     Predicate* p = createPredicate(i, includeEqualPreds);
00539     if (p) preds->append(p);
00540   }
00541 }
00542 
00543 
00544   //Caller is responsible for deleting Array and its contents
00545 void Domain::createPredicates(Array<Predicate*>* const & preds,
00546                               const Array<string>* const & predNames)
00547 {
00548   for (int i = 0; i < predNames->size(); i++)
00549   {
00550     int predId = getPredicateId((*predNames)[i].c_str());    
00551     Predicate* p = createPredicate(predId, true);
00552     if (p) preds->append(p);
00553   }
00554 }
00555 
00556 int Domain::getNumNonEvidenceAtoms() const
00557 {
00558   int numAtoms = 0;
00559   for (int i = 0; i < getNumPredicates(); i++)
00560   {
00561     numAtoms += (*numNonEvidAtomsPerPred_)[i];
00562   }
00563   return numAtoms;
00564 }
00565 
00566 /*
00567  * Caller is responsible for deleting returned Predicate* if necessary
00568  */
00569 Predicate* Domain::getNonEvidenceAtom(const int& index) const
00570 {
00571   int predId = -1;
00572   int numAtomsPerPred;
00573   int numAtoms = 0;
00574   for (int i = 0; i < getNumPredicates(); i++)
00575   {
00576     numAtomsPerPred = (*numNonEvidAtomsPerPred_)[i];
00577     if (numAtoms + numAtomsPerPred >= index + 1)
00578     {
00579       predId = i;
00580       break;
00581     }
00582     numAtoms += numAtomsPerPred;
00583   }
00584   assert(predId >= 0);
00585 
00586     // Get the newIndex-th grounding of f.o. pred with id predId   
00587   Predicate* pred = createPredicate(predId, false);
00588     // Not all groundings of pred are non-evidence, so we need the while loop
00589   bool foundNE = false;
00590   while(!foundNE)
00591   {
00592     for (int i = 0; i < pred->getNumTerms(); i++)
00593     {
00594       int termType = pred->getTermTypeAsInt(i);
00595       const Array<int>* constantsByType = getConstantsByType(termType);
00596       int constIdx = random() % constantsByType->size();
00597       pred->setTermToConstant(i, (*constantsByType)[constIdx]);
00598     }
00599     assert(pred->isGrounded());
00600     if (!db_->getEvidenceStatus(pred)) foundNE = true;
00601   }
00602   return pred;
00603 }
00604 
00605 int Domain::addPredBlock(Predicate* const & predBlock) const
00606 {
00607   int idx = predBlocks_->append(predBlock);
00608     // Record number of groundings
00609   Array<Predicate*> predArr;
00610   predBlock->createAllGroundings(this, predArr);
00611   blockSizes_->append(predArr.size());
00612   predArr.deleteItemsAndClear();
00613 
00614     // If nothing known, then no pred is set to true and no evidence in block
00615   Predicate* gndPred = NULL;
00616   truePredsInBlock_->append(gndPred);
00617   blockEvidence_->append(false);
00618   return idx;
00619 }
00620 
00628 int Domain::getBlock(const Predicate* const & pred) const
00629 {
00630   assert(((Predicate*)pred)->isGrounded());
00631   for (int i = 0; i < predBlocks_->size(); i++)
00632   {
00633     if ((*predBlocks_)[i]->canBeGroundedAs((Predicate*)pred))
00634       return i;
00635   }
00636   return -1;
00637 }
00638 
00646 int Domain::getBlock(const GroundPredicate* const & pred) const
00647 {
00648   for (int i = 0; i < predBlocks_->size(); i++)
00649   {
00650     if ((*predBlocks_)[i]->canBeGroundedAs(pred))
00651       return i;
00652   }
00653   return -1;
00654 }
00655 
00656 int Domain::getNumPredBlocks() const
00657 {
00658   return predBlocks_->size();
00659 }
00660 
00661 const Array<Predicate*>* Domain::getPredBlocks() const
00662 {
00663   return predBlocks_;
00664 }
00665 
00666 const Predicate* Domain::getPredBlock(const int& index) const
00667 {
00668   return (*predBlocks_)[index];
00669 }
00670 
00671 const Array<bool>* Domain::getBlockEvidenceArray() const
00672 {
00673   return blockEvidence_;
00674 }
00675 
00676 const bool Domain::getBlockEvidence(const int& index) const
00677 {
00678   return (*blockEvidence_)[index];
00679 }
00680 
00681 void Domain::setBlockEvidence(const int& index, const bool& value) const
00682 {
00683   (*blockEvidence_)[index] = value;
00684 }
00685 
00689 void Domain::computeNumNonEvidAtoms()
00690 {
00691   if (numNonEvidAtomsPerPred_) delete numNonEvidAtomsPerPred_;
00692   numNonEvidAtomsPerPred_ = new Array<int>(getNumPredicates());
00693   numNonEvidAtomsPerPred_->growToSize(getNumPredicates());
00694   for (int i = 0; i < getNumPredicates(); i++)
00695   {
00696     (*numNonEvidAtomsPerPred_)[i] =
00697       (db_->getNumGroundings(i) - db_->getNumEvidenceGndPreds(i));
00698   }
00699 }
00700 

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