BeliefCache.cpp
Go to the documentation of this file.
00001 #include "BeliefCache.h"
00002 #include <sstream>
00003 #include <fstream>
00004 #include <string>
00005 #include <exception>
00006 #include <stdexcept>
00007 
00008 using namespace std;
00009 
00010 BeliefCache::BeliefCache(void)
00011 {
00012         lookupTable = new UniqueBeliefHeap();
00013         currentRowCount = 0;
00014 
00015 }
00016 
00017 BeliefCache::~BeliefCache(void)
00018 {
00019 }
00020 
00021 
00022 void BeliefCache::localDelete(SharedPointer<Belief> pointer)
00023 {
00024         throw runtime_error("not implemented") ;
00025 }
00026 
00027 BeliefCacheRow* BeliefCache::getRow(int row) 
00028 {
00029         validateRowNumber(row);
00030         return belCache[row];
00031 }
00032 
00033 void BeliefCache::validateRowNumber(int row) 
00034 {
00035         if(row > currentRowCount || row < 0)
00036         {
00037                 std::ostringstream mystrm;
00038                 mystrm << "Trying to access row = "<<row<<", the total number of rows = "<<
00039                         currentRowCount;
00040                 throw runtime_error(mystrm.str().c_str());
00041         }
00042 }
00043 
00044 
00045 int BeliefCache::addBeliefRow( SharedPointer<belief_vector>& bel)
00046 {
00047         //cout << "BeliefCache::addBeliefRow";
00048         //bel.write(cout) << endl;
00049                 
00050         if(!lookupTable->isStorageMaterial(bel))
00051         {
00052                 std::ostringstream mystrm;
00053                 mystrm << "Trying to add illegal belief <>. Current " <<"belief count = "<<currentRowCount;
00054                 throw runtime_error(mystrm.str().c_str());
00055         }
00056         //    std::string hashedVal = bel.hashCode();
00057         lookupTable->addBeliefRowPair(bel, currentRowCount++);
00058         BeliefCacheRow* row = new BeliefCacheRow(); // = BeliefCacheRow.createRow();
00059         row->BELIEF = bel;
00060         belCache.push_back(row);
00061 
00062 
00063         //cout << " as beliefID " << currentRowCount-1 << endl;
00064 
00065         return currentRowCount-1;
00066 }
00067 
00068 int BeliefCache::addBeliefRowWithoutCheck( SharedPointer<belief_vector>& bel)
00069 {
00070         //cout << "BeliefCache::addBeliefRow";
00071         //bel.write(cout) << endl;
00072         lookupTable->addBeliefRowPair(bel, currentRowCount++);
00073         BeliefCacheRow* row = new BeliefCacheRow(); // = BeliefCacheRow.createRow();
00074         row->BELIEF = bel;
00075         belCache.push_back(row);
00076         //cout << " as beliefID " << currentRowCount-1 << endl;
00077 
00078         return currentRowCount-1;
00079 }
00080 
00081 
00082 bool BeliefCache::hasBelief( SharedPointer<belief_vector>& bel)
00083 {
00084         return  lookupTable->getBeliefRowIndex(bel) != -1;
00085 }
00086 
00087 int BeliefCache::getBeliefRowIndex( SharedPointer<belief_vector>& bel)
00088 {
00089         return lookupTable->getBeliefRowIndex(bel);
00090 }
00091 ofstream& BeliefCache::writeBeliefVector(std::string fileName)
00092 {
00093         static ofstream *dumpFile = NULL;
00094         if(dumpFile == NULL)
00095         {
00096                 fileName = fileName.erase( fileName.rfind( ".", fileName.length() ) ).append (".bel");
00097                 dumpFile = new ofstream();
00098                 dumpFile->open(fileName.c_str(),  ios::out | ios::trunc);
00099         }
00100 
00101         return *dumpFile;
00102 }
00103 
00104 ofstream& BeliefCache::writeBeliefFunction(std::string fileName)
00105 {
00106         static ofstream *dumpFile = NULL;
00107         if(dumpFile == NULL)
00108         {
00109                 fileName = fileName.erase( fileName.rfind( ".", fileName.length() ) ).append (".bf");
00110                 dumpFile = new ofstream();
00111                 dumpFile->open(fileName.c_str(),  ios::out | ios::trunc);
00112         }
00113 
00114         return *dumpFile;
00115 }
00116 


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:28