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