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