1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBox.cxx,v $
5 Date: $Date: 2008/12/10 09:33:18 $
6 Version: $Revision: 1.36 $
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::bbAddOutputObserver(const std::string& output,
448 OutputChangeCallbackType f)
450 bbGetOutputConnector(output).AddChangeObserver(f);
452 //=========================================================================
454 //=========================================================================
455 void BlackBox::bbRemoveOutputObserver(const std::string& output_name,
456 OutputChangeCallbackType f)
458 bbtkError("BlackBox::RemoveChangeObserver NOT IMPLEMENTED");
460 //=========================================================================
464 //=========================================================================
465 /// Sets the ChangeTime of input
466 void BlackBox::bbSetInputChangeTime(BlackBoxInputConnector* c,
469 bbtkDebugMessage("change",1,
470 "==> BlackBox::bbSetInputChangeTime("<<c<<","<<t<<") ["
471 <<bbGetFullName()<<"]"<<std::endl);
473 // If new time is greater than old one
474 if ( c->SetChangeTime(t) )
476 bool was_up_to_date = bbIsUpToDate();
477 // If new time is greater than the old max time of inputs
478 if ( mMaxInputChangeTime.Set(t) )
480 // If the box turned out-of-date
481 if ( was_up_to_date && bbIsOutOfDate() )
484 if ( ( bbBoxProcessModeIsReactive() ||
485 (c==bbGetInputConnectorMap().find("BoxExecute")->second))
488 bbtkDebugMessage("change",2,
491 <<" changed and box is in Reactive mode or BoxExecute input changed : adding it to the global execution list"
493 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
495 // Have to propagate the modification to aval boxes
496 OutputConnectorMapType::iterator i;
497 for (i = bbGetOutputConnectorMap().begin();
498 i != bbGetOutputConnectorMap().end();
501 i->second->SetChangeTime(t);
503 // update the MinOutputChangeTime
504 mMinOutputChangeTime.Set(t);
509 //=========================================================================
511 //=========================================================================
512 /// Sets the ChangeTime of output
513 void BlackBox::bbSetOutputChangeTime(BlackBoxOutputConnector* c,
516 bbtkDebugMessage("change",1,
517 "==> BlackBox::bbSetOutputChangeTime("<<c<<","<<t<<") ["
518 <<bbGetFullName()<<"]"<<std::endl);
522 // c->GetChangeTime() = t;
523 // bbUpdateMinOutputChangeTime(t);
527 //=========================================================================
531 //=========================================================================
532 void BlackBox::bbUpdateMaxInputChangeTime(const ChangeTime& t)
536 if ( t > mMaxInputChangeTime )
538 mMaxInputChangeTime = t;
539 if ( mMinOutputChangeTime > mMaxInputChangeTime )
546 //=========================================================================
548 //=========================================================================
549 void bbUpdateMinOutputChangeTime(const ChangeTime& t)
551 ChangeTime old = mMinOutputChangeTime;
552 mMinOutputChangeTime = MAXLONG;
553 OutputConnectorMapType::iterator i;
554 for (i = bbGetOutputConnectorMap.begin();
555 i != bbGetOutputConnectorMap.end();
558 if (i->second->GetChangeTime() < mMinOutputChangeTime)
559 mMinOutputChangeTime = i->second->GetChangeTime();
561 if ( mMinOutputChangeTime < old )
566 //=========================================================================
569 //=========================================================================
570 /// Signals that the BlackBox has been modified through
571 /// the input connector c
572 /// and propagates it downward
573 /// ** NOT USER INTENDED **
574 void BlackBox::bbSetStatusAndPropagate(BlackBoxInputConnector* c,
577 bbtkDebugMessageInc("change",5,
578 "=> BlackBox::bbSetStatusAndPropagate(input,"
579 <<GetIOStatusString(s)<<") ["
580 <<bbGetFullName()<<"]"<<std::endl);
582 if (s==UPTODATE) bbtkError("bbSetStatusAndPropagate with status UPTODATE!");
584 OutputConnectorMapType::const_iterator o;
585 for ( o = bbGetOutputConnectorMap().begin();
586 o != bbGetOutputConnectorMap().end(); ++o )
588 if (o->second->GetStatus()==UPTODATE)
590 o->second->SetStatus(OUTOFDATE);
591 o->second->SignalChange(GetThisPointer<BlackBox>(),o->first);
595 if ( ( bbBoxProcessModeIsReactive() ||
596 (c==bbGetInputConnectorMap().find("BoxExecute")->second))
599 bbtkDebugMessage("change",2,
600 "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
601 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
603 bbtkDebugMessageInc("change",5,
604 "<= BlackBox::bbSetStatusAndPropagate(input) ["
605 <<bbGetFullName()<<"]"<<std::endl);
607 //=========================================================================
610 //=========================================================================
611 void BlackBox::bbSignalOutputModification(bool reaction)
613 bbtkDebugMessageInc("change",5,
614 "=> BlackBox::bbSignalOutputModification("
616 <<bbGetFullName()<<"]"<<std::endl);
618 OutputConnectorMapType::iterator i;
619 for ( i = bbGetOutputConnectorMap().begin();
620 i != bbGetOutputConnectorMap().end(); ++i)
622 // std::cout << "Stat = "
623 //<<GetIOStatusString(i->second->GetStatus())
625 // LG : CANNOT SIGNAL ONLY WHEN UPTODATE
626 // See bbtkSampleOutputObserver
627 // if (i->second->GetStatus()==UPTODATE)
629 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
633 if (reaction) bbGlobalProcessExecutionList();
635 bbtkDebugMessageDec("change",5,
636 "<= BlackBox::bbSignalOutputModification() ["
637 <<bbGetFullName()<<"]"<<std::endl);
640 //=========================================================================
641 //=========================================================================
642 void BlackBox::bbSignalOutputModification(const std::string& output,
645 bbtkDebugMessageInc("change",5,
646 "=> BlackBox::bbSignalOutputModification("
647 <<output<<","<<reaction<<") ["
648 <<bbGetFullName()<<"]"<<std::endl);
650 OutputConnectorMapType::iterator i =
651 bbGetOutputConnectorMap().find(output);
654 if ( i == bbGetOutputConnectorMap().end() )
656 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
659 // if (i->second->GetStatus()==UPTODATE)
661 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
662 // Has to notify the output "BoxChange" also
663 if (output != "BoxChange")
665 i = bbGetOutputConnectorMap().find("BoxChange");
666 if ( i != bbGetOutputConnectorMap().end() )
668 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
671 if (reaction) bbGlobalProcessExecutionList();
674 bbtkDebugMessageDec("change",5,
675 "<= BlackBox::bbSignalOutputModification("
677 <<bbGetFullName()<<"]"<<std::endl);
680 //=========================================================================
681 //=========================================================================
682 void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
685 bbtkDebugMessageInc("change",5,
686 "=> BlackBox::bbSignalOutputModification(vector of outputs) ["
687 <<bbGetFullName()<<"]"<<std::endl);
688 OutputConnectorMapType::iterator i;
689 std::vector<std::string>::const_iterator o;
690 bool changed = false;
691 for (o=output.begin();o!=output.end();++o)
693 // the output "BoxChange" must be signaled **AFTER** all others
694 if (*o == "BoxChange") continue;
695 // Look for the connector
696 i = bbGetOutputConnectorMap().find(*o);
697 if ( i == bbGetOutputConnectorMap().end() )
699 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
702 // if (i->second->GetStatus()==UPTODATE)
704 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
708 // Has to notify the output "BoxChange" also
709 i = bbGetOutputConnectorMap().find("BoxChange");
710 if ( changed && (i != bbGetOutputConnectorMap().end()))
712 // if (i->second->GetStatus()==UPTODATE)
714 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
715 if (reaction) bbGlobalProcessExecutionList();
719 bbtkDebugMessageDec("change",5,
720 "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
721 <<bbGetFullName()<<"]"<<std::endl);
724 //=========================================================================
726 //=========================================================================
727 /// Updates the BlackBox inputs
728 /// Calls BackwardUpdate on all BlackBoxInputConnector
729 /// \returns The maximum of final IOStatus after each input update
730 IOStatus BlackBox::bbUpdateInputs()
732 bbtkDebugMessageInc("process",4,
733 "=> BlackBox::bbUpdateInputs() ["
734 <<bbGetFullName()<<"]"
737 IOStatus s = UPTODATE;
739 InputConnectorMapType::iterator i;
740 for ( i = bbGetInputConnectorMap().begin();
741 i!= bbGetInputConnectorMap().end(); ++i)
743 // if (i->first=="WinHide") continue;
744 // If input type is Void : no recurse
745 //if ( bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo()
748 bbtkDebugMessageDec("change",2,
749 bbGetName()<<"."<<i->first
750 <<" ["<<i->second<<"] "
751 <<" status before update = '"
752 <<GetIOStatusString(i->second->GetStatus())
754 i->second->BackwardUpdate();
755 IOStatus t = i->second->GetStatus();
757 bbtkDebugMessageDec("change",2,
758 bbGetName()<<"."<<i->first
759 <<" ["<<i->second<<"] "
760 <<" status before process = '"
761 <<GetIOStatusString(i->second->GetStatus())
765 bbtkDebugMessageDec("process",4,
766 "<= BlackBox::bbUpdateInputs() ["
767 <<bbGetFullName()<<"]"
773 //=========================================================================
775 //==================================================================
776 /// Computes the final IOStatus of inputs and outputs after processing
777 void BlackBox::bbComputePostProcessStatus()
779 bbtkDebugMessageInc("process",4,
780 "=> BlackBox::bbComputePostProcessStatus() ["
781 <<bbGetFullName()<<"]"
784 IOStatus new_output_status = UPTODATE;
785 if (bbBoxProcessModeIsAlways()) new_output_status = OUTOFDATE;
787 // Update the input statuses
788 InputConnectorMapType::iterator i;
789 for ( i = bbGetInputConnectorMap().begin();
790 i!= bbGetInputConnectorMap().end(); ++i)
792 IOStatus t = i->second->GetStatus();
793 if (t == OUTOFDATE) new_output_status = OUTOFDATE;
794 // A previously MODIFIED status turns to UPTODATE
795 if (t==MODIFIED) i->second->SetStatus(UPTODATE);
796 bbtkDebugMessage("change",2,
797 bbGetName()<<"."<<i->first<<" : "
798 << GetIOStatusString(t) << " -> "
799 << GetIOStatusString(i->second->GetStatus())
802 bbtkDebugMessage("change",2,
803 bbGetName()<<" new output status : "
804 << GetIOStatusString(new_output_status)
806 // Update the output statuses
807 OutputConnectorMapType::iterator o;
808 for ( o = bbGetOutputConnectorMap().begin();
809 o!= bbGetOutputConnectorMap().end(); ++o)
811 o->second->SetStatus(new_output_status);
814 bbtkDebugMessageInc("process",4,
815 "<= BlackBox::bbComputePostProcessStatus() ["
816 <<bbGetFullName()<<"]"
820 //==================================================================
822 //=========================================================================
823 /// Connects the input <name> to the connection c
824 void BlackBox::bbConnectInput( const std::string& name, Connection* c)
826 bbtkDebugMessage("connection",2,
827 "==> BlackBox::bbConnectInput(\""
828 <<name<<"\","<<c->GetFullName()<<") ["
829 <<bbGetFullName()<<"]"
833 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
834 if (i==bbGetInputConnectorMap().end())
836 bbtkError("no input called '"<<name<<"'");
838 i->second->SetConnection(c);
839 // The input *MUST* be set OUTOFDATE to update its input on next execution
840 bbSetStatusAndPropagate(i->second,OUTOFDATE);
842 bbtkDebugMessage("connection",2,
843 "<== BlackBox::bbConnectInput(\""
844 <<name<<"\","<<c->GetFullName()<<") ["
845 <<bbGetFullName()<<"]"
849 //=========================================================================
852 //=========================================================================
853 /// Connects the output <name> to the connection c
854 void BlackBox::bbConnectOutput( const std::string& name, Connection* c)
856 bbtkDebugMessage("connection",2,
857 "==> BlackBox::bbConnectOutput(\""<<name<<"\","
858 <<c->GetFullName()<<") ["
859 <<bbGetFullName()<<"]"<<std::endl);
861 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
862 if (i==bbGetOutputConnectorMap().end())
864 bbtkError("no output called '"<<name<<"'");
866 i->second->SetConnection(c);
868 bbtkDebugMessage("connection",2,
869 "<== BlackBox::bbConnectOutput(\""<<name<<"\","
870 <<c->GetFullName()<<") ["
871 <<bbGetFullName()<<"]"<<std::endl);
874 //=========================================================================
877 //=========================================================================
878 /// Disconnects the input <name> from the connection c
879 void BlackBox::bbDisconnectInput( const std::string& name, Connection* c)
882 bbtkDebugMessage("connection",2,
883 "==> BlackBox::bbDisconnectInput(\""<<name
884 <<"\","<<c->GetFullName()<<") ["
885 <<bbGetFullName()<<"]"
891 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
895 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
896 if (i==bbGetInputConnectorMap().end())
898 bbtkError("no input called '"<<name<<"'");
900 i->second->UnsetConnection(c);
902 bbtkDebugMessage("connection",2,
903 "<== BlackBox::bbDisconnectInput(\""<<name
904 <<"\","<<c->GetFullName()<<") ["
905 <<bbGetFullName()<<"]"
909 //=========================================================================
912 //=========================================================================
913 /// Disconnects the output <name> from the connection c
914 void BlackBox::bbDisconnectOutput( const std::string& name, Connection* c)
916 bbtkDebugMessage("connection",2,
917 "==> BlackBox::bbDisconnectOutput(\""<<name
918 <<"\","<<c->GetFullName()<<") ["
919 <<bbGetFullName()<<"]"
924 bbtkDebugMessage("connection",2,"c==0"<<std::endl);
928 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
929 if (i==bbGetOutputConnectorMap().end())
931 bbtkError("no output called '"<<name<<"'");
933 i->second->UnsetConnection(c);
935 bbtkDebugMessage("connection",2,
936 "<== BlackBox::bbDisconnectOutput(\""<<name
937 <<"\","<<c->GetFullName()<<") ["
938 <<bbGetFullName()<<"]"
941 //=========================================================================
944 //=========================================================================
946 void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
948 fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
950 //=========================================================================
953 //=========================================================================
954 std::string BlackBox::bbGetOutputAsString( const std::string &output )
957 // Looks for the adaptor
958 if (bbGetOutputType(output).name() != typeid(std::string).name() )
961 Package::Pointer p = bbGetDescriptor()->GetPackage();
962 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
964 Factory::Pointer f = p->GetFactorySet().begin()->lock();
969 bbGetOutputType(output),
972 } catch (bbtk::Exception e)
977 a->bbSetInput("In",bbGetOutput(output));
979 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
981 v="? (no adaptor found)";
986 v="? (no factory found)";
992 v = bbGetOutput(output).unsafe_get<std::string>() ;
996 //=========================================================================
998 //=========================================================================
999 std::string BlackBox::bbGetInputAsString( const std::string &input )
1002 // Looks for the adaptor
1003 if (bbGetInputType(input) != typeid(std::string))
1006 Package::Pointer p = bbGetDescriptor()->GetPackage();
1007 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
1009 Factory::Pointer f = p->GetFactorySet().begin()->lock();
1010 BlackBox::Pointer a;
1014 bbGetInputType(input),
1015 typeid(std::string),
1017 }catch (bbtk::Exception e)
1023 a->bbSetInput("In",bbGetInput(input));
1025 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
1029 v="? (no adaptor found)";
1034 v="? (no factory found)";
1039 v = bbGetInput(input).unsafe_get<std::string>() ;
1043 //=======================================================================
1045 //=======================================================================
1046 // Replaces substrings "<" by "["
1047 void SubsBrackets ( std::string& s )
1049 // std::cout << "BEFORE=["<<s<<"]"<<std::endl;
1050 std::string ss("<");
1051 std::string::size_type pos = 0;
1053 std::string cr("[");
1054 while ( pos != std::string::npos )
1056 // std::cout << "*** find one "<<std::endl;
1057 s.replace(pos,1,cr.c_str(),1);
1058 pos = s.find(ss, pos);
1064 while ( pos != std::string::npos )
1066 // std::cout << "*** find one "<<std::endl;
1067 s.replace(pos,1,cr.c_str(),1);
1068 pos = s.find(ss, pos);
1074 while ( pos != std::string::npos )
1076 // std::cout << "*** find one "<<std::endl;
1077 s.replace(pos,1,cr.c_str(),1);
1078 pos = s.find(ss, pos);
1079 } // std::cout << "AFTER=["<<s<<"]"<<std::endl;
1081 //=======================================================================
1083 //=========================================================================
1084 /// Write Graphviz-dot description in file
1085 void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
1086 BlackBox::Pointer parentblackbox,
1087 int detail, int level,
1088 bool instanceOrtype,
1089 bool relative_link )
1092 InputConnectorMapType::iterator i;
1094 std::string labelStr;
1095 std::string valueStr("");
1098 labelStr = bbGetName() ;
1100 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
1102 labelStr = bbGetName();
1103 labelStr = labelStr + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "] ";
1106 SubsBrackets(labelStr);
1109 labelStr = labelStr + " | {{ ";
1110 std::string tempStrTypeName;
1113 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1117 labelStr=labelStr+" | ";
1120 if (instanceOrtype==true)
1122 valueStr = this->bbGetInputAsString(i->first) + " = ";
1124 const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
1125 tempStrTypeName=id->GetTypeName();
1126 SubsBrackets(tempStrTypeName);
1127 std::string Name(i->first);
1129 labelStr=labelStr + "<"+i->first.c_str()+"> " + valueStr + Name.c_str() + " [" + tempStrTypeName.c_str() + "]";
1131 labelStr=labelStr+ " } | {";
1133 OutputConnectorMapType::iterator ii;
1134 for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii )
1138 labelStr=labelStr+" | ";
1141 if (instanceOrtype==true)
1143 valueStr = this->bbGetOutputAsString(ii->first) + " = ";
1145 const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first);
1146 tempStrTypeName=id->GetTypeName();
1147 SubsBrackets(tempStrTypeName);
1148 std::string Name(ii->first);
1150 labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + " ["+tempStrTypeName+"]";
1152 labelStr = labelStr+ " } }" ;
1156 bbWriteDotInputOutputName(ff,true,detail,level);
1157 std::string tmp ( bbGetTypeName() );
1161 url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
1163 url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
1165 fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
1166 // std::cout << labelStr << std::endl;
1169 if (GetThisPointer<BlackBox>()!=parentblackbox){
1170 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1174 Connection* con = i->second->GetConnection();
1176 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
1177 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
1179 a->bbWriteDotInputOutputName(ff,false,detail,level);
1182 fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
1185 b->bbWriteDotInputOutputName(ff,true,detail,level);
1188 fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
1190 fprintf(ff,"%s\n",";");
1194 } // if parentblackbox
1196 //=========================================================================
1201 //=========================================================================
1202 void BlackBox::bbShowRelations(BlackBox::Pointer parentblackbox,
1203 int detail, int level
1204 /*,Factory *factory*/ )
1207 if (this->bbGetDescriptor()->GetPackage())
1209 bbtkMessage("Help",1,"Black Box '"<<bbGetName()<<"' <"<<
1210 this->bbGetDescriptor()->GetPackage()->GetName()
1211 <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1215 bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1220 bbtkMessage("Help",1,"Up-to-date ["<<mMaxInputChangeTime<<","
1221 <<mMinOutputChangeTime<<"]"<<std::endl);
1225 bbtkMessage("Help",1,"Out-of-date ["<<mMaxInputChangeTime<<","
1226 <<mMinOutputChangeTime<<"]"<<std::endl);
1229 // bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
1230 // bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
1232 std::vector<std::string> iname;
1233 std::vector<std::string> ivalue;
1234 std::vector<std::string> iconn;
1235 std::vector<std::string> istatus;
1237 InputConnectorMapType::iterator i;
1238 unsigned int namelmax = 0;
1239 unsigned int valuelmax = 0;
1240 // unsigned int connlmax = 0;
1241 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1243 iname.push_back(i->first);
1244 if (iname.back().size()>namelmax) namelmax = iname.back().size();
1245 ivalue.push_back(bbGetInputAsString(i->first));
1246 if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
1248 Connection* con = i->second->GetConnection();
1250 s = con->GetOriginalBlackBoxFrom()->bbGetName();
1252 s += con->GetOriginalBlackBoxFromOutput();
1255 istatus.push_back(GetIOStatusString(i->second->GetStatus()));
1257 OutputConnectorMapType::iterator o;
1258 std::vector<std::string> oname;
1259 std::vector<std::string> ovalue;
1260 std::vector<std::vector<std::string> > oconn;
1261 std::vector<std::string> ostatus;
1262 for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o )
1264 oname.push_back(o->first);
1265 if (oname.back().size()>namelmax) namelmax = oname.back().size();
1266 ovalue.push_back(bbGetOutputAsString(o->first));
1267 if (ovalue.back().size()>valuelmax) valuelmax = ovalue.back().size();
1268 std::vector<std::string> ss;
1269 const std::vector<Connection*>& con
1270 = o->second->GetConnectionVector();
1271 std::vector<Connection*>::const_iterator c;
1272 for (c=con.begin();c!=con.end();++c)
1275 s = (*c)->GetOriginalBlackBoxTo()->bbGetName();
1277 s += (*c)->GetOriginalBlackBoxToInput();
1280 oconn.push_back(ss);
1281 ostatus.push_back(GetIOStatusString(o->second->GetStatus()));
1285 bbtkMessage("Help",1," * Inputs : "<<std::endl);
1287 bbtkMessage("Help",1," * No inputs"<<std::endl);
1289 std::vector<std::string>::iterator i1,i2,i3,i4;
1290 for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin(),i4=istatus.begin();
1291 i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end(),i4!=istatus.end();
1292 ++i1,++i2,++i3,++i4)
1294 std::string name(*i1);
1296 name.append(1+namelmax-name.size(),' ');
1297 std::string value(*i2);
1299 value.append(1+valuelmax-value.size(),' ');
1301 bbtkMessage("Help",1," '"<<name<<" = '"<<value<<" <-- '"
1304 bbtkMessage("Help",1," '"<<name<<" = '"<<value);
1305 bbtkMessage("Help",1," ["<<*i4<<"]"<<std::endl);
1309 bbtkMessage("Help",1," * Outputs : "<<std::endl);
1311 bbtkMessage("Help",1," * No outputs"<<std::endl);
1313 std::vector<std::vector<std::string> >::iterator i5;
1315 for (i1=oname.begin(),i2=ovalue.begin(),i5=oconn.begin(),i4=ostatus.begin();
1316 i1!=oname.end(),i2!=ovalue.end(),i5!=oconn.end(),i4!=ostatus.end();
1317 ++i1,++i2,++i4,++i5)
1319 std::string name(*i1);
1321 name.append(1+namelmax-name.size(),' ');
1322 std::string value(*i2);
1324 value.append(1+valuelmax-value.size(),' ');
1326 bbtkMessage("Help",1," '"<<name<<" = '"<<value);
1329 std::string pref = " '"+name+" = '"+value;
1330 for (i3=i5->begin();i3!=i5->end();++i3)
1332 bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'");
1333 pref.replace(0,pref.size(),pref.size(),' ');
1336 bbtkMessage("Help",1," ["<<*i4<<"]"<<std::endl);
1340 //=========================================================================
1342 static bool bbmgGlobalProcessingExecutionList = false;
1344 //=========================================================================
1345 void BlackBox::bbGlobalProcessExecutionList()
1347 bbtkDebugMessageInc("process",3,
1348 "=> BlackBox::bbGlobalProcessExecutionList()"
1350 if (bbmgGlobalProcessingExecutionList)
1352 bbtkDebugMessage("process",3,"BlackBox::bbGlobalProcessExecutionList() reentered !");
1355 bbmgGlobalProcessingExecutionList = true;
1357 std::set<BlackBox::WeakPointer>::iterator i;
1358 while (bbmgExecutionList.size()>0)
1360 i = bbmgExecutionList.begin();
1361 BlackBox::WeakPointer p = *i;
1362 bbmgExecutionList.erase(i);
1365 bbtkDebugMessage("process",4,
1367 p.lock()->bbGetFullName()<<std::endl);
1368 p.lock()->bbExecute(true);
1372 bbtkGlobalError("Strange error in BlackBox::bbGlobalProcessExecutionList() : Weak bb pointer in bbmgExecutionList is no more valid...");
1376 bbmgExecutionList.clear();
1377 bbtkDebugMessageDec("process",3,
1378 "<= BlackBox::bbGlobalProcessExecutionList()"
1381 bbmgGlobalProcessingExecutionList = false;
1384 //=========================================================================
1386 bool BlackBox::bbGlobalGetSomeBoxExecuting()
1388 return bbmgSomeBoxExecuting;
1391 void BlackBox::bbGlobalSetSomeBoxExecuting(bool b)
1393 bbmgSomeBoxExecuting = b;
1396 void BlackBox::bbGlobalSetFreezeExecution(bool b)
1398 bbmgFreezeExecution = b;
1401 bool BlackBox::bbGlobalGetFreezeExecution()
1403 return bbmgFreezeExecution;
1406 void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
1408 bbtkDebugMessage("process",3,"* bbGlobalAddToExecutionList("<<b->bbGetFullName()<<")"<<std::endl);
1409 if (bbmgGlobalProcessingExecutionList)
1411 bbtkDebugMessage("process",3,"bbGlobalAddToExecutionList called inside bbGlobalProcessExecutionList !");
1413 bbmgExecutionList.insert(b);
1417 //=========================================================================
1419 //=========================================================================
1420 void BlackBox::Check(bool recursive)
1422 bbtkMessage("debug",1,"*** Checking Black Box "<<(void*)this<<" ["<<bbGetFullName()
1423 <<"] ... OK"<<std::endl);
1425 //=========================================================================
1427 void BlackBox::bbUserOnShowWidget(std::string nameInput)
1429 bbtk::BlackBoxInputConnector *cc;
1430 cc = this->bbGetInputConnectorMap().find( nameInput.c_str() )->second;
1431 if (cc->GetConnection()!=NULL)
1433 cc->GetConnection()->GetBlackBoxFrom()->bbUserOnShow();
1439 } // EO namespace bbtk