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