1 /*=========================================================================
4 Module: $RCSfile: bbtkComplexBlackBoxDescriptor.cxx,v $
6 Date: $Date: 2008/04/09 11:16:57 $
7 Version: $Revision: 1.14 $
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);
44 bbtkDebugDecTab("Kernel",9);
46 //=======================================================================
50 //=======================================================================
52 ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor()
54 bbtkDebugMessageInc("Kernel",1,"ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor(\""<<GetTypeName()<<"\")"<<std::endl);
58 bbtkDebugMessage("Kernel",1,"EO ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor(\""<<GetTypeName()<<"\")"<<std::endl);
59 bbtkDebugDecTab("Kernel",1);
61 //=======================================================================
63 //=========================================================================
65 void ComplexBlackBoxDescriptor::Check(bool recursive) const
67 mPrototype->Check(recursive);
69 //=========================================================================
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)
75 //bbtkError("ComplexBlackBoxDescriptor::CreateInstance not implemented");
76 bbtkDebugMessageInc("Kernel",5,
77 "ComplexBlackBoxDescriptor::CreateInstance(\""
79 <<GetTypeName()<<"]"<<std::endl);
81 return mPrototype->bbClone(name);
83 bbtkDebugDecTab("Kernel",5);
85 //=======================================================================
89 //=======================================================================
90 /// Adds a black box to the complex box
91 void ComplexBlackBoxDescriptor::Add ( const std::string& type,
92 const std::string& name
95 bbtkDebugMessageInc("Kernel",5,
96 "ComplexBlackBoxDescriptor::Add(\""
97 <<type<<"\",\""<<name<<"\") ["
98 <<GetTypeName()<<"]"<<std::endl);
103 bbtkError("ComplexBlackBoxDescriptor::Add : no factory set");
106 // Verify that a box with the same name does not exist already
107 if ( mPrototype->bbUnsafeGetBlackBox( name ) )
109 bbtkError("a black box \""<<name<<"\" already exists");
111 // ok : create new one
112 mPrototype->bbAddBlackBox ( mFactory->NewBlackBox(type,name) );
114 bbtkDebugDecTab("Kernel",5);
116 //=======================================================================
118 //=======================================================================
119 /// Adds a black box to the execution list
120 void ComplexBlackBoxDescriptor::AddToExecutionList ( const std::string& box)
122 bbtkDebugMessageInc("Kernel",5,
123 "ComplexBlackBoxDescriptor::AddToExecutionList(\""
125 <<GetTypeName()<<"]"<<std::endl);
126 // Verify that the box exists
127 BlackBox* b = mPrototype->bbUnsafeGetBlackBox( box );
130 bbtkError("the black box \""<<box<<"\" does not exist");
133 mPrototype->bbAddToExecutionList ( box );
135 bbtkDebugDecTab("Kernel",5);
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
147 bbtkDebugMessageInc("Kernel",5,
148 "ComplexBlackBoxDescriptor::Connect(\""
149 <<from<<"\",\""<<output<<"\",\""
152 <<GetTypeName()<<"]"<<std::endl);
156 bbtkError("ComplexBlackBoxDescriptor::Connect : no factory set");
160 // Verify that a box with the same name does not exist already
161 BlackBox* bbfrom = mPrototype->bbGetBlackBox( from );
164 bbtkError("the black box \""<<from<<"\" does not exist");
166 BlackBox* bbto = mPrototype->bbGetBlackBox( to );
169 bbtkError("the black box \""<<to<<"\" does not exist");
172 Connection* c = mFactory->NewConnection( bbfrom, output, bbto, input );
174 mPrototype->bbAddConnection(c);
176 bbtkDebugDecTab("Kernel",5);
178 //=======================================================================
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)
188 bbtkDebugMessageInc("Kernel",5,
189 "ComplexBlackBoxDescriptor::DefineInput(\""
190 <<name<<"\",\""<<box<<"\",\""
191 <<input<<"\",\""<<help
193 <<GetTypeName()<<"]"<<std::endl);
195 BlackBox* bb = mPrototype->bbGetBlackBox( box );
198 bbtkError("the black box \""<<box<<"\" does not exist");
201 if (!bb->bbHasInput(input) )
203 bbtkError("the black box \""<<box<<"\" does not have input \""
207 const BlackBoxInputDescriptor* d =
208 bb->bbGetDescriptor()->GetInputDescriptor(input);
209 AddInputDescriptor ( new ComplexBlackBoxInputDescriptor
210 ( typeid(ComplexBlackBoxDescriptor),
219 bbtkDebugDecTab("Kernel",5);
221 //=======================================================================
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)
230 bbtkDebugMessageInc("Kernel",5,
231 "ComplexBlackBoxDescriptor::DefineOutput(\""
232 <<name<<"\",\""<<box<<"\",\""
233 <<output<<"\",\""<<help
235 <<GetTypeName()<<"]"<<std::endl);
237 BlackBox* bb = mPrototype->bbGetBlackBox( box );
240 bbtkError("the black box \""<<box<<"\" does not exist");
243 if (!bb->bbHasOutput(output) )
245 bbtkError("the black box \""<<box<<"\" does not have output \""
249 const BlackBoxOutputDescriptor* d =
250 bb->bbGetDescriptor()->GetOutputDescriptor(output);
251 AddOutputDescriptor ( new ComplexBlackBoxOutputDescriptor
252 ( typeid(ComplexBlackBoxDescriptor),
261 bbtkDebugDecTab("Kernel",5);
263 //=======================================================================
265 //=======================================================================
266 void ComplexBlackBoxDescriptor::PrintBlackBoxes()
268 mPrototype->bbPrintBlackBoxes();
270 //=======================================================================
273 //=======================================================================
274 void ComplexBlackBoxDescriptor::InsertHTMLGraph( std::ofstream& s ,
275 int detail, int level,
276 const std::string& output_dir, bool relative_link )
278 this->mPrototype->bbInsertHTMLGraph( s,
284 //=======================================================================
286 //=========================================================================
287 void ComplexBlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
288 int detail, int level,
289 const std::string& output_dir, bool relative_link)
291 bbtkDebugMessageInc("Kernel",9,
292 "ComplexBlackBoxDescriptor::InsertHtmlHelp() ["
293 <<GetTypeName()<<"]"<<std::endl);
297 std::string name = GetTypeName();
298 Utilities::html_format(name);
300 // std::ofstream* s = &s1;
303 (s) << "<a name=\""<<name<<"\"></a>\n";
305 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
306 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
307 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
308 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
311 std::string descr = GetDescription();
312 Utilities::html_format(descr);
314 std::string author = GetAuthor();
315 Utilities::html_format(author);
317 std::vector<std::string> categories;
318 // Split the category string
319 std::string delimiters = ";,";
320 Utilities::SplitString(GetCategory(),
321 delimiters,categories);
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";
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";
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)
335 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
339 std::string inc = GetScriptFileName();
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 << " <a href=\"../../../bbs/"<<inc<<"\">[source]</a>";
348 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
352 (s) << "<TR><TD style='vertical-align: top;'><b> Uses </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
354 std::set<BlackBoxDescriptor*> pdeps;
355 ComplexBlackBox::BlackBoxMapType::const_iterator b;
356 for ( b = B.begin(); b != B.end(); ++b )
358 BlackBoxDescriptor* d = b->second->bbGetDescriptor();
359 if (pdeps.find(d) != pdeps.end())
363 Package* p = d->GetPackage();
365 std::string name = b->second->bbGetTypeName();
369 url = p->GetDocRelativeURL();
371 url = p->GetDocURL();
373 s << "<a href=\"" <<url<<"#"<<name<<"\">"
374 << p->GetName()<<"::"<<name<<"</a>\n";
377 (s) << "</TD></TR>\n";
386 InsertHTMLGraph( s , detail,level, output_dir, relative_link);
390 std::string col("#CCCCFF");
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();
399 InputDescriptorMapType::const_iterator in;
401 for ( in = imap.begin(); in != imap.end(); ++in )
403 std::string name(in->second->GetName());
404 Utilities::html_format(name);
406 std::string type("<");
407 type += in->second->GetTypeName();
409 Utilities::html_format(type);
411 std::string descr(in->second->GetDescription());
412 Utilities::html_format(descr);
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";
419 // (s) << "</TABLE>\n";
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";
429 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
430 GetOutputDescriptorMap();
432 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
434 for ( o = omap.begin(); o != omap.end(); ++o )
436 std::string name(o->second->GetName());
437 Utilities::html_format(name);
439 std::string type("<");
440 type += o->second->GetTypeName();
442 Utilities::html_format(type);
444 std::string descr(o->second->GetDescription());
445 Utilities::html_format(descr);
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";
457 bbtkDebugDecTab("Kernel",9);
459 //=========================================================================
462 //=======================================================================
463 void ComplexBlackBoxDescriptor::GetHelp(bool full) const
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);
472 bbtkMessage("Help",1," * Inputs : "<<std::endl);
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 )
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();
488 OutputDescriptorMapType::const_iterator o;
491 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
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();
503 for ( i = mInput.begin(); i != mInput.end(); ++i )
505 std::string name(i->second->GetName());
507 name.append(1+namelmax-name.size(),' ');
508 std::string type(i->second->GetTypeName());
510 type.append(1+typelmax-type.size(),' ');
511 std::string nature(i->second->GetNature());
513 nature.append(1+natlmax-nature.size(),' ');
514 bbtkMessage("Help",1,
518 <<" : "<<i->second->GetDescription()<<std::endl);
523 bbtkMessage("Help",1," * Outputs : "<<std::endl);
525 bbtkMessage("Help",1," * No outputs"<<std::endl);
526 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
528 std::string name(o->second->GetName());
530 name.append(1+namelmax-name.size(),' ');
531 std::string type(o->second->GetTypeName());
533 type.append(1+typelmax-type.size(),' ');
534 std::string nature(o->second->GetNature());
536 nature.append(1+natlmax-nature.size(),' ');
537 bbtkMessage("Help",1,
541 <<" : "<<o->second->GetDescription()<<std::endl);
546 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
549 bbtkMessage("Help",1," * Boxes : "<<std::endl);
551 bbtkMessage("Help",1," * No boxes"<<std::endl);
553 ComplexBlackBox::BlackBoxMapType::const_iterator b;
554 for ( b = B.begin(); b != B.end(); ++b )
556 bbtkMessage("Help",1," '"<<b->second->bbGetName()<<
558 << b->second->bbGetDescriptor()->GetPackage()->GetName()
560 <<b->second->bbGetTypeName()<<">"<<std::endl);
565 //=======================================================================