]> Creatis software - bbtk.git/blob - kernel/src/bbtkPackage.cxx
Global factory in course of removal... does not compile but have to commit to continu...
[bbtk.git] / kernel / src / bbtkPackage.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkPackage.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/07 08:40:14 $
7   Version:   $Revision: 1.10 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See doc/license.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /**
19  *\file
20  *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
21  */
22 #include "bbtkPackage.h"
23 #include "bbtkMessageManager.h"
24 #include "bbtkConfigurationFile.h"
25 #include <fstream>
26 #include <time.h>
27 #include "bbtkUtilities.h"
28
29 namespace bbtk
30 {
31   //==========================================================================
32   /// Ctor with the name of the package
33   Package::Package(const std::string& name,
34                    const std::string& author,
35                    const std::string& description,
36                    const std::string& version,
37                    const std::string& BBTKVersion) 
38     :
39     mName(name),
40     mAuthor(author),
41     mDescription(description),
42     mVersion(version),
43     mBBTKVersion(BBTKVersion)
44   {
45     std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
46     char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
47     std::string url = default_doc_dir; 
48     if (c != '/' && c !='\\') url = url + "/";
49     url = url +  "temp_dir/" + name + "/index.html";    
50     
51     SetDocURL(url);
52     SetDocRelativeURL("Relative url not set");
53
54     /*
55     std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH));
56     relurl += "/packages/"+name+"/bbdoc/index.html";
57     std::string url = ConfigurationFile::GetInstance().Get_url()
58       + relurl; 
59     SetDocURL(url);
60     SetDocRelativeURL(relurl);   
61     */
62
63     //    std::cout  << "   url=["<<url<<"]"<<std::endl;
64     //    std::cout  << "relurl=["<<relurl<<"]"<<std::endl;
65     bbtkDebugMessage("Kernel",7,"Package::Package(\""<<name<<"\")"<<bbtkendl);
66   }
67   //==========================================================================
68
69
70
71   //==========================================================================
72   /// Dtor
73   Package::~Package()
74   {
75     bbtkDebugMessageInc("Kernel",7,"Package::~Package(\""<<mName<<"\")"<<bbtkendl);
76     BlackBoxMapType::const_iterator i;
77     for (i=mBlackBoxMap.begin();
78          i!=mBlackBoxMap.end();
79        ++i) 
80      {
81         delete i->second;
82      } 
83     // Adaptors are also stored in the black box map : hence already deleted
84     /*
85     AdaptorMapType::const_iterator j;
86     for (j=mAdaptorMap.begin();
87          j!=mAdaptorMap.end();
88        ++j) 
89       {
90          delete j->second;
91       }
92     */ 
93     bbtkDebugDecTab("Kernel",7);
94   }
95   //==========================================================================
96
97
98
99   //==========================================================================
100   /// Creates an instance of a black box of type <type> with name <name>
101   BlackBox* Package::NewBlackBox(const std::string& type, 
102                                     const std::string& name) const
103   {
104     bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
105     
106     BlackBoxMapType::const_iterator i = mBlackBoxMap.find(type);
107     if (i == mBlackBoxMap.end())  
108     {
109            bbtkDebugDecTab("Kernel",8);
110            return 0;
111     }
112     BlackBox* bb =i->second->CreateInstance(name);
113     bbtkDebugDecTab("Kernel",8);
114     return bb;   
115
116   }
117   //==========================================================================
118
119
120
121   //==========================================================================
122   /// Creates an instance of an adaptor of input type <typein> and 
123   /// output type <typeout>  with name <name>
124   BlackBox* Package::NewAdaptor(TypeInfo typein,
125                                    TypeInfo typeout,
126                                    const std::string& name) const
127   {
128     bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
129                         ">::NewAdaptor(<"
130                         <<TypeName(typein)<<">,<"
131                         <<TypeName(typeout)<<">,\""
132                         <<name<<"\")"<<bbtkendl);
133
134     AdaptorKey key(typein,typeout);
135     AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
136     if (i == mAdaptorMap.end())  
137       {
138         bbtkDebugDecTab("Kernel",8);
139         return 0;
140       }
141     BlackBox* bb =i->second->CreateInstance(name);
142     bbtkDebugDecTab("Kernel",8);
143     return bb;   
144
145   }
146   //==========================================================================
147
148
149
150
151   //==========================================================================
152   /// Registers a black box descriptor in the package
153   bool Package::RegisterBlackBox(BlackBoxDescriptor* d) 
154   {
155     bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\")"<<std::endl);
156     
157     mBlackBoxMap[d->GetTypeName()] = d;
158     d->SetPackage(this);
159     
160     // If it is a default adaptor, also register it in the adaptors map
161     if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR) 
162       {
163         TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
164         TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
165         AdaptorKey key(typein,typeout);
166         AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
167         if (i == mAdaptorMap.end())  
168           {
169             bbtkDebugMessage("Kernel",8,"The box is an adaptor, inserting it in adaptors map ..."<<std::endl);   
170             mAdaptorMap[key] = d;
171           }
172         // If already an adaptor registered : error
173         else 
174           {
175             bbtkError("Package <"<<GetName()<<
176                       "> : trying to register black box <"
177                       <<d->GetTypeName()
178                       <<"> as default adaptor but there is already a default adaptor registered (<"
179                       <<i->second->GetTypeName()<<">)");
180           }
181       }
182     
183     bbtkDebugDecTab("Kernel",8);
184    
185     return true;
186   }
187   //==========================================================================
188   
189
190   //==========================================================================
191   /// UnRegisters a black box descriptor from the package
192   void Package::UnRegisterBlackBox(const std::string& name) 
193   {
194     bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::UnRegisterBlackBox(\""<<name<<"\")"<<std::endl);
195     // Looking into the bb map
196     BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
197     if (i == mBlackBoxMap.end())  
198     {
199        bbtkDebugDecTab("Kernel",8);
200        bbtkError("UnRegister : The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
201      }
202     mBlackBoxMap.erase(i);
203     // Is it also in the adaptors map ?
204     /*
205     AdaptorMapType::iterator j = mAdaptorMap.find(name);
206     if (j != mAdaptorMap.end())  
207       {
208          mAdaptorMap.erase(j);
209       }
210     */    
211     bbtkDebugDecTab("Kernel",8);    
212   }
213   //==========================================================================
214
215   //==========================================================================
216   /// Changes the name of a black box type
217   void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
218   { 
219     bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
220     // Looking into the bb map
221     BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
222     if (i == mBlackBoxMap.end())  
223       {
224          bbtkDebugDecTab("Kernel",8);
225          bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
226       }
227
228     i->second->SetTypeName(newname);
229     mBlackBoxMap[newname] = i->second;
230     mBlackBoxMap.erase(i);
231
232     bbtkDebugDecTab("Kernel",8);    
233   }
234   //==========================================================================
235
236   /*
237
238   //==========================================================================
239   /// Registers an adaptor descriptor in the package
240   bool Package::RegisterAdaptor(BlackBoxDescriptor* d) 
241   {
242     bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterAdaptor(\""<<d->GetTypeName()<<"\")"<<std::endl);
243     
244     TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
245     TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
246     AdaptorKey key(typein,typeout);
247     
248     mAdaptorMap[key] = d;
249     return true;
250   }
251   //==========================================================================
252   */
253
254
255   //==========================================================================
256   /// Displays the list of black boxes of the package
257   void Package::PrintBlackBoxes(bool description, bool adaptors) const
258   {
259     unsigned int lmax = 0;
260     std::vector<std::string> names;
261     std::vector<std::string> kinds;
262     std::vector<std::string> descrs;
263
264     BlackBoxMapType::const_iterator i;
265     for (i=mBlackBoxMap.begin();
266          i!=mBlackBoxMap.end();
267          ++i) 
268       {
269         if ( adaptors || 
270              ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) ) 
271           {
272             std::string name("  ");
273             name += i->second->GetTypeName();
274             names.push_back(name);
275
276             std::string kind;
277             if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
278               {
279                 kind = std::string("[A]");
280               }
281             else if ( i->second->GetKind() == 
282                       BlackBoxDescriptor::DEFAULT_ADAPTOR )
283               {
284                 kind = std::string("[DA]");
285               }
286             kinds.push_back(kind);
287
288             unsigned int l = name.size()+kind.size();
289             if (l>lmax) lmax = l;
290
291             std::string descr;
292             if (description) 
293               {
294                 descr += " : ";
295                 descr += i->second->GetDescription();
296               } 
297             descrs.push_back(descr);
298           }
299       } 
300     
301
302     std::string offs;
303     offs.append(lmax+3,' ');
304     std::vector<std::string>::iterator ni,ci,di;
305     for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
306          ni != names.end(); ++ni, ++ci, ++di)
307       {
308         std::string space;
309         space.append(lmax - ni->size() - ci->size(),' ');
310         bbtkMessage("Help",1,*ni << space << *ci );
311         std::string d(*di);
312         unsigned int dmax = 75 - lmax;
313         //      while (d.size() > dmax ) 
314         //  {
315         if (d.size()>dmax) 
316           bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl);
317         else 
318           bbtkMessage("Help",1,d << std::endl);
319         //    d = d.substr(dmax,d.size());
320         //  }
321       }
322
323   }
324   //==========================================================================
325
326   //==========================================================================
327   /// Displays the list of adaptors of the package
328   void Package::PrintAdaptors(bool description) const
329   {
330     BlackBoxMapType::const_iterator i;
331     for (i=mBlackBoxMap.begin();
332          i!=mBlackBoxMap.end();
333          ++i) 
334       {
335         if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD ) 
336           {
337             bbtkMessage("Help",1,
338                         "  "<<i->second->GetTypeName());
339             if ( i->second->GetKind() == 
340                  BlackBoxDescriptor::DEFAULT_ADAPTOR )
341               {
342                 bbtkMessage("Help",1,
343                             " [default]");
344               }  
345             if (description) 
346               {
347                 bbtkMessage("Help",1,
348                             " : "<<i->second->GetDescription());
349
350               } 
351             bbtkMessage("Help",1,std::endl);
352           }
353       } 
354     /*
355     AdaptorMapType::const_iterator i;
356     for (i=mAdaptorMap.begin();
357          i!=mAdaptorMap.end();
358          ++i) 
359       {
360         bbtkMessage("Help",1,
361                     "  "<<i->second->GetTypeName());
362         if (detail_level>0) 
363           {
364             bbtkMessage("Help",1,
365                         " : "<<i->second->GetDescription());
366   
367           } 
368         bbtkMessage("Help",1,std::endl);
369       }
370     */ 
371   }
372   //==========================================================================
373
374   //==========================================================================
375   /// Prints help on a black box
376   void Package::HelpBlackBox(const std::string& name, bool full) const
377   {
378     bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
379                         <<name<<"\")"<<bbtkendl);
380
381     BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
382     if (i == mBlackBoxMap.end())  
383       {
384         bbtkDebugDecTab("Kernel",8);
385         bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
386       }
387     //    bbtkMessage("Help",1,"["<<GetName()<<"] ");
388     i->second->GetHelp(full);
389     bbtkDebugDecTab("Kernel",8);
390
391   }
392   //==========================================================================
393
394
395   //==========================================================================
396   /// Returns true iff the package contains the box of name boxname
397   bool Package::ContainsBlackBox(const std::string& name) const 
398   {
399     bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
400                         <<name<<"\")"<<bbtkendl);
401     
402     BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
403     if (i == mBlackBoxMap.end())  
404     {
405       bbtkDebugDecTab("Kernel",8);
406       return false;
407     }
408     bbtkDebugDecTab("Kernel",8);
409     return true;
410   }
411   //==========================================================================
412
413
414  
415   //==========================================================================
416   void Package::CreateHtmlPage(const std::string& filename,
417                                const std::string& caller,
418                                const std::string& source,
419                                const std::string& custom_header,
420                                const std::string& custom_title,
421                                int detail, 
422                                int level,
423                                bool relative_link ) const
424   {
425     bbtkDebugMessageInc("Kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
426                         <<filename<<"\")"<<bbtkendl);
427
428     //---------------------
429     // Open output file
430     std::ofstream s;
431     s.open(filename.c_str());
432     if (!s.good()) 
433     {
434        bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
435     }
436     
437     //----------------------
438     // Html head
439     std::string title = "BBTK Package "+GetName()+" "+GetVersion(); 
440
441     if (custom_title.length() != 0) title = custom_title;
442
443     s << "<html lang=\"en\">\n";
444     s << "<head>\n";
445     s << "<title>" << title << "</title>\n";
446     s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
447     s << "<meta name=\"description\" content=\""<<title<<"\">\n";
448     s << "<meta name=\"generator\" content=\"\">\n";
449     s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
450     //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
451     s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
452     s << "pre.display { font-family:inherit }\n";
453     s << "pre.format  { font-family:inherit }\n";
454     s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
455     s << "pre.smallformat  { font-family:inherit; font-size:smaller }\n";
456     s << "pre.smallexample { font-size:smaller }\n";
457     s << "pre.smalllisp    { font-size:smaller }\n";
458     s << "span.sc    { font-variant:small-caps }\n";
459     s << "span.roman { font-family:serif; font-weight:normal; } \n";
460     s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n"; 
461     s << "--></style>\n";
462     s << "</head>\n";
463     //----------------------
464
465     //----------------------
466     // Html body
467     s << "<body>\n";
468     s << "<a name=\"Top\"></a>\n"; 
469     
470     //----------------------
471     // Header
472     if ( custom_header.length() != 0) 
473       {
474         if ( custom_header != "none" )
475           { 
476             std::ifstream in;
477             in.open(custom_header.c_str());    
478             if (!in.good()) 
479               {
480                 bbtkError("Could not open file \""<<custom_header<<"\"");
481               }
482             char buffer[512];
483             while (!in.eof()) 
484               {
485                 in.getline(buffer,512);
486                 std::string line(buffer);
487                 s << line;
488               }
489             in.close();
490             s << "<hr>\n";
491            
492             /*   
493             s << "<object data=\"" << custom_header 
494               << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
495               << custom_header <<" could not be embedded.</object>\n";
496             
497             s << "<hr>\n";
498             */
499           }
500       }
501
502     else 
503       {
504         s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
505         s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
506         s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
507           << GetDescription() << "</TD></TR>\n";
508         s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
509           << GetAuthor() << "</TD></TR>\n";
510         s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
511           << GetCategory() << "</TD></TR>\n";
512         s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
513           << GetVersion() << "</TD></TR>\n";
514         s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
515           << GetBBTKVersion() << "</TD></TR>\n";
516         s << "</TABLE>\n";
517       }
518
519     //-------------------
520     // Table of contents
521     // Black boxes list
522     //  s << "<div class=\"contents\">\n";
523     s << "<p><b> Black Boxes : </b>\n";
524     s << "<ul>\n";
525
526     s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
527
528     BlackBoxMapType::const_iterator i;
529     for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i) 
530       {
531         if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD) 
532           continue;
533         
534         std::string name = i->second->GetTypeName();
535         Utilities::html_format(name);
536         std::string descr = i->second->GetDescription();
537         Utilities::html_format(descr);
538
539         s << "<TR>";
540         s << "<TD style='vertical-align: top;'>";
541         s << "&nbsp;&nbsp;&nbsp;<a name=\"toc_"<<name
542           <<"\" href=\"#"<<name<<"\">"
543           <<name<<"</a>";
544         s << "</TD> ";
545         s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
546         s << "</TR>\n";
547       }    
548     s << "</TABLE>\n";
549     
550     
551     s << "</ul>\n";
552     s << "</div>\n";
553     
554     //-------------------
555     // Adaptors list
556     if (mAdaptorMap.size()>0) 
557       {
558         //  s << "<div class=\"contents\">\n";
559         s << "<p><b> Adaptors : </b>\n";
560         s << "<ul>\n";
561
562         //    BlackBoxMapType::const_iterator i;
563         s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
564         for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i) 
565           {
566             if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) 
567               continue;
568     
569             std::string name = i->second->GetTypeName();
570             Utilities::html_format(name);
571             std::string descr = i->second->GetDescription();
572     
573             s << "<TR>";
574             s << "<TD style='vertical-align: top;'>";
575             s << "&nbsp;&nbsp;&nbsp;<a name=\"toc_"<<name
576               <<"\" href=\"#"<<name<<"\">"
577               <<name<<"</a>";
578             s << "</TD> ";
579             s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
580             s << "</TR>\n";
581           }    
582         s << "</TABLE>\n";
583
584         s << "</ul>\n";
585         s << "</div>\n";
586       }
587     
588     
589     //  s << "<div class=\"node\">\n";
590
591     //    s << "<p><hr>\n";
592     //    s << "<a name=\"Top\"></a>\n";
593     //  s << "Top:&nbsp;<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
594     // s << "Previous:&nbsp;<a rel="previous" accesskey="p" href="#dir">(dir)</a>,
595     // s << "Up:&nbsp;<a rel="up" accesskey="u" href="#dir">(dir)</a>
596     
597     //    s << "</div>\n";
598
599     //----------------------
600     // Boxes doc
601
602     //-------------------
603     // Computes output directory from filename to pass it to 
604     // BlackBoxDescriptor::InsertHtmlHelp
605     std::string dir;
606
607     std::string::size_type slash_position = filename.find_last_of("/\\");
608
609
610         if (slash_position != std::string::npos) {
611       if (slash_position == 0)
612          slash_position = 1;  
613       dir = filename.substr(0,slash_position);
614     }
615
616     for (i=mBlackBoxMap.begin();
617          i!=mBlackBoxMap.end();
618          ++i) 
619       {
620         i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
621       }    
622
623     //----------------------
624     // Footer 
625     time_t rawtime;
626     tm * ptm;
627     time ( &rawtime );
628     ptm = gmtime ( &rawtime );
629
630     s << "<p><hr>\n";
631     s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
632       //      <<source<<"</b>
633       <<"on "
634       << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900 
635       << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
636     s << "</body></html>\n"; 
637     s.close();
638     //----------------------
639
640     // End
641     bbtkDebugDecTab("Kernel",9);
642   }
643   //==========================================================================
644 }
645