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