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