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