]> Creatis software - bbtk.git/blob - kernel/src/bbtkFactory.cxx
#2536 BBTK Feature New Normal - wt-version Package
[bbtk.git] / kernel / src / bbtkFactory.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkFactory.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.55 $
34 =========================================================================*/
35
36                                                                        
37
38 /**
39  *\file
40  *\brief  Class bbtk::Factory : can load and unload dynamic libraries containing 
41  *        black boxes packages and create instances of the black boxes registered 
42  *       in the packages loaded.
43  */
44 #include "bbtkFactory.h"
45 #include "bbtkMessageManager.h"
46 #include "bbtkConnection.h"
47 #include "bbtkConfigurationFile.h"
48 #include "bbtkUtilities.h"
49 #include "bbtkConfigurationFile.h"
50
51 #include <sys/stat.h> // for struct stat stFileInfo
52
53 #if defined(_WIN32)
54 #include <direct.h> // for getcwd
55 #endif
56
57 #include <cctype>    // std::toupper
58
59 #include <time.h>
60
61 namespace bbtk
62 {
63
64   //===================================================================
65   /// Default ctor
66   Factory::Pointer Factory::New()
67   {
68     bbtkDebugMessage("kernel",9,"Factory::New()"<<std::endl);
69     return MakePointer(new Factory());
70   }
71   //===================================================================
72
73   //===================================================================
74   /// Default ctor
75   Factory::Factory()
76     : mExecuter()
77   {
78     bbtkDebugMessage("kernel",7,"Factory()"<<std::endl);
79   }
80   //===================================================================
81
82   //===================================================================
83   /// Dtor
84   Factory::~Factory()
85   {
86     bbtkDebugMessage("kernel",7,"==> ~Factory()"<<std::endl);
87     CloseAllPackages();
88     bbtkDebugMessage("kernel",7,"<== ~Factory()"<<std::endl);
89   }
90   //===================================================================
91
92
93
94   //===================================================================
95   void Factory::Reset()
96   {
97     bbtkDebugMessage("kernel",7,"==> Factory::Reset()"<<std::endl);
98     CloseAllPackages();
99     bbtkDebugMessage("kernel",7,"<== Factory::Reset()"<<std::endl);
100   }
101   //===================================================================
102
103
104   // ===================================================================
105   bool Factory::DoLoadPackage(std::string libname,
106                               std::string pkgname,
107                               std::string path)
108   {
109     
110     Package::Pointer p = Package::CreateFromDynamicLibrary(libname,
111                                                            pkgname,
112                                                            path);
113     if (p!=0)
114       {
115         //===================================================================
116         bbtkMessage("output",2,p->GetName()<<" "
117                     <<p->GetVersion()
118                     <<" "
119                     <<p->GetAuthor() << " Category(s) :"
120                     <<p->GetCategory()
121                     <<std::endl);
122         bbtkMessage("output",2,p->GetDescription()<<std::endl);
123         //===================================================================
124         p->AddFactory(GetThisPointer<Factory>());
125         mPackageMap[pkgname] = p;
126         return true;
127       }
128     return false;
129     
130   }
131   
132   //===================================================================
133   /// \brief Loads a package.
134   ///
135   /// The name is the system-independant name of the package (the name of the instance of bbtk::Package).
136   /// Tries to open the dynamic library :
137   /// - "libbb<name>.so" for linux systems,
138   /// - "bb<name>.dll" for windows systems.
139   /// If it succeeds, then tries to load the symbols "<name>GetPackage" and "<name>DeletePackage".
140   /// "<name>GetPackage" is called to get the pointer on the bbtk::Package of the library
141   /// ("<name>DeletePackage" is not used, its presence is just checked before loading the package).
142   
143   /// now, filename is only the last name (no longer the full name!)
144   /// it will be searched within *all* the paths given in bbtk_config.xml
145   
146
147   
148   void Factory::LoadPackage( const std::string& name )
149   {
150           
151           
152   // Note : in the following :
153   // name : the user supplied name
154   //      - abreviated name    e.g.       pkg   pkg.so   libbpkg   libbbpkg.so
155   //      - relative full name e.g.       ./libbbpkg.so   ../../libbbpkg.so
156   //      - absolute full name e.g.       /home/usrname/proj/lib/libbbpkg.so
157   //          same for Windows, with      c:, d: ...
158   //
159   // lastname : string before the last / (if any), or user supplied name
160           
161          
162           
163         if(name != ""){
164                 bbtkDebugMessageInc("kernel",7,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
165                 bbtkMessage("debug",1,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
166
167                 std::vector<std::string> package_paths;
168                 std::string libname;  // full path library name
169                 std::string pkgname;  // e.g. libbb<pkgname>.so
170
171                 std::string upath;
172
173                 pkgname = Utilities::ExtractPackageName(name,upath);
174                                 
175                 bbtkMessage("debug",1,"Package name ["<<pkgname<<"]"<<std::endl);
176                 bbtkMessage("debug",1,"Package path ["<<upath<<"]"<<std::endl);
177
178                 // no loading package if already loaded
179                 PackageMapType::iterator iUnload;
180                 iUnload = mPackageMap.find(pkgname);
181
182 //EED Debuging message
183 //     std::cout << "EED Factory::LoadPackage " <<  std::endl;
184 //              PackageMapType::iterator it;
185 //              for ( it=mPackageMap.begin() ; it != mPackageMap.end(); it++ )
186 //                      std::cout << (*it).first << " ";
187 //              std::cout << std::endl;
188
189                 
190                 if (iUnload != mPackageMap.end())
191                 {
192                   bbtkMessage("output",2,"["<< pkgname <<"] already loaded" << std::endl);
193                   return;
194                 }
195
196         // =================================================
197         // The following structure was checked to work
198         // with any type of relative/absolute path.
199         // Please don't modify it without checking
200         // *all* the cases. JP
201         //==================================================
202
203                 bool ok = false;
204                 bool foundFile = false;
205
206                 // If path provided by user will be the first scanned :
207                 // push it into vector of paths
208                 if (upath.length()>0)   // ------------------------------------- check user supplied location
209                 {
210                    if (name[0] != '.' && name[0] != '/' && name[1]!= ':')
211                    {
212                           bbtkError("Use absolute or relative path name! ["<<name<<"] is an illegal name");
213                           return;
214                    }
215
216                   // std::string path = Utilities::ExpandLibName(upath, false);
217
218                    std::string path = Utilities::ExpandLibName(name,false); // keep last item, here.
219                    if (path != "")
220                    {
221                           std::string p2;
222                           Utilities::ExtractPackageName(path,p2);
223                           //libname = Utilities::MakeLibnameFromPath(path, pkgname);
224                           libname = Utilities::MakeLibnameFromPath(p2, pkgname); // remove last item
225                           // Check if library exists
226                           if ( !Utilities::FileExists(libname) )
227                           {
228                           // The following is *NOT* a debug time message :
229                           // It's a user intended message.
230                           // Please don't remove it.
231                         bbtkMessage("output",3,"   [" <<libname 
232                                 <<"] : doesn't exist" <<std::endl);
233                           }
234                           else
235                           {
236                                  ok = DoLoadPackage( libname, pkgname, path);         
237                           }
238                    }
239                    else
240                    {
241                           bbtkError("Path ["<<upath<<"] doesn't exist");
242                           return;
243                    }
244                 }
245                 else     // ----------------------------------------------------- iterate on the paths  
246                 {
247
248                 std::string path = ".";
249                 package_paths = ConfigurationFile::GetInstance().Get_package_paths();
250                 std::vector<std::string>::iterator i;
251                 for (i=package_paths.begin();i!=package_paths.end();++i)
252                 {
253                         foundFile = false;
254                         path = *i;
255 //std::cout<<"JCP bbtkFactory.cxx void Factory::LoadPackage = path "<<path<<std::endl;
256                         // we *really* want '.' to be the current working directory
257                         if (path == ".")
258                         {
259                           char buf[2048]; // for getcwd
260                           char * currentDir = getcwd(buf, 2048);
261                           std::string cwd(currentDir);
262 //std::cout<<"JCP  bbtkFactory.cxx void Factory::LoadPackage = currentDir "<<currentDir<<std::endl;
263                           path = currentDir;
264                         }
265 //std::cout<<"JCP bbtkFactory.cxx void Factory::LoadPackage = path "<<path<<" pkgnam="<<pkgname<<std::endl;              
266                         libname = Utilities::MakeLibnameFromPath(path, pkgname);
267
268                         bbtkMessage("debug",2,"-> Trying to load [" << libname << "]" <<std::endl);
269
270                   // Check if library exists           
271                         if ( !Utilities::FileExists(libname) )
272                         {
273                         // The following is *NOT* a debug time message :
274                         // It's a user intended message.
275                         // Please don't remove it.
276                           bbtkMessage("output",3,
277                                   "   [" <<libname <<"] : doesn't exist" <<std::endl);
278                            continue;  // try next path
279                         }
280                         foundFile = true; 
281
282                   // Try to Load the library
283
284                         ok = DoLoadPackage( libname, pkgname, path);
285                         if (ok)
286                         {
287                            bbtkMessage("debug",2,"   OK"<<std::endl);
288                         }
289                 break; // we stop iterating even if error : have to signal it to user
290                 } //------------------ // end for ( package_paths.begin();i!=package_paths.end() )
291
292         }
293
294                 if( !ok )  // nothing was loaded
295                 {
296                   if (!foundFile)
297                   {
298                         bbtkError("Could not find package ["<<pkgname<< "]");
299                   }
300                   else
301                   {
302         #if defined(__GNUC__)
303                         bbtkError("Could not load package ["<< pkgname
304                                           <<"] :" << std::endl 
305                           << "  Opening "<<libname<<" failed"
306                           << "  Reason: "<< dlerror());
307         #elif defined(_WIN32)
308                         bbtkError("Could not load package ["<<pkgname
309                           <<"] :"<< std::endl << "   Error loading " <<libname);
310
311                 // look how to get the error message on win
312                 //<<dlerror());
313                 // it is the bordel !! (the bloody fucking bordel, you mean?)
314                 // look : http://msdn2.microsoft.com/en-us/library/ms680582.aspx
315         #endif
316                   }
317                 }       
318                 bbtkMessage("output",2,"[" << libname << "] loaded" << std::endl);
319         }
320
321     
322
323   }
324
325   //===================================================================
326   /// \brief UnLoads a package.
327   ///
328   /// The package must have been previously loaded by LoadPackage.
329   /// If the entry is found in the map, calls ClosePackage
330  void Factory::UnLoadPackage( const std::string& name )
331  {
332     bbtkDebugMessageInc("kernel",7,"Factory::UnLoadPackage(\""
333                        <<name<<"\")"<<std::endl);
334   
335     PackageMapType::iterator i;
336     i = mPackageMap.find(name);
337     if (i == mPackageMap.end()) 
338     {
339       bbtkError("cannot unload package \""<<name
340                 <<"\" : package not loaded !");
341     }
342     ClosePackage(i);
343     bbtkDebugDecTab("kernel",7);
344   }
345   //===================================================================
346
347
348   //===================================================================
349   void Factory::CloseAllPackages()
350   {
351     bbtkDebugMessageInc("kernel",7,"Factory::CloseAllPackages()"<<std::endl);
352     
353     std::vector< Package::WeakPointer > mAlive;  
354     do {
355       mAlive.clear();  
356       while (mPackageMap.begin() != mPackageMap.end())
357         {
358           PackageMapType::iterator i = mPackageMap.begin();
359           Package::WeakPointer p = i->second;
360           ClosePackage(i);
361           if (p.lock()) mAlive.push_back(p);
362         }
363       std::vector< Package::WeakPointer >::iterator i;
364       for (i=mAlive.begin();i!=mAlive.end();++i)
365         {
366           // If not dead : reinsert
367           if (i->lock())
368             {
369               bbtkDebugMessage("kernel",7,"Package "<<i->lock()->GetName()
370                                <<" still alive"<<std::endl);
371               // InsertPackage(i->lock());
372             }
373         }      
374     }
375     while (mPackageMap.size()>0);
376
377     bbtkDebugDecTab("kernel",7);
378   }
379   //===================================================================
380
381   //===================================================================
382   /// \brief Close the package referenced by the iterator 
383  ///
384  /// First removes the factory from the set of factories which use the package
385  /// If the set is empty then :
386  /// If it is a dynamically loaded package :
387  /// - Loads and calls the function "<name>DeletePackage" of the dynamic library (responsible for package desallocation)
388  /// - Closes the dynamic library
389  /// Else :
390  /// - deletes the package normally
391  /// 
392  /// Finally erases the package entry in the packages map
393  void Factory::ClosePackage(PackageMapType::iterator& i) 
394   {   
395      bbtkDebugMessageInc("kernel",7,"Factory::ClosePackage(\""
396                          <<i->second->GetName()
397                         <<"\")"<<std::endl);
398
399      
400      // Removes this from the set of factories which use the package
401      i->second->RemoveFactory(GetThisPointer<Factory>());
402      Package::WeakPointer p = i->second;
403      // remove the entry in the map
404      mPackageMap.erase(i);
405      // Release the package if not already destroyed
406      if (p.lock()) 
407        {
408          Package::Release(p);
409        }
410      bbtkDebugDecTab("kernel",7);
411   }
412   //===================================================================
413   
414
415
416   //===================================================================  
417   void Factory::PrintHelpListPackages(bool details, bool adaptors) const
418   {
419     bbtkDebugMessageInc("kernel",9,"Factory::PrintPackages"<<std::endl);
420
421     PackageMapType::const_iterator i;
422     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
423     {
424       bbtkMessage("help",1, i->first << std::endl);
425       if (details) {
426          i->second->PrintHelpListDescriptors(false,adaptors);
427       }
428     }
429
430     bbtkDebugDecTab("kernel",9);
431   }
432   //===================================================================
433
434   //===================================================================  
435   /// Displays help on a package
436   void Factory::PrintHelpPackage(const std::string& name, bool adaptors) const
437   {
438     bbtkDebugMessageInc("kernel",9,"Factory::PrintHelpPackage(\""
439                         <<name<<"\")"
440                         <<std::endl);
441
442     PackageMapType::const_iterator i = mPackageMap.find(name);
443     if ( i != mPackageMap.end() ) 
444       {
445       bbtkMessage("help",1, "Package "<<i->first<<" ");
446       
447       if (i->second->GetVersion().length()>0)
448         bbtkMessageCont("help",1,"v" <<i->second->GetVersion());
449         
450       if (i->second->GetAuthor().length()>0)
451         bbtkMessageCont("help",1,"- "<<i->second->GetAuthor());
452         
453       if (i->second->GetCategory().length()>0)
454         bbtkMessageCont("help",1,"- "<<i->second->GetCategory());        
455         
456       bbtkMessageCont("help",1,std::endl);
457       bbtkIncTab("help",1);
458       bbtkMessage("help",1,i->second->GetDescription()<<std::endl);
459       if (i->second->GetNumberOfDescriptors()>0) 
460         {
461           bbtkMessage("help",1, "Black boxes : "<<std::endl);
462           i->second->PrintHelpListDescriptors(true,adaptors);
463         }
464       else 
465         {
466           bbtkMessage("help",1, "No black boxes"<<std::endl);
467         }
468       bbtkDecTab("help",1);
469       }
470     else 
471       {
472       bbtkDebugDecTab("kernel",9);
473       bbtkError("package \""<<name<<"\" unknown");
474       }
475     
476     bbtkDebugDecTab("kernel",9);
477   }
478   //===================================================================
479
480   //===================================================================
481   /// Prints help on the black box of type <name>
482   /// Returns the package to which it belongs
483   void Factory::PrintHelpDescriptor(const std::string& name, 
484                              std::string& package,
485                              bool full) const
486   {
487     bbtkDebugMessageInc("kernel",9,"Factory::PrintHelpDescriptor(\""
488                         <<name<<"\")"
489                         <<std::endl);
490
491     bool found = false;
492     PackageMapType::const_iterator i;
493     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
494       {
495       if (i->second->ContainsDescriptor(name)) 
496         {
497           i->second->PrintHelpDescriptor(name,full);
498               package = i->second->GetName();
499           found = true;
500         }
501       }
502     
503     bbtkDebugDecTab("kernel",9);
504     if (!found) 
505       {
506       bbtkError("No package of the factory contains any black box <"
507                  <<name<<">");
508       }
509   }  
510   //===================================================================
511
512
513   //=================================================================== 
514   /// Inserts a package in the factory
515   void Factory::InsertPackage( Package::Pointer p )
516   {
517     bbtkDebugMessageInc("kernel",9,"Factory::InsertPackage(\""<<
518                         p->GetName()<<"\")"<<std::endl);
519
520     p->AddFactory(GetThisPointer<Factory>());     
521     mPackageMap[p->GetName()] = p;
522     bbtkDebugDecTab("kernel",9);
523   }
524   //===================================================================
525   
526   //=================================================================== 
527   /// Removes a package from the factory (and deletes it)
528   void Factory::RemovePackage( Package::Pointer p )
529   {
530     bbtkDebugMessageInc("kernel",9,"Factory::RemovePackage(\""<<
531                         p->GetName()<<"\")"<<std::endl);
532
533     PackageMapType::iterator i;
534     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
535       {
536          if (i->second == p) break;
537       };
538     
539     if (i!=mPackageMap.end())
540       {
541         ClosePackage(i);
542       }
543     else 
544       {
545         bbtkError("Factory::RemovePackage(\""<<
546                   p->GetName()<<"\") : package absent from factory");
547       }
548
549     bbtkDebugDecTab("kernel",9);
550   }
551   
552
553   //===================================================================
554         std::string Factory::GetPackageNameOfaBlackBox(std::string boxType)
555         {
556                 std::string packageName="<void bbtk package name>";
557                 PackageMapType::const_iterator i;
558
559                 std::size_t found = boxType.find(":");
560                 if (found!=std::string::npos)
561                 {
562                         packageName     = boxType.substr(0,found);
563                 } else {
564                         for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
565                         {
566                                 if ( i->second->ifBoxExist( boxType ) == true)
567                                 {
568                                         packageName = i->first;
569                                 } // if ifBoxExist
570                         } // for i
571
572                 } // if found
573                 return packageName;
574         }
575
576   //===================================================================
577         std::string Factory::GetTypeNameOfaBlackBox(std::string boxType)
578         {
579                 std::string typeName="<void bbtk type name>";
580
581                 std::size_t found = boxType.find(":");
582                 if (found!=std::string::npos)
583                 {
584                         typeName                = boxType.substr(found+1);
585                 } else {
586                         typeName=boxType;
587                 } // if found
588                 return typeName;
589         }
590
591
592         
593         
594   //===================================================================
595   /// Creates an instance of a black box of type <type> with name <name>
596   BlackBox::Pointer Factory::NewBlackBox(const std::string& type, 
597                                  const std::string& name) const
598   {
599         bbtkDebugMessageInc("kernel",7,"Factory::NewBlackBox(\""
600                         <<type<<"\",\""<<name<<"\")"<<std::endl);
601
602     BlackBox::Pointer b; 
603     PackageMapType::const_iterator i;
604
605         std::string tmp = type;
606         std::string packageName = ((bbtk::Factory*)this)->GetPackageNameOfaBlackBox(tmp);
607         std::string type2               = ((bbtk::Factory*)this)->GetTypeNameOfaBlackBox(tmp);
608         i = mPackageMap.find(packageName);
609         if (i != mPackageMap.end())
610         {
611                 b = i->second->NewBlackBox(type2,name);
612         } else {
613                 b =BlackBox::Pointer();
614         }
615
616 /*
617         std::size_t found = type.find(":");
618         if (found!=std::string::npos)
619         {
620                 std::string packageName = type.substr(0,found);
621                 std::string type2               = type.substr(found+1);
622                 i = mPackageMap.find(packageName);
623                 if (i != mPackageMap.end())
624                 {
625                 b = i->second->NewBlackBox(type2,name);
626                 } else {
627                         b =BlackBox::Pointer();
628                 }
629         } else {
630         for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
631         {
632                 b = i->second->NewBlackBox(type,name);
633                 if (b) break; 
634         } // for i
635
636         } // if found :
637 */
638
639
640     if (!b) 
641     {
642         bbtkError("black box type \""<<type<<"\" unknown");
643     }  // if !b
644
645
646     bbtkDebugDecTab("kernel",7);
647     return b;
648   }
649   //===================================================================
650
651   //===================================================================
652   /// Creates an instance of a black box of type <type> with name <name>
653   BlackBox::Pointer Factory::NewAdaptor(const DataInfo& typein,
654                                 const DataInfo& typeout,
655                                 const std::string& name) const
656   {
657     bbtkDebugMessageInc("kernel",8,"Factory::NewAdaptor("
658                         <<typein<<","
659                         <<typeout<<",\""
660                         <<name<<"\")"<<bbtkendl);
661
662
663     BlackBox::Pointer b; 
664     PackageMapType::const_iterator i;
665     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
666       {
667       b = i->second->NewAdaptor(typein,typeout,name);
668       if (b) break; 
669       }
670     if (!b) 
671       {
672         bbtkError("no "<<typein<<" to "<<typeout
673                   <<" adaptor available");
674       } 
675     
676     bbtkDebugDecTab("kernel",7);
677     return b; 
678   }
679   //===================================================================
680
681
682   //===================================================================
683   /// Creates an instance of a black box of type <type> with name <name>
684   BlackBox::Pointer Factory::NewWidgetAdaptor(const DataInfo& typein,
685                                       const DataInfo& typeout,
686                                       const std::string& name) const
687   {
688     bbtkDebugMessageInc("kernel",8,"Factory::NewWidgetAdaptor(<"
689                         <<typein<<">,<"
690                         <<typeout<<">,\""
691                         <<name<<"\")"<<bbtkendl);
692
693
694     BlackBox::Pointer b; 
695     PackageMapType::const_iterator i;
696     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
697       {
698         b = i->second->NewWidgetAdaptor(typein,
699                                                  typeout,
700                                                  name);
701       if (b) break; 
702       }
703     if (!b) 
704       {
705         bbtkError("no "<<typein<<" to "<<typeout
706                   <<"> widget adaptor available");
707       } 
708     
709     bbtkDebugDecTab("kernel",7);
710     return b; 
711   }
712   //===================================================================
713
714   //===================================================================
715   /// Creates an instance of a black box of type <type> with name <name>
716   bool Factory::FindAdaptor(const DataInfo& typein,
717                                   const DataInfo& typeout,
718                                   std::string& adaptor) const
719   {
720     bbtkDebugMessageInc("kernel",8,"Factory::FindAdaptor(<"
721                         <<typein<<">,<"
722                         <<typeout<<">)"<<bbtkendl);
723     
724     bool b = false;
725     PackageMapType::const_iterator i;
726     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
727       {
728         b = i->second->FindAdaptor(typein,
729                                             typeout,
730                                             adaptor);
731         if (b) break; 
732       }
733     /*
734     if (!b) 
735       {
736         bbtkError("no "<<typein<<" to "<<typeout
737                   <<"> widget adaptor available");
738       } 
739     */
740
741     bbtkDebugDecTab("kernel",7);
742     return b; 
743   }
744   //===================================================================
745
746   //===================================================================
747   /// Creates an instance of a black box of type <type> with name <name>
748   bool Factory::FindWidgetAdaptor(const DataInfo& typein,
749                                   const DataInfo& typeout,
750                                   std::string& adaptor) const
751   {
752     bbtkDebugMessageInc("kernel",8,"Factory::FindWidgetAdaptor(<"
753                         <<typein<<">,<"
754                         <<typeout<<">)"<<bbtkendl);
755     
756     bool b = false;
757     PackageMapType::const_iterator i;
758     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
759       {
760         b = i->second->FindWidgetAdaptor(typein,
761                                                   typeout,
762                                                   adaptor);
763         if (b) break; 
764       }
765     bbtkDebugDecTab("kernel",7);
766     return b; 
767   }
768   //===================================================================
769
770   //===================================================================
771   /// Creates an instance of a black box of type <type> with name <name>
772   bool Factory::FindWidgetAdaptor2(const DataInfo& typein,
773                                   const DataInfo& typeout,
774                                   std::string& widget,
775                                   std::string& adaptor) const
776   {
777     bbtkDebugMessageInc("kernel",8,"Factory::FindWidgetAdaptor(<"
778                         <<typein<<">,<"
779                         <<typeout<<">)"<<bbtkendl);
780     
781     bool b = false;
782     adaptor = widget = "";
783     PackageMapType::const_iterator i;
784     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
785       {
786         b = i->second->FindWidgetAdaptor(typein,
787                                                   typeout,
788                                                   widget);
789         if (b) break; 
790       }
791     if (!b) 
792       {
793         // Look for a widget adaptor with good nature out
794         bbtkMessage("kernel",5,
795                     "*** Looking for a two pieces widget adaptor for : "
796                     << typein << "->"<<typeout<<std::endl);
797         for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
798           {
799             Package::AdaptorMapType::const_iterator j;
800             for (j=i->second->GetAdaptorMap().begin();
801                  j!=i->second->GetAdaptorMap().end();
802                  ++j)
803               {
804                 if ( ( j->first.mKind ==  
805                        BlackBoxDescriptor::DEFAULT_GUI) &&
806                      //(j->first.mTypeIn == typein) &&
807                      (j->first.mTypeOut.GetNature() == typeout.GetNature() ) 
808                      )
809                   {
810                     widget = j->second.lock()->GetTypeName();
811                     bbtkMessage("kernel",5,
812                                 "===> Found first part : "<<widget
813                                 << " "<<j->first.mTypeIn<<"->"
814                                 <<j->first.mTypeOut<<std::endl);
815                     DataInfo ti( j->first.mTypeOut.GetType(), "");
816                     DataInfo to( typeout.GetType(), "");
817                     b = FindAdaptor( ti, to, adaptor );
818                     if (b) 
819                       {
820                         bbtkMessage("kernel",5,
821                                     "===> Found second part : "<<adaptor
822                                     <<std::endl);
823                         break;
824                       }
825                     else
826                       {
827                         bbtkMessage("kernel",5,
828                                     "===> No second part found"<<std::endl);
829                       }
830                   }
831               }
832             if (b) break;
833           }
834       }
835     bbtkDebugDecTab("kernel",7);
836     return b; 
837   }
838   //===================================================================
839
840   //===================================================================
841   /// Creates an instance of a connection
842   Connection::Pointer Factory::NewConnection(BlackBox::Pointer from,
843                                              const std::string& output,
844                                              BlackBox::Pointer to,
845                                              const std::string& input) const
846   {
847     bbtkDebugMessage("kernel",7,"Factory::NewConnection(\""
848                       <<from->bbGetName()<<"\",\""<<output<<"\",\""
849                       <<to->bbGetName()<<"\",\""<<input
850                       <<"\")"<<std::endl);
851     
852     return Connection::New(from,output,to,input,
853                            GetThisPointer<Factory>());
854   }
855   //===================================================================
856
857
858
859   //===================================================================
860   Package::Pointer Factory::GetPackage(const std::string& name) const
861   {
862     bbtkDebugMessageInc("kernel",9,"Factory::GetPackage(\""<<name<<"\")"
863                          <<std::endl);
864
865     PackageMapType::const_iterator i = mPackageMap.find(name);
866     if ( i != mPackageMap.end() ) 
867     {
868       bbtkDebugDecTab("kernel",9); 
869       return i->second;
870     }
871     else 
872     {
873        bbtkDebugDecTab("kernel",9);
874        bbtkError("package \""<<name<<"\" unknown");
875     }
876     
877     bbtkDebugDecTab("kernel",9);  
878   }
879   //===================================================================
880   
881
882   //===================================================================
883   void Factory::Check() const
884   {
885     bbtkMessage("debug",1,"****** Checking Factory "<<(void*)this
886                  <<std::endl);
887     PackageMapType::const_iterator i;
888     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
889       {
890         i->second->Check();
891       }
892     bbtkMessage("debug",1,"****** Checking Factory "<<(void*)this
893                 <<" ... OK"<<std::endl);
894   }
895   //===================================================================
896
897   //===================================================================
898   void Factory::WriteDotFilePackagesList(FILE *ff)
899   {
900
901     bbtkDebugMessageInc("kernel",9,"Factory::WriteDotFilePackagesList()"
902                          <<std::endl);
903
904     fprintf( ff , "\n");
905     fprintf( ff , "subgraph cluster_FACTORY {\n");
906     fprintf( ff , "  label = \"PACKAGES\"%s\n",  ";");
907     fprintf( ff , "  style=filled%s\n",";");
908     fprintf( ff , "  color=lightgrey%s\n",";");
909     fprintf( ff , "  rankdir=TB%s\n",";");
910
911     std::string url;
912     PackageMapType::const_iterator i;
913     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
914     {
915        url=GetPackage(i->first)->GetDocURL();
916        fprintf(ff,"  %s [shape=ellipse, URL=\"%s\"]%s\n",i->first.c_str(),url.c_str(),";" );
917     }
918     fprintf( ff , "}\n\n");
919     bbtkDebugDecTab("kernel",9);
920   }
921   //===================================================================
922
923
924  void Factory::ShowGraphTypes(const std::string& name) const
925  {
926    bool found = false;
927    PackageMapType::const_iterator i;
928    for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
929    {
930      if (i->second->ContainsDescriptor(name)) 
931        {
932          std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
933          
934          // Don't pollute the file store with  "temp_dir" directories ...    
935          std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
936          std::string directory = "\"" + default_doc_dir + separator + "temp_dir"  +separator + "\"";
937          std::string filename2 =  default_doc_dir + separator + "temp_dir" + separator + "tmp.html"; 
938          
939 #if defined(_WIN32)  
940          std::string command("start \"Titre\" /D ");
941 #else 
942          std::string command("gnome-open ");
943 #endif
944          command=command + directory +" tmp.html";
945          FILE *ff;
946          ff=fopen(filename2.c_str(),"w");
947          
948          fprintf(ff,"<html><head><title>TMP</title> <script type=\"text/javascript\"> <!--\n");
949          fprintf(ff,"  window.location=\"%s#%s\";\n" , i->second->GetDocURL().c_str(),name.c_str() );
950          fprintf(ff,"//--></script></head><body></body></html>\n");
951          
952          
953          //fprintf(ff, "<a  href=\"%s#%s\">Link</a>\n", i->second->GetDocURL().c_str(),name.c_str() );
954          fclose(ff);
955          system( command.c_str() );      
956          found = true;
957        }
958    }
959    
960    bbtkDebugDecTab("kernel",9);
961    if (!found) 
962    {
963      bbtkError("No package of the factory contains any black box <"
964                <<name<<">");
965    }
966  }
967     
968
969
970
971   void Factory::CreateHtmlIndex(IndexEntryType type, 
972                                 const std::string& filename)
973   {
974     bbtkDebugMessageInc("kernel",9,"Factory::CreateHtmlIndex(\""
975                         <<filename<<"\")"<<bbtkendl);
976     
977     std::string title;
978
979     typedef std::map<std::string, 
980                      std::vector<BlackBoxDescriptor::Pointer> > IndexType;
981     IndexType index;
982     // Builds the index map
983     PackageMapType::const_iterator i;
984     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
985         {
986                 Package::Pointer pack = i->second;
987                 if (pack->GetName()=="user") continue;
988                 Package::DescriptorMapType::const_iterator j;
989                 for (j = pack->GetDescriptorMap().begin(); 
990                         j!= pack->GetDescriptorMap().end(); 
991                         ++j)
992                 {
993             
994             // Skip adaptors 
995                         if ( type==Adaptors )
996                         {  
997                                 if (j->second->GetKind() == BlackBoxDescriptor::STANDARD )
998                                 continue;
999                         }
1000                         else 
1001                                 if (j->second->GetKind() != BlackBoxDescriptor::STANDARD )
1002                                 continue;
1003
1004                         std::vector<std::string> keys;
1005                         if (type==Packages)
1006                         {
1007                                 std::string k("");
1008                                 k += pack->GetName();
1009                                 keys.push_back(k);
1010                                 title = "Boxes by package";
1011                         }
1012                         else if ((type==Initials) || (type==Adaptors))
1013                         {
1014                                 std::string init(" ");
1015                                 init[0] =  std::toupper(j->second->GetTypeName()[0]);
1016                                 keys.push_back(init);
1017                                 title = "Alphabetical list";
1018                         }
1019                         else if (type==Categories)
1020                         {
1021                         // Split the category string 
1022                         std::string delimiters = ";,";
1023                         Utilities::SplitString(j->second->GetCategory(),
1024                                            delimiters,keys);
1025                         if (keys.size()==0) 
1026                                 keys.push_back(" NONE");
1027                                 title = "Boxes by category";
1028                         }
1029
1030                         std::vector<std::string>::const_iterator k;
1031                         for (k=keys.begin(); k!=keys.end(); ++k )
1032                         {
1033                                 IndexType::iterator p;
1034                                 p = index.find(*k);
1035                                 if (p != index.end()) 
1036                                 {
1037                                         p->second.push_back(j->second);
1038                                 }
1039                                 else 
1040                                 {
1041                                         std::vector<BlackBoxDescriptor::Pointer> v;
1042                                         v.push_back(j->second);
1043                                         index[*k] = v;
1044                                 }
1045                         }
1046                 }
1047         }   
1048     // Creates the file 
1049     //---------------------
1050     // Open output file
1051     std::ofstream s;
1052     s.open(filename.c_str());
1053     if (!s.good()) 
1054     {
1055        bbtkError("Factory::CreateHtmlIndex : could not open file '"
1056                  <<filename<<"'");
1057     }
1058     
1059     //----------------------
1060     // Html head
1061     s << "<html lang=\"en\">\n";
1062     s << "<head>\n";
1063     s << "<title>"<<title<<"</title>\n";
1064     s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1065     s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1066     s << "<meta name=\"generator\" content=\"\">\n";
1067     s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1068     //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1069     s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1070     s << "pre.display { font-family:inherit }\n";
1071     s << "pre.format  { font-family:inherit }\n";
1072     s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1073     s << "pre.smallformat  { font-family:inherit; font-size:smaller }\n";
1074     s << "pre.smallexample { font-size:smaller }\n";
1075     s << "pre.smalllisp    { font-size:smaller }\n";
1076     s << "span.sc    { font-variant:small-caps }\n";
1077     s << "span.roman { font-family:serif; font-weight:normal; } \n";
1078     s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n"; 
1079     s << "--></style>\n";
1080     s << "</head>\n";
1081     //----------------------
1082
1083     //----------------------
1084     // Html body
1085     s << "<body>\n";
1086     s << "<a name=\"Top\"></a>\n"; 
1087     s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1088     s << "<p>\n";
1089     IndexType::iterator ii;
1090     for (ii=index.begin();ii!=index.end();++ii)
1091       {
1092                 s << "<a href=\"#"<<ii->first<<"\">"<<ii->first<<"</a>&nbsp;&nbsp;";    
1093       }
1094
1095     for (ii=index.begin();ii!=index.end();++ii)
1096       {
1097         s << "<p><hr>\n";
1098         s << "<p><a href=\"#Top\">Top</a>";
1099         if (type==Packages)
1100           {
1101             s << "<a name=\""<<ii->first<<"\"></a>\n"; 
1102             s << "<p><a href=\""<<ii->first<<"/index.html\">"
1103               << ii->first<<"</a>\n"; 
1104
1105             s << "&nbsp;&nbsp;-&nbsp;&nbsp;\n"; 
1106
1107             s << "<a name=\"doxygen\"></a>\n"; 
1108
1109 //EED 26Mars2009
1110         /*JCP 19 Nov 2009
1111                 std::string bin_path = bbtk::ConfigurationFile::GetInstance().Get_bin_path();
1112             s << "<a href=" << bin_path <<"/../share/bbtk/doc/doxygen/" << ii->first << "/main.html>(Doxygen documentation of the source)</a>\n"; 
1113         JCP 19 Nov 2009*/
1114                 std::string bin_path = bbtk::ConfigurationFile::GetInstance().Get_bin_path();           
1115             s << "<a href=" << bin_path <<"/../share/bbtk/doc/doxygen/" << ii->first << "/main.html>(Doxygen documentation of the source)</a>\n"; 
1116           }
1117         else 
1118           {
1119             s << "<a name=\""<<ii->first<<"\"></a>\n"; 
1120             s << "<p><b>"<<ii->first<<"</b>\n";
1121           }
1122         s << "<ul>\n";
1123
1124         s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
1125
1126         std::vector<BlackBoxDescriptor::Pointer>::iterator di;
1127         for (di=ii->second.begin();di!=ii->second.end();++di)
1128           {
1129             std::string pack = (*di)->GetPackage()->GetName();
1130             std::string name = (*di)->GetTypeName();
1131             Utilities::html_format(name);
1132             std::string descr = (*di)->GetDescription();
1133             Utilities::html_format(descr);
1134             s << "<TR>";
1135             s << "<TD style='vertical-align: top;'>";
1136             s << "&nbsp;&nbsp;&nbsp;<a href=\""<<pack
1137               <<"/index.html#"<<name<<"\">"
1138               <<pack<<"::"<<name<<"</a>";
1139             s << "</TD> ";
1140             s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1141             s << "</TR>\n";
1142           }    
1143         s << "</TABLE>\n";
1144         s << "</ul>\n";
1145         s << "</div>\n";
1146       }
1147     //----------------------
1148     // Footer 
1149     time_t rawtime;
1150     tm * ptm;
1151     time ( &rawtime );
1152     ptm = gmtime ( &rawtime );
1153
1154     s << "<p><hr>\n";
1155     s << "Automatically generated by <b>bbtk</b> on "
1156       << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900 
1157       << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1158     s << "</body></html>\n"; 
1159     s.close();
1160     //----------------------
1161
1162     // End
1163     bbtkDebugDecTab("kernel",9);
1164   }
1165
1166  //==========================================================================
1167   std::string Factory::GetObjectName() const
1168   {
1169     return std::string("Factory");
1170   }
1171   //==========================================================================
1172   
1173   //==========================================================================
1174   std::string  Factory::GetObjectInfo() const 
1175   {
1176     std::stringstream i;
1177     return i.str();
1178   }
1179   //==========================================================================
1180
1181   //==========================================================================
1182 size_t  Factory::GetObjectSize() const 
1183 {
1184   size_t s = Superclass::GetObjectSize();
1185   s += Factory::GetObjectInternalSize();
1186   return s;
1187   }
1188   //==========================================================================
1189   //==========================================================================
1190 size_t  Factory::GetObjectInternalSize() const 
1191 {
1192   size_t s = sizeof(Factory);
1193   return s;
1194   }
1195   //==========================================================================
1196   //==========================================================================
1197   size_t  Factory::GetObjectRecursiveSize() const 
1198   {
1199     size_t s = Superclass::GetObjectRecursiveSize();
1200     s += Factory::GetObjectInternalSize();
1201
1202     PackageMapType::const_iterator i;
1203     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
1204     {
1205       s += i->second->GetObjectRecursiveSize();
1206     }
1207     return s;
1208   }
1209   //==========================================================================
1210
1211 }
1212