bbtkBlackBoxDescriptor.cxx

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
00004   Language:  C++
00005   Date:      $Date: 2009/12/08 14:10:36 $
00006   Version:   $Revision: 1.22 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00031 
00036 #include "bbtkBlackBoxDescriptor.h"
00037 #include "bbtkMessageManager.h"
00038 #include "bbtkPackage.h"
00039 #include "bbtkUtilities.h"
00040 #include "bbtkAtomicBlackBoxDescriptor.h"
00041 #include "bbtkWxBlackBox.h"
00042 
00043 namespace bbtk
00044 {
00045 
00046   typedef Package::Pointer PackagePointer;
00047 
00048 #define bbtkDMessage(key,level,mess) \
00049   bbtkMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
00050 #define bbtkDDebugMessage(key,level,mess)       \
00051   bbtkDebugMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
00052 
00053 
00054   //=========================================================================
00056   BlackBoxDescriptor::BlackBoxDescriptor()  
00057     : mTypeName("Unknown"), 
00058       mDescription(""), 
00059       mAuthor(""),
00060       mCategory(""),
00061       mKind(STANDARD),
00062       mPackage()
00063   {
00064     bbtkDDebugMessage("object",4,
00065                      "==> BlackBoxDescriptor()"<<std::endl);
00066     bbtkDDebugMessage("object",4,
00067                      "<== BlackBoxDescriptor()"<<std::endl);
00068   }
00069   //=========================================================================
00070 
00071   //=========================================================================
00073   BlackBoxDescriptor::BlackBoxDescriptor(const BlackBoxDescriptor&)  
00074   {
00075   }
00076   //=========================================================================
00078   BlackBoxDescriptor::~BlackBoxDescriptor()
00079   {
00080     bbtkDDebugMessage("object",4,
00081                      "==> ~BlackBoxDescriptor()"
00082                      <<std::endl);
00083 
00084     // deletes all I/O descriptors
00085     InputDescriptorMapType::iterator i;
00086     for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
00087     OutputDescriptorMapType::iterator o;
00088     for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
00089 
00090     bbtkDDebugMessage("object",4,
00091                       "<== ~BlackBoxDescriptor()"
00092                       <<std::endl);
00093   }
00094   //=========================================================================
00095 
00096   /*
00097   //=======================================================================
00099   void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
00100   {
00101     bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
00102                 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
00103     long c = desc.use_count();
00104     bbtkMessage("object",3," - ref count = "<<c<<std::endl);
00105     // If only one ref 
00106     if ((c == 1) && (desc.lock()->mPackage))
00107       {
00108         bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
00109         
00110         Package::WeakPointer pack = desc.lock()->mPackage;
00111         Package::ReleaseBlackBoxDescriptor(pack,desc);
00112       }
00113     else 
00114       {
00115         bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
00116       }    
00117     bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
00118                 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
00119  
00120   }
00121   //=========================================================================
00122   */
00123 
00124   /*
00125   //=========================================================================
00127   void BlackBoxDescriptor::UnReference()
00128   {
00129     bbtkDDebugMessageInc("kernel",1,
00130                         "BlackBoxDescriptor::UnReference() ["
00131                         <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
00132     mRefCount--;
00133     if (mRefCount<=0) 
00134       {
00135         bbtkDDebugMessage("kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
00136         delete this;
00137       }
00138   }
00139   //=========================================================================
00140   */
00141   //=========================================================================
00143   void BlackBoxDescriptor::Check(bool) const
00144   {
00145     
00146   }
00147   //=========================================================================
00148 
00149   //=========================================================================
00151   void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
00152   {
00153     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToDescription(\""
00154                       <<s<<"\")"<<std::endl);
00155     if (clear) mDescription = s; 
00156     else mDescription += s;
00157    }
00158   //=========================================================================
00159 
00160   //=========================================================================   
00162   void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
00163   {
00164     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToAuthor(\""
00165                       <<s<<"\")"
00166                       <<std::endl);
00167     if (clear) mAuthor = s;
00168     else mAuthor += s;
00169   }
00170   //=========================================================================
00171 
00172   //=========================================================================  
00174   void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
00175   {
00176     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToCategory(\""
00177                       <<s<<"\")"
00178                       <<std::endl);  
00179     if (clear) mCategory = s;
00180     else mCategory += s;
00181     mCategory += ";";
00182   }  
00183   //=========================================================================
00184   
00185   //=========================================================================
00186   const BlackBoxDescriptor::InputDescriptor* 
00187   BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
00188   {
00189     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
00190                       <<name<<"')"<<std::endl);
00191 
00192     InputDescriptorMapType::const_iterator i;
00193     i = mInput.find(name);
00194     if ( i == mInput.end() ) 
00195     {
00196             bbtkError("input '"<<name<<"' does not exist");
00197     }
00198 
00199     return i->second;
00200   }
00201   //=========================================================================
00202 
00203   //=========================================================================
00204   const BlackBoxDescriptor::OutputDescriptor* 
00205   BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
00206   {
00207     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
00208                       <<name<<"')"<<std::endl);
00209 
00210     OutputDescriptorMapType::const_iterator i;
00211     i = mOutput.find(name);
00212     if ( i == mOutput.end() ) 
00213     {
00214       bbtkError("output '"<<name<<"' does not exist");
00215     }
00216 
00217     return i->second;
00218   }
00219   //=========================================================================
00220 
00221   //=========================================================================
00222   void BlackBoxDescriptor::GetHelp(bool full) const
00223   {
00224     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetHelp()"
00225                       <<std::endl);
00226     
00227     bbtkMessage("help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
00228     bbtkMessage("help",1," "              <<GetDescription()<<std::endl);
00229     bbtkMessage("help",1," By : "         <<GetAuthor()     <<std::endl);
00230     bbtkMessage("help",1," Categories : " <<GetCategory()    <<std::endl);
00231     if (mInput.size()) 
00232       bbtkMessage("help",1," * Inputs : "<<std::endl);
00233     else 
00234       bbtkMessage("help",1," * No inputs"<<std::endl);
00235     InputDescriptorMapType::const_iterator i;
00236     unsigned int namelmax = 0;
00237     unsigned int typelmax = 0;
00238     unsigned int natlmax = 0;
00239     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
00240     {
00241            if (i->second->GetName().size()>namelmax) 
00242              namelmax = i->second->GetName().size();
00243            if (i->second->GetHumanTypeName().size()>typelmax) 
00244              typelmax = i->second->GetHumanTypeName().size();
00245            if (i->second->GetNature().size()>natlmax) 
00246              natlmax = i->second->GetNature().size();
00247     }
00248     OutputDescriptorMapType::const_iterator o;
00249     for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
00250     {
00251            if (o->second->GetName().size()>namelmax) 
00252              namelmax = o->second->GetName().size();
00253            if (o->second->GetHumanTypeName().size()>typelmax) 
00254              typelmax = o->second->GetHumanTypeName().size();
00255            if (o->second->GetNature().size()>natlmax) 
00256              natlmax = o->second->GetNature().size();
00257     }
00258     //
00259     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
00260     {
00261            std::string name(i->second->GetName());
00262            name += "'";
00263            name.append(1+namelmax-name.size(),' ');
00264            std::string type(i->second->GetHumanTypeName());
00265            type += ">";
00266            type.append(1+typelmax-type.size(),' ');
00267            std::string nature(i->second->GetNature());
00268            nature += "]";
00269            nature.append(1+natlmax-nature.size(),' ');
00270            bbtkMessage("help",1,
00271                        "    '"<<name
00272                        <<" <"<<type
00273                        <<" ["<<nature
00274                        <<" : "<<i->second->GetDescription()<<std::endl);
00275     }
00276     if (mOutput.size()) 
00277       bbtkMessage("help",1," * Outputs : "<<std::endl);
00278     else 
00279       bbtkMessage("help",1," * No outputs"<<std::endl);
00280     for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
00281     {
00282            std::string name(o->second->GetName());
00283            name += "'";
00284            name.append(1+namelmax-name.size(),' ');
00285            std::string type(o->second->GetHumanTypeName());
00286            type += ">";
00287            type.append(1+typelmax-type.size(),' ');
00288            std::string nature(o->second->GetNature());
00289            nature += "]";
00290            nature.append(1+natlmax-nature.size(),' ');
00291            bbtkMessage("help",1,
00292                     "    '"<<name
00293                        <<" <"<<type
00294                        <<" ["<<nature
00295                        <<" : "<<o->second->GetDescription()<<std::endl);
00296       }
00297    
00298   
00299   }
00300   //=========================================================================
00301    
00302   //=========================================================================
00304   std::string BlackBoxDescriptor::GetFullTypeName() const
00305   {
00306     if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
00307     return "::" + mTypeName;
00308   }
00309   //=========================================================================
00310  
00311   //=========================================================================
00312   void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s, 
00313                                             int detail, int level,
00314                                             const std::string& output_dir,
00315                                             bool relative_link )
00316   {
00317     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::InsertHtmlHelp()"
00318                       <<std::endl);
00319     
00320     //-------------
00321     // General info 
00322     std::string name = GetTypeName();
00323     Utilities::html_format(name);
00324 
00325     (s) << "<p><hr>\n";
00326     (s) << "<a name=\""<<name<<"\"></a>\n";
00327     (s) << //"Top:&nbsp;
00328       "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
00329     // (s) << "Previous:&nbsp;<a rel="previous" accesskey="p" href="#dir">(dir)</a>,
00330     // (s) << "Up:&nbsp;<a rel="up" accesskey="u" href="#dir">(dir)</a>
00331     (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
00332 
00333 
00334     std::string descr = GetDescription();
00335     //Utilities::html_format(descr);
00336     std::string author = GetAuthor();
00337     Utilities::html_format(author);
00338 
00339     std::vector<std::string> categories;
00340     // Split the category string 
00341     std::string delimiters = ";,";
00342     Utilities::SplitString(GetCategory(),
00343                            delimiters,categories);
00344 
00345     
00346     (s) << "<p><TABLE cellspacing=0  cellpadding=3>\n";
00347     (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
00348       << descr << "</TD></TR>\n";
00349     (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
00350       << author << "</TD></TR>\n";
00351     (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  ";
00352     std::vector<std::string>::iterator ci;
00353     for (ci=categories.begin(); ci!=categories.end(); ++ci)
00354       {
00355         s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci 
00356           << "</a>&nbsp;\n";
00357       }
00358     s << "</TD></TR>\n";      
00359 
00360     (s) << "<TR><TD style='vertical-align: top;'><b> To use it </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  include " 
00361         << GetPackage()->GetName() << "</TD></TR>\n";
00362     (s) << "</TABLE>\n";
00363 
00364     //-------------
00365     // Graph
00366     //i->second->InsertHTMLGraph( &s , detail,level,dir);
00367     
00368     //-------------
00369     // Inputs
00370     std::string titlecol("#BBBBFF");
00371     std::string usercol("#FFFFFF");
00372     std::string ubbcol("#DDFFFF");
00373     std::string wxbbcol("#EEFFFF");
00374 
00375 
00376     //  (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
00377     (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
00378     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
00379       <<"\">Inputs</TD></TR>\n";
00380 
00381     std::vector<std::string> user_defined;
00382     std::vector<std::string> ubb_defined;
00383     std::vector<std::string> wxbb_defined;
00384 
00385     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
00386       GetInputDescriptorMap();
00387     InputDescriptorMapType::const_iterator in;
00388     for ( in = imap.begin();  in != imap.end(); ++in ) 
00389     {
00390       // Skips system-defined inputs
00391       std::string col(usercol);
00392       int iotype = 0;
00393       if (in->second->GetCreatorTypeInfo() == 
00394           typeid(AtomicBlackBoxDescriptor))
00395         {
00396           col = ubbcol; 
00397           iotype = 1;
00398         }
00399 #ifdef USE_WXWIDGETS
00400       else if (in->second->GetCreatorTypeInfo() == 
00401                typeid(WxBlackBoxDescriptor))
00402         {
00403           col = wxbbcol; 
00404           iotype = 2;
00405         }
00406 #endif
00407 
00408       std::string name(in->second->GetName());
00409       Utilities::html_format(name);
00410       
00411       std::string type("<");
00412       type += in->second->GetTypeName();    
00413       type += ">";
00414       Utilities::html_format(type);
00415       
00416       std::string descr(in->second->GetDescription());
00417       //Utilities::html_format(descr);
00418 
00419 /*EED 10/11/2009
00420       std::string out = 
00421         "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
00422         +"\"><B><PRE> "+name+" </PRE></B></TD>"
00423         + "<TD style='vertical-align: top;' bgcolor=\""+col
00424         +"\"><I><PRE> "+type+" </PRE></I></TD>"
00425         + "<TD style='vertical-align: top;' bgcolor=\""+col
00426         +"\">"+descr+"</TD></TR>\n";
00427 */      
00428 
00429       std::string out = 
00430         "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
00431         +"\"><B><PRE> "+name+" </PRE></B></TD>"
00432         + "<TD style='vertical-align: top;' bgcolor=\""+col
00433         +"\"><I><PRE> "+descr+" </PRE></I></TD>"
00434         + "<TD style='vertical-align: top;' bgcolor=\""+col
00435         +"\">"+type+"</TD></TR>\n";
00436 
00437       if (iotype==0) user_defined.push_back(out);
00438       else if (iotype==1) ubb_defined.push_back(out);
00439       else if (iotype==2) wxbb_defined.push_back(out);
00440       
00441     }
00442 
00443     std::vector<std::string>::iterator hi;
00444     for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
00445     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
00446     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
00447 
00448     user_defined.clear();
00449     ubb_defined.clear();
00450     wxbb_defined.clear();
00451 
00452     //-------------
00453     // Outputs
00454     //  (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
00455     //  (s) << "<TABLE border=1 cellspacing=0>\n";
00456     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
00457       <<"\">Outputs</TD></TR>\n";
00458     
00459     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
00460       GetOutputDescriptorMap();
00461     
00462     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
00463     
00464     for ( o = omap.begin();  o != omap.end(); ++o ) 
00465       {
00466         std::string col(usercol);
00467         int iotype = 0;
00468         if (o->second->GetCreatorTypeInfo() == 
00469             typeid(AtomicBlackBoxDescriptor))
00470           {
00471             col = ubbcol; 
00472             iotype = 1;
00473           }
00474 #ifdef USE_WXWIDGETS
00475         else if (o->second->GetCreatorTypeInfo() == 
00476                  typeid(WxBlackBoxDescriptor))
00477           {
00478             col = wxbbcol; 
00479             iotype = 2;
00480           }
00481 #endif
00482         
00483         std::string name(o->second->GetName());
00484         Utilities::html_format(name);
00485         
00486         std::string type("<");
00487         type += o->second->GetTypeName();    
00488         type += ">";
00489         Utilities::html_format(type);
00490         
00491         std::string descr(o->second->GetDescription());
00492         //Utilities::html_format(descr);
00493         
00494 /*EED 10/11/2009
00495         std::string out = 
00496           "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
00497           +"\"><B><PRE> "+name+" </PRE></B></TD>"
00498           + "<TD style='vertical-align: top;' bgcolor=\""+col
00499           +"\"><I><PRE> "+type+" </PRE></I></TD>"
00500           + "<TD style='vertical-align: top;' bgcolor=\""+col
00501           +"\">"+descr+"</TD></TR>\n";
00502 */
00503         std::string out = 
00504           "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
00505           +"\"><B><PRE> "+name+" </PRE></B></TD>"
00506           + "<TD style='vertical-align: top;' bgcolor=\""+col
00507           +"\"><I><PRE> "+descr+" </PRE></I></TD>"
00508           + "<TD style='vertical-align: top;' bgcolor=\""+col
00509           +"\">"+type+"</TD></TR>\n";
00510 
00511         if (iotype==0) user_defined.push_back(out);
00512         else if (iotype==1) ubb_defined.push_back(out);
00513         else if (iotype==2) wxbb_defined.push_back(out);
00514         
00515       }
00516     
00517     for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
00518     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
00519     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
00520 
00521     (s) << "</TABLE>\n";
00522     
00523     //------------
00524     // End
00525 
00526 
00527    }
00528   //=========================================================================
00529  
00530 }

Generated on Thu May 31 14:12:03 2012 for BBTK by  doxygen 1.5.7.1