1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBox.cxx,v $
5 Date: $Date: 2008/11/13 14:46:43 $
6 Version: $Revision: 1.28 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 * \brief Class bbtk::BlackBox : abstract black-box interface.
35 #include "bbtkBlackBox.h"
36 #include "bbtkPackage.h"
37 #include "bbtkMessageManager.h"
38 #include "bbtkFactory.h"
40 #include "bbtkConfigurationFile.h"
41 #include "bbtkWxBlackBox.h"
51 static bool bbmgSomeBoxExecuting = false;
52 static bool bbmgFreezeExecution = false;
53 static std::set<BlackBox::WeakPointer> bbmgExecutionList;
55 //=========================================================================
56 BlackBox::Deleter::Deleter()
59 //=========================================================================
61 //=========================================================================
62 void BlackBox::Deleter::Delete(Object* p)
64 BlackBox* b = dynamic_cast<BlackBox*>(p);
67 bbtkInternalError("BlackBox::Deleter::Delete("<<p->GetObjectName()
69 <<"dynamic cast to BlackBox* failed !");
71 std::string name = p->GetObjectName();//b->bbGetNameWithParent();
72 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
75 BlackBoxDescriptor::WeakPointer desc = b->bbGetDescriptor();
76 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : deleting black box"<<std::endl);
80 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : releasing descriptor ["<<desc.lock()<<"]"<<std::endl);
84 Package::WeakPointer pack = desc.lock()->GetPackage();
87 Package::ReleaseBlackBoxDescriptor(pack,desc);
91 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : descriptor package expired (was not held by a package and the box was the last instance)"<<std::endl);
96 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);
98 bbtkDebugMessage("object",2,"<## BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
100 //=========================================================================
102 //=========================================================================
103 BlackBox::BlackBox(const std::string &name)
108 bbmBoxProcessMode("Pipeline"),
112 bbtkDebugMessage("object",4,"==> BlackBox::BlackBox(\""
113 <<name<<"\")"<<std::endl);
114 bbtkDebugMessage("object",4,"<== BlackBox::BlackBox(\""
115 <<name<<"\")"<<std::endl);
117 //=========================================================================
119 //=========================================================================
120 BlackBox::BlackBox(const BlackBox&)
123 //=========================================================================
124 BlackBox::BlackBox(BlackBox& from, const std::string &name)
126 bbmStatus(from.bbmStatus),
129 bbmBoxProcessMode(from.bbmBoxProcessMode),
133 bbtkDebugMessage("object",4,"==> BlackBox::BlackBox("
134 <<from.bbGetFullName()<<",\""
135 <<name<<"\")"<<std::endl);
136 bbtkDebugMessage("object",4,"<== BlackBox::BlackBox("
137 <<from.bbGetFullName()<<",\""
138 <<name<<"\")"<<std::endl);
140 //=========================================================================
143 //=========================================================================
144 BlackBox::~BlackBox()
146 bbtkDebugMessage("object",4,"==> BlackBox::~BlackBox() ["<<bbmName
148 this->bbDesallocateConnectors();
149 bbtkDebugMessage("object",4,"<== BlackBox::~BlackBox() ["<<bbmName
152 //=========================================================================
155 //=========================================================================
156 /// Main processing method of the box.
157 void BlackBox::bbExecute(bool force)
159 bbtkDebugMessageInc("process",2,
160 "=> BlackBox::bbExecute("<<(int)force<<") ["
161 <<bbGetFullName()<<"]"<<std::endl);
163 // If already executing : return
164 if (bbGetExecuting())
166 bbtkDebugMessage("process",2,
167 " -> already executing : bailing out"<<std::endl);
171 // If execution frozen : return
172 if (bbGlobalGetFreezeExecution())
174 bbtkDebugMessage("process",2,
175 " -> FreezeExecution global flag is 'true' : abort execution"<<std::endl);
180 // If force is true then update is triggered even if the box is UPTODATE
181 if (force) bbSetModifiedStatus();
183 // Calls the main recursive update method
184 bbBackwardUpdate(Connection::Pointer());
186 bbtkDebugMessageDec("process",2,
187 "<= BlackBox::bbExecute() ["
188 <<bbGetFullName()<<"]"<<std::endl);
190 //=========================================================================
192 //=========================================================================
193 std::string BlackBox::bbGetFullName() const
195 return this->bbGetNameWithParent()+"<"+this->bbGetDescriptor()->GetTypeName()+">";
197 //=========================================================================
201 //=========================================================================
202 /// Returns the name with the name of the parent prepended if any
203 std::string BlackBox::bbGetNameWithParent() const
205 if (bbmParent.lock())
207 return bbmParent.lock()->bbGetNameWithParent() + ":" + bbmName;
214 //=========================================================================
216 //=========================================================================
217 /// Prints the Help on the BlackBox type
218 void BlackBox::bbGetHelp(bool full) const
220 bbGetDescriptor()->GetHelp(full);
222 //=========================================================================
225 //=========================================================================
226 /// Returns true if the UserBlackBox has an input of name name
227 bool BlackBox::bbHasInput(const std::string& name) const
229 bbtkDebugMessageInc("Kernel",8,
230 "BlackBox::bbHasInput(\""
231 <<name<<"\") ["<<bbGetFullName()<<"]"
233 bool r = ( bbGetDescriptor()->GetInputDescriptorMap().find(name)
234 != bbGetDescriptor()->GetInputDescriptorMap().end());
235 bbtkDebugDecTab("Kernel",8);
238 //=========================================================================
241 //=========================================================================
242 /// Returns true if the UserBlackBox has an output of name name
243 bool BlackBox::bbHasOutput(const std::string& name) const
245 bbtkDebugMessageInc("Kernel",8,"BlackBox::bbHasOutput(\""
246 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
247 bool r = ( bbGetDescriptor()->GetOutputDescriptorMap().find(name)
248 != bbGetDescriptor()->GetOutputDescriptorMap().end());
249 bbtkDebugDecTab("Kernel",8);
252 //=========================================================================
255 //=========================================================================
256 /// Gets the output type of a given name
257 TypeInfo BlackBox::bbGetOutputType( const std::string &name ) const
259 bbtkDebugMessageInc("Kernel",8,
260 "BlackBox::bbGetOutputType(\""
261 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
262 TypeInfo r = bbGetDescriptor()->GetOutputDescriptor(name)->GetTypeInfo();
263 bbtkDebugDecTab("Kernel",8);
266 //=========================================================================
268 //=========================================================================
269 /// Gets the input type of a given name
270 TypeInfo BlackBox::bbGetInputType( const std::string &name ) const
272 bbtkDebugMessageInc("Kernel",8,
273 "BlackBox::bbGetInputType(\""
274 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
275 TypeInfo r = bbGetDescriptor()->GetInputDescriptor(name)->GetTypeInfo();
276 bbtkDebugDecTab("Kernel",8);
279 //=========================================================================
282 //=========================================================================
283 /// Allocates the i/o connectors of the black box
284 void BlackBox::bbAllocateConnectors()
286 bbtkDebugMessageInc("Kernel",8,
287 "BlackBox::bbAllocateConnectors() ["
288 <<bbGetFullName()<<"]"
290 const BlackBoxDescriptor::InputDescriptorMapType& imap
291 = bbGetDescriptor()->GetInputDescriptorMap();
292 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
293 for ( i = imap.begin(); i != imap.end(); ++i )
295 bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
296 bbGetInputConnectorMap()[i->second->GetName()]
297 = new BlackBoxInputConnector(GetThisPointer<BlackBox>());
299 const BlackBoxDescriptor::OutputDescriptorMapType& omap
300 = bbGetDescriptor()->GetOutputDescriptorMap();
301 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
302 for ( o = omap.begin(); o != omap.end(); ++o )
304 bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
305 bbGetOutputConnectorMap()[o->second->GetName()]
306 = new BlackBoxOutputConnector();
308 bbtkDebugDecTab("Kernel",8);
310 //=========================================================================
313 //=========================================================================
314 /// Desallocates the i/o connectors of the black box
315 void BlackBox::bbDesallocateConnectors()
317 bbtkDebugMessageInc("Kernel",8,
318 "BlackBox::bbDesallocateConnectors()"
321 InputConnectorMapType::const_iterator i;
322 for ( i = bbGetInputConnectorMap().begin();
323 i != bbGetInputConnectorMap().end(); ++i )
325 bbtkDebugMessage("Kernel",8,"* Delete \""<<i->first<<"\""<<std::endl);
328 OutputConnectorMapType::const_iterator o;
329 for ( o = bbGetOutputConnectorMap().begin();
330 o != bbGetOutputConnectorMap().end(); ++o )
332 bbtkDebugMessage("Kernel",8,"* Delete \""<<o->first<<"\""<<std::endl);
336 bbtkDebugDecTab("Kernel",8);
339 //=========================================================================
342 //=========================================================================
343 /// Copies the input / output values from another box
344 void BlackBox::bbCopyIOValues(BlackBox& from)
346 bbtkDebugMessageInc("Kernel",9,
347 "BlackBox::bbCopyIOValues("
348 <<from.bbGetFullName()<<") ["
349 <<bbGetFullName()<<"]"<<std::endl);
350 // copies the input values
351 const BlackBoxDescriptor::InputDescriptorMapType& imap
352 = bbGetDescriptor()->GetInputDescriptorMap();
353 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
354 for ( i = imap.begin(); i != imap.end(); ++i )
356 if (! i->second->GetCopyConstruct() ) continue;
357 std::string input = i->second->GetName();
358 this->bbSetInput(input, from.bbGetInput(input) );
360 // copies the output values
361 const BlackBoxDescriptor::OutputDescriptorMapType& omap
362 = bbGetDescriptor()->GetOutputDescriptorMap();
363 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
364 for ( o = omap.begin(); o != omap.end(); ++o )
366 if (! o->second->GetCopyConstruct() ) continue;
367 std::string output = o->second->GetName();
368 this->bbSetOutput(output, from.bbGetOutput(output) );
371 bbtkDebugDecTab("Kernel",9);
374 //=========================================================================
378 //=========================================================================
379 bool BlackBox::bbCanReact() const
381 return ( bbGlobalGetSomeBoxExecuting()
382 #ifdef _USE_WXWIDGETS_
383 || Wx::IsSomeWindowAlive()
387 //=========================================================================
391 //=========================================================================
392 /// User overloadable destruction method of a black box
393 void BlackBox::bbUserDelete()
395 bbtkDebugMessage("process",5,
396 "=> BlackBox::bbUserDelete() ["
397 <<bbGetFullName()<<"]"
398 <<" : not overloaded; using standard deletion"
402 //=========================================================================
405 //=========================================================================
406 BlackBox::BoxProcessModeValue BlackBox::bbGetBoxProcessModeValue() const
408 const std::string& p = bbmBoxProcessMode;
410 (p == "P") || (p == "p") ||
411 (p == "Pipeline") || (p == "pipeline") ) return Pipeline;
413 (p == "A") || (p == "a") ||
414 (p == "Always") || (p == "always") ) return Always;
416 (p == "R") || (p == "r") ||
417 (p == "Reactive") || (p == "reactive") ) return Reactive;
418 bbtkError(bbGetFullName()<<" : BoxProcessMode value '"<<p
419 <<"' unknown. Possible values : "
420 <<"'0'/'P'/'p'/'Pipeline'/'pipeline' | "
421 <<"'1'/'A'/'a'/'Always'/'always' | "
422 <<"'2'/'R'/'r'/'Reactive'/'reactive'"<<std::endl);
424 //=========================================================================
426 //=========================================================================
427 bool BlackBox::bbBoxProcessModeIsReactive() const
429 return (bbGetBoxProcessModeValue() == Reactive);
431 //=========================================================================
433 //=========================================================================
434 bool BlackBox::bbBoxProcessModeIsAlways() const
436 return (bbGetBoxProcessModeValue() == Always);
438 //=========================================================================
440 //=========================================================================
441 /// Signals that the BlackBox has been modified
442 void BlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
444 bbtkDebugMessage("modified",1,
445 "==> BlackBox::bbSetModifiedStatus("<<c<<") ["
446 <<bbGetFullName()<<"]"<<std::endl);
448 if ( (c==bbGetInputConnectorMap().find("WinHide")->second) )
449 // && (bbCanReact()))
451 bbtkDebugMessage("modified",2,
452 "-> Hide triggered by WinHide input change"
454 this->bbHideWindow();
455 this->bbSetStatus(MODIFIED);
458 if ( (c==bbGetInputConnectorMap().find("WinClose")->second) )
459 // && (bbCanReact()))
461 bbtkDebugMessage("modified",2,
462 "-> Close triggered by WinClose input change"
464 this->bbHideWindow();
465 this->bbSetStatus(MODIFIED);
469 if ( ( bbBoxProcessModeIsReactive() ||
470 (c==bbGetInputConnectorMap().find("BoxExecute")->second))
473 bbtkDebugMessage("modified",2,
474 "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
475 this->bbSetStatus(MODIFIED);
476 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
479 else if ( bbGetStatus() == MODIFIED ) //! this->bbIsUptodate())
481 bbtkDebugMessage("modified",2,"-> Already modified"<<std::endl);
487 bbtkDebugMessage("modified",2,"-> Status set to modified"<<std::endl);
488 this->bbSetStatus(MODIFIED);
491 this->bbSignalOutputModification(false);
494 bbtkDebugMessageDec("process",5,
495 "<= BlackBox::bbSetModifiedStatus("<<c<<") ["
496 <<bbGetFullName()<<"]"<<std::endl);
499 //=========================================================================
501 //=========================================================================
502 void BlackBox::bbSignalOutputModification(bool reaction)
504 bbtkDebugMessageInc("process",5,
505 "=> BlackBox::bbSignalOutputModification() ["
506 <<bbGetFullName()<<"]"<<std::endl);
508 OutputConnectorMapType::iterator change = bbGetOutputConnectorMap().end();
509 OutputConnectorMapType::iterator i;
510 for ( i = bbGetOutputConnectorMap().begin();
511 i != bbGetOutputConnectorMap().end(); ++i) {
512 /* if ( i->first == "BoxChange" )
518 i->second->SetModifiedStatus();
520 // if (change != bbGetOutputConnectorMap().end())
521 // change->second->SetModifiedStatus();
523 if (reaction) bbGlobalProcessExecutionList();
525 bbtkDebugMessageDec("process",5,
526 "<= BlackBox::bbSignalOutputModification() ["
527 <<bbGetFullName()<<"]"<<std::endl);
530 //=========================================================================
531 //=========================================================================
532 void BlackBox::bbSignalOutputModification(const std::string& output,
535 bbtkDebugMessageInc("process",5,
536 "=> BlackBox::bbSignalOutputModification("
538 <<bbGetFullName()<<"]"<<std::endl);
540 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(output);
541 if ( i == bbGetOutputConnectorMap().end() )
543 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
545 i->second->SetModifiedStatus();
546 // Has to notify the output "BoxChange" also
547 if (output != "BoxChange")
549 i = bbGetOutputConnectorMap().find("BoxChange");
550 if ( i != bbGetOutputConnectorMap().end() )
552 i->second->SetModifiedStatus();
555 if (reaction) bbGlobalProcessExecutionList();
557 bbtkDebugMessageDec("process",5,
558 "<= BlackBox::bbSignalOutputModification("
560 <<bbGetFullName()<<"]"<<std::endl);
563 //=========================================================================
564 //=========================================================================
565 void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
568 bbtkDebugMessageInc("process",5,
569 "=> BlackBox::bbSignalOutputModification(vector of outputs) ["
570 <<bbGetFullName()<<"]"<<std::endl);
571 OutputConnectorMapType::iterator i;
572 std::vector<std::string>::const_iterator o;
573 for (o=output.begin();o!=output.end();++o)
575 // the output "BoxChange" must be signaled **AFTER** all others
576 if (*o == "BoxChange") continue;
577 // Look for the connector
578 i = bbGetOutputConnectorMap().find(*o);
579 if ( i == bbGetOutputConnectorMap().end() )
581 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
583 i->second->SetModifiedStatus();
585 // Has to notify the output "BoxChange" also
586 i = bbGetOutputConnectorMap().find("BoxChange");
587 if ( i != bbGetOutputConnectorMap().end() )
589 i->second->SetModifiedStatus();
591 if (reaction) bbGlobalProcessExecutionList();
593 bbtkDebugMessageDec("process",5,
594 "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
595 <<bbGetFullName()<<"]"<<std::endl);
598 //=========================================================================
600 //=========================================================================
601 /// Updates the BlackBox inputs
602 /// \returns UPTODATE if all inputs are in UPTODATE status after update
604 IOStatus BlackBox::bbUpdateInputs(bool excludeParent)
606 bbtkDebugMessageInc("process",4,
607 "=> BlackBox::bbUpdateInputs() ["
608 <<bbGetFullName()<<"]"
611 IOStatus s = UPTODATE;
613 InputConnectorMapType::iterator i;
614 for ( i = bbGetInputConnectorMap().begin();
615 i!= bbGetInputConnectorMap().end(); ++i)
617 if (excludeParent && (i->first=="WinParent")) continue;
618 if (i->first=="WinHide") continue;
619 // If input type is Void : no recurse
620 //if ( bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo()
624 IOStatus t = i->second->BackwardUpdate();
625 if (t==MODIFIED) s = MODIFIED;
628 bbtkDebugMessageDec("process",4,
629 "<= BlackBox::bbUpdateInputs() ["
630 <<bbGetFullName()<<"]"
636 //=========================================================================
639 //=========================================================================
640 /// Connects the input <name> to the connection c
641 void BlackBox::bbConnectInput( const std::string& name, Connection* c)
643 bbtkDebugMessage("connection",2,
644 "==> BlackBox::bbConnectInput(\""
645 <<name<<"\","<<c->GetFullName()<<") ["
646 <<bbGetFullName()<<"]"
650 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
651 if (i==bbGetInputConnectorMap().end())
653 bbtkError("no input called '"<<name<<"'");
655 i->second->SetConnection(c);
657 bbtkDebugMessage("connection",2,
658 "<== BlackBox::bbConnectInput(\""
659 <<name<<"\","<<c->GetFullName()<<") ["
660 <<bbGetFullName()<<"]"
662 // bbSetModifiedStatus();
665 //=========================================================================
668 //=========================================================================
669 /// Connects the output <name> to the connection c
670 void BlackBox::bbConnectOutput( const std::string& name, Connection* c)
672 bbtkDebugMessage("connection",2,
673 "==> BlackBox::bbConnectOutput(\""<<name<<"\","
674 <<c->GetFullName()<<") ["
675 <<bbGetFullName()<<"]"<<std::endl);
677 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
678 if (i==bbGetOutputConnectorMap().end())
680 bbtkError("no output called '"<<name<<"'");
682 i->second->SetConnection(c);
684 bbtkDebugMessage("connection",2,
685 "<== BlackBox::bbConnectOutput(\""<<name<<"\","
686 <<c->GetFullName()<<") ["
687 <<bbGetFullName()<<"]"<<std::endl);
690 //=========================================================================
693 //=========================================================================
694 /// Disconnects the input <name> from the connection c
695 void BlackBox::bbDisconnectInput( const std::string& name, Connection* c)
698 bbtkDebugMessage("connection",2,
699 "==> BlackBox::bbDisconnectInput(\""<<name
700 <<"\","<<c->GetFullName()<<") ["
701 <<bbGetFullName()<<"]"
707 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
711 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
712 if (i==bbGetInputConnectorMap().end())
714 bbtkError("no input called '"<<name<<"'");
716 i->second->UnsetConnection(c);
718 bbtkDebugMessage("connection",2,
719 "<== BlackBox::bbDisconnectInput(\""<<name
720 <<"\","<<c->GetFullName()<<") ["
721 <<bbGetFullName()<<"]"
725 //=========================================================================
728 //=========================================================================
729 /// Disconnects the output <name> from the connection c
730 void BlackBox::bbDisconnectOutput( const std::string& name, Connection* c)
732 bbtkDebugMessage("connection",2,
733 "==> BlackBox::bbDisconnectOutput(\""<<name
734 <<"\","<<c->GetFullName()<<") ["
735 <<bbGetFullName()<<"]"
740 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
744 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
745 if (i==bbGetOutputConnectorMap().end())
747 bbtkError("no output called '"<<name<<"'");
749 i->second->UnsetConnection(c);
751 bbtkDebugMessage("connection",2,
752 "<== BlackBox::bbDisconnectOutput(\""<<name
753 <<"\","<<c->GetFullName()<<") ["
754 <<bbGetFullName()<<"]"
757 //=========================================================================
760 //=========================================================================
762 void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
764 fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
766 //=========================================================================
769 //=========================================================================
770 std::string BlackBox::bbGetOutputAsString( const std::string &output )
773 // Looks for the adaptor
774 if (bbGetOutputType(output).name() != typeid(std::string).name() )
777 Package::Pointer p = bbGetDescriptor()->GetPackage();
778 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
780 Factory::Pointer f = p->GetFactorySet().begin()->lock();
785 bbGetOutputType(output),
788 } catch (bbtk::Exception e)
793 a->bbSetInput("In",bbGetOutput(output));
795 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
797 v="? (no adaptor found)";
802 v="? (no factory found)";
808 v = bbGetOutput(output).unsafe_get<std::string>() ;
812 //=========================================================================
814 //=========================================================================
815 std::string BlackBox::bbGetInputAsString( const std::string &input )
818 // Looks for the adaptor
819 if (bbGetInputType(input) != typeid(std::string))
822 Package::Pointer p = bbGetDescriptor()->GetPackage();
823 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
825 Factory::Pointer f = p->GetFactorySet().begin()->lock();
830 bbGetInputType(input),
833 }catch (bbtk::Exception e)
839 a->bbSetInput("In",bbGetInput(input));
841 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
845 v="? (no adaptor found)";
850 v="? (no factory found)";
855 v = bbGetInput(input).unsafe_get<std::string>() ;
859 //=======================================================================
861 //=======================================================================
862 // Replaces substrings "<" by "["
863 void SubsBrackets ( std::string& s )
865 // std::cout << "BEFORE=["<<s<<"]"<<std::endl;
867 std::string::size_type pos = 0;
870 while ( pos != std::string::npos )
872 // std::cout << "*** find one "<<std::endl;
873 s.replace(pos,1,cr,1);
874 pos = s.find(ss, pos);
880 while ( pos != std::string::npos )
882 // std::cout << "*** find one "<<std::endl;
883 s.replace(pos,1,cr,1);
884 pos = s.find(ss, pos);
890 while ( pos != std::string::npos )
892 // std::cout << "*** find one "<<std::endl;
893 s.replace(pos,1,cr,1);
894 pos = s.find(ss, pos);
895 } // std::cout << "AFTER=["<<s<<"]"<<std::endl;
897 //=======================================================================
899 //=========================================================================
900 /// Write Graphviz-dot description in file
901 void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
902 BlackBox::Pointer parentblackbox,
903 int detail, int level,
908 InputConnectorMapType::iterator i;
910 std::string labelStr;
911 std::string valueStr("");
914 labelStr = bbGetName() ;
916 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
918 labelStr = bbGetName();
919 labelStr = labelStr + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "] ";
922 SubsBrackets(labelStr);
925 labelStr = labelStr + " | {{ ";
926 std::string tempStrTypeName;
929 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
933 labelStr=labelStr+" | ";
936 if (instanceOrtype==true)
938 valueStr = this->bbGetInputAsString(i->first) + " = ";
940 const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
941 tempStrTypeName=id->GetTypeName();
942 SubsBrackets(tempStrTypeName);
943 std::string Name(i->first);
945 labelStr=labelStr + "<"+i->first.c_str()+"> " + valueStr + Name.c_str() + " [" + tempStrTypeName.c_str() + "]";
947 labelStr=labelStr+ " } | {";
949 OutputConnectorMapType::iterator ii;
950 for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii )
954 labelStr=labelStr+" | ";
957 if (instanceOrtype==true)
959 valueStr = this->bbGetOutputAsString(ii->first) + " = ";
961 const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first);
962 tempStrTypeName=id->GetTypeName();
963 SubsBrackets(tempStrTypeName);
964 std::string Name(ii->first);
966 labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + " ["+tempStrTypeName+"]";
968 labelStr = labelStr+ " } }" ;
972 bbWriteDotInputOutputName(ff,true,detail,level);
973 std::string tmp ( bbGetTypeName() );
977 url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
979 url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
981 fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
982 // std::cout << labelStr << std::endl;
985 if (GetThisPointer<BlackBox>()!=parentblackbox){
986 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
990 Connection* con = i->second->GetConnection();
992 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
993 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
995 a->bbWriteDotInputOutputName(ff,false,detail,level);
998 fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
1001 b->bbWriteDotInputOutputName(ff,true,detail,level);
1004 fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
1006 fprintf(ff,"%s\n",";");
1010 } // if parentblackbox
1012 //=========================================================================
1017 //=========================================================================
1018 void BlackBox::bbShowRelations(BlackBox::Pointer parentblackbox,
1019 int detail, int level
1020 /*,Factory *factory*/ )
1023 if (this->bbGetDescriptor()->GetPackage())
1025 bbtkMessage("Help",1,"Black Box '"<<bbGetName()<<"' <"<<
1026 this->bbGetDescriptor()->GetPackage()->GetName()
1027 <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1031 bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1033 // bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
1034 // bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
1036 std::vector<std::string> iname;
1037 std::vector<std::string> ivalue;
1038 std::vector<std::string> iconn;
1040 InputConnectorMapType::iterator i;
1041 unsigned int namelmax = 0;
1042 unsigned int valuelmax = 0;
1043 // unsigned int connlmax = 0;
1044 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1046 iname.push_back(i->first);
1047 if (iname.back().size()>namelmax) namelmax = iname.back().size();
1048 ivalue.push_back(bbGetInputAsString(i->first));
1049 if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
1051 Connection* con = i->second->GetConnection();
1053 s = con->GetOriginalBlackBoxFrom()->bbGetName();
1055 s += con->GetOriginalBlackBoxFromOutput();
1059 OutputConnectorMapType::iterator o;
1060 std::vector<std::string> oname;
1061 std::vector<std::string> ovalue;
1062 std::vector<std::vector<std::string> > oconn;
1063 for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o )
1065 oname.push_back(o->first);
1066 if (oname.back().size()>namelmax) namelmax = oname.back().size();
1067 ovalue.push_back(bbGetOutputAsString(o->first));
1068 if (ovalue.back().size()>valuelmax) valuelmax = ovalue.back().size();
1069 std::vector<std::string> ss;
1070 const std::vector<Connection*>& con
1071 = o->second->GetConnectionVector();
1072 std::vector<Connection*>::const_iterator c;
1073 for (c=con.begin();c!=con.end();++c)
1076 s = (*c)->GetOriginalBlackBoxTo()->bbGetName();
1078 s += (*c)->GetOriginalBlackBoxToInput();
1081 oconn.push_back(ss);
1085 bbtkMessage("Help",1," * Inputs : "<<std::endl);
1087 bbtkMessage("Help",1," * No inputs"<<std::endl);
1089 std::vector<std::string>::iterator i1,i2,i3;
1090 for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin();
1091 i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end();
1094 std::string name(*i1);
1096 name.append(1+namelmax-name.size(),' ');
1097 std::string value(*i2);
1099 value.append(1+valuelmax-value.size(),' ');
1101 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<" <-- '"<<*i3<<"'"<<std::endl);
1103 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<std::endl);
1107 bbtkMessage("Help",1," * Outputs : "<<std::endl);
1109 bbtkMessage("Help",1," * No outputs"<<std::endl);
1111 std::vector<std::vector<std::string> >::iterator i4;
1113 for (i1=oname.begin(),i2=ovalue.begin(),i4=oconn.begin();
1114 i1!=oname.end(),i2!=ovalue.end(),i4!=oconn.end();
1117 std::string name(*i1);
1119 name.append(1+namelmax-name.size(),' ');
1120 std::string value(*i2);
1122 value.append(1+valuelmax-value.size(),' ');
1124 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<std::endl);
1127 std::string pref = " '"+name+" = '"+value;
1128 for (i3=i4->begin();i3!=i4->end();++i3)
1130 bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'"<<std::endl);
1131 pref.replace(0,pref.size(),pref.size(),' ');
1137 //=========================================================================
1139 static bool bbmgGlobalProcessingExecutionList = false;
1141 //=========================================================================
1142 void BlackBox::bbGlobalProcessExecutionList()
1144 bbtkDebugMessageInc("process",3,
1145 "=> BlackBox::bbGlobalProcessExecutionList()"
1147 if (bbmgGlobalProcessingExecutionList)
1149 bbtkDebugMessage("process",3,"BlackBox::bbGlobalProcessExecutionList() reentered !");
1152 bbmgGlobalProcessingExecutionList = true;
1154 std::set<BlackBox::WeakPointer>::iterator i;
1155 while (bbmgExecutionList.size()>0)
1157 i = bbmgExecutionList.begin();
1158 BlackBox::WeakPointer p = *i;
1159 bbmgExecutionList.erase(i);
1162 bbtkDebugMessage("process",4,
1164 p.lock()->bbGetFullName()<<std::endl);
1165 p.lock()->bbExecute(true);
1169 bbtkGlobalError("Strange error in BlackBox::bbGlobalProcessExecutionList() : Weak bb pointer in bbmgExecutionList is no more valid...");
1173 bbmgExecutionList.clear();
1174 bbtkDebugMessageDec("process",3,
1175 "<= BlackBox::bbGlobalProcessExecutionList()"
1178 bbmgGlobalProcessingExecutionList = false;
1181 //=========================================================================
1183 bool BlackBox::bbGlobalGetSomeBoxExecuting()
1185 return bbmgSomeBoxExecuting;
1188 void BlackBox::bbGlobalSetSomeBoxExecuting(bool b)
1190 bbmgSomeBoxExecuting = b;
1193 void BlackBox::bbGlobalSetFreezeExecution(bool b)
1195 bbmgFreezeExecution = b;
1198 bool BlackBox::bbGlobalGetFreezeExecution()
1200 return bbmgFreezeExecution;
1203 void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
1205 bbtkDebugMessage("process",3,"* bbGlobalAddToExecutionList("<<b->bbGetFullName()<<")"<<std::endl);
1206 if (bbmgGlobalProcessingExecutionList)
1208 bbtkDebugMessage("process",3,"bbGlobalAddToExecutionList called inside bbGlobalProcessExecutionList !");
1210 bbmgExecutionList.insert(b);
1214 //=========================================================================
1216 //=========================================================================
1217 void BlackBox::Check(bool recursive)
1219 bbtkMessage("debug",1,"*** Checking Black Box "<<(void*)this<<" ["<<bbGetFullName()
1220 <<"] ... OK"<<std::endl);
1222 //=========================================================================
1224 void BlackBox::bbUserOnShowWidget(std::string nameInput)
1226 bbtk::BlackBoxInputConnector *cc;
1227 cc = this->bbGetInputConnectorMap().find( nameInput.c_str() )->second;
1228 if (cc->GetConnection()!=NULL)
1230 cc->GetConnection()->GetBlackBoxFrom()->bbUserOnShow();
1236 } // EO namespace bbtk