1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBox.cxx,v $
5 Date: $Date: 2008/11/25 11:17:13 $
6 Version: $Revision: 1.29 $
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",1,
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 bbtkDebugMessage("Kernel",2,"* Copying input "<<input<<std::endl);
359 this->bbSetInput(input, from.bbGetInput(input) );
361 // copies the output values
362 const BlackBoxDescriptor::OutputDescriptorMapType& omap
363 = bbGetDescriptor()->GetOutputDescriptorMap();
364 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
365 for ( o = omap.begin(); o != omap.end(); ++o )
367 if (! o->second->GetCopyConstruct() ) continue;
368 std::string output = o->second->GetName();
369 bbtkDebugMessage("Kernel",2,"* Copying output "<<output<<std::endl);
370 this->bbSetOutput(output, from.bbGetOutput(output) );
373 bbtkDebugDecTab("Kernel",9);
376 //=========================================================================
380 //=========================================================================
381 bool BlackBox::bbCanReact() const
383 return ( bbGlobalGetSomeBoxExecuting()
384 #ifdef _USE_WXWIDGETS_
385 || Wx::IsSomeWindowAlive()
389 //=========================================================================
393 //=========================================================================
394 /// User overloadable destruction method of a black box
395 void BlackBox::bbUserDelete()
397 bbtkDebugMessage("process",5,
398 "=> BlackBox::bbUserDelete() ["
399 <<bbGetFullName()<<"]"
400 <<" : not overloaded; using standard deletion"
404 //=========================================================================
407 //=========================================================================
408 BlackBox::BoxProcessModeValue BlackBox::bbGetBoxProcessModeValue() const
410 const std::string& p = bbmBoxProcessMode;
412 (p == "P") || (p == "p") ||
413 (p == "Pipeline") || (p == "pipeline") ) return Pipeline;
415 (p == "A") || (p == "a") ||
416 (p == "Always") || (p == "always") ) return Always;
418 (p == "R") || (p == "r") ||
419 (p == "Reactive") || (p == "reactive") ) return Reactive;
420 bbtkError(bbGetFullName()<<" : BoxProcessMode value '"<<p
421 <<"' unknown. Possible values : "
422 <<"'0'/'P'/'p'/'Pipeline'/'pipeline' | "
423 <<"'1'/'A'/'a'/'Always'/'always' | "
424 <<"'2'/'R'/'r'/'Reactive'/'reactive'"<<std::endl);
426 //=========================================================================
428 //=========================================================================
429 bool BlackBox::bbBoxProcessModeIsReactive() const
431 return (bbGetBoxProcessModeValue() == Reactive);
433 //=========================================================================
435 //=========================================================================
436 bool BlackBox::bbBoxProcessModeIsAlways() const
438 return (bbGetBoxProcessModeValue() == Always);
440 //=========================================================================
442 //=========================================================================
443 /// Signals that the BlackBox has been modified
444 void BlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
446 bbtkDebugMessage("modified",1,
447 "==> BlackBox::bbSetModifiedStatus("<<c<<") ["
448 <<bbGetFullName()<<"]"<<std::endl);
450 if ( (c==bbGetInputConnectorMap().find("WinHide")->second) )
451 // && (bbCanReact()))
453 bbtkDebugMessage("modified",2,
454 "-> Hide triggered by WinHide input change"
456 this->bbHideWindow();
457 this->bbSetStatus(MODIFIED);
460 if ( (c==bbGetInputConnectorMap().find("WinClose")->second) )
461 // && (bbCanReact()))
463 bbtkDebugMessage("modified",2,
464 "-> Close triggered by WinClose input change"
466 this->bbHideWindow();
467 this->bbSetStatus(MODIFIED);
471 if ( ( bbBoxProcessModeIsReactive() ||
472 (c==bbGetInputConnectorMap().find("BoxExecute")->second))
475 bbtkDebugMessage("modified",2,
476 "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
477 this->bbSetStatus(MODIFIED);
478 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
481 else if ( bbGetStatus() == MODIFIED ) //! this->bbIsUptodate())
483 bbtkDebugMessage("modified",2,"-> Already modified"<<std::endl);
489 bbtkDebugMessage("modified",2,"-> Status set to modified"<<std::endl);
490 this->bbSetStatus(MODIFIED);
493 this->bbSignalOutputModification(false);
496 bbtkDebugMessageDec("process",5,
497 "<= BlackBox::bbSetModifiedStatus("<<c<<") ["
498 <<bbGetFullName()<<"]"<<std::endl);
501 //=========================================================================
503 //=========================================================================
504 void BlackBox::bbSignalOutputModification(bool reaction)
506 bbtkDebugMessageInc("process",5,
507 "=> BlackBox::bbSignalOutputModification() ["
508 <<bbGetFullName()<<"]"<<std::endl);
510 OutputConnectorMapType::iterator change = bbGetOutputConnectorMap().end();
511 OutputConnectorMapType::iterator i;
512 for ( i = bbGetOutputConnectorMap().begin();
513 i != bbGetOutputConnectorMap().end(); ++i) {
514 /* if ( i->first == "BoxChange" )
520 i->second->SetModifiedStatus();
522 // if (change != bbGetOutputConnectorMap().end())
523 // change->second->SetModifiedStatus();
525 if (reaction) bbGlobalProcessExecutionList();
527 bbtkDebugMessageDec("process",5,
528 "<= BlackBox::bbSignalOutputModification() ["
529 <<bbGetFullName()<<"]"<<std::endl);
532 //=========================================================================
533 //=========================================================================
534 void BlackBox::bbSignalOutputModification(const std::string& output,
537 bbtkDebugMessageInc("process",5,
538 "=> BlackBox::bbSignalOutputModification("
540 <<bbGetFullName()<<"]"<<std::endl);
542 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(output);
543 if ( i == bbGetOutputConnectorMap().end() )
545 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
547 i->second->SetModifiedStatus();
548 // Has to notify the output "BoxChange" also
549 if (output != "BoxChange")
551 i = bbGetOutputConnectorMap().find("BoxChange");
552 if ( i != bbGetOutputConnectorMap().end() )
554 i->second->SetModifiedStatus();
557 if (reaction) bbGlobalProcessExecutionList();
559 bbtkDebugMessageDec("process",5,
560 "<= BlackBox::bbSignalOutputModification("
562 <<bbGetFullName()<<"]"<<std::endl);
565 //=========================================================================
566 //=========================================================================
567 void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
570 bbtkDebugMessageInc("process",5,
571 "=> BlackBox::bbSignalOutputModification(vector of outputs) ["
572 <<bbGetFullName()<<"]"<<std::endl);
573 OutputConnectorMapType::iterator i;
574 std::vector<std::string>::const_iterator o;
575 for (o=output.begin();o!=output.end();++o)
577 // the output "BoxChange" must be signaled **AFTER** all others
578 if (*o == "BoxChange") continue;
579 // Look for the connector
580 i = bbGetOutputConnectorMap().find(*o);
581 if ( i == bbGetOutputConnectorMap().end() )
583 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
585 i->second->SetModifiedStatus();
587 // Has to notify the output "BoxChange" also
588 i = bbGetOutputConnectorMap().find("BoxChange");
589 if ( i != bbGetOutputConnectorMap().end() )
591 i->second->SetModifiedStatus();
593 if (reaction) bbGlobalProcessExecutionList();
595 bbtkDebugMessageDec("process",5,
596 "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
597 <<bbGetFullName()<<"]"<<std::endl);
600 //=========================================================================
602 //=========================================================================
603 /// Updates the BlackBox inputs
604 /// \returns UPTODATE if all inputs are in UPTODATE status after update
606 IOStatus BlackBox::bbUpdateInputs(bool excludeParent)
608 bbtkDebugMessageInc("process",4,
609 "=> BlackBox::bbUpdateInputs() ["
610 <<bbGetFullName()<<"]"
613 IOStatus s = UPTODATE;
615 InputConnectorMapType::iterator i;
616 for ( i = bbGetInputConnectorMap().begin();
617 i!= bbGetInputConnectorMap().end(); ++i)
619 if (excludeParent && (i->first=="WinParent")) continue;
620 if (i->first=="WinHide") continue;
621 // If input type is Void : no recurse
622 //if ( bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo()
626 IOStatus t = i->second->BackwardUpdate();
627 if (t==MODIFIED) s = MODIFIED;
630 bbtkDebugMessageDec("process",4,
631 "<= BlackBox::bbUpdateInputs() ["
632 <<bbGetFullName()<<"]"
638 //=========================================================================
641 //=========================================================================
642 /// Connects the input <name> to the connection c
643 void BlackBox::bbConnectInput( const std::string& name, Connection* c)
645 bbtkDebugMessage("connection",2,
646 "==> BlackBox::bbConnectInput(\""
647 <<name<<"\","<<c->GetFullName()<<") ["
648 <<bbGetFullName()<<"]"
652 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
653 if (i==bbGetInputConnectorMap().end())
655 bbtkError("no input called '"<<name<<"'");
657 i->second->SetConnection(c);
659 bbtkDebugMessage("connection",2,
660 "<== BlackBox::bbConnectInput(\""
661 <<name<<"\","<<c->GetFullName()<<") ["
662 <<bbGetFullName()<<"]"
664 // bbSetModifiedStatus();
667 //=========================================================================
670 //=========================================================================
671 /// Connects the output <name> to the connection c
672 void BlackBox::bbConnectOutput( const std::string& name, Connection* c)
674 bbtkDebugMessage("connection",2,
675 "==> BlackBox::bbConnectOutput(\""<<name<<"\","
676 <<c->GetFullName()<<") ["
677 <<bbGetFullName()<<"]"<<std::endl);
679 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
680 if (i==bbGetOutputConnectorMap().end())
682 bbtkError("no output called '"<<name<<"'");
684 i->second->SetConnection(c);
686 bbtkDebugMessage("connection",2,
687 "<== BlackBox::bbConnectOutput(\""<<name<<"\","
688 <<c->GetFullName()<<") ["
689 <<bbGetFullName()<<"]"<<std::endl);
692 //=========================================================================
695 //=========================================================================
696 /// Disconnects the input <name> from the connection c
697 void BlackBox::bbDisconnectInput( const std::string& name, Connection* c)
700 bbtkDebugMessage("connection",2,
701 "==> BlackBox::bbDisconnectInput(\""<<name
702 <<"\","<<c->GetFullName()<<") ["
703 <<bbGetFullName()<<"]"
709 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
713 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
714 if (i==bbGetInputConnectorMap().end())
716 bbtkError("no input called '"<<name<<"'");
718 i->second->UnsetConnection(c);
720 bbtkDebugMessage("connection",2,
721 "<== BlackBox::bbDisconnectInput(\""<<name
722 <<"\","<<c->GetFullName()<<") ["
723 <<bbGetFullName()<<"]"
727 //=========================================================================
730 //=========================================================================
731 /// Disconnects the output <name> from the connection c
732 void BlackBox::bbDisconnectOutput( const std::string& name, Connection* c)
734 bbtkDebugMessage("connection",2,
735 "==> BlackBox::bbDisconnectOutput(\""<<name
736 <<"\","<<c->GetFullName()<<") ["
737 <<bbGetFullName()<<"]"
742 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
746 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
747 if (i==bbGetOutputConnectorMap().end())
749 bbtkError("no output called '"<<name<<"'");
751 i->second->UnsetConnection(c);
753 bbtkDebugMessage("connection",2,
754 "<== BlackBox::bbDisconnectOutput(\""<<name
755 <<"\","<<c->GetFullName()<<") ["
756 <<bbGetFullName()<<"]"
759 //=========================================================================
762 //=========================================================================
764 void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
766 fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
768 //=========================================================================
771 //=========================================================================
772 std::string BlackBox::bbGetOutputAsString( const std::string &output )
775 // Looks for the adaptor
776 if (bbGetOutputType(output).name() != typeid(std::string).name() )
779 Package::Pointer p = bbGetDescriptor()->GetPackage();
780 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
782 Factory::Pointer f = p->GetFactorySet().begin()->lock();
787 bbGetOutputType(output),
790 } catch (bbtk::Exception e)
795 a->bbSetInput("In",bbGetOutput(output));
797 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
799 v="? (no adaptor found)";
804 v="? (no factory found)";
810 v = bbGetOutput(output).unsafe_get<std::string>() ;
814 //=========================================================================
816 //=========================================================================
817 std::string BlackBox::bbGetInputAsString( const std::string &input )
820 // Looks for the adaptor
821 if (bbGetInputType(input) != typeid(std::string))
824 Package::Pointer p = bbGetDescriptor()->GetPackage();
825 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
827 Factory::Pointer f = p->GetFactorySet().begin()->lock();
832 bbGetInputType(input),
835 }catch (bbtk::Exception e)
841 a->bbSetInput("In",bbGetInput(input));
843 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
847 v="? (no adaptor found)";
852 v="? (no factory found)";
857 v = bbGetInput(input).unsafe_get<std::string>() ;
861 //=======================================================================
863 //=======================================================================
864 // Replaces substrings "<" by "["
865 void SubsBrackets ( std::string& s )
867 // std::cout << "BEFORE=["<<s<<"]"<<std::endl;
869 std::string::size_type pos = 0;
872 while ( pos != std::string::npos )
874 // std::cout << "*** find one "<<std::endl;
875 s.replace(pos,1,cr,1);
876 pos = s.find(ss, pos);
882 while ( pos != std::string::npos )
884 // std::cout << "*** find one "<<std::endl;
885 s.replace(pos,1,cr,1);
886 pos = s.find(ss, pos);
892 while ( pos != std::string::npos )
894 // std::cout << "*** find one "<<std::endl;
895 s.replace(pos,1,cr,1);
896 pos = s.find(ss, pos);
897 } // std::cout << "AFTER=["<<s<<"]"<<std::endl;
899 //=======================================================================
901 //=========================================================================
902 /// Write Graphviz-dot description in file
903 void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
904 BlackBox::Pointer parentblackbox,
905 int detail, int level,
910 InputConnectorMapType::iterator i;
912 std::string labelStr;
913 std::string valueStr("");
916 labelStr = bbGetName() ;
918 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
920 labelStr = bbGetName();
921 labelStr = labelStr + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "] ";
924 SubsBrackets(labelStr);
927 labelStr = labelStr + " | {{ ";
928 std::string tempStrTypeName;
931 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
935 labelStr=labelStr+" | ";
938 if (instanceOrtype==true)
940 valueStr = this->bbGetInputAsString(i->first) + " = ";
942 const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
943 tempStrTypeName=id->GetTypeName();
944 SubsBrackets(tempStrTypeName);
945 std::string Name(i->first);
947 labelStr=labelStr + "<"+i->first.c_str()+"> " + valueStr + Name.c_str() + " [" + tempStrTypeName.c_str() + "]";
949 labelStr=labelStr+ " } | {";
951 OutputConnectorMapType::iterator ii;
952 for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii )
956 labelStr=labelStr+" | ";
959 if (instanceOrtype==true)
961 valueStr = this->bbGetOutputAsString(ii->first) + " = ";
963 const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first);
964 tempStrTypeName=id->GetTypeName();
965 SubsBrackets(tempStrTypeName);
966 std::string Name(ii->first);
968 labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + " ["+tempStrTypeName+"]";
970 labelStr = labelStr+ " } }" ;
974 bbWriteDotInputOutputName(ff,true,detail,level);
975 std::string tmp ( bbGetTypeName() );
979 url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
981 url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
983 fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
984 // std::cout << labelStr << std::endl;
987 if (GetThisPointer<BlackBox>()!=parentblackbox){
988 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
992 Connection* con = i->second->GetConnection();
994 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
995 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
997 a->bbWriteDotInputOutputName(ff,false,detail,level);
1000 fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
1003 b->bbWriteDotInputOutputName(ff,true,detail,level);
1006 fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
1008 fprintf(ff,"%s\n",";");
1012 } // if parentblackbox
1014 //=========================================================================
1019 //=========================================================================
1020 void BlackBox::bbShowRelations(BlackBox::Pointer parentblackbox,
1021 int detail, int level
1022 /*,Factory *factory*/ )
1025 if (this->bbGetDescriptor()->GetPackage())
1027 bbtkMessage("Help",1,"Black Box '"<<bbGetName()<<"' <"<<
1028 this->bbGetDescriptor()->GetPackage()->GetName()
1029 <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1033 bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1035 // bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
1036 // bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
1038 std::vector<std::string> iname;
1039 std::vector<std::string> ivalue;
1040 std::vector<std::string> iconn;
1042 InputConnectorMapType::iterator i;
1043 unsigned int namelmax = 0;
1044 unsigned int valuelmax = 0;
1045 // unsigned int connlmax = 0;
1046 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1048 iname.push_back(i->first);
1049 if (iname.back().size()>namelmax) namelmax = iname.back().size();
1050 ivalue.push_back(bbGetInputAsString(i->first));
1051 if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
1053 Connection* con = i->second->GetConnection();
1055 s = con->GetOriginalBlackBoxFrom()->bbGetName();
1057 s += con->GetOriginalBlackBoxFromOutput();
1061 OutputConnectorMapType::iterator o;
1062 std::vector<std::string> oname;
1063 std::vector<std::string> ovalue;
1064 std::vector<std::vector<std::string> > oconn;
1065 for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o )
1067 oname.push_back(o->first);
1068 if (oname.back().size()>namelmax) namelmax = oname.back().size();
1069 ovalue.push_back(bbGetOutputAsString(o->first));
1070 if (ovalue.back().size()>valuelmax) valuelmax = ovalue.back().size();
1071 std::vector<std::string> ss;
1072 const std::vector<Connection*>& con
1073 = o->second->GetConnectionVector();
1074 std::vector<Connection*>::const_iterator c;
1075 for (c=con.begin();c!=con.end();++c)
1078 s = (*c)->GetOriginalBlackBoxTo()->bbGetName();
1080 s += (*c)->GetOriginalBlackBoxToInput();
1083 oconn.push_back(ss);
1087 bbtkMessage("Help",1," * Inputs : "<<std::endl);
1089 bbtkMessage("Help",1," * No inputs"<<std::endl);
1091 std::vector<std::string>::iterator i1,i2,i3;
1092 for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin();
1093 i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end();
1096 std::string name(*i1);
1098 name.append(1+namelmax-name.size(),' ');
1099 std::string value(*i2);
1101 value.append(1+valuelmax-value.size(),' ');
1103 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<" <-- '"<<*i3<<"'"<<std::endl);
1105 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<std::endl);
1109 bbtkMessage("Help",1," * Outputs : "<<std::endl);
1111 bbtkMessage("Help",1," * No outputs"<<std::endl);
1113 std::vector<std::vector<std::string> >::iterator i4;
1115 for (i1=oname.begin(),i2=ovalue.begin(),i4=oconn.begin();
1116 i1!=oname.end(),i2!=ovalue.end(),i4!=oconn.end();
1119 std::string name(*i1);
1121 name.append(1+namelmax-name.size(),' ');
1122 std::string value(*i2);
1124 value.append(1+valuelmax-value.size(),' ');
1126 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<std::endl);
1129 std::string pref = " '"+name+" = '"+value;
1130 for (i3=i4->begin();i3!=i4->end();++i3)
1132 bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'"<<std::endl);
1133 pref.replace(0,pref.size(),pref.size(),' ');
1139 //=========================================================================
1141 static bool bbmgGlobalProcessingExecutionList = false;
1143 //=========================================================================
1144 void BlackBox::bbGlobalProcessExecutionList()
1146 bbtkDebugMessageInc("process",3,
1147 "=> BlackBox::bbGlobalProcessExecutionList()"
1149 if (bbmgGlobalProcessingExecutionList)
1151 bbtkDebugMessage("process",3,"BlackBox::bbGlobalProcessExecutionList() reentered !");
1154 bbmgGlobalProcessingExecutionList = true;
1156 std::set<BlackBox::WeakPointer>::iterator i;
1157 while (bbmgExecutionList.size()>0)
1159 i = bbmgExecutionList.begin();
1160 BlackBox::WeakPointer p = *i;
1161 bbmgExecutionList.erase(i);
1164 bbtkDebugMessage("process",4,
1166 p.lock()->bbGetFullName()<<std::endl);
1167 p.lock()->bbExecute(true);
1171 bbtkGlobalError("Strange error in BlackBox::bbGlobalProcessExecutionList() : Weak bb pointer in bbmgExecutionList is no more valid...");
1175 bbmgExecutionList.clear();
1176 bbtkDebugMessageDec("process",3,
1177 "<= BlackBox::bbGlobalProcessExecutionList()"
1180 bbmgGlobalProcessingExecutionList = false;
1183 //=========================================================================
1185 bool BlackBox::bbGlobalGetSomeBoxExecuting()
1187 return bbmgSomeBoxExecuting;
1190 void BlackBox::bbGlobalSetSomeBoxExecuting(bool b)
1192 bbmgSomeBoxExecuting = b;
1195 void BlackBox::bbGlobalSetFreezeExecution(bool b)
1197 bbmgFreezeExecution = b;
1200 bool BlackBox::bbGlobalGetFreezeExecution()
1202 return bbmgFreezeExecution;
1205 void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
1207 bbtkDebugMessage("process",3,"* bbGlobalAddToExecutionList("<<b->bbGetFullName()<<")"<<std::endl);
1208 if (bbmgGlobalProcessingExecutionList)
1210 bbtkDebugMessage("process",3,"bbGlobalAddToExecutionList called inside bbGlobalProcessExecutionList !");
1212 bbmgExecutionList.insert(b);
1216 //=========================================================================
1218 //=========================================================================
1219 void BlackBox::Check(bool recursive)
1221 bbtkMessage("debug",1,"*** Checking Black Box "<<(void*)this<<" ["<<bbGetFullName()
1222 <<"] ... OK"<<std::endl);
1224 //=========================================================================
1226 void BlackBox::bbUserOnShowWidget(std::string nameInput)
1228 bbtk::BlackBoxInputConnector *cc;
1229 cc = this->bbGetInputConnectorMap().find( nameInput.c_str() )->second;
1230 if (cc->GetConnection()!=NULL)
1232 cc->GetConnection()->GetBlackBoxFrom()->bbUserOnShow();
1238 } // EO namespace bbtk