2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkComplexBlackBoxDescriptor.cxx,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.23 $
34 =========================================================================*/
41 * \brief Class bbtk::ComplexBlackBoxDescriptor : describes a ComplexBlackBox (constituents, connections) and is able to create an instance of it.
43 #include "bbtkComplexBlackBoxDescriptor.h"
44 #include "bbtkComplexBlackBox.h"
45 //#include "bbtkFactory.h"
46 #include "bbtkMessageManager.h"
47 #include "bbtkUtilities.h"
49 #define bbtkDMessage(key,level,mess) \
50 bbtkMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
51 #define bbtkDDebugMessage(key,level,mess) \
52 bbtkDebugMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
56 //=======================================================================
58 ComplexBlackBoxDescriptor::Pointer
59 ComplexBlackBoxDescriptor::New(const std::string& name)
61 bbtkDebugMessage("object",1,"##> ComplexBlackBoxDescriptor::New(\""<<name<<"\")"<<std::endl);
62 ComplexBlackBoxDescriptor::Pointer p =
63 MakePointer(new ComplexBlackBoxDescriptor(name));
64 bbtkDebugMessage("object",1,"<## ComplexBlackBoxDescriptor::New(\""<<name<<"\")"<<std::endl);
67 //=======================================================================
69 //=======================================================================
71 ComplexBlackBoxDescriptor::ComplexBlackBoxDescriptor(const std::string& name)
73 bbtkDDebugMessage("object",2,"==> ComplexBlackBoxDescriptor(\""<<name<<"\")"<<std::endl);
74 SetTypeOfScript(TS_SCRIPT_COMPLEXBOX);
76 AddToCategory("complex box");
77 mPrototype = ComplexBlackBox::New(name+std::string("Prototype"),
78 MakePointer(this,true));
79 mPrototype->SetAsPrototype();
80 bbtkDDebugMessage("object",2,"<== ComplexBlackBoxDescriptor(\""<<name<<"\")"<<std::endl);
82 //=======================================================================
86 //=======================================================================
88 ComplexBlackBoxDescriptor::~ComplexBlackBoxDescriptor()
90 bbtkDDebugMessage("object",2,"==> ~ComplexBlackBoxDescriptor()"<<std::endl);
92 bbtkDDebugMessage("object",2,"<== ~ComplexBlackBoxDescriptor()"<<std::endl);
94 //=======================================================================
96 //=========================================================================
98 void ComplexBlackBoxDescriptor::Check(bool recursive) const
100 mPrototype->Check(recursive);
102 //=========================================================================
104 //=======================================================================
105 /// Creates an instance of name <name> of the ComplexBlackBox of which this is the descriptor
107 ComplexBlackBoxDescriptor::NewBlackBox(const std::string& name)
109 bbtkDDebugMessage("kernel",5,
110 "ComplexBlackBoxDescriptor::NewBlackBox(\""
114 return mPrototype->bbClone(name);
118 //=======================================================================
121 //=======================================================================
123 void ComplexBlackBoxDescriptor::Release(bool release_package)
126 //=======================================================================
129 //=======================================================================
130 /// Adds a black box to the complex box
131 void ComplexBlackBoxDescriptor::Add ( const std::string& type,
132 const std::string& name
135 bbtkDDebugMessage("kernel",5,
136 "ComplexBlackBoxDescriptor::Add(\""
137 <<type<<"\",\""<<name<<"\")"
143 bbtkError("ComplexBlackBoxDescriptor::Add : no factory set");
146 // Verify that a box with the same name does not exist already
147 if ( mPrototype->bbUnsafeGetBlackBox( name ) )
149 bbtkError("a black box \""<<name<<"\" already exists");
151 // ok : create new one
152 mPrototype->bbAddBlackBox ( GetFactory()->NewBlackBox(type,name) );
156 //=======================================================================
158 //=======================================================================
159 /// Removes a black box from the complex box
160 void ComplexBlackBoxDescriptor::Remove( const std::string& name,
161 bool remove_connections)
163 mPrototype->bbRemoveBlackBox(name,remove_connections);
165 //=======================================================================
168 //=======================================================================
169 /// Adds a black box to the execution list
170 void ComplexBlackBoxDescriptor::AddToExecutionList ( const std::string& box)
172 bbtkDDebugMessage("kernel",5,
173 "ComplexBlackBoxDescriptor::AddToExecutionList(\""
176 // Verify that the box exists
177 BlackBox::Pointer b = mPrototype->bbUnsafeGetBlackBox( box );
180 bbtkError("the black box \""<<box<<"\" does not exist");
183 mPrototype->bbAddToExecutionList ( box );
189 //=======================================================================
190 /// Connects two black boxes of the complex box
191 void ComplexBlackBoxDescriptor::Connect ( const std::string& from,
192 const std::string& output,
193 const std::string& to,
194 const std::string& input
197 bbtkDDebugMessage("kernel",5,
198 "ComplexBlackBoxDescriptor::Connect(\""
199 <<from<<"\",\""<<output<<"\",\""
206 bbtkError("ComplexBlackBoxDescriptor::Connect : no factory set");
209 // Verify that a box with the same name does not exist already
210 BlackBox::Pointer bbfrom = mPrototype->bbGetBlackBox( from );
213 bbtkError("the black box \""<<from<<"\" does not exist");
215 BlackBox::Pointer bbto = mPrototype->bbGetBlackBox( to );
218 bbtkError("the black box \""<<to<<"\" does not exist");
220 Connection::Pointer c = GetFactory()->NewConnection( bbfrom, output, bbto, input );
221 mPrototype->bbAddConnection(c);
223 //=======================================================================
226 //=======================================================================
227 /// Defines an input of the complex box
228 void ComplexBlackBoxDescriptor::DefineInput ( const std::string& name,
229 const std::string& box,
230 const std::string& input,
231 const std::string& help)
233 bbtkDDebugMessage("kernel",5,
234 "ComplexBlackBoxDescriptor::DefineInput(\""
235 <<name<<"\",\""<<box<<"\",\""
236 <<input<<"\",\""<<help
240 BlackBox::Pointer bb = mPrototype->bbGetBlackBox( box );
243 bbtkError("the black box \""<<box<<"\" does not exist");
246 if (!bb->bbHasInput(input) )
248 bbtkError("the black box \""<<box<<"\" does not have input \""
252 const BlackBoxInputDescriptor* d =
253 bb->bbGetDescriptor()->GetInputDescriptor(input);
254 AddInputDescriptor ( new ComplexBlackBoxInputDescriptor
255 ( typeid(ComplexBlackBoxDescriptor),
266 //=======================================================================
268 //=======================================================================
269 /// Defines an output of the complex box
270 void ComplexBlackBoxDescriptor::DefineOutput ( const std::string& name,
271 const std::string& box,
272 const std::string& output,
273 const std::string& help)
275 bbtkDDebugMessage("kernel",5,
276 "ComplexBlackBoxDescriptor::DefineOutput(\""
277 <<name<<"\",\""<<box<<"\",\""
278 <<output<<"\",\""<<help
282 BlackBox::Pointer bb = mPrototype->bbGetBlackBox( box );
285 bbtkError("the black box \""<<box<<"\" does not exist");
288 if (!bb->bbHasOutput(output) )
290 bbtkError("the black box \""<<box<<"\" does not have output \""
294 const BlackBoxOutputDescriptor* d =
295 bb->bbGetDescriptor()->GetOutputDescriptor(output);
296 AddOutputDescriptor ( new ComplexBlackBoxOutputDescriptor
297 ( typeid(ComplexBlackBoxDescriptor),
308 //=======================================================================
310 //=======================================================================
311 void ComplexBlackBoxDescriptor::PrintBlackBoxes()
313 mPrototype->bbPrintBlackBoxes();
315 //=======================================================================
318 //=======================================================================
319 void ComplexBlackBoxDescriptor::InsertHTMLGraph( std::ofstream& s ,
320 int detail, int level,
321 const std::string& output_dir, bool relative_link )
323 this->mPrototype->bbInsertHTMLGraph( s,
329 //=======================================================================
331 //=========================================================================
332 void ComplexBlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
333 int detail, int level,
334 const std::string& output_dir, bool relative_link)
336 bbtkDDebugMessage("kernel",9,
337 "ComplexBlackBoxDescriptor::InsertHtmlHelp()"
342 std::string name = GetTypeName();
343 Utilities::html_format(name);
345 // std::ofstream* s = &s1;
348 (s) << "<a name=\""<<name<<"\"></a>\n";
350 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
351 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
352 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
353 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
356 std::string descr = GetDescription();
357 //Utilities::html_format(descr);
359 std::string author = GetAuthor();
360 Utilities::html_format(author);
362 std::vector<std::string> categories;
363 // Split the category string
364 std::string delimiters = ";,";
365 Utilities::SplitString(GetCategory(),
366 delimiters,categories);
369 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
370 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
371 << descr << "</TD></TR>\n";
373 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
374 << author << "</TD></TR>\n";
376 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
377 std::vector<std::string>::iterator ci;
378 for (ci=categories.begin(); ci!=categories.end(); ++ci)
380 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
384 std::string inc = GetScriptFileName();
387 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 ";
388 // s << inc << " <a href=\"../../../bbs/"<<inc<<"\">[source]</a>";
389 // LG TODO : USE PACKAGE BBS PATH
390 s << inc << " <a href=\""<<inc<<"\">[source]</a>";
395 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
399 (s) << "<TR><TD style='vertical-align: top;'><b> Uses </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
401 std::set<BlackBoxDescriptor::Pointer> pdeps;
402 ComplexBlackBox::BlackBoxMapType::const_iterator b;
403 for ( b = B.begin(); b != B.end(); ++b )
405 BlackBoxDescriptor::Pointer d = b->second->bbGetDescriptor();
406 if (pdeps.find(d) != pdeps.end())
410 Package::Pointer p = d->GetPackage();
412 std::string name = b->second->bbGetTypeName();
416 url = p->GetDocRelativeURL();
418 url = p->GetDocURL();
420 s << "<a href=\"" <<url<<"#"<<name<<"\">"
421 << p->GetName()<<"::"<<name<<"</a>\n";
424 (s) << "</TD></TR>\n";
433 InsertHTMLGraph( s , detail,level, output_dir, relative_link);
437 std::string col("#CCCCFF");
439 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
440 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
441 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
442 <<"\">Inputs</TD></TR>\n";
443 const BlackBoxDescriptor::InputDescriptorMapType& imap =
444 GetInputDescriptorMap();
446 InputDescriptorMapType::const_iterator in;
448 for ( in = imap.begin(); in != imap.end(); ++in )
450 std::string name(in->second->GetName());
451 Utilities::html_format(name);
453 std::string type("<");
454 type += in->second->GetTypeName();
456 Utilities::html_format(type);
458 std::string descr(in->second->GetDescription());
459 //Utilities::html_format(descr);
462 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
463 << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
464 << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
467 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
468 << "<TD style='vertical-align: top;'><I><PRE> "<<descr<<" </PRE></I></TD>"
469 << "<TD style='vertical-align: top;'>"<<type<<"</TD></TR>\n";
472 // (s) << "</TABLE>\n";
477 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
478 // (s) << "<TABLE border=1 cellspacing=0>\n";
479 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
480 <<"\">Outputs</TD></TR>\n";
482 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
483 GetOutputDescriptorMap();
485 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
487 for ( o = omap.begin(); o != omap.end(); ++o )
489 std::string name(o->second->GetName());
490 Utilities::html_format(name);
492 std::string type("<");
493 type += o->second->GetTypeName();
495 Utilities::html_format(type);
497 std::string descr(o->second->GetDescription());
498 //Utilities::html_format(descr);
501 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
502 << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
503 << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
505 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
506 << "<TD style='vertical-align: top;'><I><PRE> "<<descr<<" </PRE></I></TD>"
507 << "<TD style='vertical-align: top;'>"<<type<<"</TD></TR>\n";
517 //=========================================================================
520 //=======================================================================
521 void ComplexBlackBoxDescriptor::GetHelp(bool full) const
523 if (full) bbtkMessage("help",1,"Complex Black Box <"<<
524 GetPackage()->GetName()<<"::"
525 <<GetTypeName()<<">"<<std::endl);
526 bbtkMessage("help",1," " << GetDescription() <<std::endl);
527 bbtkMessage("help",1," By : " << GetAuthor() <<std::endl);
528 bbtkMessage("help",1," Category(s) : " << GetCategory() <<std::endl);
530 bbtkMessage("help",1," * Inputs : "<<std::endl);
532 bbtkMessage("help",1," * No inputs"<<std::endl);
533 InputDescriptorMapType::const_iterator i;
534 unsigned int namelmax = 0;
535 unsigned int typelmax = 0;
536 unsigned int natlmax = 0;
537 for ( i = mInput.begin(); i != mInput.end(); ++i )
539 if (i->second->GetName().size()>namelmax)
540 namelmax = i->second->GetName().size();
541 if (i->second->GetTypeName().size()>typelmax)
542 typelmax = i->second->GetTypeName().size();
543 if (i->second->GetNature().size()>natlmax)
544 natlmax = i->second->GetNature().size();
546 OutputDescriptorMapType::const_iterator o;
549 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
551 if (o->second->GetName().size()>namelmax)
552 namelmax = o->second->GetName().size();
553 if (o->second->GetTypeName().size()>typelmax)
554 typelmax = o->second->GetTypeName().size();
555 if (o->second->GetNature().size()>natlmax)
556 natlmax = o->second->GetNature().size();
561 for ( i = mInput.begin(); i != mInput.end(); ++i )
563 std::string name(i->second->GetName());
565 name.append(1+namelmax-name.size(),' ');
566 std::string type(i->second->GetTypeName());
568 type.append(1+typelmax-type.size(),' ');
569 std::string nature(i->second->GetNature());
571 nature.append(1+natlmax-nature.size(),' ');
572 bbtkMessage("help",1,
576 <<" : "<<i->second->GetDescription()<<std::endl);
581 bbtkMessage("help",1," * Outputs : "<<std::endl);
583 bbtkMessage("help",1," * No outputs"<<std::endl);
584 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
586 std::string name(o->second->GetName());
588 name.append(1+namelmax-name.size(),' ');
589 std::string type(o->second->GetTypeName());
591 type.append(1+typelmax-type.size(),' ');
592 std::string nature(o->second->GetNature());
594 nature.append(1+natlmax-nature.size(),' ');
595 bbtkMessage("help",1,
599 <<" : "<<o->second->GetDescription()<<std::endl);
604 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
607 bbtkMessage("help",1," * Boxes : "<<std::endl);
609 bbtkMessage("help",1," * No boxes"<<std::endl);
611 ComplexBlackBox::BlackBoxMapType::const_iterator b;
612 for ( b = B.begin(); b != B.end(); ++b )
614 bbtkMessage("help",1," '"<<b->second->bbGetName()<<
616 << b->second->bbGetDescriptor()->GetPackage()->GetName()
618 <<b->second->bbGetTypeName()<<">"<<std::endl);
623 //=======================================================================
625 //==========================================================================
626 std::string ComplexBlackBoxDescriptor::GetObjectName() const
628 return std::string("ComplexBlackBoxDescriptor '")+GetFullTypeName()
631 //==========================================================================
632 //=======================================================================
633 std::string ComplexBlackBoxDescriptor::GetObjectInfo() const
638 //=======================================================================
639 //==========================================================================
640 size_t ComplexBlackBoxDescriptor::GetObjectSize() const
642 size_t s = Superclass::GetObjectSize();
643 s += ComplexBlackBoxDescriptor::GetObjectInternalSize();
646 //==========================================================================
647 //==========================================================================
648 size_t ComplexBlackBoxDescriptor::GetObjectInternalSize() const
650 size_t s = sizeof(ComplexBlackBoxDescriptor);
653 //==========================================================================
654 //==========================================================================
655 size_t ComplexBlackBoxDescriptor::GetObjectRecursiveSize() const
657 size_t s = Superclass::GetObjectRecursiveSize();
658 s += ComplexBlackBoxDescriptor::GetObjectInternalSize();
659 s += mPrototype->GetObjectRecursiveSize();
662 //==========================================================================
663 void ComplexBlackBoxDescriptor::GetBoxesInside (NodeTreeC& tree, int cont)
665 std::cout<<"getBoxesInside recursivo "<<std::endl;
667 std::string name = GetTypeName();
668 std::cout<<"despues de getTypeName : "<<name<<std::endl;
669 std::string descr = GetDescription();
670 std::string author = GetAuthor();
672 //list.push_back(name);
674 //list.push_back(descr);
675 //list.push_back(author);
678 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
680 std::cout<<"----B size "<< B.size() << std::endl;
681 std::cout<<"----antes if "<<std::endl;
684 std::set<BlackBoxDescriptor::Pointer> pdeps;
685 ComplexBlackBox::BlackBoxMapType::const_iterator b;
686 std::cout<<"----antes for "<<std::endl;
687 for ( b = B.begin(); b != B.end(); ++b )
689 BlackBoxDescriptor::Pointer d = b->second->bbGetDescriptor();
690 //std::cout<<"nombre objeto d "<< d->GetObjectName() <<std::endl;
692 //if (pdeps.find(d) != pdeps.end())
695 std::cout<<"----1~ "<<std::endl;
696 Package::Pointer p = d->GetPackage();
697 std::string nameBox = b->second->bbGetTypeName();
698 std::string packageBox = p->GetName();
699 tree.insertChild(nameBox);
700 std::cout<<"----2~ "<<std::endl;
701 //list.push_back(nameBox);
702 //list.push_back(packageBox);
703 std::cout<<"-----cont "<<cont<<std::endl;
704 //tree.treeTour(cont);
705 d->GetBoxesInside (tree.childs[k], cont);
706 std::cout<<"----3~ "<<std::endl;
707 //std::cout<<"--despues de la recursion "<<k<<std::endl;
710 std::cout<<"----despues for "<<std::endl;
712 std::cout<<"----despues if "<<std::endl;
713 std::cout<<"end getBoxesInside recursivo"<<std::endl;
716 //===================================================
718 //==========================================================================