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