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