]> Creatis software - bbtk.git/blob - kernel/src/bbtkBlackBoxDescriptor.cxx
=== MAJOR RELEASE ====
[bbtk.git] / kernel / src / bbtkBlackBoxDescriptor.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/04/18 12:59:15 $
7   Version:   $Revision: 1.16 $
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 /**
20  *  \file 
21  *  \brief Class bbtk::BlackBoxDescriptor : (abstract) describes a BlackBox (name, description, author) and is able to create an instance of it.
22  */
23 #include "bbtkBlackBoxDescriptor.h"
24 #include "bbtkMessageManager.h"
25 #include "bbtkPackage.h"
26 #include "bbtkUtilities.h"
27 #include "bbtkAtomicBlackBoxDescriptor.h"
28 #include "bbtkWxBlackBox.h"
29
30 namespace bbtk
31 {
32
33   typedef Package::Pointer PackagePointer;
34
35
36
37   //=========================================================================
38   /// Default ctor
39   BlackBoxDescriptor::BlackBoxDescriptor()  
40     : mTypeName("Unknown"), 
41       mDescription(""), 
42       mAuthor(""),
43       mCategory(""),
44       mKind(STANDARD),
45       mPackage()
46   {
47     bbtkDebugMessage("object",4,
48                      "==> BlackBoxDescriptor::BlackBoxDescriptor()"<<std::endl);
49    bbtkDebugMessage("object",4,
50                      "<== BlackBoxDescriptor::BlackBoxDescriptor()"<<std::endl);
51   }
52   //=========================================================================
53
54   //=========================================================================
55   /// Dtor
56   BlackBoxDescriptor::~BlackBoxDescriptor()
57   {
58     bbtkDebugMessage("object",4,
59                      "==> BlackBoxDescriptor::~BlackBoxDescriptor() ["
60                      <<mTypeName<<"]"<<std::endl);
61
62     // deletes all I/O descriptors
63     InputDescriptorMapType::iterator i;
64     for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
65     OutputDescriptorMapType::iterator o;
66     for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
67
68     bbtkDebugMessage("object",4,
69                      "<== BlackBoxDescriptor::~BlackBoxDescriptor() ["
70                      <<mTypeName<<"]"<<std::endl);
71   }
72   //=========================================================================
73
74   /*
75   //=======================================================================
76   /// Release
77   void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
78   {
79     bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
80                 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
81     long c = desc.use_count();
82     bbtkMessage("object",3," - ref count = "<<c<<std::endl);
83     // If only one ref 
84     if ((c == 1) && (desc.lock()->mPackage))
85       {
86         bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
87         
88         Package::WeakPointer pack = desc.lock()->mPackage;
89         Package::ReleaseBlackBoxDescriptor(pack,desc);
90       }
91     else 
92       {
93         bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
94       }    
95     bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
96                 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
97  
98   }
99   //=========================================================================
100   */
101
102   /*
103   //=========================================================================
104   /// Dtor
105   void BlackBoxDescriptor::UnReference()
106   {
107     bbtkDebugMessageInc("Kernel",1,
108                         "BlackBoxDescriptor::UnReference() ["
109                         <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
110     mRefCount--;
111     if (mRefCount<=0) 
112       {
113         bbtkDebugMessage("Kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
114         delete this;
115       }
116   }
117   //=========================================================================
118   */
119   //=========================================================================
120   /// Check
121   void BlackBoxDescriptor::Check(bool) const
122   {
123     
124   }
125   //=========================================================================
126
127   //=========================================================================
128   /// Adds the string to the BlackBox description
129   void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
130   {
131     bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToDescription(\""<<s<<
132                      "\") ["<<GetFullTypeName()<<"]"<<std::endl);
133     if (clear) mDescription = s; 
134     else mDescription += s;
135    }
136   //=========================================================================
137
138   //=========================================================================   
139   /// Adds the string to the BlackBox author list
140   void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
141   {
142     bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToAuthor(\""<<s<<"\") ["
143                      <<GetFullTypeName()<<"]"<<std::endl);
144     if (clear) mAuthor = s;
145     else mAuthor += s;
146   }
147   //=========================================================================
148
149   //=========================================================================  
150   /// Adds the string to the BlackBox category list
151   void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
152   {
153     bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToCategory(\""<<s<<"\") ["
154                      <<GetFullTypeName()<<"]"<<std::endl);  
155     if (clear) mCategory = s;
156     else mCategory += s;
157     mCategory += ";";
158   }  
159   //=========================================================================
160   
161   //=========================================================================
162   const BlackBoxDescriptor::InputDescriptor* 
163   BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
164   {
165     bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
166                         <<name<<"') ["<<GetFullTypeName()<<"]"<<std::endl);
167
168     InputDescriptorMapType::const_iterator i;
169     i = mInput.find(name);
170     if ( i == mInput.end() ) 
171     {
172             bbtkError("input '"<<name<<"' does not exist");
173     }
174     bbtkDebugDecTab("Kernel",9);
175     return i->second;
176   }
177   //=========================================================================
178
179   //=========================================================================
180   const BlackBoxDescriptor::OutputDescriptor* 
181   BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
182   {
183     bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
184                         <<name<<"') ["<<GetFullTypeName()<<"]"<<std::endl);
185
186     OutputDescriptorMapType::const_iterator i;
187     i = mOutput.find(name);
188     if ( i == mOutput.end() ) 
189     {
190             bbtkError("output '"<<name<<"' does not exist");
191     }
192     bbtkDebugDecTab("Kernel",9);
193     return i->second;
194   }
195   //=========================================================================
196
197   //=========================================================================
198   void BlackBoxDescriptor::GetHelp(bool full) const
199   {
200     bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetHelp() ["<<GetFullTypeName()<<"]"<<std::endl);
201
202     bbtkMessage("Help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
203     bbtkMessage("Help",1," "              <<GetDescription()<<std::endl);
204     bbtkMessage("Help",1," By : "         <<GetAuthor()     <<std::endl);
205     bbtkMessage("Help",1," Categories : " <<GetCategory()    <<std::endl);
206     if (mInput.size()) 
207       bbtkMessage("Help",1," * Inputs : "<<std::endl);
208     else 
209       bbtkMessage("Help",1," * No inputs"<<std::endl);
210     InputDescriptorMapType::const_iterator i;
211     unsigned int namelmax = 0;
212     unsigned int typelmax = 0;
213     unsigned int natlmax = 0;
214     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
215     {
216            if (i->second->GetName().size()>namelmax) 
217              namelmax = i->second->GetName().size();
218            if (i->second->GetHumanTypeName().size()>typelmax) 
219              typelmax = i->second->GetHumanTypeName().size();
220            if (i->second->GetNature().size()>natlmax) 
221              natlmax = i->second->GetNature().size();
222     }
223     OutputDescriptorMapType::const_iterator o;
224     for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
225     {
226            if (o->second->GetName().size()>namelmax) 
227              namelmax = o->second->GetName().size();
228            if (o->second->GetHumanTypeName().size()>typelmax) 
229              typelmax = o->second->GetHumanTypeName().size();
230            if (o->second->GetNature().size()>natlmax) 
231              natlmax = o->second->GetNature().size();
232     }
233     //
234     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
235     {
236            std::string name(i->second->GetName());
237            name += "'";
238            name.append(1+namelmax-name.size(),' ');
239            std::string type(i->second->GetHumanTypeName());
240            type += ">";
241            type.append(1+typelmax-type.size(),' ');
242            std::string nature(i->second->GetNature());
243            nature += "]";
244            nature.append(1+natlmax-nature.size(),' ');
245            bbtkMessage("Help",1,
246                        "    '"<<name
247                        <<" <"<<type
248                        <<" ["<<nature
249                        <<" : "<<i->second->GetDescription()<<std::endl);
250     }
251     if (mOutput.size()) 
252       bbtkMessage("Help",1," * Outputs : "<<std::endl);
253     else 
254       bbtkMessage("Help",1," * No outputs"<<std::endl);
255     for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
256     {
257            std::string name(o->second->GetName());
258            name += "'";
259            name.append(1+namelmax-name.size(),' ');
260            std::string type(o->second->GetHumanTypeName());
261            type += ">";
262            type.append(1+typelmax-type.size(),' ');
263            std::string nature(o->second->GetNature());
264            nature += "]";
265            nature.append(1+natlmax-nature.size(),' ');
266            bbtkMessage("Help",1,
267                     "    '"<<name
268                        <<" <"<<type
269                        <<" ["<<nature
270                        <<" : "<<o->second->GetDescription()<<std::endl);
271       }
272    
273      bbtkDebugDecTab("Kernel",9);
274   
275
276   }
277   //=========================================================================
278    
279   //=========================================================================
280   /// Returns the full name of the **TYPE** of the black box (+package name)
281   std::string BlackBoxDescriptor::GetFullTypeName() const
282   {
283     if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
284     return "::" + mTypeName;
285   }
286   //=========================================================================
287  
288   //=========================================================================
289   void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s, 
290                                             int detail, int level,
291                                             const std::string& output_dir,
292                                             bool relative_link )
293   {
294     bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::InsertHtmlHelp() ["<<GetFullTypeName()<<"]"<<std::endl);
295     
296     //-------------
297     // General info 
298     std::string name = GetTypeName();
299     Utilities::html_format(name);
300
301     (s) << "<p><hr>\n";
302     (s) << "<a name=\""<<name<<"\"></a>\n";
303     (s) << //"Top:&nbsp;
304       "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
305     // (s) << "Previous:&nbsp;<a rel="previous" accesskey="p" href="#dir">(dir)</a>,
306     // (s) << "Up:&nbsp;<a rel="up" accesskey="u" href="#dir">(dir)</a>
307     (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
308
309
310     std::string descr = GetDescription();
311     Utilities::html_format(descr);
312     std::string author = GetAuthor();
313     Utilities::html_format(author);
314
315     std::vector<std::string> categories;
316     // Split the category string 
317     std::string delimiters = ";,";
318     Utilities::SplitString(GetCategory(),
319                            delimiters,categories);
320
321     
322     (s) << "<p><TABLE cellspacing=0  cellpadding=3>\n";
323     (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
324       << descr << "</TD></TR>\n";
325     (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
326       << author << "</TD></TR>\n";
327     (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  ";
328     std::vector<std::string>::iterator ci;
329     for (ci=categories.begin(); ci!=categories.end(); ++ci)
330       {
331         s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci 
332           << "</a>&nbsp;\n";
333       }
334     s << "</TD></TR>\n";      
335
336     (s) << "<TR><TD style='vertical-align: top;'><b> To use it </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  include " 
337         << GetPackage()->GetName() << "</TD></TR>\n";
338     (s) << "</TABLE>\n";
339
340     //-------------
341     // Graph
342     //i->second->InsertHTMLGraph( &s , detail,level,dir);
343     
344     //-------------
345     // Inputs
346     std::string titlecol("#BBBBFF");
347     std::string usercol("#FFFFFF");
348     std::string ubbcol("#DDFFFF");
349     std::string wxbbcol("#EEFFFF");
350
351
352     //  (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
353     (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
354     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
355       <<"\">Inputs</TD></TR>\n";
356
357     std::vector<std::string> user_defined;
358     std::vector<std::string> ubb_defined;
359     std::vector<std::string> wxbb_defined;
360
361     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
362       GetInputDescriptorMap();
363     InputDescriptorMapType::const_iterator in;
364     for ( in = imap.begin();  in != imap.end(); ++in ) 
365     {
366       // Skips system-defined inputs
367       std::string col(usercol);
368       int iotype = 0;
369       if (in->second->GetCreatorTypeInfo() == 
370           typeid(AtomicBlackBoxDescriptor))
371         {
372           col = ubbcol; 
373           iotype = 1;
374         }
375       else if (in->second->GetCreatorTypeInfo() == 
376                typeid(WxBlackBoxDescriptor))
377         {
378           col = wxbbcol; 
379           iotype = 2;
380         }
381
382       std::string name(in->second->GetName());
383       Utilities::html_format(name);
384       
385       std::string type("<");
386       type += in->second->GetTypeName();    
387       type += ">";
388       Utilities::html_format(type);
389       
390       std::string descr(in->second->GetDescription());
391       Utilities::html_format(descr);
392       
393       std::string out = 
394         "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
395         +"\"><B><PRE> "+name+" </PRE></B></TD>"
396         + "<TD style='vertical-align: top;' bgcolor=\""+col
397         +"\"><I><PRE> "+type+" </PRE></I></TD>"
398         + "<TD style='vertical-align: top;' bgcolor=\""+col
399         +"\">"+descr+"</TD></TR>\n";
400       
401       if (iotype==0) user_defined.push_back(out);
402       else if (iotype==1) ubb_defined.push_back(out);
403       else if (iotype==2) wxbb_defined.push_back(out);
404       
405     }
406
407     std::vector<std::string>::iterator hi;
408     for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
409     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
410     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
411
412     user_defined.clear();
413     ubb_defined.clear();
414     wxbb_defined.clear();
415
416     //-------------
417     // Outputs
418     //  (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
419     //  (s) << "<TABLE border=1 cellspacing=0>\n";
420     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
421       <<"\">Outputs</TD></TR>\n";
422     
423     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
424       GetOutputDescriptorMap();
425     
426     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
427     
428     for ( o = omap.begin();  o != omap.end(); ++o ) 
429       {
430         std::string col(usercol);
431         int iotype = 0;
432         if (o->second->GetCreatorTypeInfo() == 
433             typeid(AtomicBlackBoxDescriptor))
434           {
435             col = ubbcol; 
436             iotype = 1;
437           }
438         else if (o->second->GetCreatorTypeInfo() == 
439                  typeid(WxBlackBoxDescriptor))
440           {
441             col = wxbbcol; 
442             iotype = 2;
443           }
444         
445         std::string name(o->second->GetName());
446         Utilities::html_format(name);
447         
448         std::string type("<");
449         type += o->second->GetTypeName();    
450         type += ">";
451         Utilities::html_format(type);
452         
453         std::string descr(o->second->GetDescription());
454         Utilities::html_format(descr);
455         
456         std::string out = 
457           "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
458           +"\"><B><PRE> "+name+" </PRE></B></TD>"
459           + "<TD style='vertical-align: top;' bgcolor=\""+col
460           +"\"><I><PRE> "+type+" </PRE></I></TD>"
461           + "<TD style='vertical-align: top;' bgcolor=\""+col
462           +"\">"+descr+"</TD></TR>\n";
463         
464         if (iotype==0) user_defined.push_back(out);
465         else if (iotype==1) ubb_defined.push_back(out);
466         else if (iotype==2) wxbb_defined.push_back(out);
467         
468       }
469     
470     for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
471     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
472     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
473
474     (s) << "</TABLE>\n";
475     
476     //------------
477     // End
478
479     bbtkDebugDecTab("Kernel",9);
480    }
481   //=========================================================================
482  
483 }