freSetupOptimizationMetricMonitorBase.txx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freSetupOptimizationMetricMonitorBase.txx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 #ifndef __freSetupOptimizationMetricMonitorBase_txx
00023 #define __freSetupOptimizationMetricMonitorBase_txx
00024 
00025 #include "freSetupOptimizationMetricMonitorBase.h"
00026 #include "freExceptions.h"
00027 
00028 namespace FREE
00029 {
00030 
00031 template <typename TResultClass>
00032 bool
00033 SetupOptimizationMetricMonitorBase<TResultClass>::
00034 GetUnevaluatedAdaptedSetup(const ThreadIDType& threadID, AdaptationIDType& adaptID, Setup::Pointer& smpSetup)
00035 {
00036   bool bWorkLeft = false;
00037 
00038   if (!this->IsInitialized()) throwExceptionMacro("Error thread #"<<threadID<<" requested a new adaptation, but monitor is not initialized.");
00039 
00040   if (this->ThreadIsProcessing(threadID))
00041   {
00042     throwExceptionMacro("Error thread #"<<threadID<<" requested a new adaptation, but is enlisted as have one open task. Ensure that thread has enlisted his last results.");
00043   }
00044   else
00045   { //ok, thread is allowed to get a new task.
00046     this->m_SetupMutex.Lock();
00047       AdaptationIDListType pendings = this->GetListOfPendingAdaptations();
00048       if (pendings.size()>0)
00049       {
00050         bWorkLeft = true;
00051         adaptID = pendings[0];
00052         this->SetAdaptationState(adaptID,Self::ASProcessing);
00053         this->EnlistThreadAsProcessing(threadID,adaptID);
00054 
00055         //final generate the adapted setup
00056         smpSetup = this->m_Adaptor.AdaptSetup(adaptID);
00057 
00058         this->m_EventMutex.Lock();
00059           if (this->fnOnNextAdaptation.IsNotNull()) this->fnOnNextAdaptation->Execute(adaptID,"thread ID "+Convert::ToStr(threadID),(void*)this,threadID);
00060         this->m_EventMutex.Unlock();
00061       }
00062     this->m_SetupMutex.Unlock();
00063   }
00064 
00065   return bWorkLeft;
00066 };
00067 
00068 template <typename TResultClass>
00069 void
00070 SetupOptimizationMetricMonitorBase<TResultClass>::
00071 NotifyFailedEvaluation(const ThreadIDType& threadID, const std::string& sError)
00072 {
00073   if (!this->IsInitialized()) throwExceptionMacro("Error thread #"<<threadID<<" notified a failure, but monitor is not initialized.");
00074 
00075   AdaptationIDType adaptID;
00076   if (!this->GetAdaptionProcessedByThread(threadID,adaptID)) throwExceptionMacro("Error; thread #"<<threadID<<" notified a failure, but has no adaptation enlisted as his task.");
00077 
00078   this->RemoveThreadFromProcessingList(threadID);
00079   this->SetAdaptationState(adaptID,Self::ASFailed);
00080 
00081   this->m_EventMutex.Lock();
00082     if (this->fnOnEvaluationFailed.IsNotNull()) this->fnOnEvaluationFailed->Execute(adaptID,sError,(void*)this,threadID);
00083   this->m_EventMutex.Unlock();
00084 };
00085 
00086 template <typename TResultClass>
00087 void
00088 SetupOptimizationMetricMonitorBase<TResultClass>::
00089 NotifyProgress(const ThreadIDType& threadID,
00090                const ProgressCtrlEventBase::StatusID status,
00091                const std::string& sComment)
00092 {
00093   if (!this->IsInitialized()) throwExceptionMacro("Error thread #"<<threadID<<" notified a progress, but monitor is not initialized.");
00094 
00095   AdaptationIDType adaptID;
00096   if (!this->GetAdaptionProcessedByThread(threadID,adaptID)) throwExceptionMacro("Error; thread #"<<threadID<<" notified a progress, but has no adaptation enlisted as his task.");
00097 
00098   this->m_EventMutex.Lock();
00099     if (this->fnOnEvaluationProgress.IsNotNull()) this->fnOnEvaluationProgress->Execute(status,sComment,(void*)this,threadID);
00100   this->m_EventMutex.Unlock();
00101 };
00102 
00103 template <typename TResultClass>
00104 void
00105 SetupOptimizationMetricMonitorBase<TResultClass>::
00106 EvaluationFinished(const ThreadIDType& threadID, const ResultType& results)
00107 {
00108   if (!this->IsInitialized()) throwExceptionMacro("Error thread #"<<threadID<<" wants to enlist results, but monitor is not initialized.");
00109 
00110   AdaptationIDType adaptID;
00111   if (!this->GetAdaptionProcessedByThread(threadID,adaptID)) throwExceptionMacro("Error; thread #"<<threadID<<" wants to enlist results, but has no adaptation enlisted as his task.");
00112 
00113   //remove the thread from the processing list
00114   this->RemoveThreadFromProcessingList(threadID);
00115   this->SetAdaptationState(adaptID,Self::ASEvaluated);
00116 
00117   //now allow to enlist the results
00118   this->m_ResultMutex.Lock();
00119     this->m_AdaptOrderMutex.Lock();
00120       m_AdaptOrder.push_back(adaptID);
00121     this->m_AdaptOrderMutex.Unlock();
00122 
00123     this->InsertEvaluationResults(results);
00124   this->m_ResultMutex.Unlock();
00125 
00126   this->m_StatisticMutex.Lock();
00127 
00128     //create new entry
00129     if (m_pMetricStatistics)
00130     {
00131       StatisticEntry* pNewEntry = m_pMetricStatistics->GetData().PostCreateChildEntry(m_pMetricStatistics->GetData().GetCurrentEntryID());
00132       pNewEntry->SetRefID(1);
00133 
00134       //add adaptation ID
00135       pNewEntry->AddValue(Convert::ToStr(adaptID),1);
00136 
00137       //enlist the rest of the result   
00138       this->EnlistResultsToStatistic(results,*pNewEntry);
00139 
00140       //close entry
00141       pNewEntry->CloseEntry();
00142     }
00143 
00144     this->m_EventMutex.Lock();
00145       if (this->fnOnEvaluationDone.IsNotNull()) this->fnOnEvaluationDone->Execute(adaptID,"",(void*)this,threadID);
00146     this->m_EventMutex.Unlock();
00147   this->m_StatisticMutex.Unlock();
00148 };
00149 
00150 template <typename TResultClass>
00151 void
00152 SetupOptimizationMetricMonitorBase<TResultClass>::
00153 SetMetricStatistic(Statistics* pStatistic)
00154 {
00155   this->m_StatisticMutex.Lock();
00156     this->m_pMetricStatistics = pStatistic;
00157   this->m_StatisticMutex.Unlock();
00158 };
00159 
00160 template <typename TResultClass>
00161 Statistics*
00162 SetupOptimizationMetricMonitorBase<TResultClass>::
00163 GetMetricStatistic()
00164 {
00165   this->m_StatisticMutex.Lock();
00166   return this->m_pMetricStatistics;
00167 };
00168 
00169 template <typename TResultClass>
00170 void
00171 SetupOptimizationMetricMonitorBase<TResultClass>::
00172 ReleaseMetricStatistic()
00173 {
00174   this->m_StatisticMutex.Unlock();
00175 };
00176 
00177 template <typename TResultClass>
00178 void
00179 SetupOptimizationMetricMonitorBase<TResultClass>::
00180 SetGenericSetup(Setup* pSetup)
00181 {
00182   this->m_SetupMutex.Lock();
00183     this->m_smpGenericSetup = pSetup;
00184   this->m_SetupMutex.Unlock();
00185 };
00186 
00187 template <typename TResultClass>
00188 void
00189 SetupOptimizationMetricMonitorBase<TResultClass>::
00190 SetAdaptationList(Adaptation::AdaptationList* pAdaptations)
00191 {
00192   this->m_SetupMutex.Lock();
00193     this->m_smpAdaptations = pAdaptations;
00194   this->m_SetupMutex.Unlock();
00195 };
00196 
00197 template <typename TResultClass>
00198 bool
00199 SetupOptimizationMetricMonitorBase<TResultClass>::
00200 GetAdaptationResults(const AdaptationIDType& adaptID, ResultType& results) const
00201 {
00202   if (this->GetAdaptationState(adaptID) != Self::ASEvaluated) return false;
00203 
00204   ResultIndexType index = this->GetResultIndexOfAdaptation(adaptID);
00205 
00206   if (index == -1) return false;
00207 
00208   this->m_ResultMutex.Lock();
00209     this->CopyAdaptationResults(index,results);
00210   this->m_ResultMutex.Unlock();
00211 
00212   return true;
00213 };
00214   
00215 template <typename TResultClass>
00216 typename SetupOptimizationMetricMonitorBase<TResultClass>::AdaptationIDListType
00217 SetupOptimizationMetricMonitorBase<TResultClass>::
00218 GetListOfPendingAdaptations() const
00219 {
00220   return this->GetListOfAdaptationsByState(Self::ASPending);
00221 };
00222 
00223 template <typename TResultClass>
00224 typename SetupOptimizationMetricMonitorBase<TResultClass>::AdaptationIDListType
00225 SetupOptimizationMetricMonitorBase<TResultClass>::
00226 GetListOfProcessedAdaptations() const
00227 {
00228   return this->GetListOfAdaptationsByState(Self::ASProcessing);
00229 };
00230 
00231 template <typename TResultClass>
00232 typename SetupOptimizationMetricMonitorBase<TResultClass>::AdaptationIDListType
00233 SetupOptimizationMetricMonitorBase<TResultClass>::
00234 GetListOfFailedAdaptations() const
00235 {
00236   return this->GetListOfAdaptationsByState(Self::ASFailed);
00237 };
00238 
00239 template <typename TResultClass>
00240 typename SetupOptimizationMetricMonitorBase<TResultClass>::AdaptationIDListType
00241 SetupOptimizationMetricMonitorBase<TResultClass>::
00242 GetListOfEvaluatedAdaptations() const
00243 {
00244   return this->GetListOfAdaptationsByState(Self::ASEvaluated);
00245 };
00246 
00247 template <typename TResultClass>
00248 typename SetupOptimizationMetricMonitorBase<TResultClass>::AdaptationIDListType
00249 SetupOptimizationMetricMonitorBase<TResultClass>::
00250 GetListOfAdaptationsByState(const AdaptationStateType& state) const
00251 {
00252   AdaptationIDListType result;
00253 
00254   this->m_AdaptStatesMutex.Lock();
00255     for (AdaptationCountType index = 0; index < m_AdaptStates.size(); index++)
00256     {
00257       if (m_AdaptStates[index] == state) result.push_back(index);
00258     }
00259   this->m_AdaptStatesMutex.Unlock();
00260 
00261   return result;
00262 };
00263 
00264 template <typename TResultClass>
00265 typename SetupOptimizationMetricMonitorBase<TResultClass>::AdaptationStateType
00266 SetupOptimizationMetricMonitorBase<TResultClass>::
00267 GetAdaptationState(const AdaptationIDType& adaptID) const
00268 {
00269   bool bFound = false;
00270   AdaptationStateType result;
00271 
00272   this->m_AdaptStatesMutex.Lock();
00273 
00274     if ((adaptID>-1)||(adaptID>=m_AdaptStates.size()))
00275     {
00276       result = m_AdaptStates[adaptID];
00277       bFound =true;
00278     }
00279 
00280   this->m_AdaptStatesMutex.Unlock();
00281 
00282   if (!bFound) throwExceptionMacro("Error; adaption ID ("<<adaptID<<") is out of bound.");
00283 
00284   return result;
00285 };
00286 
00287 template <typename TResultClass>
00288 bool
00289 SetupOptimizationMetricMonitorBase<TResultClass>::
00290 EvaluationIsFinished() const
00291 {
00292   AdaptationCountType adaptSize;
00293 
00294   this->m_AdaptStatesMutex.Lock();
00295     adaptSize = this->m_AdaptStates.size();
00296   this->m_AdaptStatesMutex.Unlock();
00297 
00298   AdaptationCountType failedSize = this->GetListOfFailedAdaptations().size();
00299   AdaptationCountType evaluatedSize = this->GetListOfEvaluatedAdaptations().size;
00300 
00301   return adaptSize == failedSize+evaluatedSize;
00302 };
00303 
00304 template <typename TResultClass>
00305 bool
00306 SetupOptimizationMetricMonitorBase<TResultClass>::
00307 IsInitialized() const
00308 {
00309   //no mutex safety here because the bool should be equal or less then a
00310   //processor word. Therefor the returning of the Value is atomar.
00311   return m_IsInitialized;
00312 };
00313 
00314 template <typename TResultClass>
00315 void
00316 SetupOptimizationMetricMonitorBase<TResultClass>::
00317 Initialize()
00318 {
00319   AdaptationCountType adaptSize = 0;
00320 
00321   this->m_SetupMutex.Lock();
00322     bool bSetupInit = this->m_smpGenericSetup.IsNotNull();
00323     bool bAdaptationInit = this->m_smpAdaptations.IsNotNull();
00324 
00325     if (bSetupInit && bAdaptationInit)
00326     {
00327       this->m_Adaptor.SetTemplateSetup(this->m_smpGenericSetup);
00328       this->m_Adaptor.AddAdaptations(* (this->m_smpAdaptations.GetPointer()));
00329       adaptSize = this->m_smpAdaptations->Size();
00330     }
00331   this->m_SetupMutex.Unlock();
00332 
00333   if (!bSetupInit) throwExceptionMacro("Error; cannot initialize monitor, generic setup is not set.");
00334   if (!bAdaptationInit) throwExceptionMacro("Error; cannot initialize monitor, adaptation list is not set.");
00335 
00336   this->m_StatisticMutex.Lock();
00337     bool bStatisticInit = this->m_pMetricStatistics != 0;
00338 
00339     if (bStatisticInit)
00340     {
00341       this->m_pMetricStatistics->Reset();
00342 
00343       this->m_pMetricStatistics->GetDictionary().AddValueDefinition(this->GetNameOfClass(),"Adaption ID", "ID of the adaption the results are belonging to");
00344       this->m_pMetricStatistics->GetDictionary().AddEntryDefinition(this->GetNameOfClass(),"AdaptationMetricValues",this->GetNameOfClass(),"Metric values of an adaptation");
00345       this->EnlistDictionaryEntries(this->m_pMetricStatistics->GetDictionary());
00346     }
00347   this->m_StatisticMutex.Unlock();
00348 
00349   if (!bStatisticInit) throwExceptionMacro("Error; cannot initialize monitor, metric statistic is not set.");
00350 
00351   this->m_AdaptStatesMutex.Lock();
00352     this->m_AdaptStates.assign(adaptSize, Self::ASPending);
00353   this->m_AdaptStatesMutex.Unlock();
00354 
00355   this->m_AdaptThreadMutex.Lock();
00356     this->m_AdaptThread.clear();
00357   this->m_AdaptThreadMutex.Unlock();
00358 
00359   this->m_AdaptOrderMutex.Lock();
00360     this->m_AdaptOrder.clear();
00361   this->m_AdaptOrderMutex.Unlock();
00362 
00363   this->m_SetupMutex.Lock();
00364     this->m_IsInitialized = true;
00365   this->m_SetupMutex.Unlock();
00366 };
00367 
00368 template <typename TResultClass>
00369 typename SetupOptimizationMetricMonitorBase<TResultClass>::AdaptationCountType
00370 SetupOptimizationMetricMonitorBase<TResultClass>::
00371 GetFailureCount() const
00372 {
00373   return this->GetListOfFailedAdaptations().size();
00374 };
00375 
00376 template <typename TResultClass>
00377 SetupOptimizationMetricMonitorBase<TResultClass>::
00378 SetupOptimizationMetricMonitorBase()
00379 {
00380   this->m_IsInitialized = false;
00381   this->m_AdaptOrder.clear();
00382   this->m_AdaptStates.clear();
00383   this->m_AdaptThread.clear();
00384   this->m_smpAdaptations = 0;
00385   this->m_pMetricStatistics = 0;
00386   this->m_smpGenericSetup = 0;
00387 };
00388 
00389 template <typename TResultClass>
00390 SetupOptimizationMetricMonitorBase<TResultClass>::
00391 ~SetupOptimizationMetricMonitorBase()
00392 {
00393 };
00394 
00395 
00396 template <typename TResultClass>
00397 bool
00398 SetupOptimizationMetricMonitorBase<TResultClass>::
00399 GetAdaptionProcessedByThread(const ThreadIDType& threadID, AdaptationIDType& adaptationID) const
00400 {
00401   bool result = false;
00402 
00403   this->m_AdaptThreadMutex.Lock();
00404 
00405     ThreadMapType::const_iterator finding = this->m_AdaptThread.find(threadID);
00406     if ( finding != this->m_AdaptThread.end( ) )
00407     {
00408       adaptationID = finding->second;
00409       result = true;
00410     }
00411 
00412   this->m_AdaptThreadMutex.Unlock();
00413 
00414   return result;
00415 };
00416 
00417 template <typename TResultClass>
00418 bool
00419 SetupOptimizationMetricMonitorBase<TResultClass>::
00420 ThreadIsProcessing(const ThreadIDType& threadID) const
00421 {
00422   AdaptationIDType adaptationID;
00423   return this->GetAdaptionProcessedByThread(threadID,adaptationID);
00424 };
00425 
00426 template <typename TResultClass>
00427 void
00428 SetupOptimizationMetricMonitorBase<TResultClass>::
00429 EnlistThreadAsProcessing(const ThreadIDType& threadID,const AdaptationIDType& adaptationID)
00430 {
00431   AdaptationIDType usedAdaptationID;
00432   if (this->GetAdaptionProcessedByThread(threadID,usedAdaptationID)) throwExceptionMacro("Error; tried to enlist thread #"<<threadID<<" for adaptation #"<<adaptationID<<", but thread already is enlisted for adaptation #"<<usedAdaptationID<<".");
00433 
00434   this->m_AdaptThreadMutex.Lock();
00435 
00436     typedef std::pair <ThreadIDType, AdaptationIDType> Map_Pair;
00437     this->m_AdaptThread.insert ( Map_Pair ( threadID, adaptationID ) );
00438 
00439   this->m_AdaptThreadMutex.Unlock();
00440 };
00441 
00442 template <typename TResultClass>
00443 void
00444 SetupOptimizationMetricMonitorBase<TResultClass>::
00445 RemoveThreadFromProcessingList(const ThreadIDType& threadID)
00446 {
00447   this->m_AdaptThreadMutex.Lock();
00448 
00449     ThreadMapType::iterator finding = this->m_AdaptThread.find(threadID);
00450     if ( finding != this->m_AdaptThread.end( ) )
00451     {
00452       this->m_AdaptThread.erase(finding);
00453     }
00454 
00455   this->m_AdaptThreadMutex.Unlock();
00456 };
00457 
00458 template <typename TResultClass>
00459 typename SetupOptimizationMetricMonitorBase<TResultClass>::ResultIndexType
00460 SetupOptimizationMetricMonitorBase<TResultClass>::
00461 GetResultIndexOfAdaptation(const AdaptationIDType& adaptationID) const
00462 {
00463   ResultIndexType result = -1;
00464 
00465   this->m_AdaptOrderMutex.Lock();
00466 
00467     for (AdaptationCountType index = 0; index < m_AdaptOrder.size(); index++)
00468     {
00469       if (m_AdaptOrder[index] == adaptationID) result = index;
00470     }
00471 
00472   this->m_AdaptOrderMutex.Unlock();
00473 
00474   return result;
00475 };
00476 
00477 template <typename TResultClass>
00478 void
00479 SetupOptimizationMetricMonitorBase<TResultClass>::
00480 SetAdaptationState(const AdaptationIDType& adaptID, const AdaptationStateType& state)
00481 {
00482   bool bFound = false;
00483 
00484   this->m_AdaptStatesMutex.Lock();
00485 
00486     if ((adaptID>-1)||(adaptID>=m_AdaptStates.size()))
00487     {
00488       m_AdaptStates[adaptID] = state;
00489       bFound =true;
00490     }
00491 
00492   this->m_AdaptStatesMutex.Unlock();
00493 
00494   if (!bFound) throwExceptionMacro("Error; adaption ID ("<<adaptID<<") is out of bound, state cannot be set.");
00495 };
00496 
00497 } // end namespace FREE
00498 
00499 #endif

Generated at Sat Oct 13 17:27:30 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000