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