SignatureDB.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 
00019 /************************************************************************
00020                         SignatureDB.cpp - Copyright klank
00021 
00022 
00023 **************************************************************************/
00024 
00025 #include "SignatureDB.h"
00026 
00027 #include <time.h>
00028 
00029 #include <boost/bind.hpp>
00030 #include <boost/algorithm/string.hpp>
00031 
00032 
00033 #define XML_NODE_SIGDB_INTERN_DB        "SigDBRoot"
00034 #define XML_NODE_SIGDB_INTERN_INDEX     "SigDBIndex"
00035 #define XML_NODE_SIGDB_INTERN_IDLIST "SigDBIDList"
00036 
00037 
00038 #define XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID    "SigDBIndexClass2ID"
00039 #define XML_NODE_SIGDB_INTERN_INDEX_ID2CLASS    "SigDBIndexID2Class"
00040 
00041 #define XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID_MEM        "idMem"
00042 #define XML_NODE_SIGDB_INTERN_INDEX_ID2CLASS_MEM        "classMem"
00043 
00044 #define XML_ATTRIBUTE_SIGDB_CLASS "Class"
00045 #define XML_ATTRIBUTE_SIGDB_ID "ID"
00046 
00047 
00048 
00049 using namespace cop;
00050 
00051 
00052 class Class2ID : public XMLTag
00053 {
00054 public:
00055         Class2ID(int idClass, int id) :
00056                 XMLTag(XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID_MEM)
00057         {
00058                 this->AddProperty(XML_ATTRIBUTE_SIGDB_CLASS, idClass);
00059                 this->AddProperty(XML_ATTRIBUTE_SIGDB_ID, id);
00060         }
00061 };
00062 
00063 // Constructors/Destructors
00064 //
00065 
00066 SignatureDB::SignatureDB ( XMLTag* config )
00067 {
00068         if(config != NULL)
00069         {
00070                 try
00071                 {
00072                         try
00073                         {
00074                                 XMLTag* tagSBRoot = config->GetChild(XML_NODE_SIGDB_INTERN_DB);
00075                                 if(tagSBRoot != NULL)
00076                                         m_dbStarter = tagSBRoot->Clone();
00077                                 else
00078                                         m_dbStarter = new XMLTag(XML_NODE_SIGDB_INTERN_DB);
00079                         }
00080                         catch(...)
00081                         {
00082         printf("Creating Signature DB: Error in node %s\n", XML_NODE_SIGDB_INTERN_DB);
00083                                 throw XML_NODE_SIGDB_INTERN_DB;
00084                         }
00085                         try
00086                         {
00087                                 XMLTag* tagSBIndex= config->GetChild(XML_NODE_SIGDB_INTERN_INDEX);
00088                                 if(tagSBIndex != NULL)
00089                                 {
00090                                         m_index = tagSBIndex->Clone();
00091                                         if(m_index->GetChild(XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID) == NULL)
00092                                                 m_index->AddChild(new XMLTag(XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID));
00093                                 }
00094                                 else
00095                                 {
00096                                         m_index = new XMLTag(XML_NODE_SIGDB_INTERN_INDEX);
00097                                         m_index->AddChild(new XMLTag(XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID));
00098                                 }
00099                                 XMLTag * idx = m_index->GetChild(XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID);
00100                                 int size = idx->CountChildren();
00101                                 for(int t = 0; t < size;  t++)
00102                                 {
00103                                   ObjectID_t idClass = idx->GetChild(t)->GetPropertyInt(XML_ATTRIBUTE_SIGDB_CLASS, 0);
00104                                   ObjectID_t idObject =  idx->GetChild(t)->GetPropertyInt(XML_ATTRIBUTE_SIGDB_ID, 0);
00105                                   SetClassSignature(idClass, idObject);
00106                                 }
00107                         }
00108                         catch(...)
00109                         {
00110         printf("Creating Signature DB: Error in node %s\n", XML_NODE_SIGDB_INTERN_INDEX);
00111                                 throw XML_NODE_SIGDB_INTERN_INDEX;
00112                         }
00113                         try
00114                         {
00115                                 XMLTag* tagSBIdList= config->GetChild(XML_NODE_SIGDB_INTERN_IDLIST);
00116                                 if(tagSBIdList != NULL)
00117                                 {
00118                                         m_ids = XMLTag::Load(tagSBIdList, &m_ids);
00119                                         if(m_ids.size() != m_dbStarter->CountChildren())
00120                                                 UpdateIDList();
00121                                 }
00122                                 else
00123                                         UpdateIDList();
00124                         }
00125                         catch(char const* pListError)
00126                         {
00127                                 printf("Error reading Singature DB: %s\n", pListError);
00128                                 throw XML_NODE_SIGDB_INTERN_IDLIST;
00129                         }
00130             catch(...)
00131             {
00132                                 printf("Error reading Singature DB.\n");
00133                                 throw XML_NODE_SIGDB_INTERN_IDLIST;
00134             }
00135                 }
00136                 catch(char const* exception)
00137                 {
00138                         printf("Loading of config file failed: %s\n", exception);
00139                         m_dbStarter = new XMLTag(XML_NODE_SIGDB_INTERN_DB);
00140                         m_index = new XMLTag(XML_NODE_SIGDB_INTERN_INDEX);
00141                         m_index->AddChild(new XMLTag(XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID));
00142                 }
00143         }
00144         else
00145         {
00146                 m_dbStarter = new XMLTag(XML_NODE_SIGDB_INTERN_DB);
00147                 m_index = new XMLTag(XML_NODE_SIGDB_INTERN_INDEX);
00148                 m_index->AddChild(new XMLTag(XML_NODE_SIGDB_INTERN_INDEX_CLASS2ID));
00149         }
00150 
00151 }
00152 
00153 SignatureDB::~SignatureDB ( )
00154 {
00155   delete m_dbStarter;
00156   delete m_index;
00157   std::map<PerceptionPrimitiveID_t, PerceptionPrimitive*>::iterator iter = m_ppMap.begin();
00158   for(; iter != m_ppMap.end(); )
00159   {
00160     delete (*iter).second;
00161     m_ppMap.erase(iter);
00162     iter = m_ppMap.begin();
00163   }
00164 }
00165 
00166 //
00167 // Methods
00168 //
00169 #ifdef BOOST_THREAD
00170 
00171 bool read_env = true;
00172 bool showing_on = false;
00173 void SignatureDB::AddAndShowSignatureAsync(Signature* sig, Sensor* sens)
00174 {
00175   boost::thread(boost::bind(&SignatureDB::AddSignature, this, sig));
00176 #ifdef WIN32
00177 #else
00178   if(read_env)
00179   {
00180     char* value =  getenv ("DISPLAY");
00181     printf("Read Env Display: %s\n", value);
00182     if(value && strlen(value) > 0)
00183       showing_on = true;
00184   }
00185   if(showing_on)
00186     boost::thread(boost::bind(&Signature::Show, sig, sens));
00187 #endif
00188 }
00189 
00190 
00191 void SignatureDB::AddAndShowSignaturesAsync(std::vector<Results_t> &all_matches, const int &num_results_accepted)
00192 {
00193   std::vector<Results_t> vec;
00194   vec.resize(num_results_accepted);
00195   std::copy(all_matches.begin(), all_matches.begin() + num_results_accepted, vec.begin());
00196   boost::thread(boost::bind(&SignatureDB::AddSignatures, this, vec));
00197 }
00198 
00199 #endif
00200 
00201 int SignatureDB::AddSignature(Signature* sig)
00202 {
00203   int error = -1;
00204 #ifndef WIN32
00205   sleep(0.01);
00206 #endif
00207   if(sig != NULL)
00208   {
00209     if(!Check(sig->m_ID, error))
00210     {
00211         error = m_dbStarter->AddChild(sig->Save());
00212         m_ids.push_back(sig->m_ID);
00213         for(size_t objsubsc = 0; objsubsc < m_newObjectSubscriber.size(); objsubsc++)
00214         {
00215           m_newObjectSubscriber[objsubsc]->NotifyNewObject(sig, sig->GetObjectPose());
00216         }
00217 
00218         ObjectID_t idObject = sig->m_ID;
00219         for(unsigned int classes = 0; classes < sig->CountClasses(); classes++)
00220         {
00221           Class* cl = sig->GetClass(classes);
00222           ObjectID_t idClass = cl->m_ID;
00223           cl->m_ID = AddClass(cl->GetName(), idClass);
00224           SetClassSignature(cl->m_ID, idObject);
00225         }
00226     }
00227     else
00228     {
00229        UpdateNodes(sig, error);
00230 
00231        for(size_t objsubsc = 0; objsubsc < m_newObjectSubscriber.size(); objsubsc++)
00232        {
00233          m_newObjectSubscriber[objsubsc]->NotifyNewObject(sig, sig->GetObjectPose());
00234        }
00235     }
00236     if(error != -1)
00237     {
00238       CleanUpActiveSignatureList();
00239       AddSignatureToActiveList(sig, error);
00240     }
00241   }
00242   return error;
00243 }
00244 
00245 
00246 
00247 void SignatureDB::AddSignatures(std::vector<Results_t> matches)
00248 {
00249   int error = -1;
00250   std::vector<Results_t>::iterator it = matches.begin();
00251   for(;it != matches.end(); it++)
00252   {
00253      error = AddSignature((*it).signature);
00254   }
00255 
00256 
00257 #ifdef WIN32
00258 #else
00259   if(read_env)
00260   {
00261     char* value =  getenv ("DISPLAY");
00262     printf("Read Env Display: %s\n", value);
00263     if(value && strlen(value) > 0)
00264       showing_on = true;
00265   }
00266 #endif
00267   if(showing_on)
00268   {
00269 
00270     std::vector<Results_t>::iterator it = matches.begin();
00271     for(;it != matches.end(); it++)
00272     {
00273       (*it).signature->Show((*it).camera);
00274     }
00275   }
00276 }
00277 
00278 void SignatureDB::AddSignatureToActiveList(Signature* sig, int index)
00279 {
00280   m_currentlyActiveSignatures.push_back(std::pair<Signature*, int>(sig, 1));
00281   m_activeMap[index] = m_currentlyActiveSignatures.size() - 1;
00282 }
00283 
00284 
00285 void  SignatureDB::SetClassSignature(ObjectID_t idClass, ObjectID_t idObject)
00286 {
00287   m_classToSignature[idClass].push_back(idObject);
00288 }
00289 
00290 
00291 void SignatureDB::UpdateNodes(Signature* sig, int index)
00292 {
00293   printf("SignatureDB::UpdateNodes\n");
00294   ObjectID_t idObject = sig->m_ID;
00295   for(unsigned int classes = 0; classes < sig->CountClasses(); classes++)
00296   {
00297     Class* cl = sig->GetClass(classes);
00298     cl->m_ID = AddClass(cl->GetName(), cl->m_ID);
00299     SetClassSignature(cl->m_ID, idObject);
00300   }
00301 
00302   m_dbStarter->ReplaceChild(sig->Save(), index);
00303 }
00304 
00305 Signature* SignatureDB::GetSignatureByIndex(unsigned int index)
00306 {
00307   if(index < m_dbStarter->CountChildren())
00308   {
00309     if(m_activeMap.find(index) != m_activeMap.end())
00310     {
00311       int index_active = m_activeMap[index];
00312       m_currentlyActiveSignatures[index_active].second++;
00313       return m_currentlyActiveSignatures[index_active].first;
00314     }
00315     else
00316     {
00317      try
00318      {
00319        Signature* sig = (Signature*)Elem::ElemFactory(m_dbStarter->GetChild(index));
00320        AddSignatureToActiveList(sig, index);
00321        return sig;
00322      }
00323      catch(char const* error_text)
00324      {
00325          printf("Error loading signature: %s\n", error_text);
00326          return NULL;
00327      }
00328     }
00329   }
00330   else
00331     return NULL;
00332 }
00333 
00334 #define TIME_OUT_ACTIVE_SIG 30000
00335 
00336 void SignatureDB::CleanUpActiveSignatureList()
00337 {
00338   std::vector<std::pair<Signature*, int> >::iterator it = m_currentlyActiveSignatures.begin();
00339   unsigned long timeStamp = (unsigned long)time(NULL);
00340   int index = 0;
00341   for(;it != m_currentlyActiveSignatures.end(); it++)
00342   {
00343     if((*it).second == 0 && timeStamp - (*it).first->date() > TIME_OUT_ACTIVE_SIG)
00344     {
00345       delete (*it).first;
00346       it = m_currentlyActiveSignatures.erase(it);
00347       m_activeMap.erase(m_activeMap[index]);
00348     }
00349     else
00350       index++;
00351   }
00352 }
00353 
00354 
00355 Signature* SignatureDB::GetSignatureByID(ObjectID_t ElemID)
00356 {
00357   int index;
00358   Check(ElemID, index);
00359   return GetSignatureByIndex(index);
00360 }
00361 
00362 
00363 int SignatureDB::AddClass(std::string stname, int id)
00364 {
00365   if(CheckClass(id).length() == 0)
00366   {
00367     int id_same_name = CheckClass(stname);
00368     if(id_same_name == -1)
00369     {
00370       printf("Add Class %s -> %d\n", stname.c_str(), id);
00371       m_classes.push_back(std::pair<std::string, int>(stname, id));
00372       return id;
00373     }
00374     else
00375     {
00376       printf("Class %s  already defined %d\n", stname.c_str(), id_same_name);
00377       return id_same_name;
00378     }
00379   }
00380   else
00381   {
00382      printf("Already defined class id %d: %s\n", id, CheckClass(id).c_str());
00383      if(stname.compare(CheckClass(id)) != 0)
00384      {
00385        m_classes.push_back(std::pair<std::string, int>(stname, Elem::m_LastID++));
00386        return  Elem::m_LastID - 1;
00387      }
00388      return id;
00389   }
00390 }
00391 
00392 void SignatureDB::CompleteSignature(Signature* sig_max, std::vector<ObjectID_t> class_ids)
00393 {
00394   size_t size = class_ids.size();
00395   if(sig_max != NULL)
00396   {
00397     for(unsigned int k = 0 ; k < size; k++)
00398     {
00399       bool bIn = false;
00400       for(unsigned int j = 0; j < sig_max->CountClasses(); j++)
00401       {
00402         if(sig_max->GetClass(j) != NULL && class_ids[k] == sig_max->GetClass(j)->m_ID)
00403         {
00404           bIn = true;
00405           break;
00406         }
00407       }
00408       if(!bIn)
00409       {
00410         try
00411         {
00413           Elem* elem = FindCreateDescriptor(class_ids[k]);
00414           if(elem != NULL)
00415             sig_max->SetElem(elem);
00416           else
00417           {
00418             printf("No descriptor could be found or created for class %ld (=%s)\n", class_ids[k], CheckClass(class_ids[k]).c_str());
00419             sig_max->SetClass(GetClassByID(class_ids[k]));
00420           }
00421         }
00422         catch(char const* text)
00423         {
00424           printf("Problem while building signature: %s\n", text);
00425         }
00426       }
00427     }
00428   }
00429 }
00430 
00431 Signature* SignatureDB::GetSignature(std::vector<ObjectID_t> class_ids)
00432 {
00433   size_t size = class_ids.size();
00434   int score_max = 0, score_temp = 100;
00435   Signature* sig_max = NULL;
00436   int index = -1;
00437   printf("Entering Get Signature\n");
00438   for(unsigned int i = 0 ; i < size; i++)
00439   {
00440     Signature* sig = NULL;
00441     int offset = 0;
00443     while((sig = GetSignatureByClass(class_ids[i], offset++)) != NULL)
00444     {
00446       for(unsigned int j = 0; j < sig->CountClasses(); j++)
00447       {
00448         bool bFoundThis = false;
00449         for(unsigned int k = 0 ; k < size; k++)
00450         {
00451           if(k == i)
00452           {
00453             score_temp++;
00454             continue;
00455           }
00456           if(sig->GetClass(j) != NULL && class_ids[k] == sig->GetClass(j)->m_ID)
00457           {
00458             score_temp++;
00459             bFoundThis = true;
00460             break;
00461           }
00462         }
00463         if(!bFoundThis)
00464         {
00465           score_temp--;
00466         }
00467       }
00469       if(score_temp > score_max)
00470       {
00471         score_max = score_temp;
00472         index = i;
00473         if(sig_max != NULL)
00474           FreeActiveSignature(sig_max);
00476         sig_max = (Signature*)sig;
00477       }
00478       else
00479         FreeActiveSignature(sig);
00480     }
00481     score_temp = 0;
00482   }
00484  if(sig_max != NULL)
00485     printf("Signature selected: %p: id: %ld\n", sig_max, sig_max->m_ID);
00486 
00487   if(sig_max == NULL && size != 0)
00488   {
00489     return NULL;
00490   }
00491   CompleteSignature(sig_max, class_ids);
00493   return sig_max;
00494 }
00495 
00496 Elem* SignatureDB::FindCreateDescriptor(ObjectID_t class_id)
00497 {
00498   Elem* result = NULL;
00499   Signature* sample = GetSignatureByClass(class_id);
00500   if(sample != NULL)
00501   {
00502     int count_cont = 1;
00503     std::vector<Descriptor*> cands;
00504     while(true)
00505     {
00506       for(size_t i = 0; i < sample->CountElems(); i++)
00507       {
00508         Descriptor* descr = (Descriptor*)sample->GetElement(i, ELEM);
00509         if(descr == NULL)
00510           continue;
00511         if(descr->GetClass() == NULL)
00512          continue;
00513         if(descr->GetClass()->m_ID == class_id)
00514         {
00515           cands.push_back(descr);
00516         }
00517       }
00518       Signature* sample = GetSignatureByClass(class_id, count_cont++);
00519       if(sample == NULL)
00520         break;
00521     }
00522     if(cands.size() > 0)
00523     {
00524       /*TODO introduce metric for different descriptors for a class or add all...*/
00525       /*std::sort(cands.begin(), cands.end());*/
00526       result = cands[0];
00527     }
00528   }
00529   else
00530   {
00531     std::string searchstring = CheckClass(class_id);
00532   }
00533   return result;
00534 }
00535 
00536 
00537 void SignatureDB::SetNewObjectCallback(Comm* comm, bool wait_for_new)
00538 {
00539   for(size_t i = 0; i < m_dbStarter->CountChildren(); i++)
00540   {
00541     try
00542     {
00543      Signature* sig = (Signature*)Elem::ElemFactory(m_dbStarter->GetChild(i));
00544      if(!sig)
00545        continue;
00546      comm->NotifyNewObject(sig, sig->GetObjectPose());
00547      delete sig;
00548     }
00549     catch(...)
00550     {
00551       continue;
00552     }
00553   }
00554   if(wait_for_new)
00555     m_newObjectSubscriber.push_back(comm);
00556 }
00557 
00558 void SignatureDB::FreeActiveSignature(Signature* sig)
00559 {
00560   std::vector<std::pair<Signature*, int> >::iterator it = m_currentlyActiveSignatures.begin();
00561   //unsigned long timeStamp = (unsigned long)time(NULL);
00562   /*TODO, decide whats active*/
00563   for(;it != m_currentlyActiveSignatures.end(); it++)
00564   {
00565     if(sig == (*it).first)
00566       (*it).second--;
00567   }
00568 }
00569 
00570 
00571 Class* SignatureDB::GetClassByID(ObjectID_t id)
00572 {
00573   std::string st = CheckClass(id);
00574   if(st.length() == 0)
00575     throw "Unknown Class";
00576   return new Class(st, id);
00577 }
00578 
00579 
00580 ObjectID_t SignatureDB::CheckClass(std::string name)
00581 {
00582         std::vector<std::pair<std::string, ObjectID_t> > ::const_iterator it;
00583         for(it = m_classes.begin(); it != m_classes.end(); it++)
00584         {
00585                 /*cerr<<(*it).first<<endl;*/
00586                 if(boost::iequals((*it).first,name))
00587                         return (*it).second;
00588 
00589         }
00590         return -1;
00591 }
00592 
00593 std::string SignatureDB::CheckClass(ObjectID_t id_c)
00594 {
00595         std::vector<std::pair<std::string,ObjectID_t> > ::const_iterator it;
00596         for(it = m_classes.begin(); it != m_classes.end(); it++)
00597         {
00598                 if((*it).second == id_c)
00599                         return (*it).first;
00600         }
00601         return "";
00602 }
00603 
00604 int SignatureDB::GetElemIdByClass(ObjectID_t ClassID, int index)
00605 {
00606         ObjectID_t id  = -1;
00607         std::map<ObjectID_t, std::vector<ObjectID_t> >::iterator idlist = m_classToSignature.find(ClassID);
00608         if(idlist != m_classToSignature.end())
00609         {
00610           if(index < (signed)(*idlist).second.size())
00611           {
00612             id = (*idlist).second[index];
00613           }
00614         }
00615         else
00616         {
00617           printf("Class id not in Map\n");
00618         }
00619         return id;
00620 }
00621 
00622 Signature* SignatureDB::GetSignatureByClass(ObjectID_t ClassID, int index)
00623 {
00624         ObjectID_t id = GetElemIdByClass(ClassID, index);
00625         if(id == -1)
00626                 return NULL;
00627         return GetSignatureByID(id);
00628 }
00629 
00630 XMLTag* SignatureDB::Save()
00631 {
00632         XMLTag* tag = new XMLTag(XML_NODE_SIGNATUREDB);
00633         tag->AddChild(m_dbStarter->Clone());
00634         XMLTag* index = new XMLTag(m_index->GetName());
00635         std::map<ObjectID_t, std::vector<ObjectID_t> >::iterator it =  m_classToSignature.begin();
00636         for(; it != m_classToSignature.end(); it++)
00637         {
00638           for(size_t i = 0; i < (*it).second.size(); i++)
00639           {
00640             index->AddChild(  new Class2ID((*it).first, (*it).second[i])  );
00641           }
00642         }
00643         tag->AddChild(index);
00644         return tag;
00645 }
00646 // Accessor methods
00647 //
00648 XMLTag* SignatureDB::Query(std::string stQueryString)
00649 {
00650         //TODO:
00651         int id = atoi(stQueryString.c_str());
00652         int index;
00653         XMLTag* tag = new XMLTag(XML_NODE_SIGNATURE_VEC);
00654         if(Check(id, index))
00655         {
00656                 tag->AddChild(m_dbStarter->GetChild(index));
00657         }
00658         else
00659         {
00660                 int classID = CheckClass(stQueryString);
00661                 if(classID == -1)
00662                         if(CheckClass(id).length() != 0)
00663                                 classID = id;
00664                 int count = 0;
00665                 int id_elem = -1;
00666                 do
00667                 {
00668                         id_elem = GetElemIdByClass(classID, count);
00669                         if(Check(id_elem, index))
00670                         {
00671                                 tag->AddChild(m_dbStarter->GetChild(index));
00672                                 count++;
00673                         }
00674                 }
00675                 while(id_elem != -1);
00676         }
00677         return tag;
00678 }
00679 
00680 bool SignatureDB::Check(ObjectID_t sigID, int& index) const
00681 {
00682         size_t children = m_ids.size();
00683         for(unsigned int i = 0; i < children; i++)
00684         {
00685                 if(m_ids[i] == sigID)
00686                 {
00687                         index = i;
00688                         return true;
00689                 }
00690         }
00691         index = -1;
00692         return false;
00693 }
00694 
00695 void SignatureDB::UpdateIDList()
00696 {
00697   int children = m_dbStarter->CountChildren();
00698   m_ids.clear();
00699   for(int i = 0; i < children; i++)
00700   {
00701     Signature* sig = GetSignatureByIndex(i);
00702     if(sig != NULL)
00703     {
00704       m_ids.push_back(sig->m_ID);
00705       ObjectID_t id = sig->m_ID;
00706       for(unsigned int classes = 0; classes < sig->CountClasses(); classes++)
00707       {
00708         Class* cl = sig->GetClass(classes);
00709         SetClassSignature(cl->m_ID, id);
00710         AddClass(cl->GetName(), cl->m_ID);
00711       }
00712       FreeActiveSignature(sig);
00713     }
00714   }
00715 }
00716 
00717 PerceptionPrimitive& SignatureDB::CreateNewPerceptionPrimitive(Signature* sig)
00718 {
00719   PerceptionPrimitive* pp = new PerceptionPrimitive(sig);
00720   m_ppMap[pp->GetID()] = pp;
00721   return *pp;
00722 }
00723 
00724 void SignatureDB::EvaluatePerceptionPrimitive(PerceptionPrimitiveID_t id, double value, double weight, std::vector<ObjectID_t> whitelist)
00725 {
00726 
00727   if(m_ppMap.find(id) != m_ppMap.end())
00728   {
00729     PerceptionPrimitive *pp =  m_ppMap[id];
00730     pp->SetEvaluating();
00731     for(size_t i = 0; i < pp->m_signatures.size(); i++)
00732     {
00733        for(size_t j = 0; j < pp->m_signatures[i]->CountElems(); j++)
00734        {
00735          Elem* elem = pp->m_signatures[i]->GetElement(j, ELEM);
00736          elem->Evaluate(value, weight);
00737          if(weight > PROP_DECAY)
00738          {
00739            PerceptionPrimitiveID_t id_inner = elem->GetLastPerceptionPrimitive();
00740            printf("Elem %s is connected with PP %ld \n", elem->GetNodeName().c_str() ,id_inner);
00741            std::vector<ObjectID_t> whitelist_inner;
00742            whitelist_inner.push_back(elem->m_ID);
00743            if(id_inner != id)
00744              EvaluatePerceptionPrimitive(id_inner, value, weight / 2, whitelist_inner);
00745          }
00746        }
00747        printf("This pp has %ld algs\n" , pp->m_AlgorithmIDs.size());
00748        for(size_t alg = 0; alg < pp->m_AlgorithmIDs.size(); alg++)
00749        {
00750           pp->m_AlgorithmIDs[alg].second->EvalAlgorithm(pp->m_AlgorithmIDs[alg].first, value, pp->m_timing, pp->m_signatures[i]);
00751        }
00752 
00753     }
00754     for(size_t i = 0; i < pp->m_results.size(); i++)
00755     {
00756       int index;
00757       if(CheckClass(pp->m_results[i]).length() == 0 && Check(pp->m_results[i], index))
00758       {
00759         if(whitelist.size() != 0 && !find_in_vec(whitelist, (pp->m_results[i])))
00760         {
00761           printf("Skip %ld in Eval due to white list\n", pp->m_results[i]);
00762           continue;
00763         }
00764         Signature* sig = GetSignatureByID(pp->m_results[i]);
00765         std::vector<ObjectID_t> whitelist_inner;
00766         whitelist_inner.push_back(sig->m_ID);
00767         for(size_t j = 0; j < sig->CountElems(); j++)
00768         {
00769           Elem* elem = sig->GetElement(j, ELEM);
00770           elem->Evaluate(value, weight);
00771           if(weight > PROP_DECAY)
00772           {
00773             PerceptionPrimitiveID_t id_inner = elem->GetLastPerceptionPrimitive();
00774             printf("Elem %s is connected with PP %ld \n", elem->GetNodeName().c_str() ,id_inner);
00775             if(id_inner != id)
00776                 EvaluatePerceptionPrimitive(id_inner, value, weight / 2, whitelist_inner);
00777           }
00778         }
00779         for(size_t i = 0; i < pp->m_AlgorithmIDs.size(); i++)
00780         {
00781           pp->m_AlgorithmIDs[i].second->EvalAlgorithm(pp->m_AlgorithmIDs[i].first, value, pp->m_timing, sig);
00782         }
00783       }
00784     }
00785     pp->SetEvaluated(); 
00786   }
00787 
00788   std::map<PerceptionPrimitiveID_t, PerceptionPrimitive*>::iterator iter = m_ppMap.begin();
00789   for(; iter != m_ppMap.end(); iter++)
00790   {
00791     if((*iter).second->GetCurrState() == PP_DELETABLE || ((*iter).second->GetCurrState() == PP_TERMINATED && (*iter).second->m_startTime < time(NULL) - 600))
00792     {
00793       if(((*iter).second->GetCurrState() == PP_TERMINATED && (*iter).second->m_startTime < time(NULL) - 600))
00794         printf("delete perception primitive for timing %ld < %ld + 600 = %ld\n", (*iter).second->m_startTime, time(NULL), time(NULL) - 600);
00795       delete (*iter).second;
00796       m_ppMap.erase(iter);
00797       iter = m_ppMap.begin();
00798       if(iter == m_ppMap.end())
00799         break;
00800           }
00801   }
00802 }
00803 
00804 
00805 
00806 
00807 std::vector<std::pair <PerceptionPrimitiveID_t, PerceptionPrimitiveState> > SignatureDB::GetCurrentRunState()
00808 {
00809   std::vector<std::pair <PerceptionPrimitiveID_t, PerceptionPrimitiveState> > ret;
00810   std::map<PerceptionPrimitiveID_t, PerceptionPrimitive*>::iterator iter = m_ppMap.begin();
00811   for(; iter != m_ppMap.end(); iter++)
00812   {
00813           ret.push_back(std::pair <PerceptionPrimitiveID_t, PerceptionPrimitiveState>((*iter).first, (*iter).second->GetCurrState()));
00814 
00815   }
00816   return ret;
00817 }
00818 


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Mon Oct 6 2014 10:48:45