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