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