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