1 /*=========================================================================
4 Module: $RCSfile: bbtkComplexBlackBox.cxx,v $
6 Date: $Date: 2008/04/22 08:29:09 $
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 =========================================================================*/
22 * \brief class bbtk::ComplexBlackBox : user defined complex black boxes
24 #include "bbtkComplexBlackBox.h"
25 #include "bbtkBlackBoxDescriptor.h"
26 #include "bbtkFactory.h"
27 #include "bbtkConfigurationFile.h"
32 //==========================================================================
33 /// Creates a new complex black box
34 ComplexBlackBox::Pointer ComplexBlackBox::New(const std::string& name,
35 ComplexBlackBoxDescriptor::Pointer desc)
37 bbtkDebugMessage("object",1,"##> ComplexBlackBox::New('"<<name<<"','"<<
38 desc->GetTypeName()<<"')" <<bbtkendl);
39 ComplexBlackBox::Pointer p =
40 MakeBlackBoxPointer(new ComplexBlackBox(name,desc));
41 bbtkDebugMessage("object",2,"<## ComplexBlackBox::New('"<<name<<"','"<<
42 desc->GetTypeName()<<"')" <<bbtkendl);
45 //==========================================================================
47 //=======================================================================
48 /// Usefull constructor
49 ComplexBlackBox::ComplexBlackBox(const std::string &name,
50 ComplexBlackBoxDescriptor::Pointer desc)
53 mLockedDescriptor(desc),
56 bbtkDebugMessage("object",3,
57 "##> ComplexBlackBox::ComplexBlackBox(\""
58 <<name<<"\")"<<std::endl);
59 bbAllocateConnectors();
60 bbtkDebugMessage("object",3,
61 "<## ComplexBlackBox::ComplexBlackBox(\""
62 <<name<<"\")"<<std::endl);
64 //=======================================================================
66 //=======================================================================
67 /// Constructor from an existing box (copy) with a new name
68 ComplexBlackBox::ComplexBlackBox(ComplexBlackBox& from,
69 const std::string &name)
70 : BlackBox(from,name),
71 // The locked descriptor is copied from the unlocked one
72 // to make the box a non-prototype !!
73 mLockedDescriptor(from.mDescriptor),
74 mDescriptor(from.mDescriptor),
75 mExecutionList(from.mExecutionList)
77 bbtkDebugMessage("object",3,
78 "##> ComplexBlackBox::ComplexBlackBox(\""
79 <<from.bbGetName()<<"\",\""
80 <<name<<"\")"<<std::endl);
81 bbtkDebugMessage("object",4," * Cloning Black Boxes"<<std::endl);
82 BlackBoxMapType::const_iterator i;
83 for ( i = from.mBlackBoxMap.begin(); i != from.mBlackBoxMap.end(); ++i )
85 bbtkDebugMessageInc("object",5," * Cloning \""<<i->first<<"\""<<std::endl);
86 BlackBox::Pointer B = i->second->bbClone(i->second->bbGetName());
87 bbUnsafeAddBlackBox(B);
91 bbtkDebugMessage("object",4," * Cloning Connections"<<std::endl);
92 ConnectionListType::const_iterator j;
93 for ( j = from.mConnectionList.begin(); j != from.mConnectionList.end(); ++j )
95 bbtkDebugMessage("object",5," * Cloning \""<<
96 (*j)->GetFullName()<<"\""<<std::endl);
98 BlackBox::Pointer bbfrom = bbGetBlackBox( (*j)->GetOriginalBlackBoxFrom()->bbGetName() );
99 BlackBox::Pointer bbto = bbGetBlackBox( (*j)->GetOriginalBlackBoxTo()->bbGetName() );
101 Connection::Pointer c = mDescriptor.lock()->GetFactory()->
102 NewConnection( bbfrom,
103 (*j)->GetOriginalBlackBoxFromOutput(),
105 (*j)->GetOriginalBlackBoxToInput() );
112 bbAllocateConnectors();
113 bbtkDebugMessage("object",3,
114 "<## ComplexBlackBox::ComplexBlackBox(\""
115 <<from.bbGetName()<<"\",\""
116 <<name<<"\")"<<std::endl);
118 //=======================================================================
120 //=======================================================================
122 ComplexBlackBox::~ComplexBlackBox()
124 bbtkDebugMessage("object",3,
125 "==> ComplexBlackBox::~ComplexBlackBox() ["
126 <<bbGetName()<<"]"<<std::endl);
128 bbtkDebugMessage("object",4,
129 " -> Releasing connections"<<std::endl);
130 mConnectionList.clear();
131 bbtkDebugMessage("object",4,
132 " -> Releasing boxes"<<std::endl);
133 mBlackBoxMap.clear();
137 this->bbDesallocateConnectors();
138 if (mLockedDescriptor)
140 bbtkMessage("object",3," --> Releasing descriptor"<<std::endl);
141 mLockedDescriptor.reset();
142 if (!mDescriptor.expired()) BlackBoxDescriptor::Release(mDescriptor);
145 bbtkDebugMessage("object",3,
146 "<== ComplexBlackBox::~ComplexBlackBox() ["
147 <<bbGetName()<<"]"<<std::endl);
149 //=======================================================================
152 //=========================================================================
153 /// Allocates the i/o connectors of the black box
154 void ComplexBlackBox::bbAllocateConnectors()
156 bbtkDebugMessageInc("Kernel",8,
157 "ComplexBlackBox::bbAllocateConnectors() ["
158 <<bbGetFullName()<<"]"
162 const BlackBoxDescriptor::InputDescriptorMapType& imap
163 = bbGetDescriptor()->GetInputDescriptorMap();
164 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
165 for ( i = imap.begin(); i != imap.end(); ++i )
167 bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
168 // Redirect the connector to the internal box connector
169 // Cast the BBInputDescriptor into a ComplexBBInputDescriptor
170 ComplexBlackBoxInputDescriptor* d =
171 (ComplexBlackBoxInputDescriptor*)i->second;
172 // Get the internal box connector
173 BlackBoxInputConnector* c =
174 bbUnsafeGetBlackBox ( d->GetTarget() )
175 ->bbGetInputConnectorMap()[ d->GetInput() ];
177 bbGetInputConnectorMap()[i->second->GetName()] = c;
178 //new BlackBoxInputConnector();
182 const BlackBoxDescriptor::OutputDescriptorMapType& omap
183 = bbGetDescriptor()->GetOutputDescriptorMap();
184 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
185 for ( o = omap.begin(); o != omap.end(); ++o )
187 bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
188 // Redirect the connector to the internal box connector
189 // Cast the BBOutputDescriptor into a ComplexBBOutputDescriptor
190 ComplexBlackBoxOutputDescriptor* d =
191 (ComplexBlackBoxOutputDescriptor*)o->second;
192 // Get the internal box connector
193 BlackBoxOutputConnector* c =
194 bbUnsafeGetBlackBox ( d->GetTarget() )
195 ->bbGetOutputConnectorMap()[ d->GetOutput() ];
197 bbGetOutputConnectorMap()[o->second->GetName()] = c;
198 //new BlackBoxOutputConnector();
203 bbtkDebugDecTab("Kernel",8);
205 //=========================================================================
208 //=========================================================================
209 /// Desallocates the i/o connectors of the black box
210 void ComplexBlackBox::bbDesallocateConnectors()
212 bbtkDebugMessageInc("Kernel",8,
213 "ComplexBlackBox::DesallocateConnectors()"
216 // The connectors have not been allocated by the complex box
217 // but by the internal boxes. Hence **DO NOT** desallocate !
218 // just clear the maps to avoid that
219 // BlackBox::bbDesallocateConnectors delete the connectors
222 bbGetInputConnectorMap().clear();
223 bbGetOutputConnectorMap().clear();
225 bbtkDebugDecTab("Kernel",8);
228 //=========================================================================
230 //=======================================================================
231 BlackBox::Pointer ComplexBlackBox::bbClone(const std::string& name)
233 bbtkDebugMessageInc("Kernel",9,
234 "ComplexBlackBox::bbClone(\""<<name<<"\") ["
235 <<bbGetFullName()<<"]"<<std::endl);
237 ComplexBlackBox* CBB = new ComplexBlackBox(*this,name);
238 return MakeBlackBoxPointer(CBB);
240 //=======================================================================
242 //=======================================================================
243 /// Main processing method of the box.
244 /// Executes the box so that its outputs are up-to-date on exit
245 void ComplexBlackBox::bbExecute(bool force)
247 bbtkDebugMessageInc("Process",1,
248 "ComplexBlackBox::bbExecute() ["
249 <<bbGetFullName()<<"]"<<std::endl);
252 Wx::BeginBusyCursor();
254 if (mExecutionList.size() != 0)
257 std::vector<std::string>::const_iterator i;
258 for (i=mExecutionList.begin();
259 i!=mExecutionList.end();
262 bbtkDebugMessage("Process",2," -> Executing '"<<*i<<"'"<<std::endl);
263 mBlackBoxMap[*i]->bbExecute(force);
268 std::map<std::string, BlackBox::Pointer>::iterator i;
269 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
271 i->second->bbExecute(force);
278 bbtkDebugDecTab("Process",1);
281 //==================================================================
283 //==================================================================
284 void ComplexBlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
286 bbtkDebugMessage("Process",3,
287 "ComplexBlackBox::bbSetModifiedStatus("
288 <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
290 c->GetBlackBox()->bbSetModifiedStatus(c);
292 bbtkDebugMessage("Process",3,
293 "EO ComplexBlackBox::bbSetModifiedStatus("
294 <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
296 //==================================================================
298 //==================================================================
299 void ComplexBlackBox::bbAddToExecutionList( const std::string& name )
301 bbtkDebugMessageInc("Kernel",9,
302 "ComplexBlackBox::bbAddToExecutionList(\""
304 <<bbGetFullName()<<"]"<<std::endl);
306 mExecutionList.push_back( name );
308 bbtkDebugDecTab("Kernel",9);
311 //==================================================================
313 //==================================================================
314 IOStatus ComplexBlackBox::bbBackwardUpdate(Connection::Pointer caller)
316 bbtkDebugMessageInc("Process",1,
317 "ComplexBlackBox::bbBackwardUpdate("
318 <<(caller?caller->GetFullName():"0")<<") ["
319 <<bbGetFullName()<<"]"<<std::endl);
320 // bbtkInternalError("ComplexBlackBox::bbBackwardUpdate should never be called !");
324 bbtkInternalError("ComplexBlackBox::bbBackwardUpdate called with caller=0");
327 std::cout << "CBB BUP : "<<caller->GetBlackBoxFrom()->bbGetFullName()
328 <<"."<<caller->GetBlackBoxFromOutput()<<"----"
329 <<caller->GetOriginalBlackBoxFrom()->bbGetFullName()
330 <<"."<<caller->GetOriginalBlackBoxFromOutput()<<std::endl;
335 IOStatus s = UPTODATE;
336 const BlackBoxDescriptor::OutputDescriptorMapType& omap
337 = bbGetDescriptor()->GetOutputDescriptorMap();
338 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator i
339 = omap.find(caller->GetBlackBoxFromOutput());
342 // Cast the BBOutputDescriptor into a ComplexBBOutputDescriptor
343 ComplexBlackBoxOutputDescriptor* d =
344 (ComplexBlackBoxOutputDescriptor*)i->second;
345 // Get the internal box
346 BlackBox::Pointer b = bbUnsafeGetBlackBox ( d->GetTarget() );
347 // Calls BackwardUpdate on it
348 bbtkDebugMessageInc("Process",2,"Internal box connected to output : "<<d->GetTarget()<<std::endl);
349 // Because internal box can also be a complex box we have to
350 // temporarily change the connection BlackBoxFromOutput to the
353 // std::string oldout = caller->GetBlackBoxFromOutput();
354 // std::cout << "oldout = "<<oldout<<std::endl;
355 // std::cout << "tmpout = "<<d->GetOutput()<<std::endl;
356 // caller->SetBlackBoxFromOutput(d->GetOutput());
358 //Connection newcaller(*caller);
359 //newcaller.SetBlackBoxFromOutput(d->GetOutput());
360 //IOStatus s1 = b->bbBackwardUpdate(&newcaller);
361 IOStatus s1 = b->bbBackwardUpdate(caller);
363 // restore old output
364 // caller->SetBlackBoxFromOutput(oldout);
366 // ??? STATUS OF CBBs ???
367 // ??? Here it is only the final status of the boxes connected to the output
368 if (s1==MODIFIED) s=MODIFIED;
369 bbtkDebugDecTab("Process",2);
373 bbtkError("Connection '"<<caller->GetFullName()<<"' does not point to a valid output of the complex box !");
375 bbtkDebugDecTab("Process",1);
380 //==================================================================
383 //==================================================================
384 Data ComplexBlackBox::bbGetOutput( const std::string &name )
386 bbtkDebugMessageInc("Data",7,
387 "ComplexBlackBox::bbGetOutput(\""<<name<<"\") ["
388 <<bbGetFullName()<<"]"<<std::endl);
390 ComplexBlackBoxOutputDescriptor* d =
391 (ComplexBlackBoxOutputDescriptor*)
392 bbGetDescriptor()->GetOutputDescriptor(name);
394 Data p = bbGetBlackBox(d->GetTarget())->bbGetOutput(d->GetOutput());
397 bbtkDebugDecTab("Data",7);
400 //==================================================================
402 //==================================================================
403 /// Gets the input Data of a given name
404 Data ComplexBlackBox::bbGetInput( const std::string &name )
406 bbtkDebugMessageInc("Data",7,
407 "ComplexBlackBox::bbGetInput(\""<<name<<"\") ["
408 <<bbGetFullName()<<"]"<<std::endl);
410 ComplexBlackBoxInputDescriptor* d =
411 (ComplexBlackBoxInputDescriptor*)
412 bbGetDescriptor()->GetInputDescriptor(name);
414 Data p = bbGetBlackBox(d->GetTarget())->bbGetInput(d->GetInput());
416 bbtkDebugDecTab("Data",7);
419 //==================================================================
421 //==================================================================
422 /// Sets the data of the output called <name>
423 void ComplexBlackBox::bbSetOutput( const std::string &name, Data data)
425 bbtkDebugMessageInc("Data",7,
426 "ComplexBlackBox::bbSetOutput(\""<<name<<"\",data) ["
427 <<bbGetFullName()<<"]"<<std::endl);
429 ComplexBlackBoxOutputDescriptor* d =
430 (ComplexBlackBoxOutputDescriptor*)
431 bbGetDescriptor()->GetOutputDescriptor(name);
433 bbGetBlackBox(d->GetTarget())->bbSetOutput(d->GetOutput(),data);
435 bbtkDebugDecTab("Data",7);
437 //==================================================================
439 //==================================================================
440 /// Sets the data of the input called <name>
441 void ComplexBlackBox::bbSetInput( const std::string &name, Data data,
444 bbtkDebugMessageInc("Data",7,
445 "ComplexBlackBox::bbSetInput(\""<<name<<"\",data) ["
446 <<bbGetFullName()<<"]"<<std::endl);
448 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
449 bbGetDescriptor()->GetInputDescriptor(name);
451 bbGetBlackBox(d->GetTarget())->bbSetInput(d->GetInput(),data,setModified);
453 bbtkDebugDecTab("Data",7);
455 //==================================================================
458 //==================================================================
459 /// Sets the data of the input called <name>
460 void ComplexBlackBox::bbBruteForceSetInputPointer( const std::string &name,
464 bbtkDebugMessageInc("Data",7,
465 "ComplexBlackBox::bbBruteForceSetInputPointer('"
466 <<name<<"',"<<data<<") ["
467 <<bbGetFullName()<<"]"<<std::endl);
469 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
470 bbGetDescriptor()->GetInputDescriptor(name);
472 bbGetBlackBox(d->GetTarget())->bbBruteForceSetInputPointer(d->GetInput(),
476 bbtkDebugDecTab("Data",7);
478 //==================================================================
480 //=========================================================================
481 /// Connects the input <name> to the connection c
482 void ComplexBlackBox::bbConnectInput( const std::string& name, Connection::Pointer c)
484 bbtkDebugMessage("connection",2,
485 "==> ComplexBlackBox::bbConnectInput(\""
486 <<name<<"\","<<c->GetFullName()<<") ["
487 <<bbGetFullName()<<"]"
490 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
491 bbGetDescriptor()->GetInputDescriptor(name);
494 BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
496 bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
499 c->SetBlackBoxToInput(d->GetInput());
501 bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
502 t->bbConnectInput(d->GetInput(),c);
504 bbtkDebugMessage("connection",2,
505 "<== ComplexBlackBox::bbConnectInput(\""
506 <<name<<"\","<<c->GetFullName()<<") ["
507 <<bbGetFullName()<<"]"
510 //=========================================================================
513 //=========================================================================
514 /// Connects the output <name> to the connection c
515 void ComplexBlackBox::bbConnectOutput( const std::string& name, Connection::Pointer c)
517 bbtkDebugMessage("connection",2,
518 "==> ComplexBlackBox::bbConnectOutput(\""
519 <<name<<"\","<<c->GetFullName()<<") ["
520 <<bbGetFullName()<<"]"<<std::endl);
522 ComplexBlackBoxOutputDescriptor* d = (ComplexBlackBoxOutputDescriptor*)
523 bbGetDescriptor()->GetOutputDescriptor(name);
525 BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
527 bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
529 c->SetBlackBoxFrom(t);
530 c->SetBlackBoxFromOutput(d->GetOutput());
532 bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
534 t->bbConnectOutput(d->GetOutput(),c);
536 bbtkDebugMessage("connection",2,
537 "<== ComplexBlackBox::bbConnectOutput(\""
538 <<name<<"\","<<c->GetFullName()<<") ["
539 <<bbGetFullName()<<"]"<<std::endl);
541 //=========================================================================
544 //==================================================================
545 /// Adds the black box to the complex box
546 void ComplexBlackBox::bbAddBlackBox( BlackBox::Pointer b)
548 bbtkDebugMessageInc("Kernel",7,
549 "ComplexBlackBox::AddBlackBox(\""<<b->bbGetName()
551 <<bbGetFullName()<<"]"<<std::endl);
553 if ( bbUnsafeGetBlackBox(b->bbGetName()) )
555 bbtkError("a black box called \""<<b->bbGetName()
556 <<"\" already exists");
558 b->bbSetParent(GetThisPointer<ComplexBlackBox>());
559 mBlackBoxMap[b->bbGetName()] = b;
561 bbtkDebugDecTab("Kernel",7);
563 //==================================================================
565 //==================================================================
566 /// Adds the black box to the complex box (unsafe)
567 void ComplexBlackBox::bbUnsafeAddBlackBox( BlackBox::Pointer b)
569 bbtkDebugMessageInc("Kernel",7,
570 "ComplexBlackBox::UnsafeAddBlackBox(\""<<b->bbGetName()
572 <<bbGetFullName()<<"]"<<std::endl);
574 b->bbSetParent(GetThisPointer<ComplexBlackBox>());
575 mBlackBoxMap[b->bbGetName()] = b;
577 bbtkDebugDecTab("Kernel",7);
579 //==================================================================
581 //==================================================================
582 /// Removes the black box from the complex box
583 void ComplexBlackBox::bbRemoveBlackBox( const std::string& name,
584 bool remove_connections )
586 bbtkDebugMessageInc("Kernel",7,
587 "ComplexBlackBox::RemoveBlackBox(\""<<name<<"\") ["
588 <<bbGetFullName()<<"]"<<std::endl);
590 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
591 if ( i == mBlackBoxMap.end() )
593 bbtkError("the black box \""<<name<<"\" does not exist");
595 BlackBox::WeakPointer p = i->second;
597 if (remove_connections)
599 ConnectionListType::const_iterator j;
600 for ( j = mConnectionList.begin();
601 j != mConnectionList.end(); ++j )
606 if (p.use_count()!=1)
608 bbtkError("the black box \""<<name<<"\" is still connected");
611 mBlackBoxMap.erase(i);
614 // Unload orphan dl packages
615 Package::UnLoadReleasedDynamicallyLoadedPackages();
617 bbtkDebugDecTab("Kernel",7);
619 //==================================================================
621 //==================================================================
622 /// Adds the connection to the complex box
623 void ComplexBlackBox::bbAddConnection( Connection::Pointer c)
625 bbtkDebugMessageInc("Kernel",7,
626 "ComplexBlackBox::AddConnection(\""<<"..."<<"\") ["
627 <<bbGetFullName()<<"]"<<std::endl);
629 mConnectionList.push_back(c);
631 bbtkDebugDecTab("Kernel",7);
633 //==================================================================
634 // void RemoveConnection( );
636 //==================================================================
637 /// Returns the black box with name <name>
638 BlackBox::Pointer ComplexBlackBox::bbGetBlackBox( const std::string& name )
640 bbtkDebugMessageInc("Kernel",9,
641 "ComplexBlackBox::GetBlackBox(\""<<name<<"\") ["
642 <<bbGetFullName()<<"]"<<std::endl);
644 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
645 if ( i == mBlackBoxMap.end() )
647 bbtkError("the black box \""<<name<<"\" does not exist");
650 bbtkDebugDecTab("Kernel",9);
653 //==================================================================
655 //==================================================================
656 /// Returns the black box with name <name> : does not throw an exception
657 /// if it does not exist but return a null pointer
658 BlackBox::Pointer ComplexBlackBox::bbUnsafeGetBlackBox( const std::string& name )
660 bbtkDebugMessageInc("Kernel",9,
661 "ComplexBlackBox::UnsafeGetBlackBox(\""<<name<<"\") ["
662 <<bbGetFullName()<<"]"
665 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
666 if ( i == mBlackBoxMap.end() )
668 bbtkDebugDecTab("Kernel",9);
669 return BlackBox::Pointer();
672 bbtkDebugDecTab("Kernel",9);
676 //==================================================================
678 //==================================================================
679 void ComplexBlackBox::bbPrintBlackBoxes()
681 bbtkDebugMessageInc("Kernel",9,
682 "ComplexBlackBox::PrintBlackBoxes() ["
683 <<bbGetFullName()<<"]"
686 BlackBoxMapType::iterator i;
687 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
689 bbtkMessage("Help",1,i->second->bbGetFullName()<<std::endl);
692 bbtkDebugDecTab("Kernel",9);
694 //==================================================================
698 //=========================================================================
700 void ComplexBlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
704 fprintf(ff,"%s_IN_%p",bbGetTypeName().c_str(),this);
706 fprintf(ff,"%s_OUT_%p",bbGetTypeName().c_str(),this);
709 //=========================================================================
712 //=========================================================================
713 BlackBox::Pointer ComplexBlackBox::bbFindBlackBox(const std::string &blackboxname)
715 BlackBox::Pointer blackbox;
716 std::string subname="";
717 std::string restname="";
718 std::string delimiters(">");
719 // Skip delimiters at beginning.
720 std::string::size_type lastPos = blackboxname.find_first_not_of(delimiters, 0);
721 // Find first "non-delimiter".
722 std::string::size_type pos = blackboxname.find_first_of(delimiters, lastPos);
724 // Found a token, add it to the vector.
725 subname = blackboxname.substr(lastPos, pos - lastPos);
726 restname = blackboxname.substr(lastPos+pos - lastPos+1, 999);
728 if (restname==subname)
733 BlackBoxMapType::iterator i = mBlackBoxMap.find(subname);
734 if ( i != mBlackBoxMap.end() )
736 blackbox = i->second;
739 blackbox = blackbox->bbFindBlackBox(restname);
744 //=========================================================================
746 //=========================================================================
747 void ComplexBlackBox::bbInsertHTMLGraph( std::ofstream& s,
751 const std::string& output_dir,
755 std::string directory(output_dir);
757 if (output_dir.length() == 0)
759 // Don't pollute the file store with "temp_dir" directories ...
760 std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
761 directory = default_temp_dir + "/" + "temp_dir";
764 std::string simplefilename (this->bbGetTypeName()+"_"+this->bbGetName());
765 std::string simplefilename_png (simplefilename+".png");
766 std::string filename (directory+"/"+simplefilename);
767 std::string filename_png (filename+".png");
768 std::string filename_cmap (filename+".cmap");
769 std::string filename_dot (filename+".dot");
771 std::string filename_png2 ("\"" + filename_png + "\"");
772 std::string filename_cmap2 ("\"" + filename_cmap + "\"");
773 std::string filename_dot2 ("\"" + filename_dot + "\"");
776 std::string command1 ("dot -T png -o "
777 + filename_png2 + " " + filename_dot2);
778 std::string command1a("dot -T cmap -o "
779 + filename_cmap2 + " " + filename_dot2);
781 // 1. Generating .dot file
783 ff = fopen(filename_dot.c_str(),"w");
784 fprintf(ff,"digraph bbtk_graph{\n");
785 fprintf(ff,"rankdir=LR%s\n",";");
786 fprintf(ff,"node [shape=record]%s\n",";");
788 this->bbWriteDotFileBlackBox(ff,
789 GetThisPointer<ComplexBlackBox>(),
797 // 2. Executing .dot file -> png
798 system( command1.c_str() );
799 // 3. Executing .dot file -> cmap
800 system( command1a.c_str() );
802 // 4. HTML code insertion
804 (s) << "<center><img src=\"" << simplefilename_png
805 << "\" border=\"0\" usemap=\"#map_"<< simplefilename
806 <<"\" alt=\"\"></center>\n";
809 (s) << "<map name=\"map_"<< simplefilename <<"\">\n";
812 ff2=fopen(filename_cmap.c_str(),"r");
825 //=========================================================================
828 //=========================================================================
829 /// Write Graphviz-dot description in file
830 void ComplexBlackBox::bbWriteDotFileBlackBox(FILE *ff,
831 BlackBox::Pointer parentblackbox,
832 int detail, int level,
837 std::string valueStr("");
838 Package::Pointer package = this->bbGetDescriptor()->GetPackage();
843 tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL();
845 tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocURL();
852 std::string tmp2=bbGetTypeName();
853 std::string url(tmp1 + "#" + tmp2 );
854 fprintf( ff , "subgraph cluster_%s_%p {\n",bbGetName().c_str(),this);
856 if (!( (bbGetTypeName()=="workspace") && (bbGetName()=="workspacePrototype")) )
858 fprintf( ff , " URL = \"%s\" %s",url.c_str(),";");
861 std::string boxname="["+bbGetTypeName()+"]";
862 if (GetThisPointer<ComplexBlackBox>()!=parentblackbox)
870 boxname = bbGetName();
871 boxname = boxname + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
875 fprintf( ff , " label = \"%s\"%s\n", boxname.c_str() ,";");
879 // fprintf( ff , " style=filled%s\n",";");
880 // fprintf( ff , " color=grey%s\n",";");
881 fprintf( ff , " node [style=filled]%s\n",";");
882 fprintf( ff , " fillcolor=grey%s\n",";");
883 fprintf( ff , " edge [color=blue]%s\n",";");
887 std::string labelStr1;
888 std::string labelStr2;
889 labelStr1 = boxname + "\\n(output)" ;
890 labelStr2 = " | {{ ";
893 OutputConnectorMapType::iterator i;
895 const BlackBoxDescriptor::OutputDescriptorMapType& omap = this->bbGetDescriptor()->GetOutputDescriptorMap();
896 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
897 for ( o = omap.begin(); o != omap.end(); ++o )
901 labelStr2=labelStr2+" | ";
904 if (instanceOrtype==true)
906 valueStr = this->bbGetOutputAsString(o->second->GetName()/*
909 labelStr2=labelStr2+"<"+o->second->GetName().c_str()+"> " + valueStr + o->second->GetName().c_str();
912 labelStr2 = labelStr2+ " } }";
918 labelStr1 = labelStr1 + labelStr2;
923 bbWriteDotInputOutputName(ff,false,detail,level);
924 fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" );
929 labelStr1 = boxname + "\\n(input)" ;
930 labelStr2 = " | {{ ";
932 InputConnectorMapType::iterator ii;
934 const BlackBoxDescriptor::InputDescriptorMapType& imap = this->bbGetDescriptor()->GetInputDescriptorMap();
935 BlackBoxDescriptor::InputDescriptorMapType::const_iterator iii;
936 for ( iii = imap.begin(); iii != imap.end(); ++iii )
940 labelStr2=labelStr2+" | ";
943 if (instanceOrtype==true)
945 valueStr = this->bbGetInputAsString(iii->second->GetName()/*,factory*/) + " = ";
947 labelStr2=labelStr2+"<"+iii->second->GetName().c_str()+"> " + valueStr + iii->second->GetName().c_str();
951 labelStr2 = labelStr2+ " } }";
954 labelStr1 = labelStr1 + labelStr2;
958 bbWriteDotInputOutputName(ff,true,detail,level);
959 fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" );
964 BlackBoxMapType::iterator j;
965 for ( j = mBlackBoxMap.begin(); j != mBlackBoxMap.end(); ++j )
969 j->second->bbWriteDotFileBlackBox(ff,
978 fprintf( ff , "}\n\n");
980 fprintf( ff , " edge[color=blue]%s\n",";");
984 // Relation Input with the inside BlackBox of the this ComplexBlackbox
985 ComplexBlackBoxDescriptor::InputDescriptorMapType::iterator xx;
986 ComplexBlackBoxDescriptor::InputDescriptorMapType idmt=bbGetDescriptor()->GetInputDescriptorMap();
987 for ( xx = idmt.begin(); xx != idmt.end(); ++xx )
989 ComplexBlackBoxInputDescriptor *cbbid = (ComplexBlackBoxInputDescriptor*)xx->second;
992 bbWriteDotInputOutputName(ff,true,detail,level);
995 fprintf(ff,":%s",cbbid->GetName().c_str() );
998 BlackBox::Pointer bb = bbGetBlackBox( cbbid->GetTarget() );
999 bb->bbWriteDotInputOutputName(ff,true,detail,level);
1002 fprintf(ff,":%s \n", cbbid->GetInput().c_str() );
1008 fprintf(ff,"\n \n");
1012 // Relation Output ComplexBlackBox
1013 ComplexBlackBoxDescriptor::OutputDescriptorMapType::iterator yy;
1014 ComplexBlackBoxDescriptor::OutputDescriptorMapType odmt=bbGetDescriptor()->GetOutputDescriptorMap();
1015 for ( yy = odmt.begin(); yy != odmt.end(); ++yy )
1017 ComplexBlackBoxOutputDescriptor *cbbod = (ComplexBlackBoxOutputDescriptor*)yy->second;
1019 BlackBox::Pointer bb = bbGetBlackBox( cbbod->GetTarget() );
1020 bb->bbWriteDotInputOutputName(ff,false,detail,level);
1023 fprintf(ff,":%s", cbbod->GetOutput().c_str() );
1026 bbWriteDotInputOutputName(ff,false,detail,level);
1029 fprintf(ff,":%s",cbbod->GetName().c_str() );
1038 // Relation from the out side of this ComplexBlackBox with its Inputs
1039 if (GetThisPointer<ComplexBlackBox>()!=parentblackbox) {
1040 for ( ii = bbGetInputConnectorMap().begin();
1041 ii != bbGetInputConnectorMap().end(); ++ii )
1045 Connection::Pointer con = ii->second->GetConnection();
1047 BlackBox::Pointer a=con->GetBlackBoxFrom();
1048 BlackBox::Pointer b=con->GetBlackBoxTo();
1050 a->bbWriteDotInputOutputName(ff,false,detail,level);
1053 fprintf(ff,":%s",con->GetBlackBoxFromOutput().c_str());
1056 b->bbWriteDotInputOutputName(ff,true,detail,level);
1059 fprintf(ff,":%s",con->GetBlackBoxToInput().c_str());
1061 fprintf(ff,"%s\n",";");
1065 } // if parentblackbox
1067 //=========================================================================
1074 //=======================================================================
1075 /// Generates the list of the packages of which its depends
1076 /// (cause an internal box belongs to it)
1077 void ComplexBlackBox::GetPackagesDependencies(std::vector<Package*>& deps)
1080 BlackBoxMapType::iterator i;
1081 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1083 deps.push_back(i->second->bbGetDescriptor()->GetPackage());
1087 //=======================================================================
1090 //=======================================================================
1091 void ComplexBlackBox::Check(bool recursive)
1093 bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
1094 <<" ["<<bbGetFullName()<<"]"<<std::endl);
1096 BlackBoxMapType::const_iterator i;
1097 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1099 i->second->Check(recursive);
1101 ConnectionListType::const_iterator j;
1102 for ( j = mConnectionList.begin();
1103 j != mConnectionList.end(); ++j )
1107 bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
1108 <<" ["<<bbGetFullName()<<"] ... OK"<<std::endl);
1111 //=======================================================================
1113 //=========================================================================
1114 /// Returns the name with the name of the parent prepended if any
1115 std::string ComplexBlackBox::bbGetNameWithParent() const
1117 if (!IsAPrototype()) return BlackBox::bbGetNameWithParent();
1118 if (bbGetDescriptor())
1120 return bbGetDescriptor()->GetFullTypeName() + ":" + bbGetName();
1124 return std::string(":") + bbGetName();
1127 //=========================================================================
1129 //==========================================================================
1130 std::string ComplexBlackBox::GetObjectName() const
1132 return std::string("ComplexBlackBox '")+bbGetNameWithParent()
1135 //==========================================================================
1137 //==========================================================================
1138 std::string ComplexBlackBox::GetObjectInfo() const
1140 std::stringstream i;
1141 i << " - "<<mBlackBoxMap.size() << " boxes / "
1142 <<mConnectionList.size() << " connections" << std::endl;
1145 //==========================================================================
1147 //==========================================================================
1148 size_t ComplexBlackBox::GetObjectSize() const
1150 size_t s = Superclass::GetObjectSize();
1151 s += ComplexBlackBox::GetObjectInternalSize();
1154 //==========================================================================
1155 //==========================================================================
1156 size_t ComplexBlackBox::GetObjectInternalSize() const
1158 size_t s = sizeof(ComplexBlackBox);
1161 //==========================================================================
1162 //==========================================================================
1163 size_t ComplexBlackBox::GetObjectRecursiveSize() const
1165 size_t s = Superclass::GetObjectRecursiveSize();
1166 s += ComplexBlackBox::GetObjectInternalSize();
1167 BlackBoxMapType::const_iterator i;
1168 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1170 s += i->second->GetObjectRecursiveSize();
1172 ConnectionListType::const_iterator j;
1173 for ( j = mConnectionList.begin();
1174 j != mConnectionList.end(); ++j )
1176 s += (*j)->GetObjectRecursiveSize();
1180 //==========================================================================