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");
210 // Verify that a box with the same name does not exist already
211 BlackBox::Pointer bbfrom = mPrototype->bbGetBlackBox( from );
214 bbtkError("the black box \""<<from<<"\" does not exist");
216 BlackBox::Pointer bbto = mPrototype->bbGetBlackBox( to );
219 bbtkError("the black box \""<<to<<"\" does not exist");
222 Connection::Pointer c
223 = GetFactory()->NewConnection( bbfrom, output, bbto, input );
225 mPrototype->bbAddConnection(c);
229 //=======================================================================
232 //=======================================================================
233 /// Defines an input of the complex box
234 void ComplexBlackBoxDescriptor::DefineInput ( const std::string& name,
235 const std::string& box,
236 const std::string& input,
237 const std::string& help)
239 bbtkDDebugMessage("kernel",5,
240 "ComplexBlackBoxDescriptor::DefineInput(\""
241 <<name<<"\",\""<<box<<"\",\""
242 <<input<<"\",\""<<help
246 BlackBox::Pointer bb = mPrototype->bbGetBlackBox( box );
249 bbtkError("the black box \""<<box<<"\" does not exist");
252 if (!bb->bbHasInput(input) )
254 bbtkError("the black box \""<<box<<"\" does not have input \""
258 const BlackBoxInputDescriptor* d =
259 bb->bbGetDescriptor()->GetInputDescriptor(input);
260 AddInputDescriptor ( new ComplexBlackBoxInputDescriptor
261 ( typeid(ComplexBlackBoxDescriptor),
272 //=======================================================================
274 //=======================================================================
275 /// Defines an output of the complex box
276 void ComplexBlackBoxDescriptor::DefineOutput ( const std::string& name,
277 const std::string& box,
278 const std::string& output,
279 const std::string& help)
281 bbtkDDebugMessage("kernel",5,
282 "ComplexBlackBoxDescriptor::DefineOutput(\""
283 <<name<<"\",\""<<box<<"\",\""
284 <<output<<"\",\""<<help
288 BlackBox::Pointer bb = mPrototype->bbGetBlackBox( box );
291 bbtkError("the black box \""<<box<<"\" does not exist");
294 if (!bb->bbHasOutput(output) )
296 bbtkError("the black box \""<<box<<"\" does not have output \""
300 const BlackBoxOutputDescriptor* d =
301 bb->bbGetDescriptor()->GetOutputDescriptor(output);
302 AddOutputDescriptor ( new ComplexBlackBoxOutputDescriptor
303 ( typeid(ComplexBlackBoxDescriptor),
314 //=======================================================================
316 //=======================================================================
317 void ComplexBlackBoxDescriptor::PrintBlackBoxes()
319 mPrototype->bbPrintBlackBoxes();
321 //=======================================================================
324 //=======================================================================
325 void ComplexBlackBoxDescriptor::InsertHTMLGraph( std::ofstream& s ,
326 int detail, int level,
327 const std::string& output_dir, bool relative_link )
329 this->mPrototype->bbInsertHTMLGraph( s,
335 //=======================================================================
337 //=========================================================================
338 void ComplexBlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
339 int detail, int level,
340 const std::string& output_dir, bool relative_link)
342 bbtkDDebugMessage("kernel",9,
343 "ComplexBlackBoxDescriptor::InsertHtmlHelp()"
348 std::string name = GetTypeName();
349 Utilities::html_format(name);
351 // std::ofstream* s = &s1;
354 (s) << "<a name=\""<<name<<"\"></a>\n";
356 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
357 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
358 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
359 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
362 std::string descr = GetDescription();
363 //Utilities::html_format(descr);
365 std::string author = GetAuthor();
366 Utilities::html_format(author);
368 std::vector<std::string> categories;
369 // Split the category string
370 std::string delimiters = ";,";
371 Utilities::SplitString(GetCategory(),
372 delimiters,categories);
375 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
376 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
377 << descr << "</TD></TR>\n";
379 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
380 << author << "</TD></TR>\n";
382 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
383 std::vector<std::string>::iterator ci;
384 for (ci=categories.begin(); ci!=categories.end(); ++ci)
386 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
390 std::string inc = GetScriptFileName();
393 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 ";
394 // s << inc << " <a href=\"../../../bbs/"<<inc<<"\">[source]</a>";
395 // LG TODO : USE PACKAGE BBS PATH
396 s << inc << " <a href=\""<<inc<<"\">[source]</a>";
401 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
405 (s) << "<TR><TD style='vertical-align: top;'><b> Uses </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
407 std::set<BlackBoxDescriptor::Pointer> pdeps;
408 ComplexBlackBox::BlackBoxMapType::const_iterator b;
409 for ( b = B.begin(); b != B.end(); ++b )
411 BlackBoxDescriptor::Pointer d = b->second->bbGetDescriptor();
412 if (pdeps.find(d) != pdeps.end())
416 Package::Pointer p = d->GetPackage();
418 std::string name = b->second->bbGetTypeName();
422 url = p->GetDocRelativeURL();
424 url = p->GetDocURL();
426 s << "<a href=\"" <<url<<"#"<<name<<"\">"
427 << p->GetName()<<"::"<<name<<"</a>\n";
430 (s) << "</TD></TR>\n";
439 InsertHTMLGraph( s , detail,level, output_dir, relative_link);
443 std::string col("#CCCCFF");
445 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
446 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
447 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
448 <<"\">Inputs</TD></TR>\n";
449 const BlackBoxDescriptor::InputDescriptorMapType& imap =
450 GetInputDescriptorMap();
452 InputDescriptorMapType::const_iterator in;
454 for ( in = imap.begin(); in != imap.end(); ++in )
456 std::string name(in->second->GetName());
457 Utilities::html_format(name);
459 std::string type("<");
460 type += in->second->GetTypeName();
462 Utilities::html_format(type);
464 std::string descr(in->second->GetDescription());
465 //Utilities::html_format(descr);
468 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
469 << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
470 << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
473 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
474 << "<TD style='vertical-align: top;'><I><PRE> "<<descr<<" </PRE></I></TD>"
475 << "<TD style='vertical-align: top;'>"<<type<<"</TD></TR>\n";
478 // (s) << "</TABLE>\n";
483 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
484 // (s) << "<TABLE border=1 cellspacing=0>\n";
485 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<col
486 <<"\">Outputs</TD></TR>\n";
488 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
489 GetOutputDescriptorMap();
491 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
493 for ( o = omap.begin(); o != omap.end(); ++o )
495 std::string name(o->second->GetName());
496 Utilities::html_format(name);
498 std::string type("<");
499 type += o->second->GetTypeName();
501 Utilities::html_format(type);
503 std::string descr(o->second->GetDescription());
504 //Utilities::html_format(descr);
507 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
508 << "<TD style='vertical-align: top;'><I><PRE> "<<type<<" </PRE></I></TD>"
509 << "<TD style='vertical-align: top;'>"<<descr<<"</TD></TR>\n";
511 (s) << "<TR><TD style='vertical-align: top;'><B><PRE> "<<name<<" </PRE></B></TD>"
512 << "<TD style='vertical-align: top;'><I><PRE> "<<descr<<" </PRE></I></TD>"
513 << "<TD style='vertical-align: top;'>"<<type<<"</TD></TR>\n";
523 //=========================================================================
526 //=======================================================================
527 void ComplexBlackBoxDescriptor::GetHelp(bool full) const
529 if (full) bbtkMessage("help",1,"Complex Black Box <"<<
530 GetPackage()->GetName()<<"::"
531 <<GetTypeName()<<">"<<std::endl);
532 bbtkMessage("help",1," " << GetDescription() <<std::endl);
533 bbtkMessage("help",1," By : " << GetAuthor() <<std::endl);
534 bbtkMessage("help",1," Category(s) : " << GetCategory() <<std::endl);
536 bbtkMessage("help",1," * Inputs : "<<std::endl);
538 bbtkMessage("help",1," * No inputs"<<std::endl);
539 InputDescriptorMapType::const_iterator i;
540 unsigned int namelmax = 0;
541 unsigned int typelmax = 0;
542 unsigned int natlmax = 0;
543 for ( i = mInput.begin(); i != mInput.end(); ++i )
545 if (i->second->GetName().size()>namelmax)
546 namelmax = i->second->GetName().size();
547 if (i->second->GetTypeName().size()>typelmax)
548 typelmax = i->second->GetTypeName().size();
549 if (i->second->GetNature().size()>natlmax)
550 natlmax = i->second->GetNature().size();
552 OutputDescriptorMapType::const_iterator o;
555 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
557 if (o->second->GetName().size()>namelmax)
558 namelmax = o->second->GetName().size();
559 if (o->second->GetTypeName().size()>typelmax)
560 typelmax = o->second->GetTypeName().size();
561 if (o->second->GetNature().size()>natlmax)
562 natlmax = o->second->GetNature().size();
567 for ( i = mInput.begin(); i != mInput.end(); ++i )
569 std::string name(i->second->GetName());
571 name.append(1+namelmax-name.size(),' ');
572 std::string type(i->second->GetTypeName());
574 type.append(1+typelmax-type.size(),' ');
575 std::string nature(i->second->GetNature());
577 nature.append(1+natlmax-nature.size(),' ');
578 bbtkMessage("help",1,
582 <<" : "<<i->second->GetDescription()<<std::endl);
587 bbtkMessage("help",1," * Outputs : "<<std::endl);
589 bbtkMessage("help",1," * No outputs"<<std::endl);
590 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
592 std::string name(o->second->GetName());
594 name.append(1+namelmax-name.size(),' ');
595 std::string type(o->second->GetTypeName());
597 type.append(1+typelmax-type.size(),' ');
598 std::string nature(o->second->GetNature());
600 nature.append(1+natlmax-nature.size(),' ');
601 bbtkMessage("help",1,
605 <<" : "<<o->second->GetDescription()<<std::endl);
610 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
613 bbtkMessage("help",1," * Boxes : "<<std::endl);
615 bbtkMessage("help",1," * No boxes"<<std::endl);
617 ComplexBlackBox::BlackBoxMapType::const_iterator b;
618 for ( b = B.begin(); b != B.end(); ++b )
620 bbtkMessage("help",1," '"<<b->second->bbGetName()<<
622 << b->second->bbGetDescriptor()->GetPackage()->GetName()
624 <<b->second->bbGetTypeName()<<">"<<std::endl);
629 //=======================================================================
631 //==========================================================================
632 std::string ComplexBlackBoxDescriptor::GetObjectName() const
634 return std::string("ComplexBlackBoxDescriptor '")+GetFullTypeName()
637 //==========================================================================
638 //=======================================================================
639 std::string ComplexBlackBoxDescriptor::GetObjectInfo() const
644 //=======================================================================
645 //==========================================================================
646 size_t ComplexBlackBoxDescriptor::GetObjectSize() const
648 size_t s = Superclass::GetObjectSize();
649 s += ComplexBlackBoxDescriptor::GetObjectInternalSize();
652 //==========================================================================
653 //==========================================================================
654 size_t ComplexBlackBoxDescriptor::GetObjectInternalSize() const
656 size_t s = sizeof(ComplexBlackBoxDescriptor);
659 //==========================================================================
660 //==========================================================================
661 size_t ComplexBlackBoxDescriptor::GetObjectRecursiveSize() const
663 size_t s = Superclass::GetObjectRecursiveSize();
664 s += ComplexBlackBoxDescriptor::GetObjectInternalSize();
665 s += mPrototype->GetObjectRecursiveSize();
668 //==========================================================================
669 void ComplexBlackBoxDescriptor::GetBoxesInside (NodeTreeC& tree, int cont)
671 std::cout<<"getBoxesInside recursivo "<<std::endl;
673 std::string name = GetTypeName();
674 std::cout<<"despues de getTypeName : "<<name<<std::endl;
675 std::string descr = GetDescription();
676 std::string author = GetAuthor();
678 //list.push_back(name);
680 //list.push_back(descr);
681 //list.push_back(author);
684 const ComplexBlackBox::BlackBoxMapType& B = mPrototype->bbGetBlackBoxMap();
686 std::cout<<"----B size "<< B.size() << std::endl;
687 std::cout<<"----antes if "<<std::endl;
690 std::set<BlackBoxDescriptor::Pointer> pdeps;
691 ComplexBlackBox::BlackBoxMapType::const_iterator b;
692 std::cout<<"----antes for "<<std::endl;
693 for ( b = B.begin(); b != B.end(); ++b )
695 BlackBoxDescriptor::Pointer d = b->second->bbGetDescriptor();
696 //std::cout<<"nombre objeto d "<< d->GetObjectName() <<std::endl;
698 //if (pdeps.find(d) != pdeps.end())
701 std::cout<<"----1~ "<<std::endl;
702 Package::Pointer p = d->GetPackage();
703 std::string nameBox = b->second->bbGetTypeName();
704 std::string packageBox = p->GetName();
705 tree.insertChild(nameBox);
706 std::cout<<"----2~ "<<std::endl;
707 //list.push_back(nameBox);
708 //list.push_back(packageBox);
709 std::cout<<"-----cont "<<cont<<std::endl;
710 //tree.treeTour(cont);
711 d->GetBoxesInside (tree.childs[k], cont);
712 std::cout<<"----3~ "<<std::endl;
713 //std::cout<<"--despues de la recursion "<<k<<std::endl;
716 std::cout<<"----despues for "<<std::endl;
718 std::cout<<"----despues if "<<std::endl;
719 std::cout<<"end getBoxesInside recursivo"<<std::endl;
722 //===================================================
724 //==========================================================================