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