]> Creatis software - bbtk.git/blob - kernel/src/bbtkFactory.cxx
Move some general usage methods to Utilities, to avoid dupplicate code :
[bbtk.git] / kernel / src / bbtkFactory.cxx
1 /*=========================================================================
2                                                                                 
3 Program:   bbtk
4 Module:    $RCSfile: bbtkFactory.cxx,v $
5 Language:  C++
6
7 Date:      $Date: 2008/01/22 16:55:04 $
8 Version:   $Revision: 1.2 $
9                                                                                 
10
11 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
12 l'Image). All rights reserved. See doc/license.txt or
13 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
14
15 This software is distributed WITHOUT ANY WARRANTY; without even
16 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17 PURPOSE.  See the above copyright notices for more information.
18
19 =========================================================================*/
20 /**
21  *\file
22  *\brief  Class bbtk::Factory : can load and unload dynamic libraries containing 
23  *        black boxes packages and create instances of the black boxes registered 
24  *       in the packages loaded.
25  */
26 #include "bbtkFactory.h"
27 #include "bbtkMessageManager.h"
28 #include "bbtkConnection.h"
29 #include "bbtkConfigurationFile.h"
30 #include "bbtkUtilities.h"
31
32 #include <sys/stat.h> // for struct stat stFileInfo
33
34 #if defined(_WIN32)
35 #include <direct.h> // for getcwd
36 #endif
37
38
39 // was in gdcm ...
40 /*
41 #ifdef _MSC_VER
42 #   define getcwd _getcwd
43 #endif
44
45 #if defined(_MSC_VER) || defined(__BORLANDC__)
46 #   include <direct.h>
47 #else
48 #   include <unistd.h>
49 #endif
50
51 */
52
53
54 namespace bbtk
55 {
56   typedef Package* (*PackageAccessor)();
57   typedef void (*PackageDeleteFunction)();
58
59
60   //===================================================================
61   /// Default ctor
62   Factory::Factory()
63   {
64     bbtkDebugMessage("Core",7,"Factory::Factory()"<<std::endl);
65   }
66   //===================================================================
67
68   //===================================================================
69   /// Dtor
70   Factory::~Factory()
71   {
72     bbtkDebugMessageInc("Core",7,"Factory::~Factory()"<<std::endl);
73     CloseAllPackages();
74     bbtkDebugDecTab("Core",7);
75   }
76   //===================================================================
77
78
79   //===================================================================
80   void Factory::CloseAllPackages()
81   {
82     bbtkDebugMessageInc("Core",7,"Factory::CloseAllPackages()"<<std::endl);
83     while (mPackageMap.begin() != mPackageMap.end())
84       {
85         PackageMapType::iterator i = mPackageMap.begin();
86         ClosePackage(i);
87       }
88     bbtkDebugDecTab("Core",7);
89   }
90   //===================================================================
91
92   //===================================================================
93   void Factory::Reset()
94   {
95     bbtkDebugMessageInc("Core",7,"Factory::Reset()"<<std::endl);
96     CloseAllPackages();
97     bbtkDebugDecTab("Core",7);
98   }
99   //===================================================================
100
101
102 // ===================================================================================
103
104   bool Factory::DoLoadPackage(std::string libname,
105                               std::string pkgname, 
106                               std::string path, 
107                               bool verbose)
108   {
109
110 #if defined(__GNUC__)
111
112         void *handler;            
113         handler = dlopen(libname.c_str(), 
114                                      BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
115         if (!handler)
116         {
117           if (verbose) {
118             std::cout <<"[" <<libname<<"] can't be open" << std::endl;
119             std::cout << "   " <<dlerror() << std::endl;
120           }
121           return false; // try next path
122         }
123       
124         if (verbose)
125           std::cout <<"   -->[" <<libname<<"] found" << std::endl;
126
127       // Loads the Package accessor
128
129         std::string getpackname(pkgname);
130         getpackname += "GetPackage";
131         void *getpack = dlsym(handler, getpackname.c_str());
132         if (!getpack)
133         {
134           dlclose(handler);
135           bbtkError("GetPackage : could not load package \""<<pkgname
136                    <<"\" [symbol "<<getpackname<<"] :"<<dlerror());
137         }
138
139         // Verifies that the Package delete function is present
140         std::string delfname(pkgname);
141         delfname += "DeletePackage";
142         void *delf = dlsym(handler, delfname.c_str());
143         if (!delf)
144         {
145           dlclose(handler);
146           bbtkError("DeletePackage : could not load package \""<<pkgname
147                     <<"\" [symbol "<<delfname<<"] :"<<dlerror());
148         } 
149
150 #elif defined(_WIN32)
151
152         HINSTANCE handler;
153      
154         SetErrorMode(0);
155         handler = LoadLibrary(libname.c_str());
156         if (!handler){
157           if (verbose)
158             std::cout <<"   no handler for [" <<libname<<"];" << std::endl;     
159           return false;// Problem with the found library
160         }
161         if (verbose)
162           std::cout <<"   --->[" <<libname<<"] found" << std::endl;
163
164     // Loads the Package accessor
165
166         std::string getpackname(pkgname);
167         getpackname += "GetPackage";
168         void *getpack = GetProcAddress(handler, getpackname.c_str());
169         if (!getpack)
170         {
171           FreeLibrary(handler);
172           bbtkError("[1]could not load package \""<<pkgname
173           <<"\" : "<<getpackname<<" symbol not found (is it a bbtk package lib ?)");
174           // look how to get the error message on win
175               //<<dlerror());
176
177         }
178         // Verifies that the Package delete function is present
179         std::string delfname(pkgname);
180         delfname += "DeletePackage";
181         void *delf = GetProcAddress(handler, delfname.c_str());
182         if (!delf)
183         {
184           FreeLibrary(handler);
185           bbtkError("[2]could not load package \""<<pkgname
186                      <<"\" : "<<delfname<<" symbol not found (is it a bbtk package lib ?)");
187           // look how to get the error message on win
188           //<<dlerror());
189         } 
190 #else
191         bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
192 #endif   
193   
194     // Stores the package
195         PackageInfoType pack;
196         pack.mDynamicLibraryHandler = handler;
197     // Invokes the accessor to the PackageUnit pointer
198         pack.mPackage = ((PackageAccessor)getpack)();
199
200         mPackageMap[pkgname] = pack;
201
202     // Test bbtk build version ok
203         if ( pack.mPackage->GetBBTKVersion() != bbtk::GetVersion() )
204         {
205           std::string v(pack.mPackage->GetBBTKVersion());
206           UnLoadPackage(pkgname);
207           bbtkError(" package build with bbtk version "
208                     << v
209                     << " whereas application build with version "
210                     << bbtk::GetVersion());
211         }
212
213         std::string separator = 
214           ConfigurationFile::GetInstance().Get_file_separator ();
215         //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
216         std::string docreldoc = separator + "packages" + separator + pkgname 
217           + separator + "bbdoc" + separator + "index.html";
218         std::string reldoc = ".." + separator + ".." + separator 
219           + ".." + docreldoc;
220         std::string doc = path + separator + ".." + separator
221           + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) 
222           + docreldoc;
223      
224         //std::cout << "doc='"<<doc<<"'"<<std::endl;
225         
226         pack.mPackage->SetDocURL(doc);
227         pack.mPackage->SetDocRelativeURL(reldoc);
228         
229     //===================================================================
230         bbtkMessage("Output",2,pack.mPackage->GetName()<<" "
231            <<pack.mPackage->GetVersion()
232            <<" (bbtk "
233            <<pack.mPackage->GetBBTKVersion()<<") "
234            <<pack.mPackage->GetAuthor() 
235            <<std::endl);
236         bbtkMessage("Output",2,pack.mPackage->GetDescription()<<std::endl);
237     //===================================================================
238
239         bbtkDebugDecTab("Core",7);
240         return true;
241   }
242
243  //===================================================================
244   /// \brief Loads a package.
245   ///
246   /// The name is the system-independant name of the package (the name of the instance of bbtk::Package).
247   /// Tries to open the dynamic library :
248   /// - "libbb<name>.so" for linux systems,
249   /// - "bb<name>.dll" for windows systems.
250   /// If it succeeds, then tries to load the symbols "<name>GetPackage" and "<name>DeletePackage".
251   /// "<name>GetPackage" is called to get the pointer on the bbtk::Package of the library
252   /// ("<name>DeletePackage" is not used, its presence is just checked before loading the package).
253
254   /// now, filename is only the last name (no longer the full name!)
255   /// it will be searched within *all* the paths given in bbtk_config.xml
256
257   /// verbose = true (set by "config v") displays the loading process
258   void Factory::LoadPackage( const std::string& name,
259                              bool use_configuration_file, bool verbose)
260   {
261   // Note : in the following :
262   // name : the user supplied name 
263   //      - abreviated name    e.g.       pkg   pkg.so   libbpkg   libbbpkg.so 
264   //      - relative full name e.g.       ./libbbpkg.so   ../../libbbpkg.so 
265   //      - absolute full name e.g.       /home/usrname/proj/lib/libbbpkg.so
266   //          same for Windows, with      c:, d: ...
267   //
268     bbtkDebugMessageInc("Core",7,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);    
269     bbtkMessage("Debug",1,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);    
270     bbtkMessage("Debug",1,"use_configuration_file [" 
271                 << use_configuration_file << "]" << std::endl);
272   
273     std::vector<std::string> package_paths;
274     std::string libname;  // full path library name
275     std::string pkgname;  // e.g. libbb<pkgname>.so
276     
277     std::string upath;
278     pkgname = Utilities::ExtractPackageName(name,upath);
279
280     bbtkMessage("Debug",1,"Package name ["<<pkgname<<"]"<<std::endl);
281     bbtkMessage("Debug",1,"Package path ["<<upath<<"]"<<std::endl);
282
283     // no loading package if already loaded
284     PackageMapType::iterator iUnload;
285     iUnload = mPackageMap.find(pkgname);
286     if (iUnload != mPackageMap.end()) 
287     {
288       bbtkMessage("Output",2,"["<<pkgname<<"] already loaded"<<std::endl);
289       return; 
290     }
291     
292    // If path provided by user will be the first scanned : 
293     // push it into vector of paths
294     if (upath.length()>0) package_paths.push_back(upath);
295
296     // Add the path of config file 
297     if (use_configuration_file)
298     {
299       std::vector<std::string>::const_iterator pi;
300       for (pi =ConfigurationFile::GetInstance().Get_package_paths().begin();
301                 pi!=ConfigurationFile::GetInstance().Get_package_paths().end();
302               ++pi)
303              package_paths.push_back(*pi);
304     }
305  
306    
307     bool ok = false; 
308     bool foundFile = false;    
309
310     std::vector<std::string>::iterator i;
311     for (i=package_paths.begin();i!=package_paths.end();++i)
312       {
313         foundFile = false;    
314              std::string path = *i;
315         
316         // we *really* want '.' to be the current working directory
317         if (path == ".") {
318           char buf[2048]; // for getcwd
319           char * currentDir = getcwd(buf, 2048);
320           std::string cwd(currentDir);        
321           path = currentDir;
322         }
323         
324         libname = Utilities::MakeLibnameFromPath(path, pkgname);
325         
326              bbtkMessage("Debug",2,"-> Trying to load ["<<libname<<"]"<<std::endl);
327
328       // Check if library exists           
329         if ( !Utilities::FileExists(libname) )
330         {
331         // The following is *NOT* a debug time message :
332         // It's a user intended message.
333         // Please don't remove it.
334               if (verbose)
335                  std::cout <<"   [" <<libname <<"] : doesn't exist" <<std::endl;
336             continue;  // try next path
337         }
338         foundFile = true; 
339
340       // Try to Load the library
341          
342         ok = DoLoadPackage( libname, pkgname, path, verbose);
343         if (ok)
344              {
345                 bbtkMessage("Debug",2,"   OK"<<std::endl);
346                 break; // a package was found; we stop iterating
347              }
348     } //------------------ // end for ( package_paths.begin();i!=package_paths.end() )
349       //  }
350
351     if( !ok )  // nothing was loaded
352     {
353       if (!foundFile)
354       {
355         bbtkError("could not find package ["<<pkgname<< "]");
356       }
357       else
358       {
359 #if defined(__GNUC__)
360         bbtkError("could not load package \""<< pkgname
361                   <<"\" :" << std::endl << "   " <<dlerror());
362 #elif defined(_WIN32)
363         bbtkError("could not load package \""<<pkgname
364                  <<"\" : " << std::endl << "   " <<libname<<" not found");
365
366     // look how to get the error message on win
367     //<<dlerror());
368     // it is the bordel !! (the bloody fucking bordel, you mean?)
369     // look : http://msdn2.microsoft.com/en-us/library/ms680582.aspx
370 #endif
371       }
372     }
373     bbtkMessage("Output",2,"[" << libname << "] loaded" << std::endl);
374
375   }
376     
377   //===================================================================
378   /// \brief UnLoads a package.
379   ///
380   /// The package must have been previously loaded by LoadPackage.
381   /// If the entry is found in the map, calls ClosePackage
382  void Factory::UnLoadPackage( const std::string& userSuppliedName )
383   { 
384     std::string path;
385     std::string name = Utilities::ExtractPackageName(userSuppliedName,path);
386     bbtkDebugMessageInc("Core",7,"Factory::UnLoadPackage(\""
387                        <<name<<"\")"<<std::endl);
388   
389     PackageMapType::iterator i;
390     i = mPackageMap.find(name);
391     if (i == mPackageMap.end()) 
392     {
393       bbtkError("cannot unload package \""<<name
394                 <<"\" : package not loaded !");
395     }
396     ClosePackage(i);
397     bbtkDebugDecTab("Core",7);
398   }
399   //===================================================================
400
401
402   //===================================================================
403   /// \brief Close the package referenced by the iterator 
404   ///
405   /// If it is a dynamically loaded package 
406   /// - Loads and calls the function "<name>DeletePackage" of the dynamic library (responsible for package desallocation)
407   /// - Closes the dynamic library
408   /// - Erases the package entry in the packages map
409   ///
410   /// Else simply erases the package entry in the packages map
411   void Factory::ClosePackage(PackageMapType::iterator& i) 
412   {   
413      bbtkDebugMessageInc("Core",7,"Factory::ClosePackage(\""
414                          <<i->second.mPackage->GetName()
415                         <<"\")"<<std::endl);
416
417      if (i->second.mDynamicLibraryHandler) 
418      {
419  
420       // If it is a dynamically loaded package
421       // Loads the Package delete function
422
423         std::string delfname(i->second.mPackage->GetName());
424         delfname += "DeletePackage";
425 #if defined(__GNUC__)     
426         void *delf = dlsym(i->second.mDynamicLibraryHandler, delfname.c_str());
427         if (!delf)
428         {
429            bbtkError("could not close package \""
430                      <<i->second.mPackage->GetName()
431                      <<"\" :"<<dlerror());
432         }    
433 #elif defined(_WIN32)
434         void *delf = GetProcAddress(i->second.mDynamicLibraryHandler, 
435                                  delfname.c_str());
436         if (!delf)
437         {  
438            bbtkError("could not close package \""
439                 <<i->second.mPackage->GetName()
440                 <<"\" : "<<delfname
441                 <<" symbol not found (how did you open it ???");
442                //<<"\" :"<<dlerror());
443         }    
444 #endif     
445
446    // deletes the package
447    ((PackageDeleteFunction)delf)();
448
449    // closes the dl handler
450 #if defined(__GNUC__)  
451     dlclose(i->second.mDynamicLibraryHandler);  
452 #elif defined(_WIN32)
453
454     FreeLibrary(i->second.mDynamicLibraryHandler);
455 #endif
456     }
457     else 
458     {  
459        // If it is a manually inserted package 
460        delete i->second.mPackage;
461     }
462
463     // remove the entry in the map
464     mPackageMap.erase(i);
465     bbtkDebugDecTab("Core",7);
466  }
467   //===================================================================
468
469
470
471   //===================================================================  
472   /// Displays the list of packages loaded
473   void Factory::PrintPackages(bool details, bool adaptors) const
474   {
475     bbtkDebugMessageInc("Core",9,"Factory::PrintPackages"<<std::endl);
476
477     PackageMapType::const_iterator i;
478     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
479     {
480       bbtkMessage("Help",1, i->first << std::endl);
481       if (details) {
482          i->second.mPackage->PrintBlackBoxes(false,adaptors);
483       }
484     }
485
486     bbtkDebugDecTab("Core",9);
487   }
488   //===================================================================
489
490   //===================================================================  
491   /// Displays help on a package
492   void Factory::HelpPackage(const std::string& name/* &userSuppliedName */, bool adaptors) const
493   {
494    // std::string name = ExtractPackageName(userSuppliedName);
495     bbtkDebugMessageInc("Core",9,"Factory::HelpPackage(\""<<name<<"\")"
496                         <<std::endl);
497
498     PackageMapType::const_iterator i = mPackageMap.find(name);
499     if ( i != mPackageMap.end() ) 
500       {
501       bbtkMessage("Help",1, "Package "<<i->first<<" ");
502       if (i->second.mPackage->GetVersion().length()>0)
503         bbtkMessageCont("Help",1,"v" <<i->second.mPackage->GetVersion());
504       if (i->second.mPackage->GetAuthor().length()>0)
505         bbtkMessageCont("Help",1,"- "<<i->second.mPackage->GetAuthor());
506       bbtkMessageCont("Help",1,std::endl);
507       bbtkIncTab("Help",1);
508       bbtkMessage("Help",1,i->second.mPackage->GetDescription()<<std::endl);
509       if (i->second.mPackage->GetNumberOfBlackBoxes()>0) 
510         {
511           bbtkMessage("Help",1, "Black boxes : "<<std::endl);
512           i->second.mPackage->PrintBlackBoxes(true,adaptors);
513         }
514       else 
515         {
516           bbtkMessage("Help",1, "No black boxes"<<std::endl);
517         }
518       bbtkDecTab("Help",1);
519       }
520     else 
521       {
522       bbtkDebugDecTab("Core",9);
523       bbtkError("package \""<<name<<"\" unknown");
524       }
525     
526     bbtkDebugDecTab("Core",9);
527   }
528   //===================================================================
529
530   //===================================================================
531   /// Prints help on the black box of type <name>
532   void Factory::HelpBlackBox(const std::string& name, bool full) const
533   {
534     bbtkDebugMessageInc("Core",9,"Factory::HelpBlackBox(\""<<name<<"\")"
535                         <<std::endl);
536
537     bool found = false;
538     PackageMapType::const_iterator i;
539     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
540       {
541       if (i->second.mPackage->ContainsBlackBox(name)) 
542         {
543           i->second.mPackage->HelpBlackBox(name,full);
544           found = true;
545         }
546       }
547     
548     bbtkDebugDecTab("Core",9);
549     if (!found) 
550       {
551       bbtkError("No package of the factory contains any black box <"
552                  <<name<<">");
553       }
554   }  
555   //===================================================================
556
557
558   //=================================================================== 
559   /// Inserts a package in the factory
560   void Factory::InsertPackage( Package* p )
561   {
562     bbtkDebugMessageInc("Core",9,"Factory::InsertPackage(\""<<
563                         p->GetName()<<"\")"<<std::endl);
564
565     PackageInfoType pack;
566     pack.mDynamicLibraryHandler = 0;
567     
568     pack.mPackage = p;
569
570     mPackageMap[p->GetName()] = pack;
571     bbtkDebugDecTab("Core",9);
572   }
573   //===================================================================
574   
575   //=================================================================== 
576   /// Removes a package from the factory (and deletes it)
577   void Factory::RemovePackage( Package* p )
578   {
579     bbtkDebugMessageInc("Core",9,"Factory::RemovePackage(\""<<
580                         p->GetName()<<"\")"<<std::endl);
581
582     PackageMapType::iterator i;
583     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
584       {
585          if (i->second.mPackage == p) break;
586       };
587     
588     if (i!=mPackageMap.end())
589       {
590       ClosePackage(i);
591       }
592     else 
593       {
594       bbtkError("Factory::RemovePackage(\""<<
595                  p->GetName()<<"\") : package absent from factory");
596       }
597
598     bbtkDebugDecTab("Core",9);
599   }
600   //===================================================================
601   
602
603   //===================================================================
604   /// Creates an instance of a black box of type <type> with name <name>
605   BlackBox* Factory::NewBlackBox(const std::string& type, 
606                                  const std::string& name) const
607   {
608     bbtkDebugMessageInc("Core",7,"Factory::NewBlackBox(\""
609                         <<type<<"\",\""<<name<<"\")"<<std::endl);
610
611     BlackBox* b = 0; 
612     PackageMapType::const_iterator i;
613     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
614       {
615       b = i->second.mPackage->NewBlackBox(type,name);
616       if (b) break; 
617       }
618     if (!b) 
619       {
620        bbtkError("black box type \""<<type<<"\" unknown");
621       } 
622
623     bbtkDebugDecTab("Core",7);
624     return b;
625   }
626   //===================================================================
627
628   //===================================================================
629   /// Creates an instance of a black box of type <type> with name <name>
630   BlackBox* Factory::NewAdaptor(TypeInfo typein,
631                      TypeInfo typeout,
632                      const std::string& name) const
633   {
634     bbtkDebugMessageInc("Core",8,"Factory::NewAdaptor(<"
635                         <<TypeName(typein)<<">,<"
636                         <<TypeName(typeout)<<">,\""
637                         <<name<<"\")"<<bbtkendl);
638
639
640     BlackBox* b = 0; 
641     PackageMapType::const_iterator i;
642     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
643       {
644       b = i->second.mPackage->NewAdaptor(typein,typeout,name);
645       if (b) break; 
646       }
647     if (!b) 
648       {
649       bbtkError("no <"
650           <<TypeName(typein)<<"> to <"
651           <<TypeName(typeout)
652           <<"> adaptor available");
653       } 
654     
655     bbtkDebugDecTab("Core",7);
656     return b; 
657   }
658   //===================================================================
659
660   //===================================================================
661   /// Creates an instance of a connection
662   Connection* Factory::NewConnection(BlackBox* from,
663                                      const std::string& output,
664                                      BlackBox* to,
665                                      const std::string& input) const
666   {
667     bbtkDebugMessage("Core",7,"Factory::NewConnection(\""
668                       <<from->bbGetName()<<"\",\""<<output<<"\",\""
669                       <<to->bbGetName()<<"\",\""<<input
670                       <<"\")"<<std::endl);
671     
672     return new Connection(from,output,to,input);
673     /*  
674        Connection* c;
675     // !!! WARNING : WE NEED TO TEST THE TYPE NAME EQUALITY 
676     // BECAUSE IN DIFFERENT DYN LIBS THE type_info EQUALITY CAN 
677     // BE FALSE (DIFFERENT INSTANCES !)
678   
679     std::string t1 ( from->bbGetOutputType(output).name() );
680     std::string t2 ( to->bbGetInputType(input).name() );
681
682
683     if ( t1 == t2 ) 
684        //from->bbGetOutputType(output) ==
685        // to->bbGetInputType(input) )
686       {
687          c = new Connection(from,output,to,input);
688       }
689     else 
690       {
691          //   std::cout << "Adaptive connection "<<std::endl;
692          std::string name;
693          name = from->bbGetName() + "." + output + "-" 
694                                   + to->bbGetName() + "." + input; 
695
696          BlackBox* b = 0; 
697          PackageMapType::const_iterator i;
698          for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
699          {
700              b = i->second.mPackage->NewAdaptor(from->bbGetOutputType(output),
701                                                   to->bbGetInputType(input),
702                                                   name);
703              if (b) break; 
704          } 
705          if (!b)  
706          {  
707             bbtkError("did not find any <"
708                        <<TypeName(from->bbGetOutputType(output))
709                        <<"> to <"
710                        <<TypeName(to->bbGetInputType(input))
711                        <<"> adaptor");
712          } 
713          c = new AdaptiveConnection(from,output,to,input,b);
714       }
715       bbtkDebugDecTab("Core",7);
716     
717       return c;
718     */
719   }
720   //===================================================================
721
722
723
724   //===================================================================
725   const Package* Factory::GetPackage(const std::string& name) const
726   {
727     bbtkDebugMessageInc("Core",9,"Factory::GetPackage(\""<<name<<"\")"
728                          <<std::endl);
729
730     PackageMapType::const_iterator i = mPackageMap.find(name);
731     if ( i != mPackageMap.end() ) 
732     {
733       bbtkDebugDecTab("Core",9); 
734       return i->second.mPackage;
735     }
736     else 
737     {
738        bbtkDebugDecTab("Core",9);
739        bbtkError("package \""<<name<<"\" unknown");
740     }
741     
742     bbtkDebugDecTab("Core",9);  
743   }
744   //===================================================================
745   
746   //===================================================================
747   Package* Factory::GetPackage(const std::string& name) 
748   {
749     bbtkDebugMessageInc("Core",9,"Factory::GetPackage(\""<<name<<"\")"
750                          <<std::endl);
751
752     PackageMapType::const_iterator i = mPackageMap.find(name);
753     if ( i != mPackageMap.end() ) 
754     {
755       bbtkDebugDecTab("Core",9); 
756       return i->second.mPackage;
757     }
758     else 
759     {
760        bbtkDebugDecTab("Core",9);
761        bbtkError("package \""<<name<<"\" unknown");
762     }
763     
764     bbtkDebugDecTab("Core",9);  
765   }
766   //===================================================================
767
768   //===================================================================
769   void Factory::WriteDotFilePackagesList(FILE *ff)
770   {
771     bbtkDebugMessageInc("Core",9,"Factory::WriteDotFilePackagesList()"
772                          <<std::endl);
773
774     fprintf( ff , "\n");
775     fprintf( ff , "subgraph cluster_FACTORY {\n");
776     fprintf( ff , "  label = \"PACKAGES\"%s\n",  ";");
777     fprintf( ff , "  style=filled%s\n",";");
778     fprintf( ff , "  color=lightgrey%s\n",";");
779     fprintf( ff , "  rankdir=TB%s\n",";");
780
781     PackageMapType::const_iterator i;
782     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
783     {
784        std::string url = GetPackage(i->first)->GetDocURL();
785        fprintf(ff,"  %s [shape=ellipse, URL=\"%s\"]%s\n",
786                i->first.c_str(),
787                url.c_str(),";" );
788     }
789     fprintf( ff , "}\n\n");
790     bbtkDebugDecTab("Core",9);
791   }
792   //===================================================================
793
794
795   void Factory::ShowGraphTypes(const std::string& name) const
796   {
797     bool found = false;
798     PackageMapType::const_iterator i;
799     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
800       {
801         if (i->second.mPackage->ContainsBlackBox(name)) 
802           {
803             std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
804             
805             // Don't pollute the file store with  "doc_tmp" directories ...    
806             std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
807             std::string directory = "\"" + default_doc_dir + separator + "doc_tmp"  +separator + "\"";
808             std::string filename2 =  default_doc_dir + separator + "doc_tmp" + separator + "tmp.html"; 
809             
810 #if defined(_WIN32)  
811             std::string command("start \"Titre\" /D ");
812 #else 
813             std::string command("gnome-open ");
814 #endif
815             command=command + directory +" tmp.html";
816             FILE *ff;
817             ff=fopen(filename2.c_str(),"w");
818             
819             fprintf(ff,"<html><head><title>TMP</title> <script type=\"text/javascript\"> <!--\n");
820             fprintf(ff,"  window.location=\"%s#%s\";\n" , i->second.mPackage->GetDocURL().c_str(),name.c_str() );
821             fprintf(ff,"//--></script></head><body></body></html>\n");
822             
823             
824             //fprintf(ff, "<a  href=\"%s#%s\">Link</a>\n", i->second.mPackage->GetDocURL().c_str(),name.c_str() );
825             fclose(ff);
826             system( command.c_str() );      
827             found = true;
828           }
829       }
830     
831     bbtkDebugDecTab("Core",9);
832     if (!found) 
833       {
834         bbtkError("No package of the factory contains any black box <"
835                   <<name<<">");
836       }
837   }
838
839 }
840