]> Creatis software - bbtk.git/blob - kernel/src/bbtkFactory.cxx
*** empty log message ***
[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/09/10 09:25:19 $
8 Version:   $Revision: 1.38 $
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         }
255         break; // we stop iterating even if error : have to signal it to user
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 
271                   << "  Opening "<<libname<<" failed"
272                   << "  Reason: "<< dlerror());
273 #elif defined(_WIN32)
274         bbtkError("Could not load package ["<<pkgname
275                   <<"] :"<< std::endl << "   Error loading " <<libname);
276
277     // look how to get the error message on win
278     //<<dlerror());
279     // it is the bordel !! (the bloody fucking bordel, you mean?)
280     // look : http://msdn2.microsoft.com/en-us/library/ms680582.aspx
281 #endif
282       }
283     }
284     bbtkMessage("Output",2,"[" << libname << "] loaded" << std::endl);
285
286   }
287
288   //===================================================================
289   /// \brief UnLoads a package.
290   ///
291   /// The package must have been previously loaded by LoadPackage.
292   /// If the entry is found in the map, calls ClosePackage
293  void Factory::UnLoadPackage( const std::string& name )
294  {
295     bbtkDebugMessageInc("Kernel",7,"Factory::UnLoadPackage(\""
296                        <<name<<"\")"<<std::endl);
297   
298     PackageMapType::iterator i;
299     i = mPackageMap.find(name);
300     if (i == mPackageMap.end()) 
301     {
302       bbtkError("cannot unload package \""<<name
303                 <<"\" : package not loaded !");
304     }
305     ClosePackage(i);
306     bbtkDebugDecTab("Kernel",7);
307   }
308   //===================================================================
309
310
311   //===================================================================
312   void Factory::CloseAllPackages()
313   {
314     bbtkDebugMessageInc("Kernel",7,"Factory::CloseAllPackages()"<<std::endl);
315     while (mPackageMap.begin() != mPackageMap.end())
316       {
317         PackageMapType::iterator i = mPackageMap.begin();
318         ClosePackage(i);
319       }
320     bbtkDebugDecTab("Kernel",7);
321   }
322   //===================================================================
323
324   //===================================================================
325   /// \brief Close the package referenced by the iterator 
326  ///
327  /// First removes the factory from the set of factories which use the package
328  /// If the set is empty then :
329  /// If it is a dynamically loaded package :
330  /// - Loads and calls the function "<name>DeletePackage" of the dynamic library (responsible for package desallocation)
331  /// - Closes the dynamic library
332  /// Else :
333  /// - deletes the package normally
334  /// 
335  /// Finally erases the package entry in the packages map
336  void Factory::ClosePackage(PackageMapType::iterator& i) 
337   {   
338      bbtkDebugMessageInc("Kernel",7,"Factory::ClosePackage(\""
339                          <<i->second->GetName()
340                         <<"\")"<<std::endl);
341
342      
343      // Removes this from the set of factories which use the package
344      i->second->RemoveFactory(GetThisPointer<Factory>());
345      Package::WeakPointer p = i->second;
346      // remove the entry in the map
347      mPackageMap.erase(i);
348      // Release the package if not already destroyed
349      if (p.lock()) Package::Release(p);
350
351
352      bbtkDebugDecTab("Kernel",7);
353   }
354   //===================================================================
355   
356
357
358   //===================================================================  
359   /// Displays the list of packages loaded
360   void Factory::PrintPackages(bool details, bool adaptors) const
361   {
362     bbtkDebugMessageInc("Kernel",9,"Factory::PrintPackages"<<std::endl);
363
364     PackageMapType::const_iterator i;
365     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
366     {
367       bbtkMessage("Help",1, i->first << std::endl);
368       if (details) {
369          i->second->PrintBlackBoxes(false,adaptors);
370       }
371     }
372
373     bbtkDebugDecTab("Kernel",9);
374   }
375   //===================================================================
376
377   //===================================================================  
378   /// Displays help on a package
379   void Factory::HelpPackage(const std::string& name, bool adaptors) const
380   {
381     bbtkDebugMessageInc("Kernel",9,"Factory::HelpPackage(\""<<name<<"\")"
382                         <<std::endl);
383
384     PackageMapType::const_iterator i = mPackageMap.find(name);
385     if ( i != mPackageMap.end() ) 
386       {
387       bbtkMessage("Help",1, "Package "<<i->first<<" ");
388       
389       if (i->second->GetVersion().length()>0)
390         bbtkMessageCont("Help",1,"v" <<i->second->GetVersion());
391         
392       if (i->second->GetAuthor().length()>0)
393         bbtkMessageCont("Help",1,"- "<<i->second->GetAuthor());
394         
395       if (i->second->GetCategory().length()>0)
396         bbtkMessageCont("Help",1,"- "<<i->second->GetCategory());        
397         
398       bbtkMessageCont("Help",1,std::endl);
399       bbtkIncTab("Help",1);
400       bbtkMessage("Help",1,i->second->GetDescription()<<std::endl);
401       if (i->second->GetNumberOfBlackBoxes()>0) 
402         {
403           bbtkMessage("Help",1, "Black boxes : "<<std::endl);
404           i->second->PrintBlackBoxes(true,adaptors);
405         }
406       else 
407         {
408           bbtkMessage("Help",1, "No black boxes"<<std::endl);
409         }
410       bbtkDecTab("Help",1);
411       }
412     else 
413       {
414       bbtkDebugDecTab("Kernel",9);
415       bbtkError("package \""<<name<<"\" unknown");
416       }
417     
418     bbtkDebugDecTab("Kernel",9);
419   }
420   //===================================================================
421
422   //===================================================================
423   /// Prints help on the black box of type <name>
424   /// Returns the package to which it belongs
425   void Factory::HelpBlackBox(const std::string& name, 
426                              std::string& package,
427                              bool full) const
428   {
429     bbtkDebugMessageInc("Kernel",9,"Factory::HelpBlackBox(\""<<name<<"\")"
430                         <<std::endl);
431
432     bool found = false;
433     PackageMapType::const_iterator i;
434     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
435       {
436       if (i->second->ContainsBlackBox(name)) 
437         {
438           i->second->HelpBlackBox(name,full);
439               package = i->second->GetName();
440           found = true;
441         }
442       }
443     
444     bbtkDebugDecTab("Kernel",9);
445     if (!found) 
446       {
447       bbtkError("No package of the factory contains any black box <"
448                  <<name<<">");
449       }
450   }  
451   //===================================================================
452
453
454   //=================================================================== 
455   /// Inserts a package in the factory
456   void Factory::InsertPackage( Package::Pointer p )
457   {
458     bbtkDebugMessageInc("Kernel",9,"Factory::InsertPackage(\""<<
459                         p->GetName()<<"\")"<<std::endl);
460
461     p->AddFactory(GetThisPointer<Factory>());
462     mPackageMap[p->GetName()] = p;
463
464     bbtkDebugDecTab("Kernel",9);
465   }
466   //===================================================================
467   
468   //=================================================================== 
469   /// Removes a package from the factory (and deletes it)
470   void Factory::RemovePackage( Package::Pointer p )
471   {
472     bbtkDebugMessageInc("Kernel",9,"Factory::RemovePackage(\""<<
473                         p->GetName()<<"\")"<<std::endl);
474
475     PackageMapType::iterator i;
476     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
477       {
478          if (i->second == p) break;
479       };
480     
481     if (i!=mPackageMap.end())
482       {
483         ClosePackage(i);
484       }
485     else 
486       {
487         bbtkError("Factory::RemovePackage(\""<<
488                   p->GetName()<<"\") : package absent from factory");
489       }
490
491     bbtkDebugDecTab("Kernel",9);
492   }
493   //===================================================================
494   
495
496   //===================================================================
497   /// Creates an instance of a black box of type <type> with name <name>
498   BlackBox::Pointer Factory::NewBlackBox(const std::string& type, 
499                                  const std::string& name) const
500   {
501     bbtkDebugMessageInc("Kernel",7,"Factory::NewBlackBox(\""
502                         <<type<<"\",\""<<name<<"\")"<<std::endl);
503
504     BlackBox::Pointer b; 
505     PackageMapType::const_iterator i;
506     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
507       {
508       b = i->second->NewBlackBox(type,name);
509       if (b) break; 
510       }
511     if (!b) 
512       {
513        bbtkError("black box type \""<<type<<"\" unknown");
514       } 
515
516     bbtkDebugDecTab("Kernel",7);
517     return b;
518   }
519   //===================================================================
520
521   //===================================================================
522   /// Creates an instance of a black box of type <type> with name <name>
523   BlackBox::Pointer Factory::NewAdaptor(const DataInfo& typein,
524                                 const DataInfo& typeout,
525                                 const std::string& name) const
526   {
527     bbtkDebugMessageInc("Kernel",8,"Factory::NewAdaptor("
528                         <<typein<<","
529                         <<typeout<<",\""
530                         <<name<<"\")"<<bbtkendl);
531
532
533     BlackBox::Pointer b; 
534     PackageMapType::const_iterator i;
535     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
536       {
537       b = i->second->NewAdaptor(typein,typeout,name);
538       if (b) break; 
539       }
540     if (!b) 
541       {
542         bbtkError("no "<<typein<<" to "<<typeout
543                   <<" adaptor available");
544       } 
545     
546     bbtkDebugDecTab("Kernel",7);
547     return b; 
548   }
549   //===================================================================
550
551
552   //===================================================================
553   /// Creates an instance of a black box of type <type> with name <name>
554   BlackBox::Pointer Factory::NewWidgetAdaptor(const DataInfo& typein,
555                                       const DataInfo& typeout,
556                                       const std::string& name) const
557   {
558     bbtkDebugMessageInc("Kernel",8,"Factory::NewWidgetAdaptor(<"
559                         <<typein<<">,<"
560                         <<typeout<<">,\""
561                         <<name<<"\")"<<bbtkendl);
562
563
564     BlackBox::Pointer b; 
565     PackageMapType::const_iterator i;
566     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
567       {
568         b = i->second->NewWidgetAdaptor(typein,
569                                                  typeout,
570                                                  name);
571       if (b) break; 
572       }
573     if (!b) 
574       {
575         bbtkError("no "<<typein<<" to "<<typeout
576                   <<"> widget adaptor available");
577       } 
578     
579     bbtkDebugDecTab("Kernel",7);
580     return b; 
581   }
582   //===================================================================
583
584   //===================================================================
585   /// Creates an instance of a black box of type <type> with name <name>
586   bool Factory::FindAdaptor(const DataInfo& typein,
587                                   const DataInfo& typeout,
588                                   std::string& adaptor) const
589   {
590     bbtkDebugMessageInc("Kernel",8,"Factory::FindAdaptor(<"
591                         <<typein<<">,<"
592                         <<typeout<<">)"<<bbtkendl);
593     
594     bool b = false;
595     PackageMapType::const_iterator i;
596     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
597       {
598         b = i->second->FindAdaptor(typein,
599                                             typeout,
600                                             adaptor);
601         if (b) break; 
602       }
603     /*
604     if (!b) 
605       {
606         bbtkError("no "<<typein<<" to "<<typeout
607                   <<"> widget adaptor available");
608       } 
609     */
610
611     bbtkDebugDecTab("Kernel",7);
612     return b; 
613   }
614   //===================================================================
615
616   //===================================================================
617   /// Creates an instance of a black box of type <type> with name <name>
618   bool Factory::FindWidgetAdaptor(const DataInfo& typein,
619                                   const DataInfo& typeout,
620                                   std::string& adaptor) const
621   {
622     bbtkDebugMessageInc("Kernel",8,"Factory::FindWidgetAdaptor(<"
623                         <<typein<<">,<"
624                         <<typeout<<">)"<<bbtkendl);
625     
626     bool b = false;
627     PackageMapType::const_iterator i;
628     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
629       {
630         b = i->second->FindWidgetAdaptor(typein,
631                                                   typeout,
632                                                   adaptor);
633         if (b) break; 
634       }
635     bbtkDebugDecTab("Kernel",7);
636     return b; 
637   }
638   //===================================================================
639
640   //===================================================================
641   /// Creates an instance of a black box of type <type> with name <name>
642   bool Factory::FindWidgetAdaptor2(const DataInfo& typein,
643                                   const DataInfo& typeout,
644                                   std::string& widget,
645                                   std::string& adaptor) const
646   {
647     bbtkDebugMessageInc("Kernel",8,"Factory::FindWidgetAdaptor(<"
648                         <<typein<<">,<"
649                         <<typeout<<">)"<<bbtkendl);
650     
651     bool b = false;
652     adaptor = widget = "";
653     PackageMapType::const_iterator i;
654     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
655       {
656         b = i->second->FindWidgetAdaptor(typein,
657                                                   typeout,
658                                                   widget);
659         if (b) break; 
660       }
661     if (!b) 
662       {
663         // Look for a widget adaptor with good nature out
664         bbtkMessage("Kernel",5,
665                     "*** Looking for a two pieces widget adaptor for : "
666                     << typein << "->"<<typeout<<std::endl);
667         for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
668           {
669             Package::AdaptorMapType::const_iterator j;
670             for (j=i->second->GetAdaptorMap().begin();
671                  j!=i->second->GetAdaptorMap().end();
672                  ++j)
673               {
674                 if ( ( j->first.mKind ==  
675                        BlackBoxDescriptor::DEFAULT_GUI) &&
676                      //(j->first.mTypeIn == typein) &&
677                      (j->first.mTypeOut.GetNature() == typeout.GetNature() ) 
678                      )
679                   {
680                     widget = j->second.lock()->GetTypeName();
681                     bbtkMessage("Kernel",5,
682                                 "===> Found first part : "<<widget
683                                 << " "<<j->first.mTypeIn<<"->"
684                                 <<j->first.mTypeOut<<std::endl);
685                     DataInfo ti( j->first.mTypeOut.GetType(), "");
686                     DataInfo to( typeout.GetType(), "");
687                     b = FindAdaptor( ti, to, adaptor );
688                     if (b) 
689                       {
690                         bbtkMessage("Kernel",5,
691                                     "===> Found second part : "<<adaptor
692                                     <<std::endl);
693                         break;
694                       }
695                     else
696                       {
697                         bbtkMessage("Kernel",5,
698                                     "===> No second part found"<<std::endl);
699                       }
700                   }
701               }
702             if (b) break;
703           }
704       }
705     bbtkDebugDecTab("Kernel",7);
706     return b; 
707   }
708   //===================================================================
709
710   //===================================================================
711   /// Creates an instance of a connection
712   Connection::Pointer Factory::NewConnection(BlackBox::Pointer from,
713                                              const std::string& output,
714                                              BlackBox::Pointer to,
715                                              const std::string& input) const
716   {
717     bbtkDebugMessage("Kernel",7,"Factory::NewConnection(\""
718                       <<from->bbGetName()<<"\",\""<<output<<"\",\""
719                       <<to->bbGetName()<<"\",\""<<input
720                       <<"\")"<<std::endl);
721     
722     return Connection::New(from,output,to,input,
723                            GetThisPointer<Factory>());
724   }
725   //===================================================================
726
727
728
729   //===================================================================
730   Package::Pointer Factory::GetPackage(const std::string& name) const
731   {
732     bbtkDebugMessageInc("Kernel",9,"Factory::GetPackage(\""<<name<<"\")"
733                          <<std::endl);
734
735     PackageMapType::const_iterator i = mPackageMap.find(name);
736     if ( i != mPackageMap.end() ) 
737     {
738       bbtkDebugDecTab("Kernel",9); 
739       return i->second;
740     }
741     else 
742     {
743        bbtkDebugDecTab("Kernel",9);
744        bbtkError("package \""<<name<<"\" unknown");
745     }
746     
747     bbtkDebugDecTab("Kernel",9);  
748   }
749   //===================================================================
750   
751
752   //===================================================================
753   void Factory::CheckPackages() const
754   {
755     bbtkMessage("debug",1,"****** Checking Factory "<<(void*)this
756                  <<std::endl);
757     PackageMapType::const_iterator i;
758     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
759       {
760         i->second->CheckBoxes();
761       }
762     bbtkMessage("debug",1,"****** Checking Factory "<<(void*)this
763                 <<" ... OK"<<std::endl);
764   }
765   //===================================================================
766
767   //===================================================================
768   void Factory::WriteDotFilePackagesList(FILE *ff)
769   {
770
771     bbtkDebugMessageInc("Kernel",9,"Factory::WriteDotFilePackagesList()"
772                          <<std::endl);
773
774     fprintf( ff , "\n");
775     fprintf( ff , "subgraph cluster_FACTORY {\n");
776     fprintf( ff , "  label = \"PACKAGES\"%s\n",  ";");
777     fprintf( ff , "  style=filled%s\n",";");
778     fprintf( ff , "  color=lightgrey%s\n",";");
779     fprintf( ff , "  rankdir=TB%s\n",";");
780
781     std::string url;
782     PackageMapType::const_iterator i;
783     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
784     {
785        url=GetPackage(i->first)->GetDocURL();
786        fprintf(ff,"  %s [shape=ellipse, URL=\"%s\"]%s\n",i->first.c_str(),url.c_str(),";" );
787     }
788     fprintf( ff , "}\n\n");
789     bbtkDebugDecTab("Kernel",9);
790   }
791   //===================================================================
792
793
794  void Factory::ShowGraphTypes(const std::string& name) const
795  {
796
797    bool found = false;
798    PackageMapType::const_iterator i;
799    for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
800    {
801       if (i->second->ContainsBlackBox(name)) 
802       {
803          std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
804
805             // Don't pollute the file store with  "temp_dir" directories ...    
806          std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
807          std::string directory = "\"" + default_doc_dir + separator + "temp_dir"  +separator + "\"";
808          std::string filename2 =  default_doc_dir + separator + "temp_dir" + separator + "tmp.html"; 
809
810 #if defined(_WIN32)  
811         std::string command("start \"Titre\" /D ");
812 #else 
813         std::string command("gnome-open ");
814 #endif
815         command=command + directory +" tmp.html";
816         FILE *ff;
817         ff=fopen(filename2.c_str(),"w");
818
819         fprintf(ff,"<html><head><title>TMP</title> <script type=\"text/javascript\"> <!--\n");
820         fprintf(ff,"  window.location=\"%s#%s\";\n" , i->second->GetDocURL().c_str(),name.c_str() );
821         fprintf(ff,"//--></script></head><body></body></html>\n");
822
823
824         //fprintf(ff, "<a  href=\"%s#%s\">Link</a>\n", i->second->GetDocURL().c_str(),name.c_str() );
825         fclose(ff);
826         system( command.c_str() );      
827         found = true;
828      }
829    }
830     
831    bbtkDebugDecTab("Kernel",9);
832    if (!found) 
833    {
834       bbtkError("No package of the factory contains any black box <"
835                 <<name<<">");
836    }
837  }
838     
839
840
841
842   void Factory::CreateHtmlIndex(IndexEntryType type, 
843                                 const std::string& filename)
844   {
845     bbtkDebugMessageInc("Kernel",9,"Factory::CreateHtmlIndex(\""
846                         <<filename<<"\")"<<bbtkendl);
847     
848     std::string title;
849
850     typedef std::map<std::string, 
851       std::vector<BlackBoxDescriptor::Pointer> > IndexType;
852     IndexType index;
853     // Builds the index map
854     PackageMapType::const_iterator i;
855     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
856       {
857         Package::Pointer pack = i->second;
858         if (pack->GetName()=="user") continue;
859         Package::BlackBoxMapType::const_iterator j;
860         for (j = pack->GetBlackBoxMap().begin(); 
861              j!= pack->GetBlackBoxMap().end(); 
862              ++j)
863           {
864             
865             // Skip adaptors 
866             if ( type==Adaptors )
867               {  
868                 if (j->second->GetKind() == BlackBoxDescriptor::STANDARD )
869                   continue;
870               }
871             else 
872               if (j->second->GetKind() != BlackBoxDescriptor::STANDARD )
873                 continue;
874
875             std::vector<std::string> keys;
876             if (type==Packages)
877               {
878                 std::string k("");
879                 k += pack->GetName();
880                 keys.push_back(k);
881                 title = "Boxes by package";
882               }
883             else if ((type==Initials) || (type==Adaptors))
884               {
885                 std::string init(" ");
886                 init[0] =  std::toupper(j->second->GetTypeName()[0]);
887                 keys.push_back(init);
888                 title = "Alphabetical list";
889               }
890             else if (type==Categories)
891               {
892                 // Split the category string 
893                 std::string delimiters = ";,";
894                 Utilities::SplitString(j->second->GetCategory(),
895                                        delimiters,keys);
896                 if (keys.size()==0) 
897                   keys.push_back(" NONE");
898                 title = "Boxes by category";
899               }
900     
901             
902             std::vector<std::string>::const_iterator k;
903             for (k=keys.begin(); k!=keys.end(); ++k )
904               {
905                 IndexType::iterator p;
906                 p = index.find(*k);
907                 if (p != index.end()) 
908                   {
909                     p->second.push_back(j->second);
910                   }
911                 else 
912                   {
913                     std::vector<BlackBoxDescriptor::Pointer> v;
914                     v.push_back(j->second);
915                     index[*k] = v;
916                   }
917               }
918             
919           }
920       }   
921     // Creates the file 
922     //---------------------
923     // Open output file
924     std::ofstream s;
925     s.open(filename.c_str());
926     if (!s.good()) 
927     {
928        bbtkError("Factory::CreateHtmlIndex : could not open file '"
929                  <<filename<<"'");
930     }
931     
932     //----------------------
933     // Html head
934     s << "<html lang=\"en\">\n";
935     s << "<head>\n";
936     s << "<title>"<<title<<"</title>\n";
937     s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
938     s << "<meta name=\"description\" content=\""<<title<<"\">\n";
939     s << "<meta name=\"generator\" content=\"\">\n";
940     s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
941     //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
942     s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
943     s << "pre.display { font-family:inherit }\n";
944     s << "pre.format  { font-family:inherit }\n";
945     s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
946     s << "pre.smallformat  { font-family:inherit; font-size:smaller }\n";
947     s << "pre.smallexample { font-size:smaller }\n";
948     s << "pre.smalllisp    { font-size:smaller }\n";
949     s << "span.sc    { font-variant:small-caps }\n";
950     s << "span.roman { font-family:serif; font-weight:normal; } \n";
951     s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n"; 
952     s << "--></style>\n";
953     s << "</head>\n";
954     //----------------------
955
956     //----------------------
957     // Html body
958     s << "<body>\n";
959     s << "<a name=\"Top\"></a>\n"; 
960     s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
961     s << "<p>\n";
962     IndexType::iterator ii;
963     for (ii=index.begin();ii!=index.end();++ii)
964       {
965         s << "<a href=\"#"<<ii->first<<"\">"<<ii->first<<"</a>&nbsp;&nbsp;";    
966       }
967
968     for (ii=index.begin();ii!=index.end();++ii)
969       {
970         s << "<p><hr>\n";
971         s << "<p><a href=\"#Top\">Top</a>";
972         if (type==Packages)
973           {
974             s << "<a name=\""<<ii->first<<"\"></a>\n"; 
975             s << "<p><a href=\""<<ii->first<<"/index.html\">"
976               << ii->first<<"</a>\n"; 
977
978             s << "&nbsp;&nbsp;-&nbsp;&nbsp;\n"; 
979
980             s << "<a name=\"doxygen\"></a>\n"; 
981             s << "<a href=..\\doxygen\\" << ii->first << "/main.html>(Doxygen documentation of the source)</a>\n"; 
982           }
983         else 
984           {
985             s << "<a name=\""<<ii->first<<"\"></a>\n"; 
986             s << "<p><b>"<<ii->first<<"</b>\n";
987           }
988         s << "<ul>\n";
989
990         s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
991
992         std::vector<BlackBoxDescriptor::Pointer>::iterator di;
993         for (di=ii->second.begin();di!=ii->second.end();++di)
994           {
995             std::string pack = (*di)->GetPackage()->GetName();
996             std::string name = (*di)->GetTypeName();
997             Utilities::html_format(name);
998             std::string descr = (*di)->GetDescription();
999             Utilities::html_format(descr);
1000             s << "<TR>";
1001             s << "<TD style='vertical-align: top;'>";
1002             s << "&nbsp;&nbsp;&nbsp;<a href=\""<<pack
1003               <<"/index.html#"<<name<<"\">"
1004               <<pack<<"::"<<name<<"</a>";
1005             s << "</TD> ";
1006             s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1007             s << "</TR>\n";
1008           }    
1009         s << "</TABLE>\n";
1010         s << "</ul>\n";
1011         s << "</div>\n";
1012       }
1013     //----------------------
1014     // Footer 
1015     time_t rawtime;
1016     tm * ptm;
1017     time ( &rawtime );
1018     ptm = gmtime ( &rawtime );
1019
1020     s << "<p><hr>\n";
1021     s << "Automatically generated by <b>bbtk</b> on "
1022       << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900 
1023       << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1024     s << "</body></html>\n"; 
1025     s.close();
1026     //----------------------
1027
1028     // End
1029     bbtkDebugDecTab("Kernel",9);
1030   }
1031
1032  //==========================================================================
1033   std::string Factory::GetObjectName() const
1034   {
1035     return std::string("Factory");
1036   }
1037   //==========================================================================
1038   
1039   //==========================================================================
1040   std::string  Factory::GetObjectInfo() const 
1041   {
1042     std::stringstream i;
1043     return i.str();
1044   }
1045   //==========================================================================
1046
1047   //==========================================================================
1048 size_t  Factory::GetObjectSize() const 
1049 {
1050   size_t s = Superclass::GetObjectSize();
1051   s += Factory::GetObjectInternalSize();
1052   return s;
1053   }
1054   //==========================================================================
1055   //==========================================================================
1056 size_t  Factory::GetObjectInternalSize() const 
1057 {
1058   size_t s = sizeof(Factory);
1059   return s;
1060   }
1061   //==========================================================================
1062   //==========================================================================
1063   size_t  Factory::GetObjectRecursiveSize() const 
1064   {
1065     size_t s = Superclass::GetObjectRecursiveSize();
1066     s += Factory::GetObjectInternalSize();
1067
1068     PackageMapType::const_iterator i;
1069     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
1070     {
1071       s += i->second->GetObjectRecursiveSize();
1072     }
1073     return s;
1074   }
1075   //==========================================================================
1076
1077 }
1078