1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBox.cxx,v $
5 Date: $Date: 2008/12/09 13:45:13 $
6 Version: $Revision: 1.35 $
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"
39 #include "bbtkBlackBoxOutputConnector.h"
41 #include "bbtkConfigurationFile.h"
42 #include "bbtkWxBlackBox.h"
52 static bool bbmgSomeBoxExecuting = false;
53 static bool bbmgFreezeExecution = false;
54 static std::set<BlackBox::WeakPointer> bbmgExecutionList;
56 //=========================================================================
57 BlackBox::Deleter::Deleter()
60 //=========================================================================
62 //=========================================================================
63 void BlackBox::Deleter::Delete(Object* p)
65 BlackBox* b = dynamic_cast<BlackBox*>(p);
68 bbtkInternalError("BlackBox::Deleter::Delete("<<p->GetObjectName()
70 <<"dynamic cast to BlackBox* failed !");
72 std::string name = p->GetObjectName();//b->bbGetNameWithParent();
73 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
76 BlackBoxDescriptor::WeakPointer desc = b->bbGetDescriptor();
77 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : deleting black box"<<std::endl);
81 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : releasing descriptor ["<<desc.lock()<<"]"<<std::endl);
85 Package::WeakPointer pack = desc.lock()->GetPackage();
88 Package::ReleaseBlackBoxDescriptor(pack,desc);
92 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : descriptor package expired (was not held by a package and the box was the last instance)"<<std::endl);
97 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);
99 bbtkDebugMessage("object",2,"<## BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
101 //=========================================================================
103 //=========================================================================
104 BlackBox::BlackBox(const std::string &name)
106 // bbmStatus(MODIFIED),
109 bbmBoxProcessMode("Pipeline"),
113 bbtkDebugMessage("object",4,"==> BlackBox::BlackBox(\""
114 <<name<<"\")"<<std::endl);
115 bbtkDebugMessage("object",4,"<== BlackBox::BlackBox(\""
116 <<name<<"\")"<<std::endl);
118 //=========================================================================
120 //=========================================================================
121 BlackBox::BlackBox(const BlackBox&)
124 //=========================================================================
125 BlackBox::BlackBox(BlackBox& from, const std::string &name)
127 // bbmStatus(from.bbmStatus),
130 bbmBoxProcessMode(from.bbmBoxProcessMode),
134 bbtkDebugMessage("object",4,"==> BlackBox::BlackBox("
135 <<from.bbGetFullName()<<",\""
136 <<name<<"\")"<<std::endl);
137 bbtkDebugMessage("object",4,"<== BlackBox::BlackBox("
138 <<from.bbGetFullName()<<",\""
139 <<name<<"\")"<<std::endl);
141 //=========================================================================
144 //=========================================================================
145 BlackBox::~BlackBox()
147 bbtkDebugMessage("object",4,"==> BlackBox::~BlackBox() ["<<bbmName
149 this->bbDesallocateConnectors();
150 bbtkDebugMessage("object",4,"<== BlackBox::~BlackBox() ["<<bbmName
153 //=========================================================================
156 //=========================================================================
157 /// Main processing method of the box.
158 void BlackBox::bbExecute(bool force)
160 bbtkDebugMessageInc("process",2,
161 "=> BlackBox::bbExecute("<<(int)force<<") ["
162 <<bbGetFullName()<<"]"<<std::endl);
164 // If already executing : return
165 if (bbGetExecuting())
167 bbtkDebugMessage("process",2,
168 " -> already executing : bailing out"<<std::endl);
172 // If execution frozen : return
173 if (bbGlobalGetFreezeExecution())
175 bbtkDebugMessage("process",2,
176 " -> FreezeExecution global flag is 'true' : abort execution"<<std::endl);
181 // If force is true then update is triggered even if the box is UPTODATE
182 // if (force) bbSetModifiedStatus();
184 // Calls the main recursive update method
185 bbBackwardUpdate(Connection::Pointer());
187 bbtkDebugMessageDec("process",2,
188 "<= BlackBox::bbExecute() ["
189 <<bbGetFullName()<<"]"<<std::endl);
191 //=========================================================================
193 //=========================================================================
194 std::string BlackBox::bbGetFullName() const
196 return this->bbGetNameWithParent()+"<"+this->bbGetDescriptor()->GetTypeName()+">";
198 //=========================================================================
202 //=========================================================================
203 /// Returns the name with the name of the parent prepended if any
204 std::string BlackBox::bbGetNameWithParent() const
206 if (bbmParent.lock())
208 return bbmParent.lock()->bbGetNameWithParent() + ":" + bbmName;
215 //=========================================================================
217 //=========================================================================
218 /// Prints the Help on the BlackBox type
219 void BlackBox::bbGetHelp(bool full) const
221 bbGetDescriptor()->GetHelp(full);
223 //=========================================================================
226 //=========================================================================
227 /// Returns true if the UserBlackBox has an input of name name
228 bool BlackBox::bbHasInput(const std::string& name) const
230 bbtkDebugMessageInc("Kernel",8,
231 "BlackBox::bbHasInput(\""
232 <<name<<"\") ["<<bbGetFullName()<<"]"
234 bool r = ( bbGetDescriptor()->GetInputDescriptorMap().find(name)
235 != bbGetDescriptor()->GetInputDescriptorMap().end());
236 bbtkDebugDecTab("Kernel",8);
239 //=========================================================================
242 //=========================================================================
243 /// Returns true if the UserBlackBox has an output of name name
244 bool BlackBox::bbHasOutput(const std::string& name) const
246 bbtkDebugMessageInc("Kernel",8,"BlackBox::bbHasOutput(\""
247 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
248 bool r = ( bbGetDescriptor()->GetOutputDescriptorMap().find(name)
249 != bbGetDescriptor()->GetOutputDescriptorMap().end());
250 bbtkDebugDecTab("Kernel",8);
253 //=========================================================================
256 //=========================================================================
257 /// Gets the output type of a given name
258 TypeInfo BlackBox::bbGetOutputType( const std::string &name ) const
260 bbtkDebugMessageInc("Kernel",8,
261 "BlackBox::bbGetOutputType(\""
262 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
263 TypeInfo r = bbGetDescriptor()->GetOutputDescriptor(name)->GetTypeInfo();
264 bbtkDebugDecTab("Kernel",8);
267 //=========================================================================
269 //=========================================================================
270 /// Gets the input type of a given name
271 TypeInfo BlackBox::bbGetInputType( const std::string &name ) const
273 bbtkDebugMessageInc("Kernel",8,
274 "BlackBox::bbGetInputType(\""
275 <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
276 TypeInfo r = bbGetDescriptor()->GetInputDescriptor(name)->GetTypeInfo();
277 bbtkDebugDecTab("Kernel",8);
280 //=========================================================================
283 //=========================================================================
284 /// Allocates the i/o connectors of the black box
285 void BlackBox::bbAllocateConnectors()
287 bbtkDebugMessageInc("Kernel",8,
288 "BlackBox::bbAllocateConnectors() ["
289 <<bbGetFullName()<<"]"
291 const BlackBoxDescriptor::InputDescriptorMapType& imap
292 = bbGetDescriptor()->GetInputDescriptorMap();
293 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
294 for ( i = imap.begin(); i != imap.end(); ++i )
296 bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
297 bbGetInputConnectorMap()[i->second->GetName()]
298 = new BlackBoxInputConnector(GetThisPointer<BlackBox>());
300 const BlackBoxDescriptor::OutputDescriptorMapType& omap
301 = bbGetDescriptor()->GetOutputDescriptorMap();
302 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
303 for ( o = omap.begin(); o != omap.end(); ++o )
305 bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
306 bbGetOutputConnectorMap()[o->second->GetName()]
307 = new BlackBoxOutputConnector();
309 bbtkDebugDecTab("Kernel",8);
311 //=========================================================================
314 //=========================================================================
315 /// Desallocates the i/o connectors of the black box
316 void BlackBox::bbDesallocateConnectors()
318 bbtkDebugMessageInc("Kernel",8,
319 "BlackBox::bbDesallocateConnectors()"
322 InputConnectorMapType::const_iterator i;
323 for ( i = bbGetInputConnectorMap().begin();
324 i != bbGetInputConnectorMap().end(); ++i )
326 bbtkDebugMessage("Kernel",8,"* Delete \""<<i->first<<"\""<<std::endl);
329 OutputConnectorMapType::const_iterator o;
330 for ( o = bbGetOutputConnectorMap().begin();
331 o != bbGetOutputConnectorMap().end(); ++o )
333 bbtkDebugMessage("Kernel",8,"* Delete \""<<o->first<<"\""<<std::endl);
337 bbtkDebugDecTab("Kernel",8);
340 //=========================================================================
343 //=========================================================================
344 /// Copies the input / output values from another box
345 void BlackBox::bbCopyIOValues(BlackBox& from)
347 bbtkDebugMessageInc("Kernel",1,
348 "BlackBox::bbCopyIOValues("
349 <<from.bbGetFullName()<<") ["
350 <<bbGetFullName()<<"]"<<std::endl);
351 // copies the input values
352 const BlackBoxDescriptor::InputDescriptorMapType& imap
353 = bbGetDescriptor()->GetInputDescriptorMap();
354 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
355 for ( i = imap.begin(); i != imap.end(); ++i )
357 if (! i->second->GetCopyConstruct() ) continue;
358 std::string input = i->second->GetName();
359 bbtkDebugMessage("Kernel",2,"* Copying input "<<input<<std::endl);
360 this->bbSetInput(input, from.bbGetInput(input) );
362 // copies the output values
363 const BlackBoxDescriptor::OutputDescriptorMapType& omap
364 = bbGetDescriptor()->GetOutputDescriptorMap();
365 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
366 for ( o = omap.begin(); o != omap.end(); ++o )
368 if (! o->second->GetCopyConstruct() ) continue;
369 std::string output = o->second->GetName();
370 bbtkDebugMessage("Kernel",2,"* Copying output "<<output<<std::endl);
371 this->bbSetOutput(output, from.bbGetOutput(output) );
374 bbtkDebugDecTab("Kernel",9);
377 //=========================================================================
381 //=========================================================================
382 bool BlackBox::bbCanReact() const
384 return ( bbGlobalGetSomeBoxExecuting()
385 #ifdef _USE_WXWIDGETS_
386 || Wx::IsSomeWindowAlive()
390 //=========================================================================
394 //=========================================================================
395 /// User overloadable destruction method of a black box
396 void BlackBox::bbUserDelete()
398 bbtkDebugMessage("process",5,
399 "=> BlackBox::bbUserDelete() ["
400 <<bbGetFullName()<<"]"
401 <<" : not overloaded; using standard deletion"
405 //=========================================================================
408 //=========================================================================
409 BlackBox::BoxProcessModeValue BlackBox::bbGetBoxProcessModeValue() const
411 const std::string& p = bbmBoxProcessMode;
413 (p == "P") || (p == "p") ||
414 (p == "Pipeline") || (p == "pipeline") ) return Pipeline;
416 (p == "A") || (p == "a") ||
417 (p == "Always") || (p == "always") ) return Always;
419 (p == "R") || (p == "r") ||
420 (p == "Reactive") || (p == "reactive") ) return Reactive;
421 bbtkError(bbGetFullName()<<" : BoxProcessMode value '"<<p
422 <<"' unknown. Possible values : "
423 <<"'0'/'P'/'p'/'Pipeline'/'pipeline' | "
424 <<"'1'/'A'/'a'/'Always'/'always' | "
425 <<"'2'/'R'/'r'/'Reactive'/'reactive'"<<std::endl);
427 //=========================================================================
429 //=========================================================================
430 bool BlackBox::bbBoxProcessModeIsReactive() const
432 return (bbGetBoxProcessModeValue() == Reactive);
434 //=========================================================================
436 //=========================================================================
437 bool BlackBox::bbBoxProcessModeIsAlways() const
439 return (bbGetBoxProcessModeValue() == Always);
441 //=========================================================================
446 //=========================================================================
447 void BlackBox::AddChangeObserver(const std::string& output_name,
448 OutputChangeCallbackType f)
451 //=========================================================================
453 //=========================================================================
454 void BlackBox::RemoveChangeObserver(const std::string& output_name,
455 OutputChangeCallbackType f)
458 //=========================================================================
462 //=========================================================================
463 /// Sets the ChangeTime of input
464 void BlackBox::bbSetInputChangeTime(BlackBoxInputConnector* c,
467 bbtkDebugMessage("change",1,
468 "==> BlackBox::bbSetInputChangeTime("<<c<<","<<t<<") ["
469 <<bbGetFullName()<<"]"<<std::endl);
471 // If new time is greater than old one
472 if ( c->SetChangeTime(t) )
474 bool was_up_to_date = bbIsUpToDate();
475 // If new time is greater than the old max time of inputs
476 if ( mMaxInputChangeTime.Set(t) )
478 // If the box turned out-of-date
479 if ( was_up_to_date && bbIsOutOfDate() )
482 if ( ( bbBoxProcessModeIsReactive() ||
483 (c==bbGetInputConnectorMap().find("BoxExecute")->second))
486 bbtkDebugMessage("change",2,
489 <<" changed and box is in Reactive mode or BoxExecute input changed : adding it to the global execution list"
491 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
493 // Have to propagate the modification to aval boxes
494 OutputConnectorMapType::iterator i;
495 for (i = bbGetOutputConnectorMap().begin();
496 i != bbGetOutputConnectorMap().end();
499 i->second->SetChangeTime(t);
501 // update the MinOutputChangeTime
502 mMinOutputChangeTime.Set(t);
507 //=========================================================================
509 //=========================================================================
510 /// Sets the ChangeTime of output
511 void BlackBox::bbSetOutputChangeTime(BlackBoxOutputConnector* c,
514 bbtkDebugMessage("change",1,
515 "==> BlackBox::bbSetOutputChangeTime("<<c<<","<<t<<") ["
516 <<bbGetFullName()<<"]"<<std::endl);
520 // c->GetChangeTime() = t;
521 // bbUpdateMinOutputChangeTime(t);
525 //=========================================================================
529 //=========================================================================
530 void BlackBox::bbUpdateMaxInputChangeTime(const ChangeTime& t)
534 if ( t > mMaxInputChangeTime )
536 mMaxInputChangeTime = t;
537 if ( mMinOutputChangeTime > mMaxInputChangeTime )
544 //=========================================================================
546 //=========================================================================
547 void bbUpdateMinOutputChangeTime(const ChangeTime& t)
549 ChangeTime old = mMinOutputChangeTime;
550 mMinOutputChangeTime = MAXLONG;
551 OutputConnectorMapType::iterator i;
552 for (i = bbGetOutputConnectorMap.begin();
553 i != bbGetOutputConnectorMap.end();
556 if (i->second->GetChangeTime() < mMinOutputChangeTime)
557 mMinOutputChangeTime = i->second->GetChangeTime();
559 if ( mMinOutputChangeTime < old )
564 //=========================================================================
567 //=========================================================================
568 /// Signals that the BlackBox has been modified through
569 /// the input connector c
570 /// and propagates it downward
571 /// ** NOT USER INTENDED **
572 void BlackBox::bbSetStatusAndPropagate(BlackBoxInputConnector* c,
575 bbtkDebugMessageInc("change",5,
576 "=> BlackBox::bbSetStatusAndPropagate(input,"
577 <<GetIOStatusString(s)<<") ["
578 <<bbGetFullName()<<"]"<<std::endl);
580 if (s==UPTODATE) bbtkError("bbSetStatusAndPropagate with status UPTODATE!");
582 OutputConnectorMapType::const_iterator o;
583 for ( o = bbGetOutputConnectorMap().begin();
584 o != bbGetOutputConnectorMap().end(); ++o )
586 if (o->second->GetStatus()==UPTODATE)
588 o->second->SetStatus(OUTOFDATE);
589 o->second->SignalChange(GetThisPointer<BlackBox>(),o->first);
593 if ( ( bbBoxProcessModeIsReactive() ||
594 (c==bbGetInputConnectorMap().find("BoxExecute")->second))
597 bbtkDebugMessage("change",2,
598 "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
599 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
601 bbtkDebugMessageInc("change",5,
602 "<= BlackBox::bbSetStatusAndPropagate(input) ["
603 <<bbGetFullName()<<"]"<<std::endl);
605 //=========================================================================
608 //=========================================================================
609 void BlackBox::bbSignalOutputModification(bool reaction)
611 bbtkDebugMessageInc("change",5,
612 "=> BlackBox::bbSignalOutputModification("
614 <<bbGetFullName()<<"]"<<std::endl);
616 OutputConnectorMapType::iterator i;
617 for ( i = bbGetOutputConnectorMap().begin();
618 i != bbGetOutputConnectorMap().end(); ++i)
620 if (i->second->GetStatus()==UPTODATE)
622 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
626 if (reaction) bbGlobalProcessExecutionList();
628 bbtkDebugMessageDec("change",5,
629 "<= BlackBox::bbSignalOutputModification() ["
630 <<bbGetFullName()<<"]"<<std::endl);
633 //=========================================================================
634 //=========================================================================
635 void BlackBox::bbSignalOutputModification(const std::string& output,
638 bbtkDebugMessageInc("change",5,
639 "=> BlackBox::bbSignalOutputModification("
640 <<output<<","<<reaction<<") ["
641 <<bbGetFullName()<<"]"<<std::endl);
643 OutputConnectorMapType::iterator i =
644 bbGetOutputConnectorMap().find(output);
647 if ( i == bbGetOutputConnectorMap().end() )
649 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
652 if (i->second->GetStatus()==UPTODATE)
654 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
655 // Has to notify the output "BoxChange" also
656 if (output != "BoxChange")
658 i = bbGetOutputConnectorMap().find("BoxChange");
659 if ( i != bbGetOutputConnectorMap().end() )
661 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
664 if (reaction) bbGlobalProcessExecutionList();
667 bbtkDebugMessageDec("change",5,
668 "<= BlackBox::bbSignalOutputModification("
670 <<bbGetFullName()<<"]"<<std::endl);
673 //=========================================================================
674 //=========================================================================
675 void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
678 bbtkDebugMessageInc("change",5,
679 "=> BlackBox::bbSignalOutputModification(vector of outputs) ["
680 <<bbGetFullName()<<"]"<<std::endl);
681 OutputConnectorMapType::iterator i;
682 std::vector<std::string>::const_iterator o;
683 bool changed = false;
684 for (o=output.begin();o!=output.end();++o)
686 // the output "BoxChange" must be signaled **AFTER** all others
687 if (*o == "BoxChange") continue;
688 // Look for the connector
689 i = bbGetOutputConnectorMap().find(*o);
690 if ( i == bbGetOutputConnectorMap().end() )
692 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
694 // Already OUTOFDATE : noting to do
695 if (i->second->GetStatus()==UPTODATE)
697 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
701 // Has to notify the output "BoxChange" also
702 i = bbGetOutputConnectorMap().find("BoxChange");
703 if ( changed && (i != bbGetOutputConnectorMap().end()))
705 // Already OUTOFDATE : noting to do
706 if (i->second->GetStatus()==UPTODATE)
708 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
709 if (reaction) bbGlobalProcessExecutionList();
713 bbtkDebugMessageDec("change",5,
714 "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
715 <<bbGetFullName()<<"]"<<std::endl);
718 //=========================================================================
720 //=========================================================================
721 /// Updates the BlackBox inputs
722 /// Calls BackwardUpdate on all BlackBoxInputConnector
723 /// \returns The maximum of final IOStatus after each input update
724 IOStatus BlackBox::bbUpdateInputs()
726 bbtkDebugMessageInc("process",4,
727 "=> BlackBox::bbUpdateInputs() ["
728 <<bbGetFullName()<<"]"
731 IOStatus s = UPTODATE;
733 InputConnectorMapType::iterator i;
734 for ( i = bbGetInputConnectorMap().begin();
735 i!= bbGetInputConnectorMap().end(); ++i)
737 // if (i->first=="WinHide") continue;
738 // If input type is Void : no recurse
739 //if ( bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo()
742 bbtkDebugMessageDec("change",2,
743 bbGetName()<<"."<<i->first
744 <<" ["<<i->second<<"] "
745 <<" status before update = '"
746 <<GetIOStatusString(i->second->GetStatus())
748 i->second->BackwardUpdate();
749 IOStatus t = i->second->GetStatus();
751 bbtkDebugMessageDec("change",2,
752 bbGetName()<<"."<<i->first
753 <<" ["<<i->second<<"] "
754 <<" status before process = '"
755 <<GetIOStatusString(i->second->GetStatus())
759 bbtkDebugMessageDec("process",4,
760 "<= BlackBox::bbUpdateInputs() ["
761 <<bbGetFullName()<<"]"
767 //=========================================================================
769 //==================================================================
770 /// Computes the final IOStatus of inputs and outputs after processing
771 void BlackBox::bbComputePostProcessStatus()
773 bbtkDebugMessageInc("process",4,
774 "=> BlackBox::bbComputePostProcessStatus() ["
775 <<bbGetFullName()<<"]"
778 IOStatus new_output_status = UPTODATE;
779 if (bbBoxProcessModeIsAlways()) new_output_status = OUTOFDATE;
781 // Update the input statuses
782 InputConnectorMapType::iterator i;
783 for ( i = bbGetInputConnectorMap().begin();
784 i!= bbGetInputConnectorMap().end(); ++i)
786 IOStatus t = i->second->GetStatus();
787 if (t == OUTOFDATE) new_output_status = OUTOFDATE;
788 // A previously MODIFIED status turns to UPTODATE
789 if (t==MODIFIED) i->second->SetStatus(UPTODATE);
790 bbtkDebugMessage("change",2,
791 bbGetName()<<"."<<i->first<<" : "
792 << GetIOStatusString(t) << " -> "
793 << GetIOStatusString(i->second->GetStatus())
796 bbtkDebugMessage("change",2,
797 bbGetName()<<" new output status : "
798 << GetIOStatusString(new_output_status)
800 // Update the output statuses
801 OutputConnectorMapType::iterator o;
802 for ( o = bbGetOutputConnectorMap().begin();
803 o!= bbGetOutputConnectorMap().end(); ++o)
805 o->second->SetStatus(new_output_status);
808 bbtkDebugMessageInc("process",4,
809 "<= BlackBox::bbComputePostProcessStatus() ["
810 <<bbGetFullName()<<"]"
814 //==================================================================
816 //=========================================================================
817 /// Connects the input <name> to the connection c
818 void BlackBox::bbConnectInput( const std::string& name, Connection* c)
820 bbtkDebugMessage("connection",2,
821 "==> BlackBox::bbConnectInput(\""
822 <<name<<"\","<<c->GetFullName()<<") ["
823 <<bbGetFullName()<<"]"
827 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
828 if (i==bbGetInputConnectorMap().end())
830 bbtkError("no input called '"<<name<<"'");
832 i->second->SetConnection(c);
833 // The input *MUST* be set OUTOFDATE to update its input on next execution
834 bbSetStatusAndPropagate(i->second,OUTOFDATE);
836 bbtkDebugMessage("connection",2,
837 "<== BlackBox::bbConnectInput(\""
838 <<name<<"\","<<c->GetFullName()<<") ["
839 <<bbGetFullName()<<"]"
843 //=========================================================================
846 //=========================================================================
847 /// Connects the output <name> to the connection c
848 void BlackBox::bbConnectOutput( const std::string& name, Connection* c)
850 bbtkDebugMessage("connection",2,
851 "==> BlackBox::bbConnectOutput(\""<<name<<"\","
852 <<c->GetFullName()<<") ["
853 <<bbGetFullName()<<"]"<<std::endl);
855 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
856 if (i==bbGetOutputConnectorMap().end())
858 bbtkError("no output called '"<<name<<"'");
860 i->second->SetConnection(c);
862 bbtkDebugMessage("connection",2,
863 "<== BlackBox::bbConnectOutput(\""<<name<<"\","
864 <<c->GetFullName()<<") ["
865 <<bbGetFullName()<<"]"<<std::endl);
868 //=========================================================================
871 //=========================================================================
872 /// Disconnects the input <name> from the connection c
873 void BlackBox::bbDisconnectInput( const std::string& name, Connection* c)
876 bbtkDebugMessage("connection",2,
877 "==> BlackBox::bbDisconnectInput(\""<<name
878 <<"\","<<c->GetFullName()<<") ["
879 <<bbGetFullName()<<"]"
885 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
889 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
890 if (i==bbGetInputConnectorMap().end())
892 bbtkError("no input called '"<<name<<"'");
894 i->second->UnsetConnection(c);
896 bbtkDebugMessage("connection",2,
897 "<== BlackBox::bbDisconnectInput(\""<<name
898 <<"\","<<c->GetFullName()<<") ["
899 <<bbGetFullName()<<"]"
903 //=========================================================================
906 //=========================================================================
907 /// Disconnects the output <name> from the connection c
908 void BlackBox::bbDisconnectOutput( const std::string& name, Connection* c)
910 bbtkDebugMessage("connection",2,
911 "==> BlackBox::bbDisconnectOutput(\""<<name
912 <<"\","<<c->GetFullName()<<") ["
913 <<bbGetFullName()<<"]"
918 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
922 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
923 if (i==bbGetOutputConnectorMap().end())
925 bbtkError("no output called '"<<name<<"'");
927 i->second->UnsetConnection(c);
929 bbtkDebugMessage("connection",2,
930 "<== BlackBox::bbDisconnectOutput(\""<<name
931 <<"\","<<c->GetFullName()<<") ["
932 <<bbGetFullName()<<"]"
935 //=========================================================================
938 //=========================================================================
940 void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
942 fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
944 //=========================================================================
947 //=========================================================================
948 std::string BlackBox::bbGetOutputAsString( const std::string &output )
951 // Looks for the adaptor
952 if (bbGetOutputType(output).name() != typeid(std::string).name() )
955 Package::Pointer p = bbGetDescriptor()->GetPackage();
956 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
958 Factory::Pointer f = p->GetFactorySet().begin()->lock();
963 bbGetOutputType(output),
966 } catch (bbtk::Exception e)
971 a->bbSetInput("In",bbGetOutput(output));
973 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
975 v="? (no adaptor found)";
980 v="? (no factory found)";
986 v = bbGetOutput(output).unsafe_get<std::string>() ;
990 //=========================================================================
992 //=========================================================================
993 std::string BlackBox::bbGetInputAsString( const std::string &input )
996 // Looks for the adaptor
997 if (bbGetInputType(input) != typeid(std::string))
1000 Package::Pointer p = bbGetDescriptor()->GetPackage();
1001 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
1003 Factory::Pointer f = p->GetFactorySet().begin()->lock();
1004 BlackBox::Pointer a;
1008 bbGetInputType(input),
1009 typeid(std::string),
1011 }catch (bbtk::Exception e)
1017 a->bbSetInput("In",bbGetInput(input));
1019 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
1023 v="? (no adaptor found)";
1028 v="? (no factory found)";
1033 v = bbGetInput(input).unsafe_get<std::string>() ;
1037 //=======================================================================
1039 //=======================================================================
1040 // Replaces substrings "<" by "["
1041 void SubsBrackets ( std::string& s )
1043 // std::cout << "BEFORE=["<<s<<"]"<<std::endl;
1044 std::string ss("<");
1045 std::string::size_type pos = 0;
1047 std::string cr("[");
1048 while ( pos != std::string::npos )
1050 // std::cout << "*** find one "<<std::endl;
1051 s.replace(pos,1,cr.c_str(),1);
1052 pos = s.find(ss, pos);
1058 while ( pos != std::string::npos )
1060 // std::cout << "*** find one "<<std::endl;
1061 s.replace(pos,1,cr.c_str(),1);
1062 pos = s.find(ss, pos);
1068 while ( pos != std::string::npos )
1070 // std::cout << "*** find one "<<std::endl;
1071 s.replace(pos,1,cr.c_str(),1);
1072 pos = s.find(ss, pos);
1073 } // std::cout << "AFTER=["<<s<<"]"<<std::endl;
1075 //=======================================================================
1077 //=========================================================================
1078 /// Write Graphviz-dot description in file
1079 void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
1080 BlackBox::Pointer parentblackbox,
1081 int detail, int level,
1082 bool instanceOrtype,
1083 bool relative_link )
1086 InputConnectorMapType::iterator i;
1088 std::string labelStr;
1089 std::string valueStr("");
1092 labelStr = bbGetName() ;
1094 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
1096 labelStr = bbGetName();
1097 labelStr = labelStr + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "] ";
1100 SubsBrackets(labelStr);
1103 labelStr = labelStr + " | {{ ";
1104 std::string tempStrTypeName;
1107 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1111 labelStr=labelStr+" | ";
1114 if (instanceOrtype==true)
1116 valueStr = this->bbGetInputAsString(i->first) + " = ";
1118 const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
1119 tempStrTypeName=id->GetTypeName();
1120 SubsBrackets(tempStrTypeName);
1121 std::string Name(i->first);
1123 labelStr=labelStr + "<"+i->first.c_str()+"> " + valueStr + Name.c_str() + " [" + tempStrTypeName.c_str() + "]";
1125 labelStr=labelStr+ " } | {";
1127 OutputConnectorMapType::iterator ii;
1128 for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii )
1132 labelStr=labelStr+" | ";
1135 if (instanceOrtype==true)
1137 valueStr = this->bbGetOutputAsString(ii->first) + " = ";
1139 const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first);
1140 tempStrTypeName=id->GetTypeName();
1141 SubsBrackets(tempStrTypeName);
1142 std::string Name(ii->first);
1144 labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + " ["+tempStrTypeName+"]";
1146 labelStr = labelStr+ " } }" ;
1150 bbWriteDotInputOutputName(ff,true,detail,level);
1151 std::string tmp ( bbGetTypeName() );
1155 url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
1157 url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
1159 fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
1160 // std::cout << labelStr << std::endl;
1163 if (GetThisPointer<BlackBox>()!=parentblackbox){
1164 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1168 Connection* con = i->second->GetConnection();
1170 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
1171 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
1173 a->bbWriteDotInputOutputName(ff,false,detail,level);
1176 fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
1179 b->bbWriteDotInputOutputName(ff,true,detail,level);
1182 fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
1184 fprintf(ff,"%s\n",";");
1188 } // if parentblackbox
1190 //=========================================================================
1195 //=========================================================================
1196 void BlackBox::bbShowRelations(BlackBox::Pointer parentblackbox,
1197 int detail, int level
1198 /*,Factory *factory*/ )
1201 if (this->bbGetDescriptor()->GetPackage())
1203 bbtkMessage("Help",1,"Black Box '"<<bbGetName()<<"' <"<<
1204 this->bbGetDescriptor()->GetPackage()->GetName()
1205 <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1209 bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1214 bbtkMessage("Help",1,"Up-to-date ["<<mMaxInputChangeTime<<","
1215 <<mMinOutputChangeTime<<"]"<<std::endl);
1219 bbtkMessage("Help",1,"Out-of-date ["<<mMaxInputChangeTime<<","
1220 <<mMinOutputChangeTime<<"]"<<std::endl);
1223 // bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
1224 // bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
1226 std::vector<std::string> iname;
1227 std::vector<std::string> ivalue;
1228 std::vector<std::string> iconn;
1229 std::vector<std::string> istatus;
1231 InputConnectorMapType::iterator i;
1232 unsigned int namelmax = 0;
1233 unsigned int valuelmax = 0;
1234 // unsigned int connlmax = 0;
1235 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1237 iname.push_back(i->first);
1238 if (iname.back().size()>namelmax) namelmax = iname.back().size();
1239 ivalue.push_back(bbGetInputAsString(i->first));
1240 if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
1242 Connection* con = i->second->GetConnection();
1244 s = con->GetOriginalBlackBoxFrom()->bbGetName();
1246 s += con->GetOriginalBlackBoxFromOutput();
1249 istatus.push_back(GetIOStatusString(i->second->GetStatus()));
1251 OutputConnectorMapType::iterator o;
1252 std::vector<std::string> oname;
1253 std::vector<std::string> ovalue;
1254 std::vector<std::vector<std::string> > oconn;
1255 std::vector<std::string> ostatus;
1256 for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o )
1258 oname.push_back(o->first);
1259 if (oname.back().size()>namelmax) namelmax = oname.back().size();
1260 ovalue.push_back(bbGetOutputAsString(o->first));
1261 if (ovalue.back().size()>valuelmax) valuelmax = ovalue.back().size();
1262 std::vector<std::string> ss;
1263 const std::vector<Connection*>& con
1264 = o->second->GetConnectionVector();
1265 std::vector<Connection*>::const_iterator c;
1266 for (c=con.begin();c!=con.end();++c)
1269 s = (*c)->GetOriginalBlackBoxTo()->bbGetName();
1271 s += (*c)->GetOriginalBlackBoxToInput();
1274 oconn.push_back(ss);
1275 ostatus.push_back(GetIOStatusString(o->second->GetStatus()));
1279 bbtkMessage("Help",1," * Inputs : "<<std::endl);
1281 bbtkMessage("Help",1," * No inputs"<<std::endl);
1283 std::vector<std::string>::iterator i1,i2,i3,i4;
1284 for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin(),i4=istatus.begin();
1285 i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end(),i4!=istatus.end();
1286 ++i1,++i2,++i3,++i4)
1288 std::string name(*i1);
1290 name.append(1+namelmax-name.size(),' ');
1291 std::string value(*i2);
1293 value.append(1+valuelmax-value.size(),' ');
1295 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<" <-- '"
1298 bbtkMessage("Help",1," '"<<name<<" = '"<<value);
1299 bbtkMessage("Help",1," ["<<*i4<<"]"<<std::endl);
1303 bbtkMessage("Help",1," * Outputs : "<<std::endl);
1305 bbtkMessage("Help",1," * No outputs"<<std::endl);
1307 std::vector<std::vector<std::string> >::iterator i5;
1309 for (i1=oname.begin(),i2=ovalue.begin(),i5=oconn.begin(),i4=ostatus.begin();
1310 i1!=oname.end(),i2!=ovalue.end(),i5!=oconn.end(),i4!=ostatus.end();
1311 ++i1,++i2,++i4,++i5)
1313 std::string name(*i1);
1315 name.append(1+namelmax-name.size(),' ');
1316 std::string value(*i2);
1318 value.append(1+valuelmax-value.size(),' ');
1320 bbtkMessage("Help",1," '"<<name<<" = '"<<value);
1323 std::string pref = " '"+name+" = '"+value;
1324 for (i3=i5->begin();i3!=i5->end();++i3)
1326 bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'");
1327 pref.replace(0,pref.size(),pref.size(),' ');
1330 bbtkMessage("Help",1," ["<<*i4<<"]"<<std::endl);
1334 //=========================================================================
1336 static bool bbmgGlobalProcessingExecutionList = false;
1338 //=========================================================================
1339 void BlackBox::bbGlobalProcessExecutionList()
1341 bbtkDebugMessageInc("process",3,
1342 "=> BlackBox::bbGlobalProcessExecutionList()"
1344 if (bbmgGlobalProcessingExecutionList)
1346 bbtkDebugMessage("process",3,"BlackBox::bbGlobalProcessExecutionList() reentered !");
1349 bbmgGlobalProcessingExecutionList = true;
1351 std::set<BlackBox::WeakPointer>::iterator i;
1352 while (bbmgExecutionList.size()>0)
1354 i = bbmgExecutionList.begin();
1355 BlackBox::WeakPointer p = *i;
1356 bbmgExecutionList.erase(i);
1359 bbtkDebugMessage("process",4,
1361 p.lock()->bbGetFullName()<<std::endl);
1362 p.lock()->bbExecute(true);
1366 bbtkGlobalError("Strange error in BlackBox::bbGlobalProcessExecutionList() : Weak bb pointer in bbmgExecutionList is no more valid...");
1370 bbmgExecutionList.clear();
1371 bbtkDebugMessageDec("process",3,
1372 "<= BlackBox::bbGlobalProcessExecutionList()"
1375 bbmgGlobalProcessingExecutionList = false;
1378 //=========================================================================
1380 bool BlackBox::bbGlobalGetSomeBoxExecuting()
1382 return bbmgSomeBoxExecuting;
1385 void BlackBox::bbGlobalSetSomeBoxExecuting(bool b)
1387 bbmgSomeBoxExecuting = b;
1390 void BlackBox::bbGlobalSetFreezeExecution(bool b)
1392 bbmgFreezeExecution = b;
1395 bool BlackBox::bbGlobalGetFreezeExecution()
1397 return bbmgFreezeExecution;
1400 void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
1402 bbtkDebugMessage("process",3,"* bbGlobalAddToExecutionList("<<b->bbGetFullName()<<")"<<std::endl);
1403 if (bbmgGlobalProcessingExecutionList)
1405 bbtkDebugMessage("process",3,"bbGlobalAddToExecutionList called inside bbGlobalProcessExecutionList !");
1407 bbmgExecutionList.insert(b);
1411 //=========================================================================
1413 //=========================================================================
1414 void BlackBox::Check(bool recursive)
1416 bbtkMessage("debug",1,"*** Checking Black Box "<<(void*)this<<" ["<<bbGetFullName()
1417 <<"] ... OK"<<std::endl);
1419 //=========================================================================
1421 void BlackBox::bbUserOnShowWidget(std::string nameInput)
1423 bbtk::BlackBoxInputConnector *cc;
1424 cc = this->bbGetInputConnectorMap().find( nameInput.c_str() )->second;
1425 if (cc->GetConnection()!=NULL)
1427 cc->GetConnection()->GetBlackBoxFrom()->bbUserOnShow();
1433 } // EO namespace bbtk