1 /*=========================================================================
4 Module: $RCSfile: bbtkComplexBlackBox.cxx,v $
6 Date: $Date: 2008/04/22 14:30:25 $
7 Version: $Revision: 1.15 $
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",1,
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",2," -> 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);
265 bbtkDebugDecTab("Process",1);
268 //==================================================================
270 //==================================================================
271 void ComplexBlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
273 bbtkDebugMessage("Process",3,
274 "ComplexBlackBox::bbSetModifiedStatus("
275 <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
277 c->GetBlackBox()->bbSetModifiedStatus(c);
279 bbtkDebugMessage("Process",3,
280 "EO ComplexBlackBox::bbSetModifiedStatus("
281 <<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",1,
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",2,"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;
357 bbtkDebugDecTab("Process",2);
361 bbtkError("Connection '"<<caller->GetFullName()<<"' does not point to a valid output of the complex box !");
363 bbtkDebugDecTab("Process",1);
368 //==================================================================
371 //==================================================================
372 Data ComplexBlackBox::bbGetOutput( const std::string &name )
374 bbtkDebugMessageInc("Data",7,
375 "ComplexBlackBox::bbGetOutput(\""<<name<<"\") ["
376 <<bbGetFullName()<<"]"<<std::endl);
378 ComplexBlackBoxOutputDescriptor* d =
379 (ComplexBlackBoxOutputDescriptor*)
380 bbGetDescriptor()->GetOutputDescriptor(name);
382 Data p = bbGetBlackBox(d->GetTarget())->bbGetOutput(d->GetOutput());
385 bbtkDebugDecTab("Data",7);
388 //==================================================================
390 //==================================================================
391 /// Gets the input Data of a given name
392 Data ComplexBlackBox::bbGetInput( const std::string &name )
394 bbtkDebugMessageInc("Data",7,
395 "ComplexBlackBox::bbGetInput(\""<<name<<"\") ["
396 <<bbGetFullName()<<"]"<<std::endl);
398 ComplexBlackBoxInputDescriptor* d =
399 (ComplexBlackBoxInputDescriptor*)
400 bbGetDescriptor()->GetInputDescriptor(name);
402 Data p = bbGetBlackBox(d->GetTarget())->bbGetInput(d->GetInput());
404 bbtkDebugDecTab("Data",7);
407 //==================================================================
409 //==================================================================
410 /// Sets the data of the output called <name>
411 void ComplexBlackBox::bbSetOutput( const std::string &name, Data data)
413 bbtkDebugMessageInc("Data",7,
414 "ComplexBlackBox::bbSetOutput(\""<<name<<"\",data) ["
415 <<bbGetFullName()<<"]"<<std::endl);
417 ComplexBlackBoxOutputDescriptor* d =
418 (ComplexBlackBoxOutputDescriptor*)
419 bbGetDescriptor()->GetOutputDescriptor(name);
421 bbGetBlackBox(d->GetTarget())->bbSetOutput(d->GetOutput(),data);
423 bbtkDebugDecTab("Data",7);
425 //==================================================================
427 //==================================================================
428 /// Sets the data of the input called <name>
429 void ComplexBlackBox::bbSetInput( const std::string &name, Data data,
432 bbtkDebugMessageInc("Data",7,
433 "ComplexBlackBox::bbSetInput(\""<<name<<"\",data) ["
434 <<bbGetFullName()<<"]"<<std::endl);
436 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
437 bbGetDescriptor()->GetInputDescriptor(name);
439 bbGetBlackBox(d->GetTarget())->bbSetInput(d->GetInput(),data,setModified);
441 bbtkDebugDecTab("Data",7);
443 //==================================================================
446 //==================================================================
447 /// Sets the data of the input called <name>
448 void ComplexBlackBox::bbBruteForceSetInputPointer( const std::string &name,
452 bbtkDebugMessageInc("Data",7,
453 "ComplexBlackBox::bbBruteForceSetInputPointer('"
454 <<name<<"',"<<data<<") ["
455 <<bbGetFullName()<<"]"<<std::endl);
457 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
458 bbGetDescriptor()->GetInputDescriptor(name);
460 bbGetBlackBox(d->GetTarget())->bbBruteForceSetInputPointer(d->GetInput(),
464 bbtkDebugDecTab("Data",7);
466 //==================================================================
468 //=========================================================================
469 /// Connects the input <name> to the connection c
470 void ComplexBlackBox::bbConnectInput( const std::string& name, Connection* c)
472 bbtkDebugMessage("connection",2,
473 "==> ComplexBlackBox::bbConnectInput(\""
474 <<name<<"\","<<c->GetFullName()<<") ["
475 <<bbGetFullName()<<"]"
478 ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
479 bbGetDescriptor()->GetInputDescriptor(name);
482 BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
484 bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
487 c->SetBlackBoxToInput(d->GetInput());
489 bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
490 t->bbConnectInput(d->GetInput(),c);
492 bbtkDebugMessage("connection",2,
493 "<== ComplexBlackBox::bbConnectInput(\""
494 <<name<<"\","<<c->GetFullName()<<") ["
495 <<bbGetFullName()<<"]"
498 //=========================================================================
501 //=========================================================================
502 /// Connects the output <name> to the connection c
503 void ComplexBlackBox::bbConnectOutput( const std::string& name, Connection* c)
505 bbtkDebugMessage("connection",2,
506 "==> ComplexBlackBox::bbConnectOutput(\""
507 <<name<<"\","<<c->GetFullName()<<") ["
508 <<bbGetFullName()<<"]"<<std::endl);
510 ComplexBlackBoxOutputDescriptor* d = (ComplexBlackBoxOutputDescriptor*)
511 bbGetDescriptor()->GetOutputDescriptor(name);
513 BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
515 bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
517 c->SetBlackBoxFrom(t);
518 c->SetBlackBoxFromOutput(d->GetOutput());
520 bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
522 t->bbConnectOutput(d->GetOutput(),c);
524 bbtkDebugMessage("connection",2,
525 "<== ComplexBlackBox::bbConnectOutput(\""
526 <<name<<"\","<<c->GetFullName()<<") ["
527 <<bbGetFullName()<<"]"<<std::endl);
529 //=========================================================================
532 //==================================================================
533 /// Adds the black box to the complex box
534 void ComplexBlackBox::bbAddBlackBox( BlackBox::Pointer b)
536 bbtkDebugMessageInc("Kernel",7,
537 "ComplexBlackBox::AddBlackBox(\""<<b->bbGetName()
539 <<bbGetFullName()<<"]"<<std::endl);
541 if ( bbUnsafeGetBlackBox(b->bbGetName()) )
543 bbtkError("a black box called \""<<b->bbGetName()
544 <<"\" already exists");
546 b->bbSetParent(GetThisPointer<ComplexBlackBox>());
547 mBlackBoxMap[b->bbGetName()] = b;
549 bbtkDebugDecTab("Kernel",7);
551 //==================================================================
553 //==================================================================
554 /// Adds the black box to the complex box (unsafe)
555 void ComplexBlackBox::bbUnsafeAddBlackBox( BlackBox::Pointer b)
557 bbtkDebugMessageInc("Kernel",7,
558 "ComplexBlackBox::UnsafeAddBlackBox(\""<<b->bbGetName()
560 <<bbGetFullName()<<"]"<<std::endl);
562 b->bbSetParent(GetThisPointer<ComplexBlackBox>());
563 mBlackBoxMap[b->bbGetName()] = b;
565 bbtkDebugDecTab("Kernel",7);
567 //==================================================================
569 //==================================================================
570 /// Removes the black box from the complex box
571 void ComplexBlackBox::bbRemoveBlackBox( const std::string& name,
572 bool remove_connections )
574 bbtkDebugMessageInc("Kernel",7,
575 "ComplexBlackBox::RemoveBlackBox(\""<<name<<"\") ["
576 <<bbGetFullName()<<"]"<<std::endl);
578 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
579 if ( i == mBlackBoxMap.end() )
581 bbtkError("the black box \""<<name<<"\" does not exist");
583 BlackBox::WeakPointer p = i->second;
585 if (remove_connections)
587 ConnectionListType::const_iterator j;
588 for ( j = mConnectionList.begin();
589 j != mConnectionList.end(); ++j )
594 if (p.use_count()!=1)
596 bbtkError("the black box \""<<name<<"\" is still connected");
599 mBlackBoxMap.erase(i);
602 // Unload orphan dl packages
603 Package::UnLoadReleasedDynamicallyLoadedPackages();
605 bbtkDebugDecTab("Kernel",7);
607 //==================================================================
609 //==================================================================
610 /// Adds the connection to the complex box
611 void ComplexBlackBox::bbAddConnection( Connection::Pointer c)
613 bbtkDebugMessageInc("Kernel",7,
614 "ComplexBlackBox::AddConnection(\""<<"..."<<"\") ["
615 <<bbGetFullName()<<"]"<<std::endl);
617 mConnectionList.push_back(c);
619 bbtkDebugDecTab("Kernel",7);
621 //==================================================================
622 // void RemoveConnection( );
624 //==================================================================
625 /// Returns the black box with name <name>
626 BlackBox::Pointer ComplexBlackBox::bbGetBlackBox( const std::string& name )
628 bbtkDebugMessageInc("Kernel",9,
629 "ComplexBlackBox::GetBlackBox(\""<<name<<"\") ["
630 <<bbGetFullName()<<"]"<<std::endl);
632 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
633 if ( i == mBlackBoxMap.end() )
635 bbtkError("the black box \""<<name<<"\" does not exist");
638 bbtkDebugDecTab("Kernel",9);
641 //==================================================================
643 //==================================================================
644 /// Returns the black box with name <name> : does not throw an exception
645 /// if it does not exist but return a null pointer
646 BlackBox::Pointer ComplexBlackBox::bbUnsafeGetBlackBox( const std::string& name )
648 bbtkDebugMessageInc("Kernel",9,
649 "ComplexBlackBox::UnsafeGetBlackBox(\""<<name<<"\") ["
650 <<bbGetFullName()<<"]"
653 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
654 if ( i == mBlackBoxMap.end() )
656 bbtkDebugDecTab("Kernel",9);
657 return BlackBox::Pointer();
660 bbtkDebugDecTab("Kernel",9);
664 //==================================================================
666 //==================================================================
667 void ComplexBlackBox::bbPrintBlackBoxes()
669 bbtkDebugMessageInc("Kernel",9,
670 "ComplexBlackBox::PrintBlackBoxes() ["
671 <<bbGetFullName()<<"]"
674 BlackBoxMapType::iterator i;
675 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
677 bbtkMessage("Help",1,i->second->bbGetFullName()<<std::endl);
680 bbtkDebugDecTab("Kernel",9);
682 //==================================================================
686 //=========================================================================
688 void ComplexBlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
692 fprintf(ff,"%s_IN_%p",bbGetTypeName().c_str(),this);
694 fprintf(ff,"%s_OUT_%p",bbGetTypeName().c_str(),this);
697 //=========================================================================
700 //=========================================================================
701 BlackBox::Pointer ComplexBlackBox::bbFindBlackBox(const std::string &blackboxname)
703 BlackBox::Pointer blackbox;
704 std::string subname="";
705 std::string restname="";
706 std::string delimiters(">");
707 // Skip delimiters at beginning.
708 std::string::size_type lastPos = blackboxname.find_first_not_of(delimiters, 0);
709 // Find first "non-delimiter".
710 std::string::size_type pos = blackboxname.find_first_of(delimiters, lastPos);
712 // Found a token, add it to the vector.
713 subname = blackboxname.substr(lastPos, pos - lastPos);
714 restname = blackboxname.substr(lastPos+pos - lastPos+1, 999);
716 if (restname==subname)
721 BlackBoxMapType::iterator i = mBlackBoxMap.find(subname);
722 if ( i != mBlackBoxMap.end() )
724 blackbox = i->second;
727 blackbox = blackbox->bbFindBlackBox(restname);
732 //=========================================================================
734 //=========================================================================
735 void ComplexBlackBox::bbInsertHTMLGraph( std::ofstream& s,
739 const std::string& output_dir,
743 std::string directory(output_dir);
745 if (output_dir.length() == 0)
747 // Don't pollute the file store with "temp_dir" directories ...
748 std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
749 directory = default_temp_dir + "/" + "temp_dir";
752 std::string simplefilename (this->bbGetTypeName()+"_"+this->bbGetName());
753 std::string simplefilename_png (simplefilename+".png");
754 std::string filename (directory+"/"+simplefilename);
755 std::string filename_png (filename+".png");
756 std::string filename_cmap (filename+".cmap");
757 std::string filename_dot (filename+".dot");
759 std::string filename_png2 ("\"" + filename_png + "\"");
760 std::string filename_cmap2 ("\"" + filename_cmap + "\"");
761 std::string filename_dot2 ("\"" + filename_dot + "\"");
764 std::string command1 ("dot -T png -o "
765 + filename_png2 + " " + filename_dot2);
766 std::string command1a("dot -T cmap -o "
767 + filename_cmap2 + " " + filename_dot2);
769 // 1. Generating .dot file
771 ff = fopen(filename_dot.c_str(),"w");
772 fprintf(ff,"digraph bbtk_graph{\n");
773 fprintf(ff,"rankdir=LR%s\n",";");
774 fprintf(ff,"node [shape=record]%s\n",";");
776 this->bbWriteDotFileBlackBox(ff,
777 GetThisPointer<ComplexBlackBox>(),
785 // 2. Executing .dot file -> png
786 system( command1.c_str() );
787 // 3. Executing .dot file -> cmap
788 system( command1a.c_str() );
790 // 4. HTML code insertion
792 (s) << "<center><img src=\"" << simplefilename_png
793 << "\" border=\"0\" usemap=\"#map_"<< simplefilename
794 <<"\" alt=\"\"></center>\n";
797 (s) << "<map name=\"map_"<< simplefilename <<"\">\n";
800 ff2=fopen(filename_cmap.c_str(),"r");
813 //=========================================================================
816 //=========================================================================
817 /// Write Graphviz-dot description in file
818 void ComplexBlackBox::bbWriteDotFileBlackBox(FILE *ff,
819 BlackBox::Pointer parentblackbox,
820 int detail, int level,
825 std::string valueStr("");
826 Package::Pointer package = this->bbGetDescriptor()->GetPackage();
831 tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL();
833 tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocURL();
840 std::string tmp2=bbGetTypeName();
841 std::string url(tmp1 + "#" + tmp2 );
842 fprintf( ff , "subgraph cluster_%s_%p {\n",bbGetName().c_str(),this);
844 if (!( (bbGetTypeName()=="workspace") && (bbGetName()=="workspacePrototype")) )
846 fprintf( ff , " URL = \"%s\" %s",url.c_str(),";");
849 std::string boxname="["+bbGetTypeName()+"]";
850 if (GetThisPointer<ComplexBlackBox>()!=parentblackbox)
858 boxname = bbGetName();
859 boxname = boxname + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
863 fprintf( ff , " label = \"%s\"%s\n", boxname.c_str() ,";");
867 // fprintf( ff , " style=filled%s\n",";");
868 // fprintf( ff , " color=grey%s\n",";");
869 fprintf( ff , " node [style=filled]%s\n",";");
870 fprintf( ff , " fillcolor=grey%s\n",";");
871 fprintf( ff , " edge [color=blue]%s\n",";");
875 std::string labelStr1;
876 std::string labelStr2;
877 labelStr1 = boxname + "\\n(output)" ;
878 labelStr2 = " | {{ ";
881 OutputConnectorMapType::iterator i;
883 const BlackBoxDescriptor::OutputDescriptorMapType& omap = this->bbGetDescriptor()->GetOutputDescriptorMap();
884 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
885 for ( o = omap.begin(); o != omap.end(); ++o )
889 labelStr2=labelStr2+" | ";
892 if (instanceOrtype==true)
894 valueStr = this->bbGetOutputAsString(o->second->GetName()/*
897 labelStr2=labelStr2+"<"+o->second->GetName().c_str()+"> " + valueStr + o->second->GetName().c_str();
900 labelStr2 = labelStr2+ " } }";
906 labelStr1 = labelStr1 + labelStr2;
911 bbWriteDotInputOutputName(ff,false,detail,level);
912 fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" );
917 labelStr1 = boxname + "\\n(input)" ;
918 labelStr2 = " | {{ ";
920 InputConnectorMapType::iterator ii;
922 const BlackBoxDescriptor::InputDescriptorMapType& imap = this->bbGetDescriptor()->GetInputDescriptorMap();
923 BlackBoxDescriptor::InputDescriptorMapType::const_iterator iii;
924 for ( iii = imap.begin(); iii != imap.end(); ++iii )
928 labelStr2=labelStr2+" | ";
931 if (instanceOrtype==true)
933 valueStr = this->bbGetInputAsString(iii->second->GetName()/*,factory*/) + " = ";
935 labelStr2=labelStr2+"<"+iii->second->GetName().c_str()+"> " + valueStr + iii->second->GetName().c_str();
939 labelStr2 = labelStr2+ " } }";
942 labelStr1 = labelStr1 + labelStr2;
946 bbWriteDotInputOutputName(ff,true,detail,level);
947 fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" );
952 BlackBoxMapType::iterator j;
953 for ( j = mBlackBoxMap.begin(); j != mBlackBoxMap.end(); ++j )
957 j->second->bbWriteDotFileBlackBox(ff,
966 fprintf( ff , "}\n\n");
968 fprintf( ff , " edge[color=blue]%s\n",";");
972 // Relation Input with the inside BlackBox of the this ComplexBlackbox
973 ComplexBlackBoxDescriptor::InputDescriptorMapType::iterator xx;
974 ComplexBlackBoxDescriptor::InputDescriptorMapType idmt=bbGetDescriptor()->GetInputDescriptorMap();
975 for ( xx = idmt.begin(); xx != idmt.end(); ++xx )
977 ComplexBlackBoxInputDescriptor *cbbid = (ComplexBlackBoxInputDescriptor*)xx->second;
980 bbWriteDotInputOutputName(ff,true,detail,level);
983 fprintf(ff,":%s",cbbid->GetName().c_str() );
986 BlackBox::Pointer bb = bbGetBlackBox( cbbid->GetTarget() );
987 bb->bbWriteDotInputOutputName(ff,true,detail,level);
990 fprintf(ff,":%s \n", cbbid->GetInput().c_str() );
1000 // Relation Output ComplexBlackBox
1001 ComplexBlackBoxDescriptor::OutputDescriptorMapType::iterator yy;
1002 ComplexBlackBoxDescriptor::OutputDescriptorMapType odmt=bbGetDescriptor()->GetOutputDescriptorMap();
1003 for ( yy = odmt.begin(); yy != odmt.end(); ++yy )
1005 ComplexBlackBoxOutputDescriptor *cbbod = (ComplexBlackBoxOutputDescriptor*)yy->second;
1007 BlackBox::Pointer bb = bbGetBlackBox( cbbod->GetTarget() );
1008 bb->bbWriteDotInputOutputName(ff,false,detail,level);
1011 fprintf(ff,":%s", cbbod->GetOutput().c_str() );
1014 bbWriteDotInputOutputName(ff,false,detail,level);
1017 fprintf(ff,":%s",cbbod->GetName().c_str() );
1026 // Relation from the out side of this ComplexBlackBox with its Inputs
1027 if (GetThisPointer<ComplexBlackBox>()!=parentblackbox) {
1028 for ( ii = bbGetInputConnectorMap().begin();
1029 ii != bbGetInputConnectorMap().end(); ++ii )
1033 Connection* con = ii->second->GetConnection();
1035 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
1036 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
1038 a->bbWriteDotInputOutputName(ff,false,detail,level);
1041 fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
1044 b->bbWriteDotInputOutputName(ff,true,detail,level);
1047 fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
1049 fprintf(ff,"%s\n",";");
1053 } // if parentblackbox
1055 //=========================================================================
1062 //=======================================================================
1063 /// Generates the list of the packages of which its depends
1064 /// (cause an internal box belongs to it)
1065 void ComplexBlackBox::GetPackagesDependencies(std::vector<Package*>& deps)
1068 BlackBoxMapType::iterator i;
1069 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1071 deps.push_back(i->second->bbGetDescriptor()->GetPackage());
1075 //=======================================================================
1078 //=======================================================================
1079 void ComplexBlackBox::Check(bool recursive)
1081 bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
1082 <<" ["<<bbGetFullName()<<"]"<<std::endl);
1084 BlackBoxMapType::const_iterator i;
1085 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1087 i->second->Check(recursive);
1089 ConnectionListType::const_iterator j;
1090 for ( j = mConnectionList.begin();
1091 j != mConnectionList.end(); ++j )
1095 bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
1096 <<" ["<<bbGetFullName()<<"] ... OK"<<std::endl);
1099 //=======================================================================
1101 //=========================================================================
1102 /// Returns the name with the name of the parent prepended if any
1103 std::string ComplexBlackBox::bbGetNameWithParent() const
1105 if (!IsAPrototype()) return BlackBox::bbGetNameWithParent();
1106 if (bbGetDescriptor())
1108 return bbGetDescriptor()->GetFullTypeName() + ":" + bbGetName();
1112 return std::string(":") + bbGetName();
1115 //=========================================================================
1117 //==========================================================================
1118 std::string ComplexBlackBox::GetObjectName() const
1120 return std::string("ComplexBlackBox '")+bbGetNameWithParent()
1123 //==========================================================================
1125 //==========================================================================
1126 std::string ComplexBlackBox::GetObjectInfo() const
1128 std::stringstream i;
1129 i << " - "<<mBlackBoxMap.size() << " boxes / "
1130 <<mConnectionList.size() << " connections" << std::endl;
1133 //==========================================================================
1135 //==========================================================================
1136 size_t ComplexBlackBox::GetObjectSize() const
1138 size_t s = Superclass::GetObjectSize();
1139 s += ComplexBlackBox::GetObjectInternalSize();
1142 //==========================================================================
1143 //==========================================================================
1144 size_t ComplexBlackBox::GetObjectInternalSize() const
1146 size_t s = sizeof(ComplexBlackBox);
1149 //==========================================================================
1150 //==========================================================================
1151 size_t ComplexBlackBox::GetObjectRecursiveSize() const
1153 size_t s = Superclass::GetObjectRecursiveSize();
1154 s += ComplexBlackBox::GetObjectInternalSize();
1155 BlackBoxMapType::const_iterator i;
1156 for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i )
1158 s += i->second->GetObjectRecursiveSize();
1160 ConnectionListType::const_iterator j;
1161 for ( j = mConnectionList.begin();
1162 j != mConnectionList.end(); ++j )
1164 s += (*j)->GetObjectRecursiveSize();
1168 //==========================================================================