1 /*=========================================================================
4 Module: $RCSfile: bbtkBlackBox.cxx,v $
6 Date: $Date: 2008/04/22 08:29:09 $
7 Version: $Revision: 1.13 $
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::BlackBox : abstract black-box interface.
24 #include "bbtkBlackBox.h"
25 #include "bbtkPackage.h"
26 #include "bbtkMessageManager.h"
27 #include "bbtkFactory.h"
29 #include "bbtkConfigurationFile.h"
30 #include "bbtkWxBlackBox.h"
40 static bool bbmgSomeBoxExecuting = false;
41 static bool bbmgFreezeExecution = false;
42 static std::set<BlackBox::Pointer> bbmgExecutionList;
44 //=========================================================================
45 BlackBox::Deleter::Deleter()
48 //=========================================================================
50 //=========================================================================
51 void BlackBox::Deleter::Delete(Object* p)
53 BlackBox* b = dynamic_cast<BlackBox*>(p);
56 bbtkInternalError("BlackBox::Deleter::Delete("<<p->GetObjectName()
58 <<"dynamic cast to BlackBox* failed !");
60 std::string name = p->GetObjectName();//b->bbGetNameWithParent();
61 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
64 BlackBoxDescriptor::WeakPointer desc = b->bbGetDescriptor();
65 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : deleting black box"<<std::endl);
69 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : releasing descriptor ["<<desc.lock()<<"]"<<std::endl);
73 Package::WeakPointer pack = desc.lock()->GetPackage();
76 Package::ReleaseBlackBoxDescriptor(pack,desc);
80 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : descriptor package expired (was not held by a package and the box was the last instance)"<<std::endl);
85 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : descriptor expired : nothing to do (was not held by a package or the box is a complex black box prototype)"<<std::endl);
87 bbtkDebugMessage("object",2,"<## BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
89 //=========================================================================
91 //=========================================================================
92 BlackBox::BlackBox(const std::string &name)
95 bbmBoxProcessMode("Pipeline"),
99 bbtkDebugMessage("object",4,"==> BlackBox::BlackBox(\""
100 <<name<<"\")"<<std::endl);
101 bbtkDebugMessage("object",4,"<== BlackBox::BlackBox(\""
102 <<name<<"\")"<<std::endl);
104 //=========================================================================
107 //=========================================================================
108 BlackBox::BlackBox(BlackBox& from, const std::string &name)
110 bbmStatus(from.bbmStatus),
111 bbmBoxProcessMode(from.bbmBoxProcessMode),
115 bbtkDebugMessage("object",4,"==> BlackBox::BlackBox("
116 <<from.bbGetFullName()<<",\""
117 <<name<<"\")"<<std::endl);
118 bbtkDebugMessage("object",4,"<== BlackBox::BlackBox("
119 <<from.bbGetFullName()<<",\""
120 <<name<<"\")"<<std::endl);
122 //=========================================================================
125 //=========================================================================
126 BlackBox::~BlackBox()
128 bbtkDebugMessage("object",4,"==> BlackBox::~BlackBox() ["<<bbmName
130 this->bbDesallocateConnectors();
131 bbtkDebugMessage("object",4,"<== BlackBox::~BlackBox() ["<<bbmName
134 //=========================================================================
137 //=========================================================================
138 /// Main processing method of the box.
139 void BlackBox::bbExecute(bool force)
141 bbtkDebugMessageInc("Process",1,
142 "=> BlackBox::bbExecute() ["
143 <<bbGetFullName()<<"]"<<std::endl);
145 Wx::BeginBusyCursor();
147 // If execution frozen : return
148 if (bbGlobalGetFreezeExecution())
150 bbtkDebugMessage("Process",1,
151 " -> FreezeExecution global flag is 'true' : abort execution"<<std::endl);
154 // If force is true then update is triggered even if the box is UPTODATE
155 if (force) bbSetModifiedStatus();
157 // Calls the main recursive update method
158 bbBackwardUpdate(Connection::Pointer());
162 bbtkDebugMessageDec("Process",1,
163 "<= BlackBox::bbExecute() ["
164 <<bbGetFullName()<<"]"<<std::endl);
166 //=========================================================================
168 //=========================================================================
169 std::string BlackBox::bbGetFullName() const
171 return this->bbGetNameWithParent()+"<"+this->bbGetDescriptor()->GetTypeName()+">";
173 //=========================================================================
177 //=========================================================================
178 /// Returns the name with the name of the parent prepended if any
179 std::string BlackBox::bbGetNameWithParent() const
181 if (bbmParent.lock())
183 return bbmParent.lock()->bbGetNameWithParent() + ":" + bbmName;
190 //=========================================================================
192 //=========================================================================
193 /// Prints the Help on the BlackBox type
194 void BlackBox::bbGetHelp(bool full) const
196 bbGetDescriptor()->GetHelp(full);
198 //=========================================================================
201 //=========================================================================
202 /// Returns true if the UserBlackBox has an input of name name
203 bool BlackBox::bbHasInput(const std::string& name) const
205 bbtkDebugMessageInc("Kernel",8,
206 "BlackBox::bbHasInput(\""
207 <<name<<"\") ["<<bbGetFullName()<<"]"
209 bool r = ( bbGetDescriptor()->GetInputDescriptorMap().find(name)
210 != bbGetDescriptor()->GetInputDescriptorMap().end());
211 bbtkDebugDecTab("Kernel",8);
214 //=========================================================================
217 //=========================================================================
218 /// Returns true if the UserBlackBox has an output of name name
219 bool BlackBox::bbHasOutput(const std::string& name) const
221 bbtkDebugMessageInc("Kernel",8,"BlackBox::bbHasOutput(\""
222 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
223 bool r = ( bbGetDescriptor()->GetOutputDescriptorMap().find(name)
224 != bbGetDescriptor()->GetOutputDescriptorMap().end());
225 bbtkDebugDecTab("Kernel",8);
228 //=========================================================================
231 //=========================================================================
232 /// Gets the output type of a given name
233 TypeInfo BlackBox::bbGetOutputType( const std::string &name ) const
235 bbtkDebugMessageInc("Kernel",8,
236 "BlackBox::bbGetOutputType(\""
237 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
238 TypeInfo r = bbGetDescriptor()->GetOutputDescriptor(name)->GetTypeInfo();
239 bbtkDebugDecTab("Kernel",8);
242 //=========================================================================
244 //=========================================================================
245 /// Gets the input type of a given name
246 TypeInfo BlackBox::bbGetInputType( const std::string &name ) const
248 bbtkDebugMessageInc("Kernel",8,
249 "BlackBox::bbGetInputType(\""
250 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
251 TypeInfo r = bbGetDescriptor()->GetInputDescriptor(name)->GetTypeInfo();
252 bbtkDebugDecTab("Kernel",8);
255 //=========================================================================
258 //=========================================================================
259 /// Allocates the i/o connectors of the black box
260 void BlackBox::bbAllocateConnectors()
262 bbtkDebugMessageInc("Kernel",8,
263 "BlackBox::bbAllocateConnectors() ["
264 <<bbGetFullName()<<"]"
266 const BlackBoxDescriptor::InputDescriptorMapType& imap
267 = bbGetDescriptor()->GetInputDescriptorMap();
268 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
269 for ( i = imap.begin(); i != imap.end(); ++i )
271 bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
272 bbGetInputConnectorMap()[i->second->GetName()]
273 = new BlackBoxInputConnector(GetThisPointer<BlackBox>());
275 const BlackBoxDescriptor::OutputDescriptorMapType& omap
276 = bbGetDescriptor()->GetOutputDescriptorMap();
277 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
278 for ( o = omap.begin(); o != omap.end(); ++o )
280 bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
281 bbGetOutputConnectorMap()[o->second->GetName()]
282 = new BlackBoxOutputConnector();
284 bbtkDebugDecTab("Kernel",8);
286 //=========================================================================
289 //=========================================================================
290 /// Desallocates the i/o connectors of the black box
291 void BlackBox::bbDesallocateConnectors()
293 bbtkDebugMessageInc("Kernel",8,
294 "BlackBox::bbDesallocateConnectors()"
297 InputConnectorMapType::const_iterator i;
298 for ( i = bbGetInputConnectorMap().begin();
299 i != bbGetInputConnectorMap().end(); ++i )
301 bbtkDebugMessage("Kernel",8,"* Delete \""<<i->first<<"\""<<std::endl);
304 OutputConnectorMapType::const_iterator o;
305 for ( o = bbGetOutputConnectorMap().begin();
306 o != bbGetOutputConnectorMap().end(); ++o )
308 bbtkDebugMessage("Kernel",8,"* Delete \""<<o->first<<"\""<<std::endl);
312 bbtkDebugDecTab("Kernel",8);
315 //=========================================================================
318 //=========================================================================
319 /// Copies the input / output values from another box
320 void BlackBox::bbCopyIOValues(BlackBox& from)
322 bbtkDebugMessageInc("Kernel",9,
323 "BlackBox::bbCopyIOValues("
324 <<from.bbGetFullName()<<") ["
325 <<bbGetFullName()<<"]"<<std::endl);
326 // copies the input values
327 const BlackBoxDescriptor::InputDescriptorMapType& imap
328 = bbGetDescriptor()->GetInputDescriptorMap();
329 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
330 for ( i = imap.begin(); i != imap.end(); ++i )
332 if (! i->second->GetCopyConstruct() ) continue;
333 std::string input = i->second->GetName();
334 this->bbSetInput(input, from.bbGetInput(input) );
336 // copies the output values
337 const BlackBoxDescriptor::OutputDescriptorMapType& omap
338 = bbGetDescriptor()->GetOutputDescriptorMap();
339 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
340 for ( o = omap.begin(); o != omap.end(); ++o )
342 if (! o->second->GetCopyConstruct() ) continue;
343 std::string output = o->second->GetName();
344 this->bbSetOutput(output, from.bbGetOutput(output) );
347 bbtkDebugDecTab("Kernel",9);
350 //=========================================================================
354 //=========================================================================
355 bool BlackBox::bbCanReact() const
357 return ( bbGlobalGetSomeBoxExecuting()
358 #ifdef _USE_WXWIDGETS_
359 || Wx::IsSomeWindowAlive()
363 //=========================================================================
367 //=========================================================================
368 /// User overloadable destruction method of a black box
369 void BlackBox::bbUserDelete()
371 bbtkDebugMessage("Process",5,
372 "=> BlackBox::bbUserDelete() ["
373 <<bbGetFullName()<<"]"
374 <<" : not overloaded; using standard deletion"
378 //=========================================================================
381 //=========================================================================
382 BlackBox::BoxProcessModeValue BlackBox::bbGetBoxProcessModeValue() const
384 const std::string& p = bbmBoxProcessMode;
386 (p == "P") || (p == "p") ||
387 (p == "Pipeline") || (p == "pipeline") ) return Pipeline;
389 (p == "A") || (p == "a") ||
390 (p == "Always") || (p == "always") ) return Always;
392 (p == "R") || (p == "r") ||
393 (p == "Reactive") || (p == "reactive") ) return Reactive;
394 bbtkError(bbGetFullName()<<" : BoxProcessMode value '"<<p
395 <<"' unknown. Possible values : "
396 <<"'0'/'P'/'p'/'Pipeline'/'pipeline' | "
397 <<"'1'/'A'/'a'/'Always'/'always' | "
398 <<"'2'/'R'/'r'/'Reactive'/'reactive'"<<std::endl);
400 //=========================================================================
402 //=========================================================================
403 bool BlackBox::bbBoxProcessModeIsReactive() const
405 return (bbGetBoxProcessModeValue() == Reactive);
407 //=========================================================================
409 //=========================================================================
410 bool BlackBox::bbBoxProcessModeIsAlways() const
412 return (bbGetBoxProcessModeValue() == Always);
414 //=========================================================================
416 //=========================================================================
417 /// Signals that the BlackBox has been modified
418 void BlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
420 bbtkDebugMessageInc("Process",5,
421 "=> BlackBox::bbSetModifiedStatus("<<c<<") ["
422 <<bbGetFullName()<<"]"<<std::endl);
424 if ( (c==bbGetInputConnectorMap().find("WinHide")->second) )
425 // && (bbCanReact()))
427 bbtkDebugMessage("Process",9,
428 "-> Hide triggered by WinHide input change"
430 this->bbHideWindow();
431 this->bbSetStatus(MODIFIED);
435 if ( ( bbBoxProcessModeIsReactive() ||
436 (c==bbGetInputConnectorMap().find("BoxExecute")->second))
439 bbtkDebugMessage("Process",9,
440 "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
441 this->bbSetStatus(MODIFIED);
442 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
444 else if ( bbGetStatus() == MODIFIED ) //! this->bbIsUptodate())
446 bbtkDebugMessage("Process",5,"-> Already modified"<<std::endl);
447 bbtkDebugDecTab("Process",5);
452 bbtkDebugMessage("Process",5,"-> Status set to modified"<<std::endl);
453 bbtkDebugDecTab("Process",5);
454 this->bbSetStatus(MODIFIED);
457 this->bbSignalOutputModification(false);
459 bbtkDebugMessageDec("Process",5,
460 "<= BlackBox::bbSetModifiedStatus("<<c<<") ["
461 <<bbGetFullName()<<"]"<<std::endl);
463 //=========================================================================
465 //=========================================================================
466 void BlackBox::bbSignalOutputModification(bool reaction)
468 bbtkDebugMessageInc("Process",5,
469 "=> BlackBox::bbSignalOutputModification() ["
470 <<bbGetFullName()<<"]"<<std::endl);
472 OutputConnectorMapType::iterator change = bbGetOutputConnectorMap().end();
473 OutputConnectorMapType::iterator i;
474 for ( i = bbGetOutputConnectorMap().begin();
475 i != bbGetOutputConnectorMap().end(); ++i) {
476 /* if ( i->first == "BoxChange" )
482 i->second->SetModifiedStatus();
484 // if (change != bbGetOutputConnectorMap().end())
485 // change->second->SetModifiedStatus();
487 if (reaction) bbGlobalProcessExecutionList();
489 bbtkDebugMessageDec("Process",5,
490 "<= BlackBox::bbSignalOutputModification() ["
491 <<bbGetFullName()<<"]"<<std::endl);
494 //=========================================================================
495 //=========================================================================
496 void BlackBox::bbSignalOutputModification(const std::string& output,
499 bbtkDebugMessageInc("Process",5,
500 "=> BlackBox::bbSignalOutputModification("
502 <<bbGetFullName()<<"]"<<std::endl);
504 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(output);
505 if ( i == bbGetOutputConnectorMap().end() )
507 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
509 i->second->SetModifiedStatus();
510 // Has to notify the output "BoxChange" also
511 if (output != "BoxChange")
513 i = bbGetOutputConnectorMap().find("BoxChange");
514 if ( i != bbGetOutputConnectorMap().end() )
516 i->second->SetModifiedStatus();
519 if (reaction) bbGlobalProcessExecutionList();
521 bbtkDebugMessageDec("Process",5,
522 "<= BlackBox::bbSignalOutputModification("
524 <<bbGetFullName()<<"]"<<std::endl);
527 //=========================================================================
528 //=========================================================================
529 void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
532 bbtkDebugMessageInc("Process",5,
533 "=> BlackBox::bbSignalOutputModification(vector of outputs) ["
534 <<bbGetFullName()<<"]"<<std::endl);
535 OutputConnectorMapType::iterator i;
536 std::vector<std::string>::const_iterator o;
537 for (o=output.begin();o!=output.end();++o)
539 // the output "BoxChange" must be signaled **AFTER** all others
540 if (*o == "BoxChange") continue;
541 // Look for the connector
542 i = bbGetOutputConnectorMap().find(*o);
543 if ( i == bbGetOutputConnectorMap().end() )
545 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
547 i->second->SetModifiedStatus();
549 // Has to notify the output "BoxChange" also
550 i = bbGetOutputConnectorMap().find("BoxChange");
551 if ( i != bbGetOutputConnectorMap().end() )
553 i->second->SetModifiedStatus();
555 if (reaction) bbGlobalProcessExecutionList();
557 bbtkDebugMessageDec("Process",5,
558 "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
559 <<bbGetFullName()<<"]"<<std::endl);
562 //=========================================================================
564 //=========================================================================
565 /// Updates the BlackBox inputs
566 /// \returns UPTODATE if all inputs are in UPTODATE status after update
568 IOStatus BlackBox::bbUpdateInputs(bool excludeParent)
570 bbtkDebugMessageInc("Process",4,
571 "=> BlackBox::bbUpdateInputs() ["
572 <<bbGetFullName()<<"]"
575 IOStatus s = UPTODATE;
577 InputConnectorMapType::iterator i;
578 for ( i = bbGetInputConnectorMap().begin();
579 i!= bbGetInputConnectorMap().end(); ++i)
581 if (excludeParent && (i->first=="WinParent")) continue;
582 if (i->first=="WinHide") continue;
583 // If input type is Void : no recurse
584 //if ( bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo()
588 IOStatus t = i->second->BackwardUpdate();
589 if (t==MODIFIED) s = MODIFIED;
592 bbtkDebugMessageDec("Process",4,
593 "<= BlackBox::bbUpdateInputs() ["
594 <<bbGetFullName()<<"]"
600 //=========================================================================
603 //=========================================================================
604 /// Connects the input <name> to the connection c
605 void BlackBox::bbConnectInput( const std::string& name, Connection::Pointer c)
607 bbtkDebugMessage("connection",2,
608 "==> BlackBox::bbConnectInput(\""
609 <<name<<"\","<<c->GetFullName()<<") ["
610 <<bbGetFullName()<<"]"
614 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
615 if (i==bbGetInputConnectorMap().end())
617 bbtkError("no input called '"<<name<<"'");
619 i->second->SetConnection(c);
621 bbtkDebugMessage("connection",2,
622 "<== BlackBox::bbConnectInput(\""
623 <<name<<"\","<<c->GetFullName()<<") ["
624 <<bbGetFullName()<<"]"
626 // bbSetModifiedStatus();
629 //=========================================================================
632 //=========================================================================
633 /// Connects the output <name> to the connection c
634 void BlackBox::bbConnectOutput( const std::string& name, Connection::Pointer c)
636 bbtkDebugMessage("connection",2,
637 "==> BlackBox::bbConnectOutput(\""<<name<<"\","
638 <<c->GetFullName()<<") ["
639 <<bbGetFullName()<<"]"<<std::endl);
641 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
642 if (i==bbGetOutputConnectorMap().end())
644 bbtkError("no output called '"<<name<<"'");
646 i->second->SetConnection(c);
648 bbtkDebugMessage("connection",2,
649 "<== BlackBox::bbConnectOutput(\""<<name<<"\","
650 <<c->GetFullName()<<") ["
651 <<bbGetFullName()<<"]"<<std::endl);
654 //=========================================================================
657 //=========================================================================
658 /// Disconnects the input <name> from the connection c
659 void BlackBox::bbDisconnectInput( const std::string& name, Connection::Pointer c)
662 bbtkDebugMessage("connection",2,
663 "==> BlackBox::bbDisconnectInput(\""<<name
664 <<"\","<<c->GetFullName()<<") ["
665 <<bbGetFullName()<<"]"
671 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
675 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
676 if (i==bbGetInputConnectorMap().end())
678 bbtkError("no input called '"<<name<<"'");
680 i->second->UnsetConnection(c);
682 bbtkDebugMessage("connection",2,
683 "<== BlackBox::bbDisconnectInput(\""<<name
684 <<"\","<<c->GetFullName()<<") ["
685 <<bbGetFullName()<<"]"
689 //=========================================================================
692 //=========================================================================
693 /// Disconnects the output <name> from the connection c
694 void BlackBox::bbDisconnectOutput( const std::string& name, Connection::Pointer c)
696 bbtkDebugMessage("connection",2,
697 "==> BlackBox::bbDisconnectOutput(\""<<name
698 <<"\","<<c->GetFullName()<<") ["
699 <<bbGetFullName()<<"]"
704 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
708 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
709 if (i==bbGetOutputConnectorMap().end())
711 bbtkError("no output called '"<<name<<"'");
713 i->second->UnsetConnection(c);
715 bbtkDebugMessage("connection",2,
716 "<== BlackBox::bbDisconnectOutput(\""<<name
717 <<"\","<<c->GetFullName()<<") ["
718 <<bbGetFullName()<<"]"
721 //=========================================================================
724 //=========================================================================
726 void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
728 fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
730 //=========================================================================
733 //=========================================================================
734 std::string BlackBox::bbGetOutputAsString( const std::string &output )
737 // Looks for the adaptor
738 if (bbGetOutputType(output).name() != typeid(std::string).name() )
741 Package::Pointer p = bbGetDescriptor()->GetPackage();
742 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
744 Factory::Pointer f = p->GetFactorySet().begin()->lock();
749 bbGetOutputType(output),
752 } catch (bbtk::Exception e)
757 a->bbSetInput("In",bbGetOutput(output));
759 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
761 v="? (no adaptor found)";
766 v="? (no factory found)";
772 v = bbGetOutput(output).unsafe_get<std::string>() ;
776 //=========================================================================
778 //=========================================================================
779 std::string BlackBox::bbGetInputAsString( const std::string &input )
782 // Looks for the adaptor
783 if (bbGetInputType(input) != typeid(std::string))
786 Package::Pointer p = bbGetDescriptor()->GetPackage();
787 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
789 Factory::Pointer f = p->GetFactorySet().begin()->lock();
794 bbGetInputType(input),
797 }catch (bbtk::Exception e)
803 a->bbSetInput("In",bbGetInput(input));
805 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
809 v="? (no adaptor found)";
814 v="? (no factory found)";
819 v = bbGetInput(input).unsafe_get<std::string>() ;
823 //=======================================================================
825 //=======================================================================
826 // Replaces substrings "<" by "["
827 void SubsBrackets ( std::string& s )
829 // std::cout << "BEFORE=["<<s<<"]"<<std::endl;
831 std::string::size_type pos = 0;
834 while ( pos != std::string::npos )
836 // std::cout << "*** find one "<<std::endl;
837 s.replace(pos,1,cr,1);
838 pos = s.find(ss, pos);
844 while ( pos != std::string::npos )
846 // std::cout << "*** find one "<<std::endl;
847 s.replace(pos,1,cr,1);
848 pos = s.find(ss, pos);
854 while ( pos != std::string::npos )
856 // std::cout << "*** find one "<<std::endl;
857 s.replace(pos,1,cr,1);
858 pos = s.find(ss, pos);
859 } // std::cout << "AFTER=["<<s<<"]"<<std::endl;
861 //=======================================================================
863 //=========================================================================
864 /// Write Graphviz-dot description in file
865 void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
866 BlackBox::Pointer parentblackbox,
867 int detail, int level,
872 InputConnectorMapType::iterator i;
874 std::string labelStr;
875 std::string valueStr("");
878 labelStr = bbGetName() ;
880 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
882 labelStr = bbGetName();
883 labelStr = labelStr + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "] ";
886 SubsBrackets(labelStr);
889 labelStr = labelStr + " | {{ ";
890 std::string tempStrTypeName;
893 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
897 labelStr=labelStr+" | ";
900 if (instanceOrtype==true)
902 valueStr = this->bbGetInputAsString(i->first) + " = ";
904 const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
905 tempStrTypeName=id->GetTypeName();
906 SubsBrackets(tempStrTypeName);
907 std::string Name(i->first);
909 labelStr=labelStr + "<"+i->first.c_str()+"> " + valueStr + Name.c_str() + " [" + tempStrTypeName.c_str() + "]";
911 labelStr=labelStr+ " } | {";
913 OutputConnectorMapType::iterator ii;
914 for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii )
918 labelStr=labelStr+" | ";
921 if (instanceOrtype==true)
923 valueStr = this->bbGetOutputAsString(ii->first) + " = ";
925 const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first);
926 tempStrTypeName=id->GetTypeName();
927 SubsBrackets(tempStrTypeName);
928 std::string Name(ii->first);
930 labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + " ["+tempStrTypeName+"]";
932 labelStr = labelStr+ " } }" ;
936 bbWriteDotInputOutputName(ff,true,detail,level);
937 std::string tmp ( bbGetTypeName() );
941 url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
943 url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
945 fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
946 // std::cout << labelStr << std::endl;
949 if (GetThisPointer<BlackBox>()!=parentblackbox){
950 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
954 Connection::Pointer con = i->second->GetConnection();
956 BlackBox::Pointer a=con->GetBlackBoxFrom();
957 BlackBox::Pointer b=con->GetBlackBoxTo();
959 a->bbWriteDotInputOutputName(ff,false,detail,level);
962 fprintf(ff,":%s",con->GetBlackBoxFromOutput().c_str());
965 b->bbWriteDotInputOutputName(ff,true,detail,level);
968 fprintf(ff,":%s",con->GetBlackBoxToInput().c_str());
970 fprintf(ff,"%s\n",";");
974 } // if parentblackbox
976 //=========================================================================
981 //=========================================================================
982 void BlackBox::bbShowRelations(BlackBox::Pointer parentblackbox,
983 int detail, int level
984 /*,Factory *factory*/ )
987 if (this->bbGetDescriptor()->GetPackage())
989 bbtkMessage("Help",1,"Black Box '"<<bbGetName()<<"' <"<<
990 this->bbGetDescriptor()->GetPackage()->GetName()
991 <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
995 bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
997 // bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
998 // bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
1000 std::vector<std::string> iname;
1001 std::vector<std::string> ivalue;
1002 std::vector<std::string> iconn;
1004 InputConnectorMapType::iterator i;
1005 unsigned int namelmax = 0;
1006 unsigned int valuelmax = 0;
1007 unsigned int connlmax = 0;
1008 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1010 iname.push_back(i->first);
1011 if (iname.back().size()>namelmax) namelmax = iname.back().size();
1012 ivalue.push_back(bbGetInputAsString(i->first));
1013 if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
1015 Connection::Pointer con = i->second->GetConnection();
1017 s = con->GetBlackBoxFrom()->bbGetName();
1019 s += con->GetBlackBoxFromOutput();
1023 OutputConnectorMapType::iterator o;
1024 std::vector<std::string> oname;
1025 std::vector<std::string> ovalue;
1026 std::vector<std::vector<std::string> > oconn;
1027 for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o )
1029 oname.push_back(o->first);
1030 if (oname.back().size()>namelmax) namelmax = oname.back().size();
1031 ovalue.push_back(bbGetOutputAsString(o->first));
1032 if (ovalue.back().size()>valuelmax) valuelmax = ovalue.back().size();
1033 std::vector<std::string> ss;
1034 const std::vector<Connection::WeakPointer>& con
1035 = o->second->GetConnectionVector();
1036 std::vector<Connection::WeakPointer>::const_iterator c;
1037 for (c=con.begin();c!=con.end();++c)
1040 s = (*c).lock()->GetBlackBoxTo()->bbGetName();
1042 s += (*c).lock()->GetBlackBoxToInput();
1045 oconn.push_back(ss);
1049 bbtkMessage("Help",1," * Inputs : "<<std::endl);
1051 bbtkMessage("Help",1," * No inputs"<<std::endl);
1053 std::vector<std::string>::iterator i1,i2,i3;
1054 for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin();
1055 i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end();
1058 std::string name(*i1);
1060 name.append(1+namelmax-name.size(),' ');
1061 std::string value(*i2);
1063 value.append(1+valuelmax-value.size(),' ');
1065 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<" <-- '"<<*i3<<"'"<<std::endl);
1067 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<std::endl);
1071 bbtkMessage("Help",1," * Outputs : "<<std::endl);
1073 bbtkMessage("Help",1," * No outputs"<<std::endl);
1075 std::vector<std::vector<std::string> >::iterator i4;
1077 for (i1=oname.begin(),i2=ovalue.begin(),i4=oconn.begin();
1078 i1!=oname.end(),i2!=ovalue.end(),i4!=oconn.end();
1081 std::string name(*i1);
1083 name.append(1+namelmax-name.size(),' ');
1084 std::string value(*i2);
1086 value.append(1+valuelmax-value.size(),' ');
1088 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<std::endl);
1091 std::string pref = " '"+name+" = '"+value;
1092 for (i3=i4->begin();i3!=i4->end();++i3)
1094 bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'"<<std::endl);
1095 pref.replace(0,pref.size(),pref.size(),' ');
1101 //=========================================================================
1104 //=========================================================================
1105 void BlackBox::bbGlobalProcessExecutionList()
1107 bbtkDebugMessageInc("Process",1,
1108 "=> BlackBox::bbGlobalProcessExecutionList()"
1111 std::set<BlackBox::Pointer>::iterator i;
1112 for (i=bbmgExecutionList.begin();
1113 i!=bbmgExecutionList.end();
1116 bbtkDebugMessage("Process",2,
1117 " -> Executing "<<(*i)->bbGetFullName()<<std::endl);
1118 (*i)->bbExecute(true);
1121 bbmgExecutionList.clear();
1122 bbtkDebugMessageDec("Process",1,
1123 "<= BlackBox::bbGlobalProcessExecutionList()"
1128 //=========================================================================
1130 bool BlackBox::bbGlobalGetSomeBoxExecuting()
1132 return bbmgSomeBoxExecuting;
1135 void BlackBox::bbGlobalSetSomeBoxExecuting(bool b)
1137 bbmgSomeBoxExecuting = b;
1140 void BlackBox::bbGlobalSetFreezeExecution(bool b)
1142 bbmgFreezeExecution = b;
1145 bool BlackBox::bbGlobalGetFreezeExecution()
1147 return bbmgFreezeExecution;
1150 void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
1152 bbmgExecutionList.insert(b);
1156 //=========================================================================
1157 // Static members initialization
1159 bool BlackBox::bbmgSomeBoxExecuting = false;
1160 bool BlackBox::bbmgFreezeExecution = false;
1161 std::set<BlackBox*> BlackBox::bbmgExecutionList;
1163 //=========================================================================
1165 //=========================================================================
1166 void BlackBox::Check(bool recursive)
1168 bbtkMessage("debug",1,"*** Checking Black Box "<<(void*)this<<" ["<<bbGetFullName()
1169 <<"] ... OK"<<std::endl);
1171 //=========================================================================
1175 } // EO namespace bbtk