1 /*=========================================================================
4 Module: $RCSfile: bbtkComplexBlackBox.cxx,v $
6 Date: $Date: 2008/04/24 10:11:28 $
7 Version: $Revision: 1.17 $
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();
136 this->bbDesallocateConnectors();
138 bbtkDebugMessage("object",3,
139 "<== ComplexBlackBox::~ComplexBlackBox() ["
140 <<bbGetName()<<"]"<<std::endl);
142 //=======================================================================
145 //=========================================================================
146 /// Allocates the i/o connectors of the black box
147 void ComplexBlackBox::bbAllocateConnectors()
149 bbtkDebugMessageInc("Kernel",8,
150 "ComplexBlackBox::bbAllocateConnectors() ["
151 <<bbGetFullName()<<"]"
155 const BlackBoxDescriptor::InputDescriptorMapType& imap
156 = bbGetDescriptor()->GetInputDescriptorMap();
157 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
158 for ( i = imap.begin(); i != imap.end(); ++i )
160 bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
161 // Redirect the connector to the internal box connector
162 // Cast the BBInputDescriptor into a ComplexBBInputDescriptor
163 ComplexBlackBoxInputDescriptor* d =
164 (ComplexBlackBoxInputDescriptor*)i->second;
165 // Get the internal box connector
166 BlackBoxInputConnector* c =
167 bbUnsafeGetBlackBox ( d->GetTarget() )
168 ->bbGetInputConnectorMap()[ d->GetInput() ];
170 bbGetInputConnectorMap()[i->second->GetName()] = c;
174 const BlackBoxDescriptor::OutputDescriptorMapType& omap
175 = bbGetDescriptor()->GetOutputDescriptorMap();
176 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
177 for ( o = omap.begin(); o != omap.end(); ++o )
179 bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
180 // Redirect the connector to the internal box connector
181 // Cast the BBOutputDescriptor into a ComplexBBOutputDescriptor
182 ComplexBlackBoxOutputDescriptor* d =
183 (ComplexBlackBoxOutputDescriptor*)o->second;
184 // Get the internal box connector
185 BlackBoxOutputConnector* c =
186 bbUnsafeGetBlackBox ( d->GetTarget() )
187 ->bbGetOutputConnectorMap()[ d->GetOutput() ];
189 bbGetOutputConnectorMap()[o->second->GetName()] = c;
192 bbtkDebugDecTab("Kernel",8);
194 //=========================================================================
197 //=========================================================================
198 /// Desallocates the i/o connectors of the black box
199 void ComplexBlackBox::bbDesallocateConnectors()
201 bbtkDebugMessageInc("Kernel",8,
202 "ComplexBlackBox::DesallocateConnectors()"
205 // The connectors have not been allocated by the complex box
206 // but by the internal boxes. Hence **DO NOT** desallocate !
207 // just clear the maps to avoid that
208 // BlackBox::bbDesallocateConnectors delete the connectors
209 bbGetInputConnectorMap().clear();
210 bbGetOutputConnectorMap().clear();
212 bbtkDebugDecTab("Kernel",8);
215 //=========================================================================
217 //=======================================================================
218 BlackBox::Pointer ComplexBlackBox::bbClone(const std::string& name)
220 bbtkDebugMessageInc("Kernel",9,
221 "ComplexBlackBox::bbClone(\""<<name<<"\") ["
222 <<bbGetFullName()<<"]"<<std::endl);
224 ComplexBlackBox* CBB = new ComplexBlackBox(*this,name);
225 return MakeBlackBoxPointer(CBB);
227 //=======================================================================
229 //=======================================================================
230 /// Main processing method of the box.
231 /// Executes the box so that its outputs are up-to-date on exit
232 void ComplexBlackBox::bbExecute(bool force)
234 bbtkDebugMessageInc("process",2,
235 "=> ComplexBlackBox::bbExecute() ["
236 <<bbGetFullName()<<"]"<<std::endl);
239 Wx::BeginBusyCursor();
241 if (mExecutionList.size() != 0)
244 std::vector<std::string>::const_iterator i;
245 for (i=mExecutionList.begin();
246 i!=mExecutionList.end();
249 bbtkDebugMessage("process",3," -> Executing '"<<*i<<"'"<<std::endl);
250 mBlackBoxMap[*i]->bbExecute(force);
255 std::map<std::string, BlackBox::Pointer>::iterator i;
256 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
258 i->second->bbExecute(force);
266 //==================================================================
268 //==================================================================
269 void ComplexBlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
271 bbtkDebugMessage("modified",1,
272 "==> ComplexBlackBox::bbSetModifiedStatus("
273 <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
275 c->GetBlackBox()->bbSetModifiedStatus(c);
278 bbtkDebugMessage("modified",1,
279 "<== ComplexBlackBox::bbSetModifiedStatus("
280 <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
283 //==================================================================
285 //==================================================================
286 void ComplexBlackBox::bbAddToExecutionList( const std::string& name )
288 bbtkDebugMessageInc("Kernel",9,
289 "ComplexBlackBox::bbAddToExecutionList(\""
291 <<bbGetFullName()<<"]"<<std::endl);
293 mExecutionList.push_back( name );
295 bbtkDebugDecTab("Kernel",9);
298 //==================================================================
300 //==================================================================
301 IOStatus ComplexBlackBox::bbBackwardUpdate(Connection::Pointer caller)
303 bbtkDebugMessageInc("process",3,
304 "==> ComplexBlackBox::bbBackwardUpdate("
305 <<(caller?caller->GetFullName():"0")<<") ["
306 <<bbGetFullName()<<"]"<<std::endl);
307 // bbtkInternalError("ComplexBlackBox::bbBackwardUpdate should never be called !");
311 bbtkInternalError("ComplexBlackBox::bbBackwardUpdate called with caller=0");
315 std::cout << "CBB BUP : "<<caller->GetBlackBoxFrom()->bbGetFullName()
316 <<"."<<caller->GetBlackBoxFromOutput()<<"----"
317 <<caller->GetOriginalBlackBoxFrom()->bbGetFullName()
318 <<"."<<caller->GetOriginalBlackBoxFromOutput()<<std::endl;
323 IOStatus s = UPTODATE;
324 const BlackBoxDescriptor::OutputDescriptorMapType& omap
325 = bbGetDescriptor()->GetOutputDescriptorMap();
326 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator i
327 = omap.find(caller->GetBlackBoxFromOutput());
330 // Cast the BBOutputDescriptor into a ComplexBBOutputDescriptor
331 ComplexBlackBoxOutputDescriptor* d =
332 (ComplexBlackBoxOutputDescriptor*)i->second;
333 // Get the internal box
334 BlackBox::Pointer b = bbUnsafeGetBlackBox ( d->GetTarget() );
335 // Calls BackwardUpdate on it
336 bbtkDebugMessageInc("process",4,"Internal box connected to output : "<<d->GetTarget()<<std::endl);
337 // Because internal box can also be a complex box we have to
338 // temporarily change the connection BlackBoxFromOutput to the
341 // std::string oldout = caller->GetBlackBoxFromOutput();
342 // std::cout << "oldout = "<<oldout<<std::endl;
343 // std::cout << "tmpout = "<<d->GetOutput()<<std::endl;
344 // caller->SetBlackBoxFromOutput(d->GetOutput());
346 //Connection newcaller(*caller);
347 //newcaller.SetBlackBoxFromOutput(d->GetOutput());
348 //IOStatus s1 = b->bbBackwardUpdate(&newcaller);
349 IOStatus s1 = b->bbBackwardUpdate(caller);
351 // restore old output
352 // caller->SetBlackBoxFromOutput(oldout);
354 // ??? STATUS OF CBBs ???
355 // ??? Here it is only the final status of the boxes connected to the output
356 if (s1==MODIFIED) s=MODIFIED;
360 bbtkError("Connection '"<<caller->GetFullName()<<"' does not point to a valid output of the complex box !");
366 //==================================================================
369 //==================================================================
370 Data ComplexBlackBox::bbGetOutput( const std::string &name )
372 bbtkDebugMessageInc("Data",7,
373 "ComplexBlackBox::bbGetOutput(\""<<name<<"\") ["
374 <<bbGetFullName()<<"]"<<std::endl);
376 ComplexBlackBoxOutputDescriptor* d =
377 (ComplexBlackBoxOutputDescriptor*)
378 bbGetDescriptor()->GetOutputDescriptor(name);
380 Data p = bbGetBlackBox(d->GetTarget())->bbGetOutput(d->GetOutput());
383 bbtkDebugDecTab("Data",7);
386 //==================================================================
388 //==================================================================
389 /// Gets the input Data of a given name
390 Data ComplexBlackBox::bbGetInput( const std::string &name )
392 bbtkDebugMessageInc("Data",7,
393 "ComplexBlackBox::bbGetInput(\""<<name<<"\") ["
394 <<bbGetFullName()<<"]"<<std::endl);
396 ComplexBlackBoxInputDescriptor* d =
397 (ComplexBlackBoxInputDescriptor*)
398 bbGetDescriptor()->GetInputDescriptor(name);
400 Data p = bbGetBlackBox(d->GetTarget())->bbGetInput(d->GetInput());
402 bbtkDebugDecTab("Data",7);
405 //==================================================================
407 //==================================================================
408 /// Sets the data of the output called <name>
409 void ComplexBlackBox::bbSetOutput( const std::string &name, Data data)
411 bbtkDebugMessageInc("Data",7,
412 "ComplexBlackBox::bbSetOutput(\""<<name<<"\",data) ["
413 <<bbGetFullName()<<"]"<<std::endl);
415 ComplexBlackBoxOutputDescriptor* d =
416 (ComplexBlackBoxOutputDescriptor*)
417 bbGetDescriptor()->GetOutputDescriptor(name);
419 bbGetBlackBox(d->GetTarget())->bbSetOutput(d->GetOutput(),data);
421 bbtkDebugDecTab("Data",7);
423 //==================================================================
425 //==================================================================
426 /// Sets the data of the input called <name>
427 void ComplexBlackBox::bbSetInput( const std::string &name, Data data,
430 bbtkDebugMessageInc("Data",7,
431 "ComplexBlackBox::bbSetInput(\""<<name<<"\",data) ["
432 <<bbGetFullName()<<"]"<<std::endl);
434 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
435 bbGetDescriptor()->GetInputDescriptor(name);
437 bbGetBlackBox(d->GetTarget())->bbSetInput(d->GetInput(),data,setModified);
439 bbtkDebugDecTab("Data",7);
441 //==================================================================
444 //==================================================================
445 /// Sets the data of the input called <name>
446 void ComplexBlackBox::bbBruteForceSetInputPointer( const std::string &name,
450 bbtkDebugMessageInc("Data",7,
451 "ComplexBlackBox::bbBruteForceSetInputPointer('"
452 <<name<<"',"<<data<<") ["
453 <<bbGetFullName()<<"]"<<std::endl);
455 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
456 bbGetDescriptor()->GetInputDescriptor(name);
458 bbGetBlackBox(d->GetTarget())->bbBruteForceSetInputPointer(d->GetInput(),
462 bbtkDebugDecTab("Data",7);
464 //==================================================================
466 //=========================================================================
467 /// Connects the input <name> to the connection c
468 void ComplexBlackBox::bbConnectInput( const std::string& name, Connection* c)
470 bbtkDebugMessage("connection",2,
471 "==> ComplexBlackBox::bbConnectInput(\""
472 <<name<<"\","<<c->GetFullName()<<") ["
473 <<bbGetFullName()<<"]"
476 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
477 bbGetDescriptor()->GetInputDescriptor(name);
480 BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
482 bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
485 c->SetBlackBoxToInput(d->GetInput());
487 bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
488 t->bbConnectInput(d->GetInput(),c);
490 bbtkDebugMessage("connection",2,
491 "<== ComplexBlackBox::bbConnectInput(\""
492 <<name<<"\","<<c->GetFullName()<<") ["
493 <<bbGetFullName()<<"]"
496 //=========================================================================
499 //=========================================================================
500 /// Connects the output <name> to the connection c
501 void ComplexBlackBox::bbConnectOutput( const std::string& name, Connection* c)
503 bbtkDebugMessage("connection",2,
504 "==> ComplexBlackBox::bbConnectOutput(\""
505 <<name<<"\","<<c->GetFullName()<<") ["
506 <<bbGetFullName()<<"]"<<std::endl);
508 ComplexBlackBoxOutputDescriptor* d = (ComplexBlackBoxOutputDescriptor*)
509 bbGetDescriptor()->GetOutputDescriptor(name);
511 BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
513 bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
515 c->SetBlackBoxFrom(t);
516 c->SetBlackBoxFromOutput(d->GetOutput());
518 bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
520 t->bbConnectOutput(d->GetOutput(),c);
522 bbtkDebugMessage("connection",2,
523 "<== ComplexBlackBox::bbConnectOutput(\""
524 <<name<<"\","<<c->GetFullName()<<") ["
525 <<bbGetFullName()<<"]"<<std::endl);
527 //=========================================================================
530 //==================================================================
531 /// Adds the black box to the complex box
532 void ComplexBlackBox::bbAddBlackBox( BlackBox::Pointer b)
534 bbtkDebugMessageInc("Kernel",7,
535 "ComplexBlackBox::AddBlackBox(\""<<b->bbGetName()
537 <<bbGetFullName()<<"]"<<std::endl);
539 if ( bbUnsafeGetBlackBox(b->bbGetName()) )
541 bbtkError("a black box called \""<<b->bbGetName()
542 <<"\" already exists");
544 b->bbSetParent(GetThisPointer<ComplexBlackBox>());
545 mBlackBoxMap[b->bbGetName()] = b;
547 bbtkDebugDecTab("Kernel",7);
549 //==================================================================
551 //==================================================================
552 /// Adds the black box to the complex box (unsafe)
553 void ComplexBlackBox::bbUnsafeAddBlackBox( BlackBox::Pointer b)
555 bbtkDebugMessageInc("Kernel",7,
556 "ComplexBlackBox::UnsafeAddBlackBox(\""<<b->bbGetName()
558 <<bbGetFullName()<<"]"<<std::endl);
560 b->bbSetParent(GetThisPointer<ComplexBlackBox>());
561 mBlackBoxMap[b->bbGetName()] = b;
563 bbtkDebugDecTab("Kernel",7);
565 //==================================================================
567 //==================================================================
568 /// Removes the black box from the complex box
569 void ComplexBlackBox::bbRemoveBlackBox( const std::string& name,
570 bool remove_connections )
572 bbtkDebugMessageInc("Kernel",7,
573 "ComplexBlackBox::RemoveBlackBox(\""<<name<<"\") ["
574 <<bbGetFullName()<<"]"<<std::endl);
576 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
577 if ( i == mBlackBoxMap.end() )
579 bbtkError("the black box \""<<name<<"\" does not exist");
581 BlackBox::WeakPointer p = i->second;
583 if (remove_connections)
585 ConnectionListType::const_iterator j;
586 for ( j = mConnectionList.begin();
587 j != mConnectionList.end(); ++j )
592 if (p.use_count()!=1)
594 bbtkError("the black box \""<<name<<"\" is still connected");
597 mBlackBoxMap.erase(i);
600 // Unload orphan dl packages
601 Package::UnLoadReleasedDynamicallyLoadedPackages();
603 bbtkDebugDecTab("Kernel",7);
605 //==================================================================
607 //==================================================================
608 /// Adds the connection to the complex box
609 void ComplexBlackBox::bbAddConnection( Connection::Pointer c)
611 bbtkDebugMessageInc("Kernel",7,
612 "ComplexBlackBox::AddConnection(\""<<"..."<<"\") ["
613 <<bbGetFullName()<<"]"<<std::endl);
615 mConnectionList.push_back(c);
617 bbtkDebugDecTab("Kernel",7);
619 //==================================================================
620 // void RemoveConnection( );
622 //==================================================================
623 /// Returns the black box with name <name>
624 BlackBox::Pointer ComplexBlackBox::bbGetBlackBox( const std::string& name )
626 bbtkDebugMessageInc("Kernel",9,
627 "ComplexBlackBox::GetBlackBox(\""<<name<<"\") ["
628 <<bbGetFullName()<<"]"<<std::endl);
630 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
631 if ( i == mBlackBoxMap.end() )
633 bbtkError("the black box \""<<name<<"\" does not exist");
636 bbtkDebugDecTab("Kernel",9);
639 //==================================================================
641 //==================================================================
642 /// Returns the black box with name <name> : does not throw an exception
643 /// if it does not exist but return a null pointer
644 BlackBox::Pointer ComplexBlackBox::bbUnsafeGetBlackBox( const std::string& name )
646 bbtkDebugMessageInc("Kernel",9,
647 "ComplexBlackBox::UnsafeGetBlackBox(\""<<name<<"\") ["
648 <<bbGetFullName()<<"]"
651 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
652 if ( i == mBlackBoxMap.end() )
654 bbtkDebugDecTab("Kernel",9);
655 return BlackBox::Pointer();
658 bbtkDebugDecTab("Kernel",9);
662 //==================================================================
664 //==================================================================
665 void ComplexBlackBox::bbPrintBlackBoxes()
667 bbtkDebugMessageInc("Kernel",9,
668 "ComplexBlackBox::PrintBlackBoxes() ["
669 <<bbGetFullName()<<"]"
672 BlackBoxMapType::iterator i;
673 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
675 bbtkMessage("Help",1,i->second->bbGetFullName()<<std::endl);
678 bbtkDebugDecTab("Kernel",9);
680 //==================================================================
684 //=========================================================================
686 void ComplexBlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
690 fprintf(ff,"%s_IN_%p",bbGetTypeName().c_str(),this);
692 fprintf(ff,"%s_OUT_%p",bbGetTypeName().c_str(),this);
695 //=========================================================================
698 //=========================================================================
699 BlackBox::Pointer ComplexBlackBox::bbFindBlackBox(const std::string &blackboxname)
701 BlackBox::Pointer blackbox;
702 std::string subname="";
703 std::string restname="";
704 std::string delimiters(">");
705 // Skip delimiters at beginning.
706 std::string::size_type lastPos = blackboxname.find_first_not_of(delimiters, 0);
707 // Find first "non-delimiter".
708 std::string::size_type pos = blackboxname.find_first_of(delimiters, lastPos);
710 // Found a token, add it to the vector.
711 subname = blackboxname.substr(lastPos, pos - lastPos);
712 restname = blackboxname.substr(lastPos+pos - lastPos+1, 999);
714 if (restname==subname)
719 BlackBoxMapType::iterator i = mBlackBoxMap.find(subname);
720 if ( i != mBlackBoxMap.end() )
722 blackbox = i->second;
725 blackbox = blackbox->bbFindBlackBox(restname);
730 //=========================================================================
732 //=========================================================================
733 void ComplexBlackBox::bbInsertHTMLGraph( std::ofstream& s,
737 const std::string& output_dir,
741 std::string directory(output_dir);
743 if (output_dir.length() == 0)
745 // Don't pollute the file store with "temp_dir" directories ...
746 std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
747 directory = default_temp_dir + "/" + "temp_dir";
750 std::string simplefilename (this->bbGetTypeName()+"_"+this->bbGetName());
751 std::string simplefilename_png (simplefilename+".png");
752 std::string filename (directory+"/"+simplefilename);
753 std::string filename_png (filename+".png");
754 std::string filename_cmap (filename+".cmap");
755 std::string filename_dot (filename+".dot");
757 std::string filename_png2 ("\"" + filename_png + "\"");
758 std::string filename_cmap2 ("\"" + filename_cmap + "\"");
759 std::string filename_dot2 ("\"" + filename_dot + "\"");
762 std::string command1 ("dot -T png -o "
763 + filename_png2 + " " + filename_dot2);
764 std::string command1a("dot -T cmap -o "
765 + filename_cmap2 + " " + filename_dot2);
767 // 1. Generating .dot file
769 ff = fopen(filename_dot.c_str(),"w");
770 fprintf(ff,"digraph bbtk_graph{\n");
771 fprintf(ff,"rankdir=LR%s\n",";");
772 fprintf(ff,"node [shape=record]%s\n",";");
774 this->bbWriteDotFileBlackBox(ff,
775 GetThisPointer<ComplexBlackBox>(),
783 // 2. Executing .dot file -> png
784 system( command1.c_str() );
785 // 3. Executing .dot file -> cmap
786 system( command1a.c_str() );
788 // 4. HTML code insertion
790 (s) << "<center><img src=\"" << simplefilename_png
791 << "\" border=\"0\" usemap=\"#map_"<< simplefilename
792 <<"\" alt=\"\"></center>\n";
795 (s) << "<map name=\"map_"<< simplefilename <<"\">\n";
798 ff2=fopen(filename_cmap.c_str(),"r");
811 //=========================================================================
814 //=========================================================================
815 /// Write Graphviz-dot description in file
816 void ComplexBlackBox::bbWriteDotFileBlackBox(FILE *ff,
817 BlackBox::Pointer parentblackbox,
818 int detail, int level,
823 std::string valueStr("");
824 Package::Pointer package = this->bbGetDescriptor()->GetPackage();
829 tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL();
831 tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocURL();
838 std::string tmp2=bbGetTypeName();
839 std::string url(tmp1 + "#" + tmp2 );
840 fprintf( ff , "subgraph cluster_%s_%p {\n",bbGetName().c_str(),this);
842 if (!( (bbGetTypeName()=="workspace") && (bbGetName()=="workspacePrototype")) )
844 fprintf( ff , " URL = \"%s\" %s",url.c_str(),";");
847 std::string boxname="["+bbGetTypeName()+"]";
848 if (GetThisPointer<ComplexBlackBox>()!=parentblackbox)
856 boxname = bbGetName();
857 boxname = boxname + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
861 fprintf( ff , " label = \"%s\"%s\n", boxname.c_str() ,";");
865 // fprintf( ff , " style=filled%s\n",";");
866 // fprintf( ff , " color=grey%s\n",";");
867 fprintf( ff , " node [style=filled]%s\n",";");
868 fprintf( ff , " fillcolor=grey%s\n",";");
869 fprintf( ff , " edge [color=blue]%s\n",";");
873 std::string labelStr1;
874 std::string labelStr2;
875 labelStr1 = boxname + "\\n(output)" ;
876 labelStr2 = " | {{ ";
879 OutputConnectorMapType::iterator i;
881 const BlackBoxDescriptor::OutputDescriptorMapType& omap = this->bbGetDescriptor()->GetOutputDescriptorMap();
882 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
883 for ( o = omap.begin(); o != omap.end(); ++o )
887 labelStr2=labelStr2+" | ";
890 if (instanceOrtype==true)
892 valueStr = this->bbGetOutputAsString(o->second->GetName()/*
895 labelStr2=labelStr2+"<"+o->second->GetName().c_str()+"> " + valueStr + o->second->GetName().c_str();
898 labelStr2 = labelStr2+ " } }";
904 labelStr1 = labelStr1 + labelStr2;
909 bbWriteDotInputOutputName(ff,false,detail,level);
910 fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" );
915 labelStr1 = boxname + "\\n(input)" ;
916 labelStr2 = " | {{ ";
918 InputConnectorMapType::iterator ii;
920 const BlackBoxDescriptor::InputDescriptorMapType& imap = this->bbGetDescriptor()->GetInputDescriptorMap();
921 BlackBoxDescriptor::InputDescriptorMapType::const_iterator iii;
922 for ( iii = imap.begin(); iii != imap.end(); ++iii )
926 labelStr2=labelStr2+" | ";
929 if (instanceOrtype==true)
931 valueStr = this->bbGetInputAsString(iii->second->GetName()/*,factory*/) + " = ";
933 labelStr2=labelStr2+"<"+iii->second->GetName().c_str()+"> " + valueStr + iii->second->GetName().c_str();
937 labelStr2 = labelStr2+ " } }";
940 labelStr1 = labelStr1 + labelStr2;
944 bbWriteDotInputOutputName(ff,true,detail,level);
945 fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" );
950 BlackBoxMapType::iterator j;
951 for ( j = mBlackBoxMap.begin(); j != mBlackBoxMap.end(); ++j )
955 j->second->bbWriteDotFileBlackBox(ff,
964 fprintf( ff , "}\n\n");
966 fprintf( ff , " edge[color=blue]%s\n",";");
970 // Relation Input with the inside BlackBox of the this ComplexBlackbox
971 ComplexBlackBoxDescriptor::InputDescriptorMapType::iterator xx;
972 ComplexBlackBoxDescriptor::InputDescriptorMapType idmt=bbGetDescriptor()->GetInputDescriptorMap();
973 for ( xx = idmt.begin(); xx != idmt.end(); ++xx )
975 ComplexBlackBoxInputDescriptor *cbbid = (ComplexBlackBoxInputDescriptor*)xx->second;
978 bbWriteDotInputOutputName(ff,true,detail,level);
981 fprintf(ff,":%s",cbbid->GetName().c_str() );
984 BlackBox::Pointer bb = bbGetBlackBox( cbbid->GetTarget() );
985 bb->bbWriteDotInputOutputName(ff,true,detail,level);
988 fprintf(ff,":%s \n", cbbid->GetInput().c_str() );
998 // Relation Output ComplexBlackBox
999 ComplexBlackBoxDescriptor::OutputDescriptorMapType::iterator yy;
1000 ComplexBlackBoxDescriptor::OutputDescriptorMapType odmt=bbGetDescriptor()->GetOutputDescriptorMap();
1001 for ( yy = odmt.begin(); yy != odmt.end(); ++yy )
1003 ComplexBlackBoxOutputDescriptor *cbbod = (ComplexBlackBoxOutputDescriptor*)yy->second;
1005 BlackBox::Pointer bb = bbGetBlackBox( cbbod->GetTarget() );
1006 bb->bbWriteDotInputOutputName(ff,false,detail,level);
1009 fprintf(ff,":%s", cbbod->GetOutput().c_str() );
1012 bbWriteDotInputOutputName(ff,false,detail,level);
1015 fprintf(ff,":%s",cbbod->GetName().c_str() );
1024 // Relation from the out side of this ComplexBlackBox with its Inputs
1025 if (GetThisPointer<ComplexBlackBox>()!=parentblackbox) {
1026 for ( ii = bbGetInputConnectorMap().begin();
1027 ii != bbGetInputConnectorMap().end(); ++ii )
1031 Connection* con = ii->second->GetConnection();
1033 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
1034 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
1036 a->bbWriteDotInputOutputName(ff,false,detail,level);
1039 fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
1042 b->bbWriteDotInputOutputName(ff,true,detail,level);
1045 fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
1047 fprintf(ff,"%s\n",";");
1051 } // if parentblackbox
1053 //=========================================================================
1060 //=======================================================================
1061 /// Generates the list of the packages of which its depends
1062 /// (cause an internal box belongs to it)
1063 void ComplexBlackBox::GetPackagesDependencies(std::vector<Package*>& deps)
1066 BlackBoxMapType::iterator i;
1067 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1069 deps.push_back(i->second->bbGetDescriptor()->GetPackage());
1073 //=======================================================================
1076 //=======================================================================
1077 void ComplexBlackBox::Check(bool recursive)
1079 bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
1080 <<" ["<<bbGetFullName()<<"]"<<std::endl);
1082 BlackBoxMapType::const_iterator i;
1083 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1085 i->second->Check(recursive);
1087 ConnectionListType::const_iterator j;
1088 for ( j = mConnectionList.begin();
1089 j != mConnectionList.end(); ++j )
1093 bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
1094 <<" ["<<bbGetFullName()<<"] ... OK"<<std::endl);
1097 //=======================================================================
1099 //=========================================================================
1100 /// Returns the name with the name of the parent prepended if any
1101 std::string ComplexBlackBox::bbGetNameWithParent() const
1103 if (!IsAPrototype()) return BlackBox::bbGetNameWithParent();
1104 if (bbGetDescriptor())
1106 return bbGetDescriptor()->GetFullTypeName() + ":" + bbGetName();
1110 return std::string(":") + bbGetName();
1113 //=========================================================================
1115 //==========================================================================
1116 std::string ComplexBlackBox::GetObjectName() const
1118 return std::string("ComplexBlackBox '")+bbGetNameWithParent()
1121 //==========================================================================
1123 //==========================================================================
1124 std::string ComplexBlackBox::GetObjectInfo() const
1126 std::stringstream i;
1127 i << " - "<<mBlackBoxMap.size() << " boxes / "
1128 <<mConnectionList.size() << " connections" << std::endl;
1131 //==========================================================================
1133 //==========================================================================
1134 size_t ComplexBlackBox::GetObjectSize() const
1136 size_t s = Superclass::GetObjectSize();
1137 s += ComplexBlackBox::GetObjectInternalSize();
1140 //==========================================================================
1141 //==========================================================================
1142 size_t ComplexBlackBox::GetObjectInternalSize() const
1144 size_t s = sizeof(ComplexBlackBox);
1147 //==========================================================================
1148 //==========================================================================
1149 size_t ComplexBlackBox::GetObjectRecursiveSize() const
1151 size_t s = Superclass::GetObjectRecursiveSize();
1152 s += ComplexBlackBox::GetObjectInternalSize();
1153 BlackBoxMapType::const_iterator i;
1154 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1156 s += i->second->GetObjectRecursiveSize();
1158 ConnectionListType::const_iterator j;
1159 for ( j = mConnectionList.begin();
1160 j != mConnectionList.end(); ++j )
1162 s += (*j)->GetObjectRecursiveSize();
1166 //==========================================================================