1 /*=========================================================================
4 Module: $RCSfile: bbtkComplexBlackBoxDescriptor.cxx,v $
6 Date: $Date: 2008/03/07 10:21:30 $
7 Version: $Revision: 1.12 $
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 AddInputDescriptor ( new ComplexBlackBoxInputDescriptor
199 ( typeid(ComplexBlackBoxDescriptor),
204 bb->bbGetInputType(input)));
207 bbtkDebugDecTab("Kernel",5);
209 //=======================================================================
211 //=======================================================================
212 /// Defines an output of the complex box
213 void ComplexBlackBoxDescriptor::DefineOutput ( const std::string& name,
214 const std::string& box,
215 const std::string& output,
216 const std::string& help)
218 bbtkDebugMessageInc("Kernel",5,
219 "ComplexBlackBoxDescriptor::DefineOutput(\""
220 <<name<<"\",\""<<box<<"\",\""
221 <<output<<"\",\""<<help
223 <<GetTypeName()<<"]"<<std::endl);
225 BlackBox* bb = mPrototype->bbGetBlackBox( box );
228 bbtkError("the black box \""<<box<<"\" does not exist");
231 if (!bb->bbHasOutput(output) )
233 bbtkError("the black box \""<<box<<"\" does not have output \""
237 AddOutputDescriptor ( new ComplexBlackBoxOutputDescriptor
238 ( typeid(ComplexBlackBoxDescriptor),
243 bb->bbGetOutputType(output)));
246 bbtkDebugDecTab("Kernel",5);
248 //=======================================================================
250 //=======================================================================
251 void ComplexBlackBoxDescriptor::PrintBlackBoxes()
253 mPrototype->bbPrintBlackBoxes();
255 //=======================================================================
258 //=======================================================================
259 void ComplexBlackBoxDescriptor::InsertHTMLGraph( std::ofstream& s ,
260 int detail, int level,
261 const std::string& output_dir, bool relative_link )
263 this->mPrototype->bbInsertHTMLGraph( s,
269 //=======================================================================
271 //=========================================================================
272 void ComplexBlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
273 int detail, int level,
274 const std::string& output_dir, bool relative_link)
276 bbtkDebugMessageInc("Kernel",9,
277 "ComplexBlackBoxDescriptor::InsertHtmlHelp() ["
278 <<GetTypeName()<<"]"<<std::endl);
282 std::string name = GetTypeName();
283 Utilities::html_format(name);
285 // std::ofstream* s = &s1;
288 (s) << "<a name=\""<<name<<"\"></a>\n";
290 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
291 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
292 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
293 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
296 std::string descr = GetDescription();
297 Utilities::html_format(descr);
299 std::string author = GetAuthor();
300 Utilities::html_format(author);
302 std::vector<std::string> categories;
303 // Split the category string
304 std::string delimiters = ";,";
305 Utilities::SplitString(GetCategory(),
306 delimiters,categories);
309 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
310 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
311 << descr << "</TD></TR>\n";
313 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
314 << author << "</TD></TR>\n";
316 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
317 std::vector<std::string>::iterator ci;
318 for (ci=categories.begin(); ci!=categories.end(); ++ci)
320 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
324 std::string inc = GetScriptFileName();
327 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 ";
328 s << inc << " <a href=\"../../../bbs/"<<inc<<"\">[source]</a>";
333 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
337 (s) << "<TR><TD style='vertical-align: top;'><b> Uses </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
339 std::set<BlackBoxDescriptor*> pdeps;
340 ComplexBlackBox::BlackBoxMapType::const_iterator b;
341 for ( b = B.begin(); b != B.end(); ++b )
343 BlackBoxDescriptor* d = b->second->bbGetDescriptor();
344 if (pdeps.find(d) != pdeps.end())
348 Package* p = d->GetPackage();
350 std::string name = b->second->bbGetTypeName();
354 url = p->GetDocRelativeURL();
356 url = p->GetDocURL();
358 s << "<a href=\"" <<url<<"#"<<name<<"\">"
359 << p->GetName()<<"::"<<name<<"</a>\n";
362 (s) << "</TD></TR>\n";
371 InsertHTMLGraph( s , detail,level, output_dir, relative_link);
375 std::string col("#CCCCFF");
377 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
378 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
379 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
380 <<"\">Inputs</TD></TR>\n";
381 const BlackBoxDescriptor::InputDescriptorMapType& imap =
382 GetInputDescriptorMap();
384 InputDescriptorMapType::const_iterator in;
386 for ( in = imap.begin(); in != imap.end(); ++in )
388 std::string name(in->second->GetName());
389 Utilities::html_format(name);
391 std::string type("<");
392 type += in->second->GetTypeName();
394 Utilities::html_format(type);
396 std::string descr(in->second->GetDescription());
397 Utilities::html_format(descr);
399 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
400 << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
401 << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
404 // (s) << "</TABLE>\n";
409 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
410 // (s) << "<TABLE border=1 cellspacing=0>\n";
411 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
412 <<"\">Outputs</TD></TR>\n";
414 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
415 GetOutputDescriptorMap();
417 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
419 for ( o = omap.begin(); o != omap.end(); ++o )
421 std::string name(o->second->GetName());
422 Utilities::html_format(name);
424 std::string type("<");
425 type += o->second->GetTypeName();
427 Utilities::html_format(type);
429 std::string descr(o->second->GetDescription());
430 Utilities::html_format(descr);
432 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
433 << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
434 << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
442 bbtkDebugDecTab("Kernel",9);
444 //=========================================================================
447 //=======================================================================
448 void ComplexBlackBoxDescriptor::GetHelp(bool full) const
450 if (full) bbtkMessage("Help",1,"Complex Black Box <"<<
451 GetPackage()->GetName()<<"::"
452 <<GetTypeName()<<">"<<std::endl);
453 bbtkMessage("Help",1," " << GetDescription() <<std::endl);
454 bbtkMessage("Help",1," By : " << GetAuthor() <<std::endl);
455 bbtkMessage("Help",1," Category(s) : " << GetCategory() <<std::endl);
457 bbtkMessage("Help",1," * Inputs : "<<std::endl);
459 bbtkMessage("Help",1," * No inputs"<<std::endl);
460 InputDescriptorMapType::const_iterator i;
461 unsigned int namelmax = 0;
462 unsigned int typelmax = 0;
463 for ( i = mInput.begin(); i != mInput.end(); ++i )
465 if (i->second->GetName().size()>namelmax)
466 namelmax = i->second->GetName().size();
467 if (i->second->GetTypeName().size()>typelmax)
468 typelmax = i->second->GetTypeName().size();
470 OutputDescriptorMapType::const_iterator o;
473 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
475 if (o->second->GetName().size()>namelmax)
476 namelmax = o->second->GetName().size();
477 if (o->second->GetTypeName().size()>typelmax)
478 typelmax = o->second->GetTypeName().size();
483 for ( i = mInput.begin(); i != mInput.end(); ++i )
485 std::string name(i->second->GetName());
487 name.append(1+namelmax-name.size(),' ');
488 std::string type(i->second->GetTypeName());
490 type.append(1+typelmax-type.size(),' ');
491 bbtkMessage("Help",1,
494 <<" : "<<i->second->GetDescription()<<std::endl);
499 bbtkMessage("Help",1," * Outputs : "<<std::endl);
501 bbtkMessage("Help",1," * No outputs"<<std::endl);
502 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
504 std::string name(o->second->GetName());
506 name.append(1+namelmax-name.size(),' ');
507 std::string type(o->second->GetTypeName());
509 type.append(1+typelmax-type.size(),' ');
510 bbtkMessage("Help",1,
513 <<" : "<<o->second->GetDescription()<<std::endl);
518 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
521 bbtkMessage("Help",1," * Boxes : "<<std::endl);
523 bbtkMessage("Help",1," * No boxes"<<std::endl);
525 ComplexBlackBox::BlackBoxMapType::const_iterator b;
526 for ( b = B.begin(); b != B.end(); ++b )
528 bbtkMessage("Help",1," '"<<b->second->bbGetName()<<
530 << b->second->bbGetDescriptor()->GetPackage()->GetName()
532 <<b->second->bbGetTypeName()<<">"<<std::endl);
537 //=======================================================================