1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBox.cxx,v $
5 Date: $Date: 2008/11/13 10:37:27 $
6 Version: $Revision: 1.27 $
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)
107 bbmBoxProcessMode("Pipeline"),
111 bbtkDebugMessage("object",4,"==> BlackBox::BlackBox(\""
112 <<name<<"\")"<<std::endl);
113 bbtkDebugMessage("object",4,"<== BlackBox::BlackBox(\""
114 <<name<<"\")"<<std::endl);
116 //=========================================================================
118 //=========================================================================
119 BlackBox::BlackBox(const BlackBox&)
122 //=========================================================================
123 BlackBox::BlackBox(BlackBox& from, const std::string &name)
125 bbmStatus(from.bbmStatus),
127 bbmBoxProcessMode(from.bbmBoxProcessMode),
131 bbtkDebugMessage("object",4,"==> BlackBox::BlackBox("
132 <<from.bbGetFullName()<<",\""
133 <<name<<"\")"<<std::endl);
134 bbtkDebugMessage("object",4,"<== BlackBox::BlackBox("
135 <<from.bbGetFullName()<<",\""
136 <<name<<"\")"<<std::endl);
138 //=========================================================================
141 //=========================================================================
142 BlackBox::~BlackBox()
144 bbtkDebugMessage("object",4,"==> BlackBox::~BlackBox() ["<<bbmName
146 this->bbDesallocateConnectors();
147 bbtkDebugMessage("object",4,"<== BlackBox::~BlackBox() ["<<bbmName
150 //=========================================================================
153 //=========================================================================
154 /// Main processing method of the box.
155 void BlackBox::bbExecute(bool force)
157 bbtkDebugMessageInc("process",2,
158 "=> BlackBox::bbExecute() ["
159 <<bbGetFullName()<<"]"<<std::endl);
162 // If execution frozen : return
163 if (bbGlobalGetFreezeExecution())
165 bbtkDebugMessage("process",2,
166 " -> FreezeExecution global flag is 'true' : abort execution"<<std::endl);
171 // If force is true then update is triggered even if the box is UPTODATE
172 if (force) bbSetModifiedStatus();
174 // Calls the main recursive update method
175 bbBackwardUpdate(Connection::Pointer());
177 bbtkDebugMessageDec("process",2,
178 "<= BlackBox::bbExecute() ["
179 <<bbGetFullName()<<"]"<<std::endl);
181 //=========================================================================
183 //=========================================================================
184 std::string BlackBox::bbGetFullName() const
186 return this->bbGetNameWithParent()+"<"+this->bbGetDescriptor()->GetTypeName()+">";
188 //=========================================================================
192 //=========================================================================
193 /// Returns the name with the name of the parent prepended if any
194 std::string BlackBox::bbGetNameWithParent() const
196 if (bbmParent.lock())
198 return bbmParent.lock()->bbGetNameWithParent() + ":" + bbmName;
205 //=========================================================================
207 //=========================================================================
208 /// Prints the Help on the BlackBox type
209 void BlackBox::bbGetHelp(bool full) const
211 bbGetDescriptor()->GetHelp(full);
213 //=========================================================================
216 //=========================================================================
217 /// Returns true if the UserBlackBox has an input of name name
218 bool BlackBox::bbHasInput(const std::string& name) const
220 bbtkDebugMessageInc("Kernel",8,
221 "BlackBox::bbHasInput(\""
222 <<name<<"\") ["<<bbGetFullName()<<"]"
224 bool r = ( bbGetDescriptor()->GetInputDescriptorMap().find(name)
225 != bbGetDescriptor()->GetInputDescriptorMap().end());
226 bbtkDebugDecTab("Kernel",8);
229 //=========================================================================
232 //=========================================================================
233 /// Returns true if the UserBlackBox has an output of name name
234 bool BlackBox::bbHasOutput(const std::string& name) const
236 bbtkDebugMessageInc("Kernel",8,"BlackBox::bbHasOutput(\""
237 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
238 bool r = ( bbGetDescriptor()->GetOutputDescriptorMap().find(name)
239 != bbGetDescriptor()->GetOutputDescriptorMap().end());
240 bbtkDebugDecTab("Kernel",8);
243 //=========================================================================
246 //=========================================================================
247 /// Gets the output type of a given name
248 TypeInfo BlackBox::bbGetOutputType( const std::string &name ) const
250 bbtkDebugMessageInc("Kernel",8,
251 "BlackBox::bbGetOutputType(\""
252 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
253 TypeInfo r = bbGetDescriptor()->GetOutputDescriptor(name)->GetTypeInfo();
254 bbtkDebugDecTab("Kernel",8);
257 //=========================================================================
259 //=========================================================================
260 /// Gets the input type of a given name
261 TypeInfo BlackBox::bbGetInputType( const std::string &name ) const
263 bbtkDebugMessageInc("Kernel",8,
264 "BlackBox::bbGetInputType(\""
265 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
266 TypeInfo r = bbGetDescriptor()->GetInputDescriptor(name)->GetTypeInfo();
267 bbtkDebugDecTab("Kernel",8);
270 //=========================================================================
273 //=========================================================================
274 /// Allocates the i/o connectors of the black box
275 void BlackBox::bbAllocateConnectors()
277 bbtkDebugMessageInc("Kernel",8,
278 "BlackBox::bbAllocateConnectors() ["
279 <<bbGetFullName()<<"]"
281 const BlackBoxDescriptor::InputDescriptorMapType& imap
282 = bbGetDescriptor()->GetInputDescriptorMap();
283 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
284 for ( i = imap.begin(); i != imap.end(); ++i )
286 bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
287 bbGetInputConnectorMap()[i->second->GetName()]
288 = new BlackBoxInputConnector(GetThisPointer<BlackBox>());
290 const BlackBoxDescriptor::OutputDescriptorMapType& omap
291 = bbGetDescriptor()->GetOutputDescriptorMap();
292 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
293 for ( o = omap.begin(); o != omap.end(); ++o )
295 bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
296 bbGetOutputConnectorMap()[o->second->GetName()]
297 = new BlackBoxOutputConnector();
299 bbtkDebugDecTab("Kernel",8);
301 //=========================================================================
304 //=========================================================================
305 /// Desallocates the i/o connectors of the black box
306 void BlackBox::bbDesallocateConnectors()
308 bbtkDebugMessageInc("Kernel",8,
309 "BlackBox::bbDesallocateConnectors()"
312 InputConnectorMapType::const_iterator i;
313 for ( i = bbGetInputConnectorMap().begin();
314 i != bbGetInputConnectorMap().end(); ++i )
316 bbtkDebugMessage("Kernel",8,"* Delete \""<<i->first<<"\""<<std::endl);
319 OutputConnectorMapType::const_iterator o;
320 for ( o = bbGetOutputConnectorMap().begin();
321 o != bbGetOutputConnectorMap().end(); ++o )
323 bbtkDebugMessage("Kernel",8,"* Delete \""<<o->first<<"\""<<std::endl);
327 bbtkDebugDecTab("Kernel",8);
330 //=========================================================================
333 //=========================================================================
334 /// Copies the input / output values from another box
335 void BlackBox::bbCopyIOValues(BlackBox& from)
337 bbtkDebugMessageInc("Kernel",9,
338 "BlackBox::bbCopyIOValues("
339 <<from.bbGetFullName()<<") ["
340 <<bbGetFullName()<<"]"<<std::endl);
341 // copies the input values
342 const BlackBoxDescriptor::InputDescriptorMapType& imap
343 = bbGetDescriptor()->GetInputDescriptorMap();
344 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
345 for ( i = imap.begin(); i != imap.end(); ++i )
347 if (! i->second->GetCopyConstruct() ) continue;
348 std::string input = i->second->GetName();
349 this->bbSetInput(input, from.bbGetInput(input) );
351 // copies the output values
352 const BlackBoxDescriptor::OutputDescriptorMapType& omap
353 = bbGetDescriptor()->GetOutputDescriptorMap();
354 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
355 for ( o = omap.begin(); o != omap.end(); ++o )
357 if (! o->second->GetCopyConstruct() ) continue;
358 std::string output = o->second->GetName();
359 this->bbSetOutput(output, from.bbGetOutput(output) );
362 bbtkDebugDecTab("Kernel",9);
365 //=========================================================================
369 //=========================================================================
370 bool BlackBox::bbCanReact() const
372 return ( bbGlobalGetSomeBoxExecuting()
373 #ifdef _USE_WXWIDGETS_
374 || Wx::IsSomeWindowAlive()
378 //=========================================================================
382 //=========================================================================
383 /// User overloadable destruction method of a black box
384 void BlackBox::bbUserDelete()
386 bbtkDebugMessage("process",5,
387 "=> BlackBox::bbUserDelete() ["
388 <<bbGetFullName()<<"]"
389 <<" : not overloaded; using standard deletion"
393 //=========================================================================
396 //=========================================================================
397 BlackBox::BoxProcessModeValue BlackBox::bbGetBoxProcessModeValue() const
399 const std::string& p = bbmBoxProcessMode;
401 (p == "P") || (p == "p") ||
402 (p == "Pipeline") || (p == "pipeline") ) return Pipeline;
404 (p == "A") || (p == "a") ||
405 (p == "Always") || (p == "always") ) return Always;
407 (p == "R") || (p == "r") ||
408 (p == "Reactive") || (p == "reactive") ) return Reactive;
409 bbtkError(bbGetFullName()<<" : BoxProcessMode value '"<<p
410 <<"' unknown. Possible values : "
411 <<"'0'/'P'/'p'/'Pipeline'/'pipeline' | "
412 <<"'1'/'A'/'a'/'Always'/'always' | "
413 <<"'2'/'R'/'r'/'Reactive'/'reactive'"<<std::endl);
415 //=========================================================================
417 //=========================================================================
418 bool BlackBox::bbBoxProcessModeIsReactive() const
420 return (bbGetBoxProcessModeValue() == Reactive);
422 //=========================================================================
424 //=========================================================================
425 bool BlackBox::bbBoxProcessModeIsAlways() const
427 return (bbGetBoxProcessModeValue() == Always);
429 //=========================================================================
431 //=========================================================================
432 /// Signals that the BlackBox has been modified
433 void BlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
435 bbtkDebugMessage("modified",1,
436 "==> BlackBox::bbSetModifiedStatus("<<c<<") ["
437 <<bbGetFullName()<<"]"<<std::endl);
439 if ( (c==bbGetInputConnectorMap().find("WinHide")->second) )
440 // && (bbCanReact()))
442 bbtkDebugMessage("modified",2,
443 "-> Hide triggered by WinHide input change"
445 this->bbHideWindow();
446 this->bbSetStatus(MODIFIED);
449 if ( (c==bbGetInputConnectorMap().find("WinClose")->second) )
450 // && (bbCanReact()))
452 bbtkDebugMessage("modified",2,
453 "-> Close triggered by WinClose input change"
455 this->bbHideWindow();
456 this->bbSetStatus(MODIFIED);
460 if ( ( bbBoxProcessModeIsReactive() ||
461 (c==bbGetInputConnectorMap().find("BoxExecute")->second))
464 bbtkDebugMessage("modified",2,
465 "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
466 this->bbSetStatus(MODIFIED);
467 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
470 else if ( bbGetStatus() == MODIFIED ) //! this->bbIsUptodate())
472 bbtkDebugMessage("modified",2,"-> Already modified"<<std::endl);
478 bbtkDebugMessage("modified",2,"-> Status set to modified"<<std::endl);
479 this->bbSetStatus(MODIFIED);
482 this->bbSignalOutputModification(false);
485 bbtkDebugMessageDec("process",5,
486 "<= BlackBox::bbSetModifiedStatus("<<c<<") ["
487 <<bbGetFullName()<<"]"<<std::endl);
490 //=========================================================================
492 //=========================================================================
493 void BlackBox::bbSignalOutputModification(bool reaction)
495 bbtkDebugMessageInc("process",5,
496 "=> BlackBox::bbSignalOutputModification() ["
497 <<bbGetFullName()<<"]"<<std::endl);
499 OutputConnectorMapType::iterator change = bbGetOutputConnectorMap().end();
500 OutputConnectorMapType::iterator i;
501 for ( i = bbGetOutputConnectorMap().begin();
502 i != bbGetOutputConnectorMap().end(); ++i) {
503 /* if ( i->first == "BoxChange" )
509 i->second->SetModifiedStatus();
511 // if (change != bbGetOutputConnectorMap().end())
512 // change->second->SetModifiedStatus();
514 if (reaction) bbGlobalProcessExecutionList();
516 bbtkDebugMessageDec("process",5,
517 "<= BlackBox::bbSignalOutputModification() ["
518 <<bbGetFullName()<<"]"<<std::endl);
521 //=========================================================================
522 //=========================================================================
523 void BlackBox::bbSignalOutputModification(const std::string& output,
526 bbtkDebugMessageInc("process",5,
527 "=> BlackBox::bbSignalOutputModification("
529 <<bbGetFullName()<<"]"<<std::endl);
531 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(output);
532 if ( i == bbGetOutputConnectorMap().end() )
534 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
536 i->second->SetModifiedStatus();
537 // Has to notify the output "BoxChange" also
538 if (output != "BoxChange")
540 i = bbGetOutputConnectorMap().find("BoxChange");
541 if ( i != bbGetOutputConnectorMap().end() )
543 i->second->SetModifiedStatus();
546 if (reaction) bbGlobalProcessExecutionList();
548 bbtkDebugMessageDec("process",5,
549 "<= BlackBox::bbSignalOutputModification("
551 <<bbGetFullName()<<"]"<<std::endl);
554 //=========================================================================
555 //=========================================================================
556 void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
559 bbtkDebugMessageInc("process",5,
560 "=> BlackBox::bbSignalOutputModification(vector of outputs) ["
561 <<bbGetFullName()<<"]"<<std::endl);
562 OutputConnectorMapType::iterator i;
563 std::vector<std::string>::const_iterator o;
564 for (o=output.begin();o!=output.end();++o)
566 // the output "BoxChange" must be signaled **AFTER** all others
567 if (*o == "BoxChange") continue;
568 // Look for the connector
569 i = bbGetOutputConnectorMap().find(*o);
570 if ( i == bbGetOutputConnectorMap().end() )
572 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
574 i->second->SetModifiedStatus();
576 // Has to notify the output "BoxChange" also
577 i = bbGetOutputConnectorMap().find("BoxChange");
578 if ( i != bbGetOutputConnectorMap().end() )
580 i->second->SetModifiedStatus();
582 if (reaction) bbGlobalProcessExecutionList();
584 bbtkDebugMessageDec("process",5,
585 "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
586 <<bbGetFullName()<<"]"<<std::endl);
589 //=========================================================================
591 //=========================================================================
592 /// Updates the BlackBox inputs
593 /// \returns UPTODATE if all inputs are in UPTODATE status after update
595 IOStatus BlackBox::bbUpdateInputs(bool excludeParent)
597 bbtkDebugMessageInc("process",4,
598 "=> BlackBox::bbUpdateInputs() ["
599 <<bbGetFullName()<<"]"
602 IOStatus s = UPTODATE;
604 InputConnectorMapType::iterator i;
605 for ( i = bbGetInputConnectorMap().begin();
606 i!= bbGetInputConnectorMap().end(); ++i)
608 if (excludeParent && (i->first=="WinParent")) continue;
609 if (i->first=="WinHide") continue;
610 // If input type is Void : no recurse
611 //if ( bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo()
615 IOStatus t = i->second->BackwardUpdate();
616 if (t==MODIFIED) s = MODIFIED;
619 bbtkDebugMessageDec("process",4,
620 "<= BlackBox::bbUpdateInputs() ["
621 <<bbGetFullName()<<"]"
627 //=========================================================================
630 //=========================================================================
631 /// Connects the input <name> to the connection c
632 void BlackBox::bbConnectInput( const std::string& name, Connection* c)
634 bbtkDebugMessage("connection",2,
635 "==> BlackBox::bbConnectInput(\""
636 <<name<<"\","<<c->GetFullName()<<") ["
637 <<bbGetFullName()<<"]"
641 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
642 if (i==bbGetInputConnectorMap().end())
644 bbtkError("no input called '"<<name<<"'");
646 i->second->SetConnection(c);
648 bbtkDebugMessage("connection",2,
649 "<== BlackBox::bbConnectInput(\""
650 <<name<<"\","<<c->GetFullName()<<") ["
651 <<bbGetFullName()<<"]"
653 // bbSetModifiedStatus();
656 //=========================================================================
659 //=========================================================================
660 /// Connects the output <name> to the connection c
661 void BlackBox::bbConnectOutput( const std::string& name, Connection* c)
663 bbtkDebugMessage("connection",2,
664 "==> BlackBox::bbConnectOutput(\""<<name<<"\","
665 <<c->GetFullName()<<") ["
666 <<bbGetFullName()<<"]"<<std::endl);
668 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
669 if (i==bbGetOutputConnectorMap().end())
671 bbtkError("no output called '"<<name<<"'");
673 i->second->SetConnection(c);
675 bbtkDebugMessage("connection",2,
676 "<== BlackBox::bbConnectOutput(\""<<name<<"\","
677 <<c->GetFullName()<<") ["
678 <<bbGetFullName()<<"]"<<std::endl);
681 //=========================================================================
684 //=========================================================================
685 /// Disconnects the input <name> from the connection c
686 void BlackBox::bbDisconnectInput( const std::string& name, Connection* c)
689 bbtkDebugMessage("connection",2,
690 "==> BlackBox::bbDisconnectInput(\""<<name
691 <<"\","<<c->GetFullName()<<") ["
692 <<bbGetFullName()<<"]"
698 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
702 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
703 if (i==bbGetInputConnectorMap().end())
705 bbtkError("no input called '"<<name<<"'");
707 i->second->UnsetConnection(c);
709 bbtkDebugMessage("connection",2,
710 "<== BlackBox::bbDisconnectInput(\""<<name
711 <<"\","<<c->GetFullName()<<") ["
712 <<bbGetFullName()<<"]"
716 //=========================================================================
719 //=========================================================================
720 /// Disconnects the output <name> from the connection c
721 void BlackBox::bbDisconnectOutput( const std::string& name, Connection* c)
723 bbtkDebugMessage("connection",2,
724 "==> BlackBox::bbDisconnectOutput(\""<<name
725 <<"\","<<c->GetFullName()<<") ["
726 <<bbGetFullName()<<"]"
731 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
735 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
736 if (i==bbGetOutputConnectorMap().end())
738 bbtkError("no output called '"<<name<<"'");
740 i->second->UnsetConnection(c);
742 bbtkDebugMessage("connection",2,
743 "<== BlackBox::bbDisconnectOutput(\""<<name
744 <<"\","<<c->GetFullName()<<") ["
745 <<bbGetFullName()<<"]"
748 //=========================================================================
751 //=========================================================================
753 void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
755 fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
757 //=========================================================================
760 //=========================================================================
761 std::string BlackBox::bbGetOutputAsString( const std::string &output )
764 // Looks for the adaptor
765 if (bbGetOutputType(output).name() != typeid(std::string).name() )
768 Package::Pointer p = bbGetDescriptor()->GetPackage();
769 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
771 Factory::Pointer f = p->GetFactorySet().begin()->lock();
776 bbGetOutputType(output),
779 } catch (bbtk::Exception e)
784 a->bbSetInput("In",bbGetOutput(output));
786 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
788 v="? (no adaptor found)";
793 v="? (no factory found)";
799 v = bbGetOutput(output).unsafe_get<std::string>() ;
803 //=========================================================================
805 //=========================================================================
806 std::string BlackBox::bbGetInputAsString( const std::string &input )
809 // Looks for the adaptor
810 if (bbGetInputType(input) != typeid(std::string))
813 Package::Pointer p = bbGetDescriptor()->GetPackage();
814 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
816 Factory::Pointer f = p->GetFactorySet().begin()->lock();
821 bbGetInputType(input),
824 }catch (bbtk::Exception e)
830 a->bbSetInput("In",bbGetInput(input));
832 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
836 v="? (no adaptor found)";
841 v="? (no factory found)";
846 v = bbGetInput(input).unsafe_get<std::string>() ;
850 //=======================================================================
852 //=======================================================================
853 // Replaces substrings "<" by "["
854 void SubsBrackets ( std::string& s )
856 // std::cout << "BEFORE=["<<s<<"]"<<std::endl;
858 std::string::size_type pos = 0;
861 while ( pos != std::string::npos )
863 // std::cout << "*** find one "<<std::endl;
864 s.replace(pos,1,cr,1);
865 pos = s.find(ss, pos);
871 while ( pos != std::string::npos )
873 // std::cout << "*** find one "<<std::endl;
874 s.replace(pos,1,cr,1);
875 pos = s.find(ss, pos);
881 while ( pos != std::string::npos )
883 // std::cout << "*** find one "<<std::endl;
884 s.replace(pos,1,cr,1);
885 pos = s.find(ss, pos);
886 } // std::cout << "AFTER=["<<s<<"]"<<std::endl;
888 //=======================================================================
890 //=========================================================================
891 /// Write Graphviz-dot description in file
892 void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
893 BlackBox::Pointer parentblackbox,
894 int detail, int level,
899 InputConnectorMapType::iterator i;
901 std::string labelStr;
902 std::string valueStr("");
905 labelStr = bbGetName() ;
907 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
909 labelStr = bbGetName();
910 labelStr = labelStr + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "] ";
913 SubsBrackets(labelStr);
916 labelStr = labelStr + " | {{ ";
917 std::string tempStrTypeName;
920 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
924 labelStr=labelStr+" | ";
927 if (instanceOrtype==true)
929 valueStr = this->bbGetInputAsString(i->first) + " = ";
931 const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
932 tempStrTypeName=id->GetTypeName();
933 SubsBrackets(tempStrTypeName);
934 std::string Name(i->first);
936 labelStr=labelStr + "<"+i->first.c_str()+"> " + valueStr + Name.c_str() + " [" + tempStrTypeName.c_str() + "]";
938 labelStr=labelStr+ " } | {";
940 OutputConnectorMapType::iterator ii;
941 for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii )
945 labelStr=labelStr+" | ";
948 if (instanceOrtype==true)
950 valueStr = this->bbGetOutputAsString(ii->first) + " = ";
952 const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first);
953 tempStrTypeName=id->GetTypeName();
954 SubsBrackets(tempStrTypeName);
955 std::string Name(ii->first);
957 labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + " ["+tempStrTypeName+"]";
959 labelStr = labelStr+ " } }" ;
963 bbWriteDotInputOutputName(ff,true,detail,level);
964 std::string tmp ( bbGetTypeName() );
968 url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
970 url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
972 fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
973 // std::cout << labelStr << std::endl;
976 if (GetThisPointer<BlackBox>()!=parentblackbox){
977 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
981 Connection* con = i->second->GetConnection();
983 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
984 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
986 a->bbWriteDotInputOutputName(ff,false,detail,level);
989 fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
992 b->bbWriteDotInputOutputName(ff,true,detail,level);
995 fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
997 fprintf(ff,"%s\n",";");
1001 } // if parentblackbox
1003 //=========================================================================
1008 //=========================================================================
1009 void BlackBox::bbShowRelations(BlackBox::Pointer parentblackbox,
1010 int detail, int level
1011 /*,Factory *factory*/ )
1014 if (this->bbGetDescriptor()->GetPackage())
1016 bbtkMessage("Help",1,"Black Box '"<<bbGetName()<<"' <"<<
1017 this->bbGetDescriptor()->GetPackage()->GetName()
1018 <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1022 bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1024 // bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
1025 // bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
1027 std::vector<std::string> iname;
1028 std::vector<std::string> ivalue;
1029 std::vector<std::string> iconn;
1031 InputConnectorMapType::iterator i;
1032 unsigned int namelmax = 0;
1033 unsigned int valuelmax = 0;
1034 // unsigned int connlmax = 0;
1035 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1037 iname.push_back(i->first);
1038 if (iname.back().size()>namelmax) namelmax = iname.back().size();
1039 ivalue.push_back(bbGetInputAsString(i->first));
1040 if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
1042 Connection* con = i->second->GetConnection();
1044 s = con->GetOriginalBlackBoxFrom()->bbGetName();
1046 s += con->GetOriginalBlackBoxFromOutput();
1050 OutputConnectorMapType::iterator o;
1051 std::vector<std::string> oname;
1052 std::vector<std::string> ovalue;
1053 std::vector<std::vector<std::string> > oconn;
1054 for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o )
1056 oname.push_back(o->first);
1057 if (oname.back().size()>namelmax) namelmax = oname.back().size();
1058 ovalue.push_back(bbGetOutputAsString(o->first));
1059 if (ovalue.back().size()>valuelmax) valuelmax = ovalue.back().size();
1060 std::vector<std::string> ss;
1061 const std::vector<Connection*>& con
1062 = o->second->GetConnectionVector();
1063 std::vector<Connection*>::const_iterator c;
1064 for (c=con.begin();c!=con.end();++c)
1067 s = (*c)->GetOriginalBlackBoxTo()->bbGetName();
1069 s += (*c)->GetOriginalBlackBoxToInput();
1072 oconn.push_back(ss);
1076 bbtkMessage("Help",1," * Inputs : "<<std::endl);
1078 bbtkMessage("Help",1," * No inputs"<<std::endl);
1080 std::vector<std::string>::iterator i1,i2,i3;
1081 for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin();
1082 i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end();
1085 std::string name(*i1);
1087 name.append(1+namelmax-name.size(),' ');
1088 std::string value(*i2);
1090 value.append(1+valuelmax-value.size(),' ');
1092 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<" <-- '"<<*i3<<"'"<<std::endl);
1094 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<std::endl);
1098 bbtkMessage("Help",1," * Outputs : "<<std::endl);
1100 bbtkMessage("Help",1," * No outputs"<<std::endl);
1102 std::vector<std::vector<std::string> >::iterator i4;
1104 for (i1=oname.begin(),i2=ovalue.begin(),i4=oconn.begin();
1105 i1!=oname.end(),i2!=ovalue.end(),i4!=oconn.end();
1108 std::string name(*i1);
1110 name.append(1+namelmax-name.size(),' ');
1111 std::string value(*i2);
1113 value.append(1+valuelmax-value.size(),' ');
1115 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<std::endl);
1118 std::string pref = " '"+name+" = '"+value;
1119 for (i3=i4->begin();i3!=i4->end();++i3)
1121 bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'"<<std::endl);
1122 pref.replace(0,pref.size(),pref.size(),' ');
1128 //=========================================================================
1130 static bool bbmgGlobalProcessingExecutionList = false;
1132 //=========================================================================
1133 void BlackBox::bbGlobalProcessExecutionList()
1135 bbtkDebugMessageInc("process",3,
1136 "=> BlackBox::bbGlobalProcessExecutionList()"
1138 if (bbmgGlobalProcessingExecutionList)
1140 bbtkDebugMessage("process",3,"BlackBox::bbGlobalProcessExecutionList() reentered !");
1143 bbmgGlobalProcessingExecutionList = true;
1145 std::set<BlackBox::WeakPointer>::iterator i;
1146 while (bbmgExecutionList.size()>0)
1148 i = bbmgExecutionList.begin();
1149 BlackBox::WeakPointer p = *i;
1150 bbmgExecutionList.erase(i);
1153 bbtkDebugMessage("process",4,
1155 p.lock()->bbGetFullName()<<std::endl);
1156 p.lock()->bbExecute(true);
1160 bbtkGlobalError("Strange error in BlackBox::bbGlobalProcessExecutionList() : Weak bb pointer in bbmgExecutionList is no more valid...");
1164 bbmgExecutionList.clear();
1165 bbtkDebugMessageDec("process",3,
1166 "<= BlackBox::bbGlobalProcessExecutionList()"
1169 bbmgGlobalProcessingExecutionList = false;
1172 //=========================================================================
1174 bool BlackBox::bbGlobalGetSomeBoxExecuting()
1176 return bbmgSomeBoxExecuting;
1179 void BlackBox::bbGlobalSetSomeBoxExecuting(bool b)
1181 bbmgSomeBoxExecuting = b;
1184 void BlackBox::bbGlobalSetFreezeExecution(bool b)
1186 bbmgFreezeExecution = b;
1189 bool BlackBox::bbGlobalGetFreezeExecution()
1191 return bbmgFreezeExecution;
1194 void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
1196 bbtkDebugMessage("process",3,"* bbGlobalAddToExecutionList("<<b->bbGetFullName()<<")"<<std::endl);
1197 if (bbmgGlobalProcessingExecutionList)
1199 bbtkDebugMessage("process",3,"bbGlobalAddToExecutionList called inside bbGlobalProcessExecutionList !");
1201 bbmgExecutionList.insert(b);
1205 //=========================================================================
1207 //=========================================================================
1208 void BlackBox::Check(bool recursive)
1210 bbtkMessage("debug",1,"*** Checking Black Box "<<(void*)this<<" ["<<bbGetFullName()
1211 <<"] ... OK"<<std::endl);
1213 //=========================================================================
1215 void BlackBox::bbUserOnShowWidget(std::string nameInput)
1217 bbtk::BlackBoxInputConnector *cc;
1218 cc = this->bbGetInputConnectorMap().find( nameInput.c_str() )->second;
1219 if (cc->GetConnection()!=NULL)
1221 cc->GetConnection()->GetBlackBoxFrom()->bbUserOnShow();
1227 } // EO namespace bbtk