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