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