]> Creatis software - bbtk.git/blob - kernel/src/bbtkComplexBlackBoxDescriptor.cxx
Windows compilation
[bbtk.git] / kernel / src / bbtkComplexBlackBoxDescriptor.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkComplexBlackBoxDescriptor.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/02/18 10:41:02 $
7   Version:   $Revision: 1.10 $
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::ComplexBlackBoxDescriptor : describes a ComplexBlackBox (constituents, connections) and is able to create an instance of it.
22  */
23 #include "bbtkComplexBlackBoxDescriptor.h"
24 #include "bbtkComplexBlackBox.h"
25 //#include "bbtkFactory.h"
26 #include "bbtkMessageManager.h"
27 #include "bbtkUtilities.h"
28
29 namespace bbtk
30 {
31   
32
33   //=======================================================================
34   /// Default ctor
35   ComplexBlackBoxDescriptor::ComplexBlackBoxDescriptor(const std::string& name)
36     : BlackBoxDescriptor()
37   {
38     bbtkDebugMessageInc("Kernel",9,"ComplexBlackBoxDescriptor::ComplexBlackBoxDescriptor(\""<<name<<"\")"<<std::endl);
39     SetTypeName(name);
40     AddToCategory("complex box");
41     mPrototype = new ComplexBlackBox(name+std::string("Prototype"),this);
42     bbtkDebugDecTab("Kernel",9);
43   }
44   //=======================================================================
45
46
47
48   //=======================================================================
49   /// Default dtor
50   ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor()
51   {
52     bbtkDebugMessageInc("Kernel",9,"ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor(\""<<GetTypeName()<<"\")"<<std::endl);
53
54     delete mPrototype;
55
56     bbtkDebugDecTab("Kernel",9);
57   }
58   //=======================================================================
59
60
61   //=======================================================================
62   /// Creates an instance of name <name> of the ComplexBlackBox of which this is the descriptor 
63   BlackBox* ComplexBlackBoxDescriptor::CreateInstance(const std::string& name)
64   {
65     //bbtkError("ComplexBlackBoxDescriptor::CreateInstance not implemented");
66     bbtkDebugMessageInc("Kernel",5,
67                         "ComplexBlackBoxDescriptor::CreateInstance(\""
68                         <<name<<"\") ["
69                         <<GetTypeName()<<"]"<<std::endl);
70     
71     return mPrototype->bbClone(name);
72
73     bbtkDebugDecTab("Kernel",5);
74   }
75   //=======================================================================
76
77
78
79   //=======================================================================
80   /// Adds a black box to the complex box
81   void ComplexBlackBoxDescriptor::Add ( const std::string& type,
82                                         const std::string& name
83                                         )
84   {
85     bbtkDebugMessageInc("Kernel",5,
86                         "ComplexBlackBoxDescriptor::Add(\""
87                         <<type<<"\",\""<<name<<"\") ["
88                         <<GetTypeName()<<"]"<<std::endl);
89     
90     
91     // Verify that a box with the same name does not exist already
92     if ( mPrototype->bbUnsafeGetBlackBox( name ) ) 
93       {
94         bbtkError("a black box \""<<name<<"\" already exists");
95       }
96     // ok : create new one
97     mPrototype->bbAddBlackBox ( /*mFactory->Create*/ NewBlackBox(type,name) );
98
99     bbtkDebugDecTab("Kernel",5);
100   }
101   //=======================================================================
102
103  //=======================================================================
104   /// Adds a black box to the execution list 
105   void ComplexBlackBoxDescriptor::AddToExecutionList ( const std::string& box)
106   {
107     bbtkDebugMessageInc("Kernel",5,
108                         "ComplexBlackBoxDescriptor::AddToExecutionList(\""
109                         <<box<<"\" ["
110                         <<GetTypeName()<<"]"<<std::endl);
111     // Verify that the box exists
112     BlackBox* b = mPrototype->bbUnsafeGetBlackBox( box ); 
113     if ( !b ) 
114       {
115         bbtkError("the black box \""<<box<<"\" does not exist");
116       }
117     // ok 
118     mPrototype->bbAddToExecutionList ( box  );
119
120     bbtkDebugDecTab("Kernel",5);
121     }
122
123
124   //=======================================================================
125   /// Connects two black boxes of the complex box
126   void ComplexBlackBoxDescriptor::Connect ( const std::string& from,
127                                             const std::string& output,
128                                             const std::string& to,
129                                             const std::string& input
130                                             )
131   {
132     bbtkDebugMessageInc("Kernel",5,
133                         "ComplexBlackBoxDescriptor::Connect(\""
134                         <<from<<"\",\""<<output<<"\",\""
135                         <<to<<"\",\""<<input
136                         <<"\") ["
137                         <<GetTypeName()<<"]"<<std::endl);
138   // Verify that a box with the same name does not exist already
139     BlackBox* bbfrom = mPrototype->bbGetBlackBox( from );
140     if ( !bbfrom ) 
141       {
142         bbtkError("the black box \""<<from<<"\" does not exist");
143       }
144     BlackBox* bbto = mPrototype->bbGetBlackBox( to );
145     if ( !bbto ) 
146       {
147         bbtkError("the black box \""<<to<<"\" does not exist");
148       }
149     
150     Connection* c = /*mFactory->*/ NewConnection( bbfrom, output, bbto, input );
151
152     mPrototype->bbAddConnection(c);
153
154     bbtkDebugDecTab("Kernel",5);
155   }
156   //=======================================================================
157
158
159   //=======================================================================
160   /// Defines an input of the complex box
161   void ComplexBlackBoxDescriptor::DefineInput ( const std::string& name,
162                                                 const std::string& box,
163                                                 const std::string& input,
164                                                 const std::string& help)
165   {
166     bbtkDebugMessageInc("Kernel",5,
167                         "ComplexBlackBoxDescriptor::DefineInput(\""
168                         <<name<<"\",\""<<box<<"\",\""
169                         <<input<<"\",\""<<help
170                         <<"\") ["
171                         <<GetTypeName()<<"]"<<std::endl);
172
173     BlackBox* bb = mPrototype->bbGetBlackBox( box );
174     if ( !bb ) 
175       {
176         bbtkError("the black box \""<<box<<"\" does not exist");
177       }
178
179     if (!bb->bbHasInput(input) )
180       {
181         bbtkError("the black box \""<<box<<"\" does not have input \""
182                   <<input<<"\"");
183       }
184     
185     AddInputDescriptor ( new ComplexBlackBoxInputDescriptor 
186                          ( typeid(ComplexBlackBoxDescriptor),
187                            name,
188                            help,
189                            box,
190                            input,
191                            bb->bbGetInputType(input)));
192     
193     
194     bbtkDebugDecTab("Kernel",5);
195   }
196   //=======================================================================
197
198   //=======================================================================
199   /// Defines an output of the complex box
200   void ComplexBlackBoxDescriptor::DefineOutput ( const std::string& name,
201                                                  const std::string& box,
202                                                  const std::string& output,
203                                                  const std::string& help)
204   {
205     bbtkDebugMessageInc("Kernel",5,
206                         "ComplexBlackBoxDescriptor::DefineOutput(\""
207                         <<name<<"\",\""<<box<<"\",\""
208                         <<output<<"\",\""<<help
209                         <<"\") ["
210                         <<GetTypeName()<<"]"<<std::endl);
211
212     BlackBox* bb = mPrototype->bbGetBlackBox( box );
213     if ( !bb ) 
214       {
215         bbtkError("the black box \""<<box<<"\" does not exist");
216       }
217
218     if (!bb->bbHasOutput(output) )
219       {
220         bbtkError("the black box \""<<box<<"\" does not have output \""
221                   <<output<<"\"");
222       }
223     
224     AddOutputDescriptor ( new ComplexBlackBoxOutputDescriptor 
225                           ( typeid(ComplexBlackBoxDescriptor),
226                             name,
227                             help,
228                             box,
229                             output,
230                             bb->bbGetOutputType(output)));
231     
232     
233     bbtkDebugDecTab("Kernel",5);
234   }
235   //=======================================================================
236
237   //=======================================================================
238   void ComplexBlackBoxDescriptor::PrintBlackBoxes()
239   {
240     mPrototype->bbPrintBlackBoxes(); 
241   }
242   //=======================================================================
243
244
245   //=======================================================================
246   void ComplexBlackBoxDescriptor::InsertHTMLGraph( std::ofstream& s , 
247                                                    int detail, int level, 
248                                                    const std::string& output_dir, bool relative_link )   
249   {
250     this->mPrototype->bbInsertHTMLGraph( s, 
251                                          detail, level, 
252                                          false, 
253                                          output_dir,
254                                          relative_link );
255   }
256   //=======================================================================
257
258   //=========================================================================
259   void ComplexBlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s, 
260                                                    int detail, int level,
261                                                    const std::string& output_dir, bool relative_link)
262   {
263 printf("ComplexBlackBoxDescriptor::InsertHtmlHelp  01  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
264     bbtkDebugMessageInc("Kernel",9,
265                         "ComplexBlackBoxDescriptor::InsertHtmlHelp() ["
266                         <<GetTypeName()<<"]"<<std::endl);
267     
268     //-------------
269     // General info 
270     std::string name = GetTypeName();
271     Utilities::html_format(name);
272
273     //   std::ofstream* s = &s1;
274
275     (s) << "<p><hr>\n";
276     (s) << "<a name=\""<<name<<"\"></a>\n";
277     (s) << //"Top:&nbsp;
278       "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
279     // (s) << "Previous:&nbsp;<a rel="previous" accesskey="p" href="#dir">(dir)</a>,
280     // (s) << "Up:&nbsp;<a rel="up" accesskey="u" href="#dir">(dir)</a>
281     (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
282
283
284     std::string descr = GetDescription();
285     Utilities::html_format(descr);
286     
287     std::string author = GetAuthor();
288     Utilities::html_format(author);
289     
290     std::vector<std::string> categories;
291     // Split the category string 
292     std::string delimiters = ";,";
293     Utilities::SplitString(GetCategory(),
294                            delimiters,categories);
295
296         
297     (s) << "<p><TABLE cellspacing=0  cellpadding=3>\n";
298     (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
299         << descr << "</TD></TR>\n";
300
301     (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
302         << author << "</TD></TR>\n";
303
304     (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  ";
305     std::vector<std::string>::iterator ci;
306     for (ci=categories.begin(); ci!=categories.end(); ++ci)
307       {
308         s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci 
309           << "</a>&nbsp;\n";
310       }
311     s << "</TD></TR>\n";      
312     std::string inc = GetScriptFileName();
313     if (inc.size()>0) 
314       {
315         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 ";
316         s << inc << "&nbsp;&nbsp;<a href=\"../../../bbs/"<<inc<<"\">[source]</a>";
317         s << "</TD></TR>\n";
318         
319       }
320     
321     const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
322         
323     if (B.size()) 
324     {
325            (s) << "<TR><TD style='vertical-align: top;'><b> Uses </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  ";
326
327            std::set<BlackBoxDescriptor*> pdeps;
328            ComplexBlackBox::BlackBoxMapType::const_iterator b;
329            for ( b = B.begin(); b != B.end(); ++b ) 
330            {
331               BlackBoxDescriptor* d = b->second->bbGetDescriptor();
332               if (pdeps.find(d) != pdeps.end()) 
333             continue;
334               pdeps.insert(d);
335
336               Package* p = d->GetPackage();
337             
338               std::string name = b->second->bbGetTypeName();
339
340               std::string url;
341               if (relative_link) 
342                  url = p->GetDocRelativeURL();
343               else 
344                  url = p->GetDocURL();
345
346               s << "<a href=\"" <<url<<"#"<<name<<"\">" 
347                 << p->GetName()<<"::"<<name<<"</a>\n";
348             }   
349         
350             (s) << "</TD></TR>\n";
351
352      }
353
354      (s) << "</TABLE>\n";
355
356  
357    //-------------
358     // Graph
359     InsertHTMLGraph( s , detail,level, output_dir, relative_link);
360     
361     //-------------
362     // Inputs
363     std::string col("#CCCCFF");
364     
365     //  (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
366     (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
367     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
368       <<"\">Inputs</TD></TR>\n";
369     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
370       GetInputDescriptorMap();
371     
372     InputDescriptorMapType::const_iterator in;
373     
374     for ( in = imap.begin();  in != imap.end(); ++in ) 
375       {
376         std::string name(in->second->GetName());
377         Utilities::html_format(name);
378         
379         std::string type("<");
380         type += in->second->GetTypeName();    
381         type += ">";
382         Utilities::html_format(type);
383         
384         std::string descr(in->second->GetDescription());
385         Utilities::html_format(descr);
386
387         (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
388           << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
389           << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
390         
391       }
392     //  (s) << "</TABLE>\n";
393     
394     
395     //-------------
396     // Outputs
397     //  (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
398     //  (s) << "<TABLE border=1 cellspacing=0>\n";
399     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
400       <<"\">Outputs</TD></TR>\n";
401     
402     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
403       GetOutputDescriptorMap();
404     
405     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
406     
407     for ( o = omap.begin();  o != omap.end(); ++o ) 
408       {
409         std::string name(o->second->GetName());
410         Utilities::html_format(name);
411         
412         std::string type("<");
413         type += o->second->GetTypeName();    
414         type += ">";
415         Utilities::html_format(type);
416         
417         std::string descr(o->second->GetDescription());
418         Utilities::html_format(descr);
419         
420         (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
421           << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
422           << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
423         
424       }
425     (s) << "</TABLE>\n";
426
427     //------------
428     // End
429
430     bbtkDebugDecTab("Kernel",9);
431 printf("ComplexBlackBoxDescriptor::InsertHtmlHelp  02  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
432    }
433   //=========================================================================
434  
435
436   //=======================================================================
437   void ComplexBlackBoxDescriptor::GetHelp(bool full) const
438   {
439     if (full) bbtkMessage("Help",1,"Complex Black Box <"<<
440                           GetPackage()->GetName()<<"::"
441                           <<GetTypeName()<<">"<<std::endl);
442     bbtkMessage("Help",1," "                << GetDescription() <<std::endl);
443     bbtkMessage("Help",1," By : "           << GetAuthor()      <<std::endl);
444     bbtkMessage("Help",1," Category(s) : "  << GetCategory()     <<std::endl);    
445     if (mInput.size()) 
446       bbtkMessage("Help",1," * Inputs : "<<std::endl);
447     else 
448       bbtkMessage("Help",1," * No inputs"<<std::endl);
449     InputDescriptorMapType::const_iterator i;
450     unsigned int namelmax = 0;
451     unsigned int typelmax = 0;
452     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
453     {
454            if (i->second->GetName().size()>namelmax) 
455              namelmax = i->second->GetName().size();
456            if (i->second->GetTypeName().size()>typelmax) 
457              typelmax = i->second->GetTypeName().size();
458     }
459     OutputDescriptorMapType::const_iterator o;
460     if (full) 
461     {
462            for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
463            {
464              if (o->second->GetName().size()>namelmax) 
465                namelmax = o->second->GetName().size();
466             if (o->second->GetTypeName().size()>typelmax) 
467                typelmax = o->second->GetTypeName().size();
468            }
469     }
470     //
471
472     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
473     {
474            std::string name(i->second->GetName());
475            name += "'";
476            name.append(1+namelmax-name.size(),' ');
477            std::string type(i->second->GetTypeName());
478            type += ">";
479            type.append(1+typelmax-type.size(),' ');
480            bbtkMessage("Help",1,
481                     "    '"<<name
482                     <<" <"<<type
483                     <<" : "<<i->second->GetDescription()<<std::endl);
484     }
485     if (full) 
486     {
487            if (mOutput.size()) 
488              bbtkMessage("Help",1," * Outputs : "<<std::endl);
489            else 
490              bbtkMessage("Help",1," * No outputs"<<std::endl);
491            for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
492            {
493              std::string name(o->second->GetName());
494              name += "'";
495              name.append(1+namelmax-name.size(),' ');
496              std::string type(o->second->GetTypeName());
497              type += ">";
498              type.append(1+typelmax-type.size(),' ');
499              bbtkMessage("Help",1,
500                          "    '"<<name
501                          <<" <"<<type
502                          <<" : "<<o->second->GetDescription()<<std::endl);
503            }
504     }
505     if (full) 
506     {
507            const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
508         
509            if (B.size()) 
510              bbtkMessage("Help",1," * Boxes : "<<std::endl);
511            else 
512              bbtkMessage("Help",1," * No boxes"<<std::endl);
513         
514            ComplexBlackBox::BlackBoxMapType::const_iterator b;
515            for ( b = B.begin(); b != B.end(); ++b ) 
516            {
517              bbtkMessage("Help",1,"    '"<<b->second->bbGetName()<<
518                          "' <"
519                          << b->second->bbGetDescriptor()->GetPackage()->GetName() 
520                          <<"::"
521                          <<b->second->bbGetTypeName()<<">"<<std::endl);
522            }
523     }
524
525   }   
526   //=======================================================================
527
528
529
530 }