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