]> Creatis software - bbtk.git/blob - kernel/src/bbtkComplexBlackBoxDescriptor.cxx
Html doc
[bbtk.git] / kernel / src / bbtkComplexBlackBoxDescriptor.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkComplexBlackBoxDescriptor.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/10/08 13:39:33 $
7   Version:   $Revision: 1.17 $
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         // LG TODO : USE PACKAGE BBS PATH
371         s << inc << "&nbsp;&nbsp;<a href=\""<<inc<<"\">[source]</a>";
372         s << "</TD></TR>\n";
373         
374       }
375     
376     const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
377         
378     if (B.size()) 
379     {
380            (s) << "<TR><TD style='vertical-align: top;'><b> Uses </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  ";
381
382            std::set<BlackBoxDescriptor::Pointer> pdeps;
383            ComplexBlackBox::BlackBoxMapType::const_iterator b;
384            for ( b = B.begin(); b != B.end(); ++b ) 
385            {
386              BlackBoxDescriptor::Pointer d = b->second->bbGetDescriptor();
387               if (pdeps.find(d) != pdeps.end()) 
388             continue;
389               pdeps.insert(d);
390
391               Package::Pointer p = d->GetPackage();
392             
393               std::string name = b->second->bbGetTypeName();
394
395               std::string url;
396               if (relative_link) 
397                  url = p->GetDocRelativeURL();
398               else 
399                  url = p->GetDocURL();
400
401               s << "<a href=\"" <<url<<"#"<<name<<"\">" 
402                 << p->GetName()<<"::"<<name<<"</a>\n";
403             }   
404         
405             (s) << "</TD></TR>\n";
406
407      }
408
409      (s) << "</TABLE>\n";
410
411  
412    //-------------
413     // Graph
414     InsertHTMLGraph( s , detail,level, output_dir, relative_link);
415     
416     //-------------
417     // Inputs
418     std::string col("#CCCCFF");
419     
420     //  (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
421     (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
422     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
423       <<"\">Inputs</TD></TR>\n";
424     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
425       GetInputDescriptorMap();
426     
427     InputDescriptorMapType::const_iterator in;
428     
429     for ( in = imap.begin();  in != imap.end(); ++in ) 
430       {
431         std::string name(in->second->GetName());
432         Utilities::html_format(name);
433         
434         std::string type("<");
435         type += in->second->GetTypeName();    
436         type += ">";
437         Utilities::html_format(type);
438         
439         std::string descr(in->second->GetDescription());
440         //Utilities::html_format(descr);
441
442         (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
443           << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
444           << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
445         
446       }
447     //  (s) << "</TABLE>\n";
448     
449     
450     //-------------
451     // Outputs
452     //  (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
453     //  (s) << "<TABLE border=1 cellspacing=0>\n";
454     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
455       <<"\">Outputs</TD></TR>\n";
456     
457     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
458       GetOutputDescriptorMap();
459     
460     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
461     
462     for ( o = omap.begin();  o != omap.end(); ++o ) 
463       {
464         std::string name(o->second->GetName());
465         Utilities::html_format(name);
466         
467         std::string type("<");
468         type += o->second->GetTypeName();    
469         type += ">";
470         Utilities::html_format(type);
471         
472         std::string descr(o->second->GetDescription());
473         //Utilities::html_format(descr);
474         
475         (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
476           << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
477           << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
478         
479       }
480     (s) << "</TABLE>\n";
481
482     //------------
483     // End
484
485     bbtkDebugDecTab("Kernel",9);
486    }
487   //=========================================================================
488  
489
490   //=======================================================================
491   void ComplexBlackBoxDescriptor::GetHelp(bool full) const
492   {
493     if (full) bbtkMessage("Help",1,"Complex Black Box <"<<
494                           GetPackage()->GetName()<<"::"
495                           <<GetTypeName()<<">"<<std::endl);
496     bbtkMessage("Help",1," "                << GetDescription() <<std::endl);
497     bbtkMessage("Help",1," By : "           << GetAuthor()      <<std::endl);
498     bbtkMessage("Help",1," Category(s) : "  << GetCategory()     <<std::endl);    
499     if (mInput.size()) 
500       bbtkMessage("Help",1," * Inputs : "<<std::endl);
501     else 
502       bbtkMessage("Help",1," * No inputs"<<std::endl);
503     InputDescriptorMapType::const_iterator i;
504     unsigned int namelmax = 0;
505     unsigned int typelmax = 0;
506     unsigned int natlmax = 0;
507     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
508     {
509            if (i->second->GetName().size()>namelmax) 
510              namelmax = i->second->GetName().size();
511            if (i->second->GetTypeName().size()>typelmax) 
512              typelmax = i->second->GetTypeName().size();
513            if (i->second->GetNature().size()>natlmax) 
514              natlmax = i->second->GetNature().size();
515     }
516     OutputDescriptorMapType::const_iterator o;
517     if (full) 
518     {
519            for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
520            {
521              if (o->second->GetName().size()>namelmax) 
522                namelmax = o->second->GetName().size();
523             if (o->second->GetTypeName().size()>typelmax) 
524                typelmax = o->second->GetTypeName().size();
525            if (o->second->GetNature().size()>natlmax) 
526              natlmax = o->second->GetNature().size();
527            }
528     }
529     //
530
531     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
532     {
533            std::string name(i->second->GetName());
534            name += "'";
535            name.append(1+namelmax-name.size(),' ');
536            std::string type(i->second->GetTypeName());
537            type += ">";
538            type.append(1+typelmax-type.size(),' ');
539            std::string nature(i->second->GetNature());
540            nature += "]";
541            nature.append(1+natlmax-nature.size(),' ');
542            bbtkMessage("Help",1,
543                        "    '"<<name
544                        <<" <"<<type
545                        <<" ["<<nature
546                        <<" : "<<i->second->GetDescription()<<std::endl);
547     }
548     if (full) 
549     {
550            if (mOutput.size()) 
551              bbtkMessage("Help",1," * Outputs : "<<std::endl);
552            else 
553              bbtkMessage("Help",1," * No outputs"<<std::endl);
554            for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
555            {
556              std::string name(o->second->GetName());
557              name += "'";
558              name.append(1+namelmax-name.size(),' ');
559              std::string type(o->second->GetTypeName());
560              type += ">";
561              type.append(1+typelmax-type.size(),' ');
562              std::string nature(o->second->GetNature());
563              nature += "]";
564              nature.append(1+natlmax-nature.size(),' ');
565              bbtkMessage("Help",1,
566                        "    '"<<name
567                          <<" <"<<type
568                          <<" ["<<nature
569                          <<" : "<<o->second->GetDescription()<<std::endl);
570            }
571     }
572     if (full) 
573     {
574            const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
575         
576            if (B.size()) 
577              bbtkMessage("Help",1," * Boxes : "<<std::endl);
578            else 
579              bbtkMessage("Help",1," * No boxes"<<std::endl);
580         
581            ComplexBlackBox::BlackBoxMapType::const_iterator b;
582            for ( b = B.begin(); b != B.end(); ++b ) 
583            {
584              bbtkMessage("Help",1,"    '"<<b->second->bbGetName()<<
585                          "' <"
586                          << b->second->bbGetDescriptor()->GetPackage()->GetName() 
587                          <<"::"
588                          <<b->second->bbGetTypeName()<<">"<<std::endl);
589            }
590     }
591
592   }   
593   //=======================================================================
594
595   //==========================================================================
596   std::string ComplexBlackBoxDescriptor::GetObjectName() const
597   {
598     return std::string("ComplexBlackBoxDescriptor '")+GetFullTypeName()
599       +std::string("'");
600   }
601   //==========================================================================
602   //=======================================================================
603   std::string ComplexBlackBoxDescriptor::GetObjectInfo() const
604   {
605     std::string i;
606     return i;     
607   }
608   //=======================================================================
609  //==========================================================================
610 size_t  ComplexBlackBoxDescriptor::GetObjectSize() const 
611 {
612   size_t s = Superclass::GetObjectSize();
613   s += ComplexBlackBoxDescriptor::GetObjectInternalSize();
614   return s;
615   }
616   //==========================================================================
617   //==========================================================================
618 size_t  ComplexBlackBoxDescriptor::GetObjectInternalSize() const 
619 {
620   size_t s = sizeof(ComplexBlackBoxDescriptor);
621   return s;
622   }
623   //==========================================================================
624   //==========================================================================
625   size_t  ComplexBlackBoxDescriptor::GetObjectRecursiveSize() const 
626   {
627     size_t s = Superclass::GetObjectRecursiveSize();
628     s += ComplexBlackBoxDescriptor::GetObjectInternalSize();
629     s += mPrototype->GetObjectRecursiveSize();
630     return s;
631   }
632   //==========================================================================
633
634 }