bbtkFactory.cxx

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkFactory.cxx,v $
00004   Language:  C++
00005   Date:      $Date: 2012/05/30 17:17:00 $
00006   Version:   $Revision: 1.52 $
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 
00037 #include "bbtkFactory.h"
00038 #include "bbtkMessageManager.h"
00039 #include "bbtkConnection.h"
00040 #include "bbtkConfigurationFile.h"
00041 #include "bbtkUtilities.h"
00042 #include "bbtkConfigurationFile.h"
00043 
00044 #include <sys/stat.h> // for struct stat stFileInfo
00045 
00046 #if defined(_WIN32)
00047 #include <direct.h> // for getcwd
00048 #endif
00049 
00050 #include <cctype>    // std::toupper
00051 
00052 #include <time.h>
00053 
00054 namespace bbtk
00055 {
00056 
00057   //===================================================================
00059   Factory::Pointer Factory::New()
00060   {
00061     bbtkDebugMessage("kernel",9,"Factory::New()"<<std::endl);
00062     return MakePointer(new Factory());
00063   }
00064   //===================================================================
00065 
00066   //===================================================================
00068   Factory::Factory()
00069     : mExecuter()
00070   {
00071     bbtkDebugMessage("kernel",7,"Factory()"<<std::endl);
00072   }
00073   //===================================================================
00074 
00075   //===================================================================
00077   Factory::~Factory()
00078   {
00079     bbtkDebugMessage("kernel",7,"==> ~Factory()"<<std::endl);
00080     CloseAllPackages();
00081     bbtkDebugMessage("kernel",7,"<== ~Factory()"<<std::endl);
00082   }
00083   //===================================================================
00084 
00085 
00086 
00087   //===================================================================
00088   void Factory::Reset()
00089   {
00090     bbtkDebugMessage("kernel",7,"==> Factory::Reset()"<<std::endl);
00091     CloseAllPackages();
00092     bbtkDebugMessage("kernel",7,"<== Factory::Reset()"<<std::endl);
00093   }
00094   //===================================================================
00095 
00096 
00097   // ===================================================================
00098   bool Factory::DoLoadPackage(std::string libname,
00099                               std::string pkgname,
00100                               std::string path)
00101   {
00102     
00103     Package::Pointer p = Package::CreateFromDynamicLibrary(libname,
00104                                                            pkgname,
00105                                                            path);
00106     if (p!=0)
00107       {
00108         //===================================================================
00109         bbtkMessage("output",2,p->GetName()<<" "
00110                     <<p->GetVersion()
00111                     <<" "
00112                     <<p->GetAuthor() << " Category(s) :"
00113                     <<p->GetCategory()
00114                     <<std::endl);
00115         bbtkMessage("output",2,p->GetDescription()<<std::endl);
00116         //===================================================================
00117         p->AddFactory(GetThisPointer<Factory>());
00118 printf("EED Factory::DoLoadPackage > %s %s  %s \n",libname.c_str(), pkgname.c_str(),path.c_str());                
00119         mPackageMap[pkgname] = p;
00120         return true;
00121       }
00122     return false;
00123     
00124   }
00125   
00126   //===================================================================
00136   
00139   
00140 
00141   
00142   void Factory::LoadPackage( const std::string& name )
00143   {
00144           
00145           
00146   // Note : in the following :
00147   // name : the user supplied name
00148   //      - abreviated name    e.g.       pkg   pkg.so   libbpkg   libbbpkg.so
00149   //      - relative full name e.g.       ./libbbpkg.so   ../../libbbpkg.so
00150   //      - absolute full name e.g.       /home/usrname/proj/lib/libbbpkg.so
00151   //          same for Windows, with      c:, d: ...
00152   //
00153   // lastname : string before the last / (if any), or user supplied name
00154           
00155          
00156           
00157         if(name != ""){
00158                 bbtkDebugMessageInc("kernel",7,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
00159                 bbtkMessage("debug",1,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
00160 
00161                 std::vector<std::string> package_paths;
00162                 std::string libname;  // full path library name
00163                 std::string pkgname;  // e.g. libbb<pkgname>.so
00164 
00165                 std::string upath;
00166 
00167                 
00168                 pkgname = Utilities::ExtractPackageName(name,upath);
00169                 
00170                 printf("EED Factory::LoadPackage A. %s\n", name.c_str());
00171                 printf("EED Factory::LoadPackage A. %s\n", upath.c_str());
00172                 printf("EED Factory::LoadPackage A. %s\n", pkgname.c_str());
00173                 
00174                 bbtkMessage("debug",1,"Package name ["<<pkgname<<"]"<<std::endl);
00175                 bbtkMessage("debug",1,"Package path ["<<upath<<"]"<<std::endl);
00176 
00177                 // no loading package if already loaded
00178                 PackageMapType::iterator iUnload;
00179                 iUnload = mPackageMap.find(pkgname);
00180                 
00181                 PackageMapType::iterator it;
00182 std::cout << "EED Factory::LoadPackage PackageMap ";
00183                 for ( it=mPackageMap.begin() ; it != mPackageMap.end(); it++ )
00184                         std::cout << (*it).first << " ";
00185                 std::cout << std::endl;
00186 
00187                 if (iUnload != mPackageMap.end())
00188                 {
00189                   bbtkMessage("output",2,"["<< pkgname <<"] already loaded" << std::endl);
00190                   return;
00191                 }
00192 
00193         // =================================================
00194         // The following structure was checked to work
00195         // with any type of relative/absolute path.
00196         // Please don't modify it without checking
00197         // *all* the cases. JP
00198         //==================================================
00199 
00200                 printf("\nEED Factory::LoadPackage B. %s\n\n", name.c_str());
00201         //std::cout << "upath [" << name << "]" << std::endl;
00202 
00203                 bool ok = false;
00204                 bool foundFile = false;
00205 
00206                 // If path provided by user will be the first scanned :
00207                 // push it into vector of paths
00208                 if (upath.length()>0)   // ------------------------------------- check user supplied location
00209                 {
00210                    if (name[0] != '.' && name[0] != '/' && name[1]!= ':')
00211                    {
00212                           bbtkError("Use absolute or relative path name! ["<<name<<"] is an illegal name");
00213                           return;
00214                    }
00215 
00216                   // std::string path = Utilities::ExpandLibName(upath, false);
00217 
00218                    std::string path = Utilities::ExpandLibName(name,false); // keep last item, here.
00219                    if (path != "")
00220                    {
00221                           std::string p2;
00222                           Utilities::ExtractPackageName(path,p2);
00223                           //libname = Utilities::MakeLibnameFromPath(path, pkgname);
00224                           libname = Utilities::MakeLibnameFromPath(p2, pkgname); // remove last item
00225                           // Check if library exists
00226                           if ( !Utilities::FileExists(libname) )
00227                           {
00228                           // The following is *NOT* a debug time message :
00229                           // It's a user intended message.
00230                           // Please don't remove it.
00231                         bbtkMessage("output",3,"   [" <<libname 
00232                                 <<"] : doesn't exist" <<std::endl);
00233                           }
00234                           else
00235                           {
00236                                  ok = DoLoadPackage( libname, pkgname, path);         
00237                           }
00238                    }
00239                    else
00240                    {
00241                           bbtkError("Path ["<<upath<<"] doesn't exist");
00242                           return;
00243                    }
00244                 }
00245                 else     // ----------------------------------------------------- iterate on the paths  
00246                 {
00247 
00248                 std::string path = ".";
00249                 package_paths = ConfigurationFile::GetInstance().Get_package_paths();
00250                 std::vector<std::string>::iterator i;
00251                 for (i=package_paths.begin();i!=package_paths.end();++i)
00252                 {
00253                         foundFile = false;
00254                         path = *i;
00255 //std::cout<<"JCP bbtkFactory.cxx void Factory::LoadPackage = path "<<path<<std::endl;
00256                         // we *really* want '.' to be the current working directory
00257                         if (path == ".")
00258                         {
00259                           char buf[2048]; // for getcwd
00260                           char * currentDir = getcwd(buf, 2048);
00261                           std::string cwd(currentDir);
00262 //std::cout<<"JCP  bbtkFactory.cxx void Factory::LoadPackage = currentDir "<<currentDir<<std::endl;
00263                           path = currentDir;
00264                         }
00265 //std::cout<<"JCP bbtkFactory.cxx void Factory::LoadPackage = path "<<path<<" pkgnam="<<pkgname<<std::endl;              
00266                         libname = Utilities::MakeLibnameFromPath(path, pkgname);
00267 
00268                         bbtkMessage("debug",2,"-> Trying to load [" << libname << "]" <<std::endl);
00269 
00270                   // Check if library exists           
00271                         if ( !Utilities::FileExists(libname) )
00272                         {
00273                         // The following is *NOT* a debug time message :
00274                         // It's a user intended message.
00275                         // Please don't remove it.
00276                           bbtkMessage("output",3,
00277                                   "   [" <<libname <<"] : doesn't exist" <<std::endl);
00278                            continue;  // try next path
00279                         }
00280                         foundFile = true; 
00281 
00282                   // Try to Load the library
00283 
00284                         ok = DoLoadPackage( libname, pkgname, path);
00285                         if (ok)
00286                         {
00287                            bbtkMessage("debug",2,"   OK"<<std::endl);
00288                         }
00289                 break; // we stop iterating even if error : have to signal it to user
00290                 } //------------------ // end for ( package_paths.begin();i!=package_paths.end() )
00291 
00292         }
00293 
00294                 if( !ok )  // nothing was loaded
00295                 {
00296                   if (!foundFile)
00297                   {
00298                         bbtkError("Could not find package ["<<pkgname<< "]");
00299                   }
00300                   else
00301                   {
00302         #if defined(__GNUC__)
00303                         bbtkError("Could not load package ["<< pkgname
00304                                           <<"] :" << std::endl 
00305                           << "  Opening "<<libname<<" failed"
00306                           << "  Reason: "<< dlerror());
00307         #elif defined(_WIN32)
00308                         bbtkError("Could not load package ["<<pkgname
00309                           <<"] :"<< std::endl << "   Error loading " <<libname);
00310 
00311                 // look how to get the error message on win
00312                 //<<dlerror());
00313                 // it is the bordel !! (the bloody fucking bordel, you mean?)
00314                 // look : http://msdn2.microsoft.com/en-us/library/ms680582.aspx
00315         #endif
00316                   }
00317                 }       
00318                 bbtkMessage("output",2,"[" << libname << "] loaded" << std::endl);
00319         }
00320 
00321     
00322 
00323   }
00324 
00325   //===================================================================
00330  void Factory::UnLoadPackage( const std::string& name )
00331  {
00332     bbtkDebugMessageInc("kernel",7,"Factory::UnLoadPackage(\""
00333                        <<name<<"\")"<<std::endl);
00334   
00335     PackageMapType::iterator i;
00336     i = mPackageMap.find(name);
00337     if (i == mPackageMap.end()) 
00338     {
00339       bbtkError("cannot unload package \""<<name
00340                 <<"\" : package not loaded !");
00341     }
00342     ClosePackage(i);
00343     bbtkDebugDecTab("kernel",7);
00344   }
00345   //===================================================================
00346 
00347 
00348   //===================================================================
00349   void Factory::CloseAllPackages()
00350   {
00351     bbtkDebugMessageInc("kernel",7,"Factory::CloseAllPackages()"<<std::endl);
00352     
00353     std::vector< Package::WeakPointer > mAlive;  
00354     do {
00355       mAlive.clear();  
00356       while (mPackageMap.begin() != mPackageMap.end())
00357         {
00358           PackageMapType::iterator i = mPackageMap.begin();
00359           Package::WeakPointer p = i->second;
00360           ClosePackage(i);
00361           if (p.lock()) mAlive.push_back(p);
00362         }
00363       std::vector< Package::WeakPointer >::iterator i;
00364       for (i=mAlive.begin();i!=mAlive.end();++i)
00365         {
00366           // If not dead : reinsert
00367           if (i->lock())
00368             {
00369               bbtkDebugMessage("kernel",7,"Package "<<i->lock()->GetName()
00370                                <<" still alive"<<std::endl);
00371               // InsertPackage(i->lock());
00372             }
00373         }      
00374     }
00375     while (mPackageMap.size()>0);
00376 
00377     bbtkDebugDecTab("kernel",7);
00378   }
00379   //===================================================================
00380 
00381   //===================================================================
00393  void Factory::ClosePackage(PackageMapType::iterator& i) 
00394   {   
00395      bbtkDebugMessageInc("kernel",7,"Factory::ClosePackage(\""
00396                          <<i->second->GetName()
00397                         <<"\")"<<std::endl);
00398 
00399      
00400      // Removes this from the set of factories which use the package
00401      i->second->RemoveFactory(GetThisPointer<Factory>());
00402      Package::WeakPointer p = i->second;
00403      // remove the entry in the map
00404      mPackageMap.erase(i);
00405      // Release the package if not already destroyed
00406      if (p.lock()) 
00407        {
00408          Package::Release(p);
00409        }
00410      bbtkDebugDecTab("kernel",7);
00411   }
00412   //===================================================================
00413   
00414 
00415 
00416   //===================================================================  
00417   void Factory::PrintHelpListPackages(bool details, bool adaptors) const
00418   {
00419     bbtkDebugMessageInc("kernel",9,"Factory::PrintPackages"<<std::endl);
00420 
00421     PackageMapType::const_iterator i;
00422     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00423     {
00424       bbtkMessage("help",1, i->first << std::endl);
00425       if (details) {
00426          i->second->PrintHelpListDescriptors(false,adaptors);
00427       }
00428     }
00429 
00430     bbtkDebugDecTab("kernel",9);
00431   }
00432   //===================================================================
00433 
00434   //===================================================================  
00436   void Factory::PrintHelpPackage(const std::string& name, bool adaptors) const
00437   {
00438     bbtkDebugMessageInc("kernel",9,"Factory::PrintHelpPackage(\""
00439                         <<name<<"\")"
00440                         <<std::endl);
00441 
00442     PackageMapType::const_iterator i = mPackageMap.find(name);
00443     if ( i != mPackageMap.end() ) 
00444       {
00445       bbtkMessage("help",1, "Package "<<i->first<<" ");
00446       
00447       if (i->second->GetVersion().length()>0)
00448         bbtkMessageCont("help",1,"v" <<i->second->GetVersion());
00449         
00450       if (i->second->GetAuthor().length()>0)
00451         bbtkMessageCont("help",1,"- "<<i->second->GetAuthor());
00452         
00453       if (i->second->GetCategory().length()>0)
00454         bbtkMessageCont("help",1,"- "<<i->second->GetCategory());        
00455         
00456       bbtkMessageCont("help",1,std::endl);
00457       bbtkIncTab("help",1);
00458       bbtkMessage("help",1,i->second->GetDescription()<<std::endl);
00459       if (i->second->GetNumberOfDescriptors()>0) 
00460         {
00461           bbtkMessage("help",1, "Black boxes : "<<std::endl);
00462           i->second->PrintHelpListDescriptors(true,adaptors);
00463         }
00464       else 
00465         {
00466           bbtkMessage("help",1, "No black boxes"<<std::endl);
00467         }
00468       bbtkDecTab("help",1);
00469       }
00470     else 
00471       {
00472       bbtkDebugDecTab("kernel",9);
00473       bbtkError("package \""<<name<<"\" unknown");
00474       }
00475     
00476     bbtkDebugDecTab("kernel",9);
00477   }
00478   //===================================================================
00479 
00480   //===================================================================
00483   void Factory::PrintHelpDescriptor(const std::string& name, 
00484                              std::string& package,
00485                              bool full) const
00486   {
00487     bbtkDebugMessageInc("kernel",9,"Factory::PrintHelpDescriptor(\""
00488                         <<name<<"\")"
00489                         <<std::endl);
00490 
00491     bool found = false;
00492     PackageMapType::const_iterator i;
00493     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00494       {
00495       if (i->second->ContainsDescriptor(name)) 
00496         {
00497           i->second->PrintHelpDescriptor(name,full);
00498               package = i->second->GetName();
00499           found = true;
00500         }
00501       }
00502     
00503     bbtkDebugDecTab("kernel",9);
00504     if (!found) 
00505       {
00506       bbtkError("No package of the factory contains any black box <"
00507                  <<name<<">");
00508       }
00509   }  
00510   //===================================================================
00511 
00512 
00513   //=================================================================== 
00515   void Factory::InsertPackage( Package::Pointer p )
00516   {
00517     bbtkDebugMessageInc("kernel",9,"Factory::InsertPackage(\""<<
00518                         p->GetName()<<"\")"<<std::endl);
00519 
00520     p->AddFactory(GetThisPointer<Factory>());
00521           
00522 printf("EED Factory::InsertPackage %s\n", p->GetName().c_str() );         
00523           
00524     mPackageMap[p->GetName()] = p;
00525 
00526     bbtkDebugDecTab("kernel",9);
00527   }
00528   //===================================================================
00529   
00530   //=================================================================== 
00532   void Factory::RemovePackage( Package::Pointer p )
00533   {
00534     bbtkDebugMessageInc("kernel",9,"Factory::RemovePackage(\""<<
00535                         p->GetName()<<"\")"<<std::endl);
00536 
00537     PackageMapType::iterator i;
00538     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00539       {
00540          if (i->second == p) break;
00541       };
00542     
00543     if (i!=mPackageMap.end())
00544       {
00545         ClosePackage(i);
00546       }
00547     else 
00548       {
00549         bbtkError("Factory::RemovePackage(\""<<
00550                   p->GetName()<<"\") : package absent from factory");
00551       }
00552 
00553     bbtkDebugDecTab("kernel",9);
00554   }
00555   //===================================================================
00556   
00557 
00558 //      ups3 EED borrame        
00559         std::string Factory::GetPackageNameOfaBlackBox(std::string boxType)
00560         {
00561                 std::string result="<void bbtk package name>";
00562                 PackageMapType::const_iterator i;
00563                 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00564                 {
00565                         if ( i->second->ifBoxExist( boxType ) == true)
00566                         {
00567                                 result = i->first;
00568                         }
00569                 }
00570                 return result;
00571         }
00572         
00573         
00574   //===================================================================
00576   BlackBox::Pointer Factory::NewBlackBox(const std::string& type, 
00577                                  const std::string& name) const
00578   {
00579     bbtkDebugMessageInc("kernel",7,"Factory::NewBlackBox(\""
00580                         <<type<<"\",\""<<name<<"\")"<<std::endl);
00581 
00582     BlackBox::Pointer b; 
00583     PackageMapType::const_iterator i;
00584     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00585       {
00586       b = i->second->NewBlackBox(type,name);
00587       if (b) break; 
00588       }
00589     if (!b) 
00590       {
00591        bbtkError("black box type \""<<type<<"\" unknown");
00592       } 
00593 
00594     bbtkDebugDecTab("kernel",7);
00595     return b;
00596   }
00597   //===================================================================
00598 
00599   //===================================================================
00601   BlackBox::Pointer Factory::NewAdaptor(const DataInfo& typein,
00602                                 const DataInfo& typeout,
00603                                 const std::string& name) const
00604   {
00605     bbtkDebugMessageInc("kernel",8,"Factory::NewAdaptor("
00606                         <<typein<<","
00607                         <<typeout<<",\""
00608                         <<name<<"\")"<<bbtkendl);
00609 
00610 
00611     BlackBox::Pointer b; 
00612     PackageMapType::const_iterator i;
00613     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00614       {
00615       b = i->second->NewAdaptor(typein,typeout,name);
00616       if (b) break; 
00617       }
00618     if (!b) 
00619       {
00620         bbtkError("no "<<typein<<" to "<<typeout
00621                   <<" adaptor available");
00622       } 
00623     
00624     bbtkDebugDecTab("kernel",7);
00625     return b; 
00626   }
00627   //===================================================================
00628 
00629 
00630   //===================================================================
00632   BlackBox::Pointer Factory::NewWidgetAdaptor(const DataInfo& typein,
00633                                       const DataInfo& typeout,
00634                                       const std::string& name) const
00635   {
00636     bbtkDebugMessageInc("kernel",8,"Factory::NewWidgetAdaptor(<"
00637                         <<typein<<">,<"
00638                         <<typeout<<">,\""
00639                         <<name<<"\")"<<bbtkendl);
00640 
00641 
00642     BlackBox::Pointer b; 
00643     PackageMapType::const_iterator i;
00644     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00645       {
00646         b = i->second->NewWidgetAdaptor(typein,
00647                                                  typeout,
00648                                                  name);
00649       if (b) break; 
00650       }
00651     if (!b) 
00652       {
00653         bbtkError("no "<<typein<<" to "<<typeout
00654                   <<"> widget adaptor available");
00655       } 
00656     
00657     bbtkDebugDecTab("kernel",7);
00658     return b; 
00659   }
00660   //===================================================================
00661 
00662   //===================================================================
00664   bool Factory::FindAdaptor(const DataInfo& typein,
00665                                   const DataInfo& typeout,
00666                                   std::string& adaptor) const
00667   {
00668     bbtkDebugMessageInc("kernel",8,"Factory::FindAdaptor(<"
00669                         <<typein<<">,<"
00670                         <<typeout<<">)"<<bbtkendl);
00671     
00672     bool b = false;
00673     PackageMapType::const_iterator i;
00674     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00675       {
00676         b = i->second->FindAdaptor(typein,
00677                                             typeout,
00678                                             adaptor);
00679         if (b) break; 
00680       }
00681     /*
00682     if (!b) 
00683       {
00684         bbtkError("no "<<typein<<" to "<<typeout
00685                   <<"> widget adaptor available");
00686       } 
00687     */
00688 
00689     bbtkDebugDecTab("kernel",7);
00690     return b; 
00691   }
00692   //===================================================================
00693 
00694   //===================================================================
00696   bool Factory::FindWidgetAdaptor(const DataInfo& typein,
00697                                   const DataInfo& typeout,
00698                                   std::string& adaptor) const
00699   {
00700     bbtkDebugMessageInc("kernel",8,"Factory::FindWidgetAdaptor(<"
00701                         <<typein<<">,<"
00702                         <<typeout<<">)"<<bbtkendl);
00703     
00704     bool b = false;
00705     PackageMapType::const_iterator i;
00706     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00707       {
00708         b = i->second->FindWidgetAdaptor(typein,
00709                                                   typeout,
00710                                                   adaptor);
00711         if (b) break; 
00712       }
00713     bbtkDebugDecTab("kernel",7);
00714     return b; 
00715   }
00716   //===================================================================
00717 
00718   //===================================================================
00720   bool Factory::FindWidgetAdaptor2(const DataInfo& typein,
00721                                   const DataInfo& typeout,
00722                                   std::string& widget,
00723                                   std::string& adaptor) const
00724   {
00725     bbtkDebugMessageInc("kernel",8,"Factory::FindWidgetAdaptor(<"
00726                         <<typein<<">,<"
00727                         <<typeout<<">)"<<bbtkendl);
00728     
00729     bool b = false;
00730     adaptor = widget = "";
00731     PackageMapType::const_iterator i;
00732     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00733       {
00734         b = i->second->FindWidgetAdaptor(typein,
00735                                                   typeout,
00736                                                   widget);
00737         if (b) break; 
00738       }
00739     if (!b) 
00740       {
00741         // Look for a widget adaptor with good nature out
00742         bbtkMessage("kernel",5,
00743                     "*** Looking for a two pieces widget adaptor for : "
00744                     << typein << "->"<<typeout<<std::endl);
00745         for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00746           {
00747             Package::AdaptorMapType::const_iterator j;
00748             for (j=i->second->GetAdaptorMap().begin();
00749                  j!=i->second->GetAdaptorMap().end();
00750                  ++j)
00751               {
00752                 if ( ( j->first.mKind ==  
00753                        BlackBoxDescriptor::DEFAULT_GUI) &&
00754                      //(j->first.mTypeIn == typein) &&
00755                      (j->first.mTypeOut.GetNature() == typeout.GetNature() ) 
00756                      )
00757                   {
00758                     widget = j->second.lock()->GetTypeName();
00759                     bbtkMessage("kernel",5,
00760                                 "===> Found first part : "<<widget
00761                                 << " "<<j->first.mTypeIn<<"->"
00762                                 <<j->first.mTypeOut<<std::endl);
00763                     DataInfo ti( j->first.mTypeOut.GetType(), "");
00764                     DataInfo to( typeout.GetType(), "");
00765                     b = FindAdaptor( ti, to, adaptor );
00766                     if (b) 
00767                       {
00768                         bbtkMessage("kernel",5,
00769                                     "===> Found second part : "<<adaptor
00770                                     <<std::endl);
00771                         break;
00772                       }
00773                     else
00774                       {
00775                         bbtkMessage("kernel",5,
00776                                     "===> No second part found"<<std::endl);
00777                       }
00778                   }
00779               }
00780             if (b) break;
00781           }
00782       }
00783     bbtkDebugDecTab("kernel",7);
00784     return b; 
00785   }
00786   //===================================================================
00787 
00788   //===================================================================
00790   Connection::Pointer Factory::NewConnection(BlackBox::Pointer from,
00791                                              const std::string& output,
00792                                              BlackBox::Pointer to,
00793                                              const std::string& input) const
00794   {
00795     bbtkDebugMessage("kernel",7,"Factory::NewConnection(\""
00796                       <<from->bbGetName()<<"\",\""<<output<<"\",\""
00797                       <<to->bbGetName()<<"\",\""<<input
00798                       <<"\")"<<std::endl);
00799     
00800     return Connection::New(from,output,to,input,
00801                            GetThisPointer<Factory>());
00802   }
00803   //===================================================================
00804 
00805 
00806 
00807   //===================================================================
00808   Package::Pointer Factory::GetPackage(const std::string& name) const
00809   {
00810     bbtkDebugMessageInc("kernel",9,"Factory::GetPackage(\""<<name<<"\")"
00811                          <<std::endl);
00812 
00813     PackageMapType::const_iterator i = mPackageMap.find(name);
00814     if ( i != mPackageMap.end() ) 
00815     {
00816       bbtkDebugDecTab("kernel",9); 
00817       return i->second;
00818     }
00819     else 
00820     {
00821        bbtkDebugDecTab("kernel",9);
00822        bbtkError("package \""<<name<<"\" unknown");
00823     }
00824     
00825     bbtkDebugDecTab("kernel",9);  
00826   }
00827   //===================================================================
00828   
00829 
00830   //===================================================================
00831   void Factory::Check() const
00832   {
00833     bbtkMessage("debug",1,"****** Checking Factory "<<(void*)this
00834                  <<std::endl);
00835     PackageMapType::const_iterator i;
00836     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00837       {
00838         i->second->Check();
00839       }
00840     bbtkMessage("debug",1,"****** Checking Factory "<<(void*)this
00841                 <<" ... OK"<<std::endl);
00842   }
00843   //===================================================================
00844 
00845   //===================================================================
00846   void Factory::WriteDotFilePackagesList(FILE *ff)
00847   {
00848 
00849     bbtkDebugMessageInc("kernel",9,"Factory::WriteDotFilePackagesList()"
00850                          <<std::endl);
00851 
00852     fprintf( ff , "\n");
00853     fprintf( ff , "subgraph cluster_FACTORY {\n");
00854     fprintf( ff , "  label = \"PACKAGES\"%s\n",  ";");
00855     fprintf( ff , "  style=filled%s\n",";");
00856     fprintf( ff , "  color=lightgrey%s\n",";");
00857     fprintf( ff , "  rankdir=TB%s\n",";");
00858 
00859     std::string url;
00860     PackageMapType::const_iterator i;
00861     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00862     {
00863        url=GetPackage(i->first)->GetDocURL();
00864        fprintf(ff,"  %s [shape=ellipse, URL=\"%s\"]%s\n",i->first.c_str(),url.c_str(),";" );
00865     }
00866     fprintf( ff , "}\n\n");
00867     bbtkDebugDecTab("kernel",9);
00868   }
00869   //===================================================================
00870 
00871 
00872  void Factory::ShowGraphTypes(const std::string& name) const
00873  {
00874    bool found = false;
00875    PackageMapType::const_iterator i;
00876    for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00877    {
00878      if (i->second->ContainsDescriptor(name)) 
00879        {
00880          std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
00881          
00882          // Don't pollute the file store with  "temp_dir" directories ...    
00883          std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
00884          std::string directory = "\"" + default_doc_dir + separator + "temp_dir"  +separator + "\"";
00885          std::string filename2 =  default_doc_dir + separator + "temp_dir" + separator + "tmp.html"; 
00886          
00887 #if defined(_WIN32)  
00888          std::string command("start \"Titre\" /D ");
00889 #else 
00890          std::string command("gnome-open ");
00891 #endif
00892          command=command + directory +" tmp.html";
00893          FILE *ff;
00894          ff=fopen(filename2.c_str(),"w");
00895          
00896          fprintf(ff,"<html><head><title>TMP</title> <script type=\"text/javascript\"> <!--\n");
00897          fprintf(ff,"  window.location=\"%s#%s\";\n" , i->second->GetDocURL().c_str(),name.c_str() );
00898          fprintf(ff,"//--></script></head><body></body></html>\n");
00899          
00900          
00901          //fprintf(ff, "<a  href=\"%s#%s\">Link</a>\n", i->second->GetDocURL().c_str(),name.c_str() );
00902          fclose(ff);
00903          system( command.c_str() );      
00904          found = true;
00905        }
00906    }
00907    
00908    bbtkDebugDecTab("kernel",9);
00909    if (!found) 
00910    {
00911      bbtkError("No package of the factory contains any black box <"
00912                <<name<<">");
00913    }
00914  }
00915     
00916 
00917 
00918 
00919   void Factory::CreateHtmlIndex(IndexEntryType type, 
00920                                 const std::string& filename)
00921   {
00922     bbtkDebugMessageInc("kernel",9,"Factory::CreateHtmlIndex(\""
00923                         <<filename<<"\")"<<bbtkendl);
00924     
00925     std::string title;
00926 
00927     typedef std::map<std::string, 
00928                      std::vector<BlackBoxDescriptor::Pointer> > IndexType;
00929     IndexType index;
00930     // Builds the index map
00931     PackageMapType::const_iterator i;
00932     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00933         {
00934                 Package::Pointer pack = i->second;
00935                 if (pack->GetName()=="user") continue;
00936                 Package::DescriptorMapType::const_iterator j;
00937                 for (j = pack->GetDescriptorMap().begin(); 
00938                         j!= pack->GetDescriptorMap().end(); 
00939                         ++j)
00940                 {
00941             
00942             // Skip adaptors 
00943                         if ( type==Adaptors )
00944                         {  
00945                                 if (j->second->GetKind() == BlackBoxDescriptor::STANDARD )
00946                                 continue;
00947                         }
00948                         else 
00949                                 if (j->second->GetKind() != BlackBoxDescriptor::STANDARD )
00950                                 continue;
00951 
00952                         std::vector<std::string> keys;
00953                         if (type==Packages)
00954                         {
00955                                 std::string k("");
00956                                 k += pack->GetName();
00957                                 keys.push_back(k);
00958                                 title = "Boxes by package";
00959                         }
00960                         else if ((type==Initials) || (type==Adaptors))
00961                         {
00962                                 std::string init(" ");
00963                                 init[0] =  std::toupper(j->second->GetTypeName()[0]);
00964                                 keys.push_back(init);
00965                                 title = "Alphabetical list";
00966                         }
00967                         else if (type==Categories)
00968                         {
00969                         // Split the category string 
00970                         std::string delimiters = ";,";
00971                         Utilities::SplitString(j->second->GetCategory(),
00972                                            delimiters,keys);
00973                         if (keys.size()==0) 
00974                                 keys.push_back(" NONE");
00975                                 title = "Boxes by category";
00976                         }
00977 
00978                         std::vector<std::string>::const_iterator k;
00979                         for (k=keys.begin(); k!=keys.end(); ++k )
00980                         {
00981                                 IndexType::iterator p;
00982                                 p = index.find(*k);
00983                                 if (p != index.end()) 
00984                                 {
00985                                         p->second.push_back(j->second);
00986                                 }
00987                                 else 
00988                                 {
00989                                         std::vector<BlackBoxDescriptor::Pointer> v;
00990                                         v.push_back(j->second);
00991                                         index[*k] = v;
00992                                 }
00993                         }
00994                 }
00995         }   
00996     // Creates the file 
00997     //---------------------
00998     // Open output file
00999     std::ofstream s;
01000     s.open(filename.c_str());
01001     if (!s.good()) 
01002     {
01003        bbtkError("Factory::CreateHtmlIndex : could not open file '"
01004                  <<filename<<"'");
01005     }
01006     
01007     //----------------------
01008     // Html head
01009     s << "<html lang=\"en\">\n";
01010     s << "<head>\n";
01011     s << "<title>"<<title<<"</title>\n";
01012     s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
01013     s << "<meta name=\"description\" content=\""<<title<<"\">\n";
01014     s << "<meta name=\"generator\" content=\"\">\n";
01015     s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
01016     //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
01017     s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
01018     s << "pre.display { font-family:inherit }\n";
01019     s << "pre.format  { font-family:inherit }\n";
01020     s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
01021     s << "pre.smallformat  { font-family:inherit; font-size:smaller }\n";
01022     s << "pre.smallexample { font-size:smaller }\n";
01023     s << "pre.smalllisp    { font-size:smaller }\n";
01024     s << "span.sc    { font-variant:small-caps }\n";
01025     s << "span.roman { font-family:serif; font-weight:normal; } \n";
01026     s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n"; 
01027     s << "--></style>\n";
01028     s << "</head>\n";
01029     //----------------------
01030 
01031     //----------------------
01032     // Html body
01033     s << "<body>\n";
01034     s << "<a name=\"Top\"></a>\n"; 
01035     s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
01036     s << "<p>\n";
01037     IndexType::iterator ii;
01038     for (ii=index.begin();ii!=index.end();++ii)
01039       {
01040                 s << "<a href=\"#"<<ii->first<<"\">"<<ii->first<<"</a>&nbsp;&nbsp;";    
01041       }
01042 
01043     for (ii=index.begin();ii!=index.end();++ii)
01044       {
01045         s << "<p><hr>\n";
01046         s << "<p><a href=\"#Top\">Top</a>";
01047         if (type==Packages)
01048           {
01049             s << "<a name=\""<<ii->first<<"\"></a>\n"; 
01050             s << "<p><a href=\""<<ii->first<<"/index.html\">"
01051               << ii->first<<"</a>\n"; 
01052 
01053             s << "&nbsp;&nbsp;-&nbsp;&nbsp;\n"; 
01054 
01055             s << "<a name=\"doxygen\"></a>\n"; 
01056 
01057 //EED 26Mars2009
01058         /*JCP 19 Nov 2009
01059                 std::string bin_path = bbtk::ConfigurationFile::GetInstance().Get_bin_path();
01060             s << "<a href=" << bin_path <<"/../share/bbtk/doc/doxygen/" << ii->first << "/main.html>(Doxygen documentation of the source)</a>\n"; 
01061         JCP 19 Nov 2009*/
01062                 std::string bin_path = bbtk::ConfigurationFile::GetInstance().Get_bin_path();           
01063             s << "<a href=" << bin_path <<"/../share/bbtk/doc/doxygen/" << ii->first << "/main.html>(Doxygen documentation of the source)</a>\n"; 
01064           }
01065         else 
01066           {
01067             s << "<a name=\""<<ii->first<<"\"></a>\n"; 
01068             s << "<p><b>"<<ii->first<<"</b>\n";
01069           }
01070         s << "<ul>\n";
01071 
01072         s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
01073 
01074         std::vector<BlackBoxDescriptor::Pointer>::iterator di;
01075         for (di=ii->second.begin();di!=ii->second.end();++di)
01076           {
01077             std::string pack = (*di)->GetPackage()->GetName();
01078             std::string name = (*di)->GetTypeName();
01079             Utilities::html_format(name);
01080             std::string descr = (*di)->GetDescription();
01081             Utilities::html_format(descr);
01082             s << "<TR>";
01083             s << "<TD style='vertical-align: top;'>";
01084             s << "&nbsp;&nbsp;&nbsp;<a href=\""<<pack
01085               <<"/index.html#"<<name<<"\">"
01086               <<pack<<"::"<<name<<"</a>";
01087             s << "</TD> ";
01088             s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
01089             s << "</TR>\n";
01090           }    
01091         s << "</TABLE>\n";
01092         s << "</ul>\n";
01093         s << "</div>\n";
01094       }
01095     //----------------------
01096     // Footer 
01097     time_t rawtime;
01098     tm * ptm;
01099     time ( &rawtime );
01100     ptm = gmtime ( &rawtime );
01101 
01102     s << "<p><hr>\n";
01103     s << "Automatically generated by <b>bbtk</b> on "
01104       << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900 
01105       << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
01106     s << "</body></html>\n"; 
01107     s.close();
01108     //----------------------
01109 
01110     // End
01111     bbtkDebugDecTab("kernel",9);
01112   }
01113 
01114  //==========================================================================
01115   std::string Factory::GetObjectName() const
01116   {
01117     return std::string("Factory");
01118   }
01119   //==========================================================================
01120   
01121   //==========================================================================
01122   std::string  Factory::GetObjectInfo() const 
01123   {
01124     std::stringstream i;
01125     return i.str();
01126   }
01127   //==========================================================================
01128 
01129   //==========================================================================
01130 size_t  Factory::GetObjectSize() const 
01131 {
01132   size_t s = Superclass::GetObjectSize();
01133   s += Factory::GetObjectInternalSize();
01134   return s;
01135   }
01136   //==========================================================================
01137   //==========================================================================
01138 size_t  Factory::GetObjectInternalSize() const 
01139 {
01140   size_t s = sizeof(Factory);
01141   return s;
01142   }
01143   //==========================================================================
01144   //==========================================================================
01145   size_t  Factory::GetObjectRecursiveSize() const 
01146   {
01147     size_t s = Superclass::GetObjectRecursiveSize();
01148     s += Factory::GetObjectInternalSize();
01149 
01150     PackageMapType::const_iterator i;
01151     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
01152     {
01153       s += i->second->GetObjectRecursiveSize();
01154     }
01155     return s;
01156   }
01157   //==========================================================================
01158 
01159 }
01160   

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