1 /*=========================================================================
4 Module: $RCSfile: bbtkComplexBlackBoxDescriptor.cxx,v $
6 Date: $Date: 2008/04/08 06:59:30 $
7 Version: $Revision: 1.13 $
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"
33 //=======================================================================
35 ComplexBlackBoxDescriptor::ComplexBlackBoxDescriptor(const std::string& name)
36 : BlackBoxDescriptor(),
39 bbtkDebugMessageInc("Kernel",9,"ComplexBlackBoxDescriptor::ComplexBlackBoxDescriptor(\""<<name<<"\")"<<std::endl);
41 AddToCategory("complex box");
42 mPrototype = new ComplexBlackBox(name+std::string("Prototype"),this);
43 bbtkDebugDecTab("Kernel",9);
45 //=======================================================================
49 //=======================================================================
51 ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor()
53 bbtkDebugMessageInc("Kernel",9,"ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor(\""<<GetTypeName()<<"\")"<<std::endl);
57 bbtkDebugDecTab("Kernel",9);
59 //=======================================================================
62 //=======================================================================
63 /// Creates an instance of name <name> of the ComplexBlackBox of which this is the descriptor
64 BlackBox* ComplexBlackBoxDescriptor::CreateInstance(const std::string& name)
66 //bbtkError("ComplexBlackBoxDescriptor::CreateInstance not implemented");
67 bbtkDebugMessageInc("Kernel",5,
68 "ComplexBlackBoxDescriptor::CreateInstance(\""
70 <<GetTypeName()<<"]"<<std::endl);
72 return mPrototype->bbClone(name);
74 bbtkDebugDecTab("Kernel",5);
76 //=======================================================================
80 //=======================================================================
81 /// Adds a black box to the complex box
82 void ComplexBlackBoxDescriptor::Add ( const std::string& type,
83 const std::string& name
86 bbtkDebugMessageInc("Kernel",5,
87 "ComplexBlackBoxDescriptor::Add(\""
88 <<type<<"\",\""<<name<<"\") ["
89 <<GetTypeName()<<"]"<<std::endl);
94 bbtkError("ComplexBlackBoxDescriptor::Add : no factory set");
97 // Verify that a box with the same name does not exist already
98 if ( mPrototype->bbUnsafeGetBlackBox( name ) )
100 bbtkError("a black box \""<<name<<"\" already exists");
102 // ok : create new one
103 mPrototype->bbAddBlackBox ( mFactory->NewBlackBox(type,name) );
105 bbtkDebugDecTab("Kernel",5);
107 //=======================================================================
109 //=======================================================================
110 /// Adds a black box to the execution list
111 void ComplexBlackBoxDescriptor::AddToExecutionList ( const std::string& box)
113 bbtkDebugMessageInc("Kernel",5,
114 "ComplexBlackBoxDescriptor::AddToExecutionList(\""
116 <<GetTypeName()<<"]"<<std::endl);
117 // Verify that the box exists
118 BlackBox* b = mPrototype->bbUnsafeGetBlackBox( box );
121 bbtkError("the black box \""<<box<<"\" does not exist");
124 mPrototype->bbAddToExecutionList ( box );
126 bbtkDebugDecTab("Kernel",5);
130 //=======================================================================
131 /// Connects two black boxes of the complex box
132 void ComplexBlackBoxDescriptor::Connect ( const std::string& from,
133 const std::string& output,
134 const std::string& to,
135 const std::string& input
138 bbtkDebugMessageInc("Kernel",5,
139 "ComplexBlackBoxDescriptor::Connect(\""
140 <<from<<"\",\""<<output<<"\",\""
143 <<GetTypeName()<<"]"<<std::endl);
147 bbtkError("ComplexBlackBoxDescriptor::Connect : no factory set");
151 // Verify that a box with the same name does not exist already
152 BlackBox* bbfrom = mPrototype->bbGetBlackBox( from );
155 bbtkError("the black box \""<<from<<"\" does not exist");
157 BlackBox* bbto = mPrototype->bbGetBlackBox( to );
160 bbtkError("the black box \""<<to<<"\" does not exist");
163 Connection* c = mFactory->NewConnection( bbfrom, output, bbto, input );
165 mPrototype->bbAddConnection(c);
167 bbtkDebugDecTab("Kernel",5);
169 //=======================================================================
172 //=======================================================================
173 /// Defines an input of the complex box
174 void ComplexBlackBoxDescriptor::DefineInput ( const std::string& name,
175 const std::string& box,
176 const std::string& input,
177 const std::string& help)
179 bbtkDebugMessageInc("Kernel",5,
180 "ComplexBlackBoxDescriptor::DefineInput(\""
181 <<name<<"\",\""<<box<<"\",\""
182 <<input<<"\",\""<<help
184 <<GetTypeName()<<"]"<<std::endl);
186 BlackBox* bb = mPrototype->bbGetBlackBox( box );
189 bbtkError("the black box \""<<box<<"\" does not exist");
192 if (!bb->bbHasInput(input) )
194 bbtkError("the black box \""<<box<<"\" does not have input \""
198 const BlackBoxInputDescriptor* d =
199 bb->bbGetDescriptor()->GetInputDescriptor(input);
200 AddInputDescriptor ( new ComplexBlackBoxInputDescriptor
201 ( typeid(ComplexBlackBoxDescriptor),
210 bbtkDebugDecTab("Kernel",5);
212 //=======================================================================
214 //=======================================================================
215 /// Defines an output of the complex box
216 void ComplexBlackBoxDescriptor::DefineOutput ( const std::string& name,
217 const std::string& box,
218 const std::string& output,
219 const std::string& help)
221 bbtkDebugMessageInc("Kernel",5,
222 "ComplexBlackBoxDescriptor::DefineOutput(\""
223 <<name<<"\",\""<<box<<"\",\""
224 <<output<<"\",\""<<help
226 <<GetTypeName()<<"]"<<std::endl);
228 BlackBox* bb = mPrototype->bbGetBlackBox( box );
231 bbtkError("the black box \""<<box<<"\" does not exist");
234 if (!bb->bbHasOutput(output) )
236 bbtkError("the black box \""<<box<<"\" does not have output \""
240 const BlackBoxOutputDescriptor* d =
241 bb->bbGetDescriptor()->GetOutputDescriptor(output);
242 AddOutputDescriptor ( new ComplexBlackBoxOutputDescriptor
243 ( typeid(ComplexBlackBoxDescriptor),
252 bbtkDebugDecTab("Kernel",5);
254 //=======================================================================
256 //=======================================================================
257 void ComplexBlackBoxDescriptor::PrintBlackBoxes()
259 mPrototype->bbPrintBlackBoxes();
261 //=======================================================================
264 //=======================================================================
265 void ComplexBlackBoxDescriptor::InsertHTMLGraph( std::ofstream& s ,
266 int detail, int level,
267 const std::string& output_dir, bool relative_link )
269 this->mPrototype->bbInsertHTMLGraph( s,
275 //=======================================================================
277 //=========================================================================
278 void ComplexBlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
279 int detail, int level,
280 const std::string& output_dir, bool relative_link)
282 bbtkDebugMessageInc("Kernel",9,
283 "ComplexBlackBoxDescriptor::InsertHtmlHelp() ["
284 <<GetTypeName()<<"]"<<std::endl);
288 std::string name = GetTypeName();
289 Utilities::html_format(name);
291 // std::ofstream* s = &s1;
294 (s) << "<a name=\""<<name<<"\"></a>\n";
296 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
297 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
298 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
299 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
302 std::string descr = GetDescription();
303 Utilities::html_format(descr);
305 std::string author = GetAuthor();
306 Utilities::html_format(author);
308 std::vector<std::string> categories;
309 // Split the category string
310 std::string delimiters = ";,";
311 Utilities::SplitString(GetCategory(),
312 delimiters,categories);
315 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
316 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
317 << descr << "</TD></TR>\n";
319 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
320 << author << "</TD></TR>\n";
322 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
323 std::vector<std::string>::iterator ci;
324 for (ci=categories.begin(); ci!=categories.end(); ++ci)
326 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
330 std::string inc = GetScriptFileName();
333 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 ";
334 s << inc << " <a href=\"../../../bbs/"<<inc<<"\">[source]</a>";
339 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
343 (s) << "<TR><TD style='vertical-align: top;'><b> Uses </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
345 std::set<BlackBoxDescriptor*> pdeps;
346 ComplexBlackBox::BlackBoxMapType::const_iterator b;
347 for ( b = B.begin(); b != B.end(); ++b )
349 BlackBoxDescriptor* d = b->second->bbGetDescriptor();
350 if (pdeps.find(d) != pdeps.end())
354 Package* p = d->GetPackage();
356 std::string name = b->second->bbGetTypeName();
360 url = p->GetDocRelativeURL();
362 url = p->GetDocURL();
364 s << "<a href=\"" <<url<<"#"<<name<<"\">"
365 << p->GetName()<<"::"<<name<<"</a>\n";
368 (s) << "</TD></TR>\n";
377 InsertHTMLGraph( s , detail,level, output_dir, relative_link);
381 std::string col("#CCCCFF");
383 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
384 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
385 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
386 <<"\">Inputs</TD></TR>\n";
387 const BlackBoxDescriptor::InputDescriptorMapType& imap =
388 GetInputDescriptorMap();
390 InputDescriptorMapType::const_iterator in;
392 for ( in = imap.begin(); in != imap.end(); ++in )
394 std::string name(in->second->GetName());
395 Utilities::html_format(name);
397 std::string type("<");
398 type += in->second->GetTypeName();
400 Utilities::html_format(type);
402 std::string descr(in->second->GetDescription());
403 Utilities::html_format(descr);
405 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
406 << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
407 << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
410 // (s) << "</TABLE>\n";
415 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
416 // (s) << "<TABLE border=1 cellspacing=0>\n";
417 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
418 <<"\">Outputs</TD></TR>\n";
420 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
421 GetOutputDescriptorMap();
423 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
425 for ( o = omap.begin(); o != omap.end(); ++o )
427 std::string name(o->second->GetName());
428 Utilities::html_format(name);
430 std::string type("<");
431 type += o->second->GetTypeName();
433 Utilities::html_format(type);
435 std::string descr(o->second->GetDescription());
436 Utilities::html_format(descr);
438 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
439 << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
440 << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
448 bbtkDebugDecTab("Kernel",9);
450 //=========================================================================
453 //=======================================================================
454 void ComplexBlackBoxDescriptor::GetHelp(bool full) const
456 if (full) bbtkMessage("Help",1,"Complex Black Box <"<<
457 GetPackage()->GetName()<<"::"
458 <<GetTypeName()<<">"<<std::endl);
459 bbtkMessage("Help",1," " << GetDescription() <<std::endl);
460 bbtkMessage("Help",1," By : " << GetAuthor() <<std::endl);
461 bbtkMessage("Help",1," Category(s) : " << GetCategory() <<std::endl);
463 bbtkMessage("Help",1," * Inputs : "<<std::endl);
465 bbtkMessage("Help",1," * No inputs"<<std::endl);
466 InputDescriptorMapType::const_iterator i;
467 unsigned int namelmax = 0;
468 unsigned int typelmax = 0;
469 for ( i = mInput.begin(); i != mInput.end(); ++i )
471 if (i->second->GetName().size()>namelmax)
472 namelmax = i->second->GetName().size();
473 if (i->second->GetTypeName().size()>typelmax)
474 typelmax = i->second->GetTypeName().size();
476 OutputDescriptorMapType::const_iterator o;
479 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
481 if (o->second->GetName().size()>namelmax)
482 namelmax = o->second->GetName().size();
483 if (o->second->GetTypeName().size()>typelmax)
484 typelmax = o->second->GetTypeName().size();
489 for ( i = mInput.begin(); i != mInput.end(); ++i )
491 std::string name(i->second->GetName());
493 name.append(1+namelmax-name.size(),' ');
494 std::string type(i->second->GetTypeName());
496 type.append(1+typelmax-type.size(),' ');
497 bbtkMessage("Help",1,
500 <<" : "<<i->second->GetDescription()<<std::endl);
505 bbtkMessage("Help",1," * Outputs : "<<std::endl);
507 bbtkMessage("Help",1," * No outputs"<<std::endl);
508 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
510 std::string name(o->second->GetName());
512 name.append(1+namelmax-name.size(),' ');
513 std::string type(o->second->GetTypeName());
515 type.append(1+typelmax-type.size(),' ');
516 bbtkMessage("Help",1,
519 <<" : "<<o->second->GetDescription()<<std::endl);
524 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
527 bbtkMessage("Help",1," * Boxes : "<<std::endl);
529 bbtkMessage("Help",1," * No boxes"<<std::endl);
531 ComplexBlackBox::BlackBoxMapType::const_iterator b;
532 for ( b = B.begin(); b != B.end(); ++b )
534 bbtkMessage("Help",1," '"<<b->second->bbGetName()<<
536 << b->second->bbGetDescriptor()->GetPackage()->GetName()
538 <<b->second->bbGetTypeName()<<">"<<std::endl);
543 //=======================================================================