]> Creatis software - bbtk.git/blob - kernel/src/bbtkComplexBlackBoxDescriptor.cxx
Keywords
[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/30 12:14:43 $
7   Version:   $Revision: 1.2 $
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     
282     std::string author = GetAuthor();
283     Utilities::html_format(author);
284     
285     std::string keyword = GetKeyword();
286     Utilities::html_format(keyword);
287         
288     (s) << "<p><TABLE cellspacing=0  cellpadding=3>\n";
289     (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
290         << descr << "</TD></TR>\n";
291
292     (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
293         << author << "</TD></TR>\n";
294
295     (s) << "<TR><TD style='vertical-align: top;'><b> Keyword(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
296         << keyword << "</TD></TR>\n";      
297
298     std::string inc = GetScriptFileName();
299     if (inc.size()>0) 
300       {
301     (s) << "<TR><TD style='vertical-align: top;'><b> Include </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
302       << inc << "</TD></TR>\n";
303         
304       }
305     
306     const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
307         
308     if (B.size()) 
309     {
310            (s) << "<TR><TD style='vertical-align: top;'><b> Dependencies </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  ";
311
312            std::set<BlackBoxDescriptor*> pdeps;
313            ComplexBlackBox::BlackBoxMapType::const_iterator b;
314            for ( b = B.begin(); b != B.end(); ++b ) 
315            {
316               BlackBoxDescriptor* d = b->second->bbGetDescriptor();
317               if (pdeps.find(d) != pdeps.end()) 
318             continue;
319               pdeps.insert(d);
320
321               Package* p = d->GetPackage();
322             
323               std::string name = b->second->bbGetTypeName();
324
325               std::string url;
326               if (relative_link) 
327                  url = p->GetDocRelativeURL();
328               else 
329                  url = p->GetDocURL();
330
331               s << "<a href=\"" <<url<<"#"<<name<<"\">" 
332                 << p->GetName()<<"::"<<name<<"</a>\n";
333             }   
334         
335             (s) << "</TD></TR>\n";
336
337      }
338
339      (s) << "</TABLE>\n";
340
341  
342    //-------------
343     // Graph
344     InsertHTMLGraph( s , detail,level, output_dir, relative_link);
345     
346     //-------------
347     // Inputs
348     std::string col("#CCCCFF");
349     
350     //  (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
351     (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
352     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
353       <<"\">Inputs</TD></TR>\n";
354     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
355       GetInputDescriptorMap();
356     
357     InputDescriptorMapType::const_iterator in;
358     
359     for ( in = imap.begin();  in != imap.end(); ++in ) 
360       {
361         std::string name(in->second->GetName());
362         Utilities::html_format(name);
363         
364         std::string type("<");
365         type += in->second->GetTypeName();    
366         type += ">";
367         Utilities::html_format(type);
368         
369         std::string descr(in->second->GetDescription());
370         //Utilities::html_format(descr);
371
372         (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
373           << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
374           << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
375         
376       }
377     //  (s) << "</TABLE>\n";
378     
379     
380     //-------------
381     // Outputs
382     //  (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
383     //  (s) << "<TABLE border=1 cellspacing=0>\n";
384     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
385       <<"\">Outputs</TD></TR>\n";
386     
387     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
388       GetOutputDescriptorMap();
389     
390     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
391     
392     for ( o = omap.begin();  o != omap.end(); ++o ) 
393       {
394         std::string name(o->second->GetName());
395         Utilities::html_format(name);
396         
397         std::string type("<");
398         type += o->second->GetTypeName();    
399         type += ">";
400         Utilities::html_format(type);
401         
402         std::string descr(o->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     // End
414
415     bbtkDebugDecTab("Core",9);
416    }
417   //=========================================================================
418  
419
420   //=======================================================================
421   void ComplexBlackBoxDescriptor::GetHelp(bool full) const
422   {
423     if (full) bbtkMessage("Help",1,"Complex Black Box <"<<
424                           GetPackage()->GetName()<<"::"
425                           <<GetTypeName()<<">"<<std::endl);
426     bbtkMessage("Help",1," "              << GetDescription() <<std::endl);
427     bbtkMessage("Help",1," By : "         << GetAuthor()      <<std::endl);
428     bbtkMessage("Help",1," Keyword(s) : " << GetKeyword()     <<std::endl);    
429     if (mInput.size()) 
430       bbtkMessage("Help",1," * Inputs : "<<std::endl);
431     else 
432       bbtkMessage("Help",1," * No inputs"<<std::endl);
433     InputDescriptorMapType::const_iterator i;
434     unsigned int namelmax = 0;
435     unsigned int typelmax = 0;
436     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
437     {
438            if (i->second->GetName().size()>namelmax) 
439              namelmax = i->second->GetName().size();
440            if (i->second->GetTypeName().size()>typelmax) 
441              typelmax = i->second->GetTypeName().size();
442     }
443     OutputDescriptorMapType::const_iterator o;
444     if (full) 
445     {
446            for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
447            {
448              if (o->second->GetName().size()>namelmax) 
449                namelmax = o->second->GetName().size();
450             if (o->second->GetTypeName().size()>typelmax) 
451                typelmax = o->second->GetTypeName().size();
452            }
453     }
454     //
455
456     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
457     {
458            std::string name(i->second->GetName());
459            name += "'";
460            name.append(1+namelmax-name.size(),' ');
461            std::string type(i->second->GetTypeName());
462            type += ">";
463            type.append(1+typelmax-type.size(),' ');
464            bbtkMessage("Help",1,
465                     "    '"<<name
466                     <<" <"<<type
467                     <<" : "<<i->second->GetDescription()<<std::endl);
468     }
469     if (full) 
470     {
471            if (mOutput.size()) 
472              bbtkMessage("Help",1," * Outputs : "<<std::endl);
473            else 
474              bbtkMessage("Help",1," * No outputs"<<std::endl);
475            for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
476            {
477              std::string name(o->second->GetName());
478              name += "'";
479              name.append(1+namelmax-name.size(),' ');
480              std::string type(o->second->GetTypeName());
481              type += ">";
482              type.append(1+typelmax-type.size(),' ');
483              bbtkMessage("Help",1,
484                          "    '"<<name
485                          <<" <"<<type
486                          <<" : "<<o->second->GetDescription()<<std::endl);
487            }
488     }
489     if (full) 
490     {
491            const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
492         
493            if (B.size()) 
494              bbtkMessage("Help",1," * Boxes : "<<std::endl);
495            else 
496              bbtkMessage("Help",1," * No boxes"<<std::endl);
497         
498            ComplexBlackBox::BlackBoxMapType::const_iterator b;
499            for ( b = B.begin(); b != B.end(); ++b ) 
500            {
501              bbtkMessage("Help",1,"    '"<<b->second->bbGetName()<<
502                          "' <"
503                          << b->second->bbGetDescriptor()->GetPackage()->GetName() 
504                          <<"::"
505                          <<b->second->bbGetTypeName()<<">"<<std::endl);
506            }
507     }
508
509   }   
510   //=======================================================================
511
512
513
514 }