freSessionBuilder.cxx

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: freSessionBuilder.cxx,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 #if defined(_MSC_VER)
00023 #pragma warning ( disable : 4786 )
00024 #endif
00025 
00026 #include "freSessionBuilder.h"
00027 #include "freExceptions.h"
00028 
00029 #include "freControllerCentral.h"
00030 #include "freSessionInfo.h"
00031 #include "freSessionAccessor.h"
00032 #include "freMediaControllerBase.h"
00033 
00034 namespace FREE
00035 {
00036 
00037 SessionBuilder::
00038 SessionBuilder()
00039 {
00040 }
00041 
00042 SessionBuilder::
00043 ~SessionBuilder()
00044 {
00045 }
00046 
00047 bool
00048 SessionBuilder::
00049 CheckComponentNecessity(const SessionComponentCache* pComponentCache)
00050 {
00051         if (!pComponentCache) throwStaticExceptionMacro("Error while evaluating component's necessity. Invalid component cache passed (null pointer).");
00052   if (!pComponentCache->SetupIsAssigned()) throwStaticExceptionMacro("Error while evaluating component's necessity. Component cache has no setup.");
00053 
00054   const SessionComponentCache* pParentCache = pComponentCache->GetParentCache();
00055 
00056   return CheckComponentNecessity(pParentCache,pComponentCache->GetComponentID());
00057 };
00058 
00059 bool
00060 SessionBuilder::
00061 CheckComponentNecessity(const SessionComponentCache* pParentCache, const ComponentID& componentID)
00062 {
00063   if (!pParentCache) return false; //No parent, no requirement.
00064         if (!pParentCache->ControllerIsAssigned()) return false; //No controller, no requirement.
00065         
00066   CtrlProfile::ControllerProfile::Pointer smpProfile = pParentCache->Controller()->GetProfile(pParentCache);
00067 
00068   const CtrlProfile::ProfileSubComponents& rProfilSubComps = smpProfile->SubComponents();
00069   CtrlProfile::SubComponent* pCompProfil = rProfilSubComps.GetElement(componentID);
00070 
00071         if (!pCompProfil) throwStaticExceptionMacro("Error while evaluating component's necessity. Component was not found in parental profil (ComponentID): "<<componentID);
00072 
00073         if (pCompProfil->GetIsRequired()==0) return false;
00074         else if (pCompProfil->GetIsRequired()==1) return true;
00075         else
00076         {
00077                 for ( CtrlProfile::ProfileSubComponents::ElementsCountType index = 0; index < rProfilSubComps.Size(); index++)
00078                 {
00079                         CtrlProfile::SubComponent* pOtherProfil = rProfilSubComps.GetElement(index);
00080                         if ((pOtherProfil->GetComponentID()!=pCompProfil->GetComponentID()) && (pOtherProfil->GetIsRequired()==pCompProfil->GetIsRequired()))
00081                         { //check if the other component has a controller assigned.
00082                                 const ComponentSetup* pOtherSetup = pParentCache->Setup()->Components().GetElement(pOtherProfil->GetComponentID());
00083                                 if (!pOtherSetup) throwStaticExceptionMacro("Error while evaluating component's necessity. Sibling component has no component setup (ComponentID): " << pOtherProfil->GetComponentID());
00084 
00085                                 if (pOtherSetup->ControllerIsAssigned()) return true;
00086                                 //there is at least one other subcomponent in the same requrement group that has a controller assigned.
00087                         }
00088                 }
00089 
00090                 return false;
00091         }
00092 };
00093 
00094 SessionComponentCache::Pointer
00095 SessionBuilder::
00096 BuildComponent (ComponentSetup* pComponentSetup, SessionComponentCache* pParentComponentCache)
00097 {
00098   if (!pComponentSetup) throwStaticExceptionMacro("Passed component setup is NULL.");
00099 
00100   GenericComponentController* pController = NULL;
00101 
00102         if (!pComponentSetup->ControllerIsAssigned())
00103   { 
00104                 if (pParentComponentCache)
00105                 { // there is a parent and so maybe requirements so check
00106       if (CheckComponentNecessity(pParentComponentCache,pComponentSetup->GetComponentID())) throwStaticExceptionMacro("No Controller specified for a needed component. Passed ComponentID: "<<pComponentSetup->GetComponentID());
00107                 }
00108     return SessionComponentCache::New(pParentComponentCache,pComponentSetup,0,0);
00109   }
00110   else
00111   {
00112     pController = ControllerCentral::GetController(pComponentSetup->GetControllerID());
00113                 if (!pController) throwStaticExceptionMacro("Controller not found for building. ControllerID: "<< pComponentSetup->GetControllerID());
00114     ControllerCentral::TriggerBuildEvent(1,"... building @ "+ pComponentSetup->GetIDPath().ToStr()+" : "+pComponentSetup->GetControllerID(), pComponentSetup);
00115 
00116     return pController->BuildSetup(pComponentSetup, pParentComponentCache);
00117   };
00118 };
00119 
00120 SessionInfo::Pointer
00121 SessionBuilder::
00122 BuildSession (Setup* pSetup)
00123 {
00124   if (!pSetup) throwStaticExceptionMacro("Cannot build session; invalid setup (NULL)"); 
00125   try
00126   {
00127     SessionInfo::Pointer smpNewSession = SessionInfo::New(pSetup);
00128 
00129     // build sections
00130     ControllerCentral::TriggerBuildEvent(2,"Building sections of the setup",smpNewSession.GetPointer());
00131     Setup::SectionVector sections = pSetup->GetSections();
00132 
00133                 unsigned int iSectionIndex = 0;
00134 
00135     for (Setup::SectionVector::iterator iter = sections.begin(); iter!=sections.end(); iter++, iSectionIndex++)
00136     {
00137       SessionComponentCache::Pointer smpComponentCache = BuildComponent(*iter,smpNewSession->GetSessionCache());
00138       if (smpComponentCache.IsNull()) throwStaticExceptionMacro("Controller was unable to build component (IDPath: "<<(*iter)->GetIDPath().ToStr()<<").");
00139       smpNewSession->GetSessionCache()->SubCaches().AddElement(smpComponentCache);
00140     }
00141 
00142     return smpNewSession;
00143   }
00144   catchAllNPassStaticMacro("Unknown exception, while building registration session.");
00145 };
00146 
00147 SessionComponentCache::Pointer
00148 SessionBuilder::
00149 GeneratePassiveComponentCache (ComponentSetup* pComponentSetup, SessionComponentCache* pParentComponentCache)
00150 {
00151   if (!pComponentSetup) throwStaticExceptionMacro("Passed component setup is NULL.");
00152 
00153   GenericComponentController* pController = NULL;
00154   pController = ControllerCentral::GetController(pComponentSetup->GetControllerID());
00155   SessionComponentCache::Pointer smpCache = SessionComponentCache::New(pParentComponentCache,pComponentSetup,pController,0,true);
00156 
00157         for (ComponentSetupCollection::ComponentsCountType index=0; index<pComponentSetup->Components().Size(); index++)
00158   {
00159     ComponentSetup* pSubSetup = pComponentSetup->Components().GetElement(index); 
00160     SessionComponentCache::Pointer smpSubCache = SessionBuilder::GeneratePassiveComponentCache(pSubSetup,smpCache);
00161 
00162                 smpCache->SubCaches().AddElement(smpSubCache);
00163   }
00164 
00165         return smpCache;
00166 };
00167 
00168 void
00169 SessionBuilder::
00170 ActualizeComponent(SessionComponentCache* pComponentCache, SessionInfo* pSessionInfo, const unsigned int& iActiveLayer)
00171 {
00172   if (!pComponentCache) throwStaticExceptionMacro("Passed component setup is NULL.");
00173 
00174   if (!pComponentCache->ControllerIsAssigned())
00175   {
00176                 if (CheckComponentNecessity(pComponentCache)) throwStaticExceptionMacro("No Controller specified for a needed component. Passed ComponentID: " << pComponentCache->GetComponentID());
00177   }
00178   else
00179   {
00180     if ((pComponentCache->Controller()->CheckComponentActualizationNecessity(pComponentCache)) ||
00181         (iActiveLayer==0))
00182        pComponentCache->Controller()->ActualizeComponent(pComponentCache, pSessionInfo, iActiveLayer);
00183   };
00184 };   
00185 
00186 void
00187 SessionBuilder::
00188 ActualizeSession (SessionInfo* pSessionInfo, const unsigned int& iActiveLayer)
00189 {
00190   try
00191   {
00192     if (!pSessionInfo) throwStaticExceptionMacro("No valid session info is defined; null pointer passed.");
00193 
00194     for (unsigned int i = 0; i < pSessionInfo->GetComponentCaches().Size(); i++)
00195     {
00196       ActualizeComponent(pSessionInfo->GetComponentCaches().GetElement(i),pSessionInfo,iActiveLayer);
00197     }
00198   }
00199   catchAllNPassStaticMacro("Unknown exception, while actualizing components.");
00200 };
00201 
00202 void
00203 SessionBuilder::
00204 SetStatisticEntry(StatisticEntry& rStatisticEntry, SessionComponentCache* pComponentCache,
00205                  SessionInfo* pSessionInfo)
00206 {
00207   GenericComponentController* pController = NULL;
00208 
00209   if (!pComponentCache) throwStaticExceptionMacro("Passed component cache is NULL.");
00210   if (!pSessionInfo) throwStaticExceptionMacro("Passed session info is NULL.");
00211 
00212   if (pComponentCache->ControllerIsAssigned())
00213   {
00214     pComponentCache->Controller()->SetStatisticEntry(rStatisticEntry,pComponentCache,pSessionInfo);
00215   }
00216   else
00217   {
00218     if (pComponentCache->ComponentIsAssigned())
00219     {
00220       std::string sComponentID = "unknown";
00221       if (pComponentCache->SetupIsAssigned())
00222       {
00223         sComponentID = pComponentCache->GetComponentID();
00224       }
00225       throwStaticExceptionMacro("Cannot set step statistic. No Controller specified for a present component in cache. ComponentID: " << sComponentID);
00226     }
00227   }
00228 };
00229 
00230 void
00231 SessionBuilder::
00232 LinkMediaToComponent(SessionComponentCache* pComponentCache, SessionInfo* pInfo)
00233 {
00234   if (!pComponentCache) throwStaticExceptionMacro("Passed component cache is NULL.");
00235   if (!pComponentCache->SetupIsAssigned()) throwStaticExceptionMacro("Setup missing, cannot link media.");
00236   if (!pInfo) throwStaticExceptionMacro("Passed session info is NULL.");
00237   
00238   //Check own media links
00239   if (pComponentCache->Setup()->MediaLinks().Size()!=0)
00240   {
00241     for (unsigned int i=0; i < pComponentCache->Setup()->MediaLinks().Size(); i++)
00242     {
00243       ComponentMediaLink::Pointer pLink = pComponentCache->Setup()->MediaLinks().GetElement(i);
00244 
00245       ControllerCentral::TriggerBuildEvent(5,"... linking "+ pComponentCache->GetIDPath().ToStr()+ "/#" +pLink->GetMediaID() + " to " + pLink->GetSourceIDPath().ToStr(), pComponentCache);
00246 
00247       IDPath mediaPath = pLink->GetSourceIDPath().TransformIDPath(pComponentCache->GetIDPath());
00248 
00249       GenericMediaType::Pointer smpMedia = SessionAccessor::GetMedia(mediaPath,pInfo);
00250       
00251                         pComponentCache->Controller()->SetMedia(pLink->GetMediaID(),smpMedia,pComponentCache,pInfo);
00252     }
00253   }
00254 
00255   //Check the media links of sub caches
00256   for (unsigned int iSub=0; iSub<pComponentCache->SubCaches().Size(); iSub++)
00257   {
00258     LinkMediaToComponent(pComponentCache->SubCaches().GetElement(iSub),pInfo);
00259   }
00260 };
00261 
00262 void
00263 SessionBuilder::
00264 LinkMediaToComponentByID(const std::string& sMediaID, SessionComponentCache* pComponentCache, SessionInfo* pInfo)
00265 {
00266   if (!pComponentCache) throwStaticExceptionMacro("Passed component cache is NULL.");
00267   if (!pComponentCache->SetupIsAssigned()) throwStaticExceptionMacro("Setup missing, cannot link media.");
00268   if (!pInfo) throwStaticExceptionMacro("Passed session info is NULL.");
00269   
00270   //Check own media links
00271   ComponentMediaLink::Pointer smpLink = pComponentCache->Setup()->MediaLinks().GetElement(sMediaID);
00272   if (smpLink.IsNotNull())
00273   {
00274     ControllerCentral::TriggerBuildEvent(5,"... linking "+ pComponentCache->GetIDPath().ToStr()+ "/#" +smpLink->GetMediaID() + " to " + smpLink->GetSourceIDPath().ToStr(), pComponentCache);
00275 
00276     IDPath mediaPath = smpLink->GetSourceIDPath().TransformIDPath(pComponentCache->GetIDPath());
00277 
00278     GenericMediaType::Pointer smpMedia = SessionAccessor::GetMedia(mediaPath,pInfo);
00279       
00280                 pComponentCache->Controller()->SetMedia(smpLink->GetMediaID(),smpMedia,pComponentCache,pInfo);
00281   }
00282 };
00283 
00284 } //end of namspace free

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