1 /*=========================================================================
4 Module: $RCSfile: bbtkComplexBlackBoxDescriptor.cxx,v $
6 Date: $Date: 2008/10/08 13:39:33 $
7 Version: $Revision: 1.17 $
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.
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.
17 =========================================================================*/
21 * \brief Class bbtk::ComplexBlackBoxDescriptor : describes a ComplexBlackBox (constituents, connections) and is able to create an instance of it.
23 #include "bbtkComplexBlackBoxDescriptor.h"
24 #include "bbtkComplexBlackBox.h"
25 //#include "bbtkFactory.h"
26 #include "bbtkMessageManager.h"
27 #include "bbtkUtilities.h"
31 //=======================================================================
33 ComplexBlackBoxDescriptor::Pointer
34 ComplexBlackBoxDescriptor::New(const std::string& name)
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);
42 //=======================================================================
44 //=======================================================================
46 ComplexBlackBoxDescriptor::ComplexBlackBoxDescriptor(const std::string& name)
48 bbtkDebugMessage("object",2,"==> ComplexBlackBoxDescriptor::ComplexBlackBoxDescriptor(\""<<name<<"\")"<<std::endl);
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);
56 //=======================================================================
60 //=======================================================================
62 ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor()
64 bbtkDebugMessage("object",2,"==> ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor(\""<<GetTypeName()<<"\")"<<std::endl);
65 // mPrototype->Delete();
67 bbtkDebugMessage("object",2,"<== ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor(\""<<GetTypeName()<<"\")"<<std::endl);
69 //=======================================================================
71 //=========================================================================
73 void ComplexBlackBoxDescriptor::Check(bool recursive) const
75 mPrototype->Check(recursive);
77 //=========================================================================
79 //=======================================================================
80 /// Creates an instance of name <name> of the ComplexBlackBox of which this is the descriptor
82 ComplexBlackBoxDescriptor::NewBlackBox(const std::string& name)
84 bbtkDebugMessageInc("Kernel",5,
85 "ComplexBlackBoxDescriptor::NewBlackBox(\""
87 <<GetTypeName()<<"]"<<std::endl);
89 return mPrototype->bbClone(name);
91 bbtkDebugDecTab("Kernel",5);
93 //=======================================================================
96 //=======================================================================
98 void ComplexBlackBoxDescriptor::Release(bool release_package)
101 //=======================================================================
104 //=======================================================================
105 /// Adds a black box to the complex box
106 void ComplexBlackBoxDescriptor::Add ( const std::string& type,
107 const std::string& name
110 bbtkDebugMessageInc("Kernel",5,
111 "ComplexBlackBoxDescriptor::Add(\""
112 <<type<<"\",\""<<name<<"\") ["
113 <<GetTypeName()<<"]"<<std::endl);
118 bbtkError("ComplexBlackBoxDescriptor::Add : no factory set");
121 // Verify that a box with the same name does not exist already
122 if ( mPrototype->bbUnsafeGetBlackBox( name ) )
124 bbtkError("a black box \""<<name<<"\" already exists");
126 // ok : create new one
127 mPrototype->bbAddBlackBox ( GetFactory()->NewBlackBox(type,name) );
129 bbtkDebugDecTab("Kernel",5);
131 //=======================================================================
133 //=======================================================================
134 /// Removes a black box from the complex box
135 void ComplexBlackBoxDescriptor::Remove( const std::string& name,
136 bool remove_connections)
138 mPrototype->bbRemoveBlackBox(name,remove_connections);
140 //=======================================================================
143 //=======================================================================
144 /// Adds a black box to the execution list
145 void ComplexBlackBoxDescriptor::AddToExecutionList ( const std::string& box)
147 bbtkDebugMessageInc("Kernel",5,
148 "ComplexBlackBoxDescriptor::AddToExecutionList(\""
150 <<GetTypeName()<<"]"<<std::endl);
151 // Verify that the box exists
152 BlackBox::Pointer b = mPrototype->bbUnsafeGetBlackBox( box );
155 bbtkError("the black box \""<<box<<"\" does not exist");
158 mPrototype->bbAddToExecutionList ( box );
160 bbtkDebugDecTab("Kernel",5);
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
172 bbtkDebugMessageInc("Kernel",5,
173 "ComplexBlackBoxDescriptor::Connect(\""
174 <<from<<"\",\""<<output<<"\",\""
177 <<GetTypeName()<<"]"<<std::endl);
181 bbtkError("ComplexBlackBoxDescriptor::Connect : no factory set");
185 // Verify that a box with the same name does not exist already
186 BlackBox::Pointer bbfrom = mPrototype->bbGetBlackBox( from );
189 bbtkError("the black box \""<<from<<"\" does not exist");
191 BlackBox::Pointer bbto = mPrototype->bbGetBlackBox( to );
194 bbtkError("the black box \""<<to<<"\" does not exist");
197 Connection::Pointer c
198 = GetFactory()->NewConnection( bbfrom, output, bbto, input );
200 mPrototype->bbAddConnection(c);
202 bbtkDebugDecTab("Kernel",5);
204 //=======================================================================
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)
214 bbtkDebugMessageInc("Kernel",5,
215 "ComplexBlackBoxDescriptor::DefineInput(\""
216 <<name<<"\",\""<<box<<"\",\""
217 <<input<<"\",\""<<help
219 <<GetTypeName()<<"]"<<std::endl);
221 BlackBox::Pointer bb = mPrototype->bbGetBlackBox( box );
224 bbtkError("the black box \""<<box<<"\" does not exist");
227 if (!bb->bbHasInput(input) )
229 bbtkError("the black box \""<<box<<"\" does not have input \""
233 const BlackBoxInputDescriptor* d =
234 bb->bbGetDescriptor()->GetInputDescriptor(input);
235 AddInputDescriptor ( new ComplexBlackBoxInputDescriptor
236 ( typeid(ComplexBlackBoxDescriptor),
245 bbtkDebugDecTab("Kernel",5);
247 //=======================================================================
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)
256 bbtkDebugMessageInc("Kernel",5,
257 "ComplexBlackBoxDescriptor::DefineOutput(\""
258 <<name<<"\",\""<<box<<"\",\""
259 <<output<<"\",\""<<help
261 <<GetTypeName()<<"]"<<std::endl);
263 BlackBox::Pointer bb = mPrototype->bbGetBlackBox( box );
266 bbtkError("the black box \""<<box<<"\" does not exist");
269 if (!bb->bbHasOutput(output) )
271 bbtkError("the black box \""<<box<<"\" does not have output \""
275 const BlackBoxOutputDescriptor* d =
276 bb->bbGetDescriptor()->GetOutputDescriptor(output);
277 AddOutputDescriptor ( new ComplexBlackBoxOutputDescriptor
278 ( typeid(ComplexBlackBoxDescriptor),
287 bbtkDebugDecTab("Kernel",5);
289 //=======================================================================
291 //=======================================================================
292 void ComplexBlackBoxDescriptor::PrintBlackBoxes()
294 mPrototype->bbPrintBlackBoxes();
296 //=======================================================================
299 //=======================================================================
300 void ComplexBlackBoxDescriptor::InsertHTMLGraph( std::ofstream& s ,
301 int detail, int level,
302 const std::string& output_dir, bool relative_link )
304 this->mPrototype->bbInsertHTMLGraph( s,
310 //=======================================================================
312 //=========================================================================
313 void ComplexBlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
314 int detail, int level,
315 const std::string& output_dir, bool relative_link)
317 bbtkDebugMessageInc("Kernel",9,
318 "ComplexBlackBoxDescriptor::InsertHtmlHelp() ["
319 <<GetTypeName()<<"]"<<std::endl);
323 std::string name = GetTypeName();
324 Utilities::html_format(name);
326 // std::ofstream* s = &s1;
329 (s) << "<a name=\""<<name<<"\"></a>\n";
331 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
332 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
333 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
334 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
337 std::string descr = GetDescription();
338 //Utilities::html_format(descr);
340 std::string author = GetAuthor();
341 Utilities::html_format(author);
343 std::vector<std::string> categories;
344 // Split the category string
345 std::string delimiters = ";,";
346 Utilities::SplitString(GetCategory(),
347 delimiters,categories);
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";
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";
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)
361 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
365 std::string inc = GetScriptFileName();
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 << " <a href=\"../../../bbs/"<<inc<<"\">[source]</a>";
370 // LG TODO : USE PACKAGE BBS PATH
371 s << inc << " <a href=\""<<inc<<"\">[source]</a>";
376 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
380 (s) << "<TR><TD style='vertical-align: top;'><b> Uses </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
382 std::set<BlackBoxDescriptor::Pointer> pdeps;
383 ComplexBlackBox::BlackBoxMapType::const_iterator b;
384 for ( b = B.begin(); b != B.end(); ++b )
386 BlackBoxDescriptor::Pointer d = b->second->bbGetDescriptor();
387 if (pdeps.find(d) != pdeps.end())
391 Package::Pointer p = d->GetPackage();
393 std::string name = b->second->bbGetTypeName();
397 url = p->GetDocRelativeURL();
399 url = p->GetDocURL();
401 s << "<a href=\"" <<url<<"#"<<name<<"\">"
402 << p->GetName()<<"::"<<name<<"</a>\n";
405 (s) << "</TD></TR>\n";
414 InsertHTMLGraph( s , detail,level, output_dir, relative_link);
418 std::string col("#CCCCFF");
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();
427 InputDescriptorMapType::const_iterator in;
429 for ( in = imap.begin(); in != imap.end(); ++in )
431 std::string name(in->second->GetName());
432 Utilities::html_format(name);
434 std::string type("<");
435 type += in->second->GetTypeName();
437 Utilities::html_format(type);
439 std::string descr(in->second->GetDescription());
440 //Utilities::html_format(descr);
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";
447 // (s) << "</TABLE>\n";
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";
457 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
458 GetOutputDescriptorMap();
460 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
462 for ( o = omap.begin(); o != omap.end(); ++o )
464 std::string name(o->second->GetName());
465 Utilities::html_format(name);
467 std::string type("<");
468 type += o->second->GetTypeName();
470 Utilities::html_format(type);
472 std::string descr(o->second->GetDescription());
473 //Utilities::html_format(descr);
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";
485 bbtkDebugDecTab("Kernel",9);
487 //=========================================================================
490 //=======================================================================
491 void ComplexBlackBoxDescriptor::GetHelp(bool full) const
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);
500 bbtkMessage("Help",1," * Inputs : "<<std::endl);
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 )
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();
516 OutputDescriptorMapType::const_iterator o;
519 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
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();
531 for ( i = mInput.begin(); i != mInput.end(); ++i )
533 std::string name(i->second->GetName());
535 name.append(1+namelmax-name.size(),' ');
536 std::string type(i->second->GetTypeName());
538 type.append(1+typelmax-type.size(),' ');
539 std::string nature(i->second->GetNature());
541 nature.append(1+natlmax-nature.size(),' ');
542 bbtkMessage("Help",1,
546 <<" : "<<i->second->GetDescription()<<std::endl);
551 bbtkMessage("Help",1," * Outputs : "<<std::endl);
553 bbtkMessage("Help",1," * No outputs"<<std::endl);
554 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
556 std::string name(o->second->GetName());
558 name.append(1+namelmax-name.size(),' ');
559 std::string type(o->second->GetTypeName());
561 type.append(1+typelmax-type.size(),' ');
562 std::string nature(o->second->GetNature());
564 nature.append(1+natlmax-nature.size(),' ');
565 bbtkMessage("Help",1,
569 <<" : "<<o->second->GetDescription()<<std::endl);
574 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
577 bbtkMessage("Help",1," * Boxes : "<<std::endl);
579 bbtkMessage("Help",1," * No boxes"<<std::endl);
581 ComplexBlackBox::BlackBoxMapType::const_iterator b;
582 for ( b = B.begin(); b != B.end(); ++b )
584 bbtkMessage("Help",1," '"<<b->second->bbGetName()<<
586 << b->second->bbGetDescriptor()->GetPackage()->GetName()
588 <<b->second->bbGetTypeName()<<">"<<std::endl);
593 //=======================================================================
595 //==========================================================================
596 std::string ComplexBlackBoxDescriptor::GetObjectName() const
598 return std::string("ComplexBlackBoxDescriptor '")+GetFullTypeName()
601 //==========================================================================
602 //=======================================================================
603 std::string ComplexBlackBoxDescriptor::GetObjectInfo() const
608 //=======================================================================
609 //==========================================================================
610 size_t ComplexBlackBoxDescriptor::GetObjectSize() const
612 size_t s = Superclass::GetObjectSize();
613 s += ComplexBlackBoxDescriptor::GetObjectInternalSize();
616 //==========================================================================
617 //==========================================================================
618 size_t ComplexBlackBoxDescriptor::GetObjectInternalSize() const
620 size_t s = sizeof(ComplexBlackBoxDescriptor);
623 //==========================================================================
624 //==========================================================================
625 size_t ComplexBlackBoxDescriptor::GetObjectRecursiveSize() const
627 size_t s = Superclass::GetObjectRecursiveSize();
628 s += ComplexBlackBoxDescriptor::GetObjectInternalSize();
629 s += mPrototype->GetObjectRecursiveSize();
632 //==========================================================================