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