2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkBlackBox.cxx,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.56 $
34 =========================================================================*/
40 * \brief Class bbtk::BlackBox : abstract black-box interface.
42 #include "bbtkBlackBox.h"
43 #include "bbtkPackage.h"
44 #include "bbtkMessageManager.h"
45 #include "bbtkFactory.h"
46 #include "bbtkBlackBoxOutputConnector.h"
48 #include "bbtkConfigurationFile.h"
49 #include "bbtkWxBlackBox.h"
58 static bool bbmgSomeBoxExecuting = false;
59 static bool bbmgFreezeExecution = false;
60 static std::set<BlackBox::WeakPointer> bbmgExecutionList;
62 //=========================================================================
64 BlackBox::Deleter::Deleter()
67 //=========================================================================
69 //=========================================================================
70 int BlackBox::Deleter::Delete(Object* p)
72 BlackBox* b = dynamic_cast<BlackBox*>(p);
75 bbtkInternalError("BlackBox::Deleter::Delete("<<p->GetObjectName()
77 <<"dynamic cast to BlackBox* failed !");
79 std::string name = p->GetObjectName();//b->bbGetNameWithParent();
80 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
83 BlackBoxDescriptor::WeakPointer desc = b->bbGetDescriptor();
84 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : deleting black box"<<std::endl);
86 int refs = b->bbDelete();
88 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : releasing descriptor"<<std::endl);
92 Package::WeakPointer pack = desc.lock()->GetPackage();
95 Package::ReleaseBlackBoxDescriptor(pack,desc);
97 bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : descriptor package expired (was not held by a package and the box was the last instance)"<<std::endl);
100 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);
102 bbtkDebugMessage("object",2,"<## BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
105 //=========================================================================
107 //=========================================================================
108 BlackBox::BlackBox(const std::string &name)
110 // bbmStatus(MODIFIED),
111 bbmInitialized(false),
114 bbmBoxProcessMode("Pipeline"),
115 bbLetRecursiveExecuteManualMode(false),
120 // bbmBoxProcessMode = "Pipeline";
121 //std::cout<<"JCP BlackBox::BlackBox(const std::string &name) name=" <<name
122 // <<"bbmBoxProcessMode="<<bbmBoxProcessMode<<std::endl;
123 bbtkBlackBoxDebugMessage("object",4,"==> BlackBox::BlackBox(\""
124 <<name<<"\")"<<std::endl);
125 bbtkBlackBoxDebugMessage("object",4,"<== BlackBox::BlackBox(\""
126 <<name<<"\")"<<std::endl);
128 //=========================================================================
130 //=========================================================================
131 BlackBox::BlackBox(const BlackBox&)
134 //=========================================================================
135 BlackBox::BlackBox(BlackBox& from, const std::string &name)
137 // bbmStatus(from.bbmStatus),
138 bbmInitialized(false),
141 bbmBoxProcessMode(from.bbmBoxProcessMode),
142 bbLetRecursiveExecuteManualMode(false),
147 //bbmBoxProcessMode = from.bbmBoxProcessMode;
148 //std::cout<<"JCP BlackBox::BlackBox(const std::string &name) name=" <<name
149 // <<"bbmBoxProcessMode="<<bbmBoxProcessMode<<std::endl;
150 bbtkBlackBoxDebugMessage("object",4,"==> BlackBox::BlackBox("
151 <<from.bbGetFullName()<<",\""
152 <<name<<"\")"<<std::endl);
153 bbtkBlackBoxDebugMessage("object",4,"<== BlackBox::BlackBox("
154 <<from.bbGetFullName()<<",\""
155 <<name<<"\")"<<std::endl);
157 //=========================================================================
160 //=========================================================================
161 BlackBox::~BlackBox()
163 bbtkBlackBoxDebugMessage("object",4,"==> BlackBox::~BlackBox() ["<<bbmName
165 this->bbDesallocateConnectors();
166 bbtkBlackBoxDebugMessage("object",4,"<== BlackBox::~BlackBox() ["<<bbmName
169 //=========================================================================
173 //=========================================================================
174 std::string BlackBox::bbGetFullName() const
176 return this->bbGetNameWithParent()+"<"+this->bbGetDescriptor()->GetTypeName()+">";
178 //=========================================================================
182 //=========================================================================
183 std::string BlackBox::bbGetNameWithParent() const
185 if (bbmParent.lock())
187 return bbmParent.lock()->bbGetNameWithParent() + ":" + bbmName;
192 //=========================================================================
194 //=========================================================================
195 void BlackBox::bbGetHelp(bool full) const
197 bbGetDescriptor()->GetHelp(full);
199 //=========================================================================
202 //=========================================================================
203 bool BlackBox::bbHasInput(const std::string& name) const
205 bbtkBlackBoxDebugMessage("kernel",8,
206 "BlackBox::bbHasInput(\""
209 bool r = ( bbGetDescriptor()->GetInputDescriptorMap().find(name)
210 != bbGetDescriptor()->GetInputDescriptorMap().end());
211 bbtkDebugDecTab("kernel",8);
214 //=========================================================================
217 //=========================================================================
218 bool BlackBox::bbHasOutput(const std::string& name) const
220 bbtkBlackBoxDebugMessage("kernel",8,"BlackBox::bbHasOutput(\""
223 bool r = ( bbGetDescriptor()->GetOutputDescriptorMap().find(name)
224 != bbGetDescriptor()->GetOutputDescriptorMap().end());
225 bbtkDebugDecTab("kernel",8);
228 //=========================================================================
231 //=========================================================================
232 TypeInfo BlackBox::bbGetOutputType( const std::string &name ) const
234 bbtkBlackBoxDebugMessage("kernel",8,
235 "BlackBox::bbGetOutputType(\""
238 TypeInfo r = bbGetDescriptor()->GetOutputDescriptor(name)->GetTypeInfo();
239 bbtkDebugDecTab("kernel",8);
242 //=========================================================================
244 //=========================================================================
245 TypeInfo BlackBox::bbGetInputType( const std::string &name ) const
247 bbtkBlackBoxDebugMessage("kernel",8,
248 "BlackBox::bbGetInputType(\""
251 TypeInfo r = bbGetDescriptor()->GetInputDescriptor(name)->GetTypeInfo();
252 bbtkDebugDecTab("kernel",8);
255 //=========================================================================
258 //=========================================================================
259 void BlackBox::bbAllocateConnectors()
261 bbtkBlackBoxDebugMessage("kernel",8,
262 "BlackBox::bbAllocateConnectors()"
265 MakeBlackBoxPointer(this,true);
267 const BlackBoxDescriptor::InputDescriptorMapType& imap
268 = bbGetDescriptor()->GetInputDescriptorMap();
269 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
270 for ( i = imap.begin(); i != imap.end(); ++i )
272 bbtkBlackBoxDebugMessage("kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
273 bbGetInputConnectorMap()[i->second->GetName()]
274 = new BlackBoxInputConnector(GetThisPointer<BlackBox>());
276 const BlackBoxDescriptor::OutputDescriptorMapType& omap
277 = bbGetDescriptor()->GetOutputDescriptorMap();
278 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
279 for ( o = omap.begin(); o != omap.end(); ++o )
281 bbtkBlackBoxDebugMessage("kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
282 bbGetOutputConnectorMap()[o->second->GetName()]
283 = new BlackBoxOutputConnector(GetThisPointer<BlackBox>());
286 //=========================================================================
289 //=========================================================================
290 void BlackBox::bbDesallocateConnectors()
292 bbtkBlackBoxDebugMessage("kernel",8,
293 "BlackBox::bbDesallocateConnectors()"
296 InputConnectorMapType::const_iterator i;
297 for ( i = bbGetInputConnectorMap().begin();
298 i != bbGetInputConnectorMap().end(); ++i )
300 bbtkBlackBoxDebugMessage("kernel",8,"* Delete \""<<i->first<<"\""<<std::endl);
303 OutputConnectorMapType::const_iterator o;
304 for ( o = bbGetOutputConnectorMap().begin();
305 o != bbGetOutputConnectorMap().end(); ++o )
307 bbtkBlackBoxDebugMessage("kernel",8,"* Delete \""<<o->first<<"\""<<std::endl);
311 bbtkDebugDecTab("kernel",8);
313 //=========================================================================
316 //=========================================================================
317 void BlackBox::bbCopyIOValues(BlackBox& from)
319 bbtkBlackBoxDebugMessage("kernel",1,
320 "BlackBox::bbCopyIOValues("
321 <<from.bbGetFullName()<<")"
323 // copies the input values
324 const BlackBoxDescriptor::InputDescriptorMapType& imap
325 = bbGetDescriptor()->GetInputDescriptorMap();
326 BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;
327 for ( i = imap.begin(); i != imap.end(); ++i )
329 if (! i->second->GetCopyConstruct() ) continue;
330 std::string input = i->second->GetName();
331 bbtkBlackBoxDebugMessage("kernel",2,"* Copying input "<<input<<std::endl);
332 this->bbSetInput(input, from.bbGetInput(input) );
334 // copies the output values
335 const BlackBoxDescriptor::OutputDescriptorMapType& omap
336 = bbGetDescriptor()->GetOutputDescriptorMap();
337 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
338 for ( o = omap.begin(); o != omap.end(); ++o )
340 if (! o->second->GetCopyConstruct() ) continue;
341 std::string output = o->second->GetName();
342 bbtkBlackBoxDebugMessage("kernel",2,"* Copying output "<<output<<std::endl);
343 this->bbSetOutput(output, from.bbGetOutput(output) );
346 bbtkDebugDecTab("kernel",9);
348 //=========================================================================
352 //=========================================================================
353 bool BlackBox::bbCanReact() const
355 return ( bbGlobalGetSomeBoxExecuting()
357 || Wx::IsSomeWindowAlive()
361 //=========================================================================
365 //=========================================================================
366 BlackBox::BoxProcessModeValue BlackBox::bbGetBoxProcessModeValue() const
368 const std::string& p = bbmBoxProcessMode;
370 (p == "P") || (p == "p") ||
371 (p == "Pipeline") || (p == "pipeline") ) return bbPipeline;
373 (p == "A") || (p == "a") ||
374 (p == "Always") || (p == "always") ) return bbAlways;
376 (p == "R") || (p == "r") ||
377 (p == "Reactive") || (p == "reactive") )
381 (p == "F") || (p == "f") ||
382 (p == "Flash") || (p == "flash") ) return Flash;
386 (p == "M") || (p == "m") ||
387 (p == "Manual") || (p == "manual") ) return bbManual;
389 bbtkError(bbGetFullName()<<" : BoxProcessMode value '"<<p
390 <<"' unknown. Possible values : "
391 <<"'0'/'P'/'p'/'Pipeline'/'pipeline' | "
392 <<"'1'/'A'/'a'/'Always'/'always' | "
393 <<"'2'/'R'/'r'/'Reactive'/'reactive'"
394 // <<"'3'/'F'/'f'/'Flash'/'flash'"
395 <<"'3'/'M'/'m'/'Manual'/'manual'"
398 //=========================================================================
400 //=========================================================================
401 bool BlackBox::bbBoxProcessModeIsReactive() const
403 return (bbGetBoxProcessModeValue() == bbReactive);
405 //=========================================================================
407 //=========================================================================
408 bool BlackBox::bbBoxProcessModeIsAlways() const
410 return (bbGetBoxProcessModeValue() == bbAlways);
412 //=========================================================================
415 //=========================================================================
416 bool BlackBox::bbBoxProcessModeIsManual() const
418 return (bbGetBoxProcessModeValue() == bbManual);
420 //=========================================================================
423 //=========================================================================
424 void BlackBox::bbAddOutputObserver(const std::string& output,
425 OutputChangeCallbackType f)
427 bbGetOutputConnector(output).AddChangeObserver(f);
429 //=========================================================================
431 //=========================================================================
432 void BlackBox::bbRemoveOutputObserver(const std::string& output_name,
433 OutputChangeCallbackType f)
435 bbtkError("BlackBox::RemoveChangeObserver NOT IMPLEMENTED");
437 //=========================================================================
440 //=========================================================================
441 void BlackBox::bbSetStatusAndPropagate(BlackBoxInputConnector* c,
444 bbtkBlackBoxDebugMessage("change",5,
445 "=> BlackBox::bbSetStatusAndPropagate(input,"
446 <<GetIOStatusString(s)<<")"
449 if (s==UPTODATE) bbtkError("bbSetStatusAndPropagate with status UPTODATE!");
454 if (bbGetBoxProcessModeValue() == Flash)
461 OutputConnectorMapType::const_iterator o;
462 for ( o = bbGetOutputConnectorMap().begin();
463 o != bbGetOutputConnectorMap().end(); ++o )
467 if (o->first=="BoxChange")
469 o->second->SetStatus(UPTODATE);
476 if (o->second->GetStatus()==UPTODATE)
477 // if ((o->second->GetStatus()==UPTODATE) || (o->second->GetStatus()==OUTOFDATE))
478 // if ((o->second->GetStatus()==UPTODATE) || (o->second->GetStatus()==MODIFIED))
480 o->second->SetStatus(OUTOFDATE);
481 o->second->SignalChange(GetThisPointer<BlackBox>(),o->first);
486 if ( ( bbBoxProcessModeIsReactive()
487 || (c==bbGetInputConnectorMap().find("BoxExecute")->second))
490 bbtkBlackBoxDebugMessage("change",2,
491 "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
492 bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
494 bbtkBlackBoxDebugMessage("change",5,
495 "<= BlackBox::bbSetStatusAndPropagate(input)"
498 //=========================================================================
501 //=========================================================================
502 void BlackBox::bbSignalOutputModification(bool reaction)
504 bbtkBlackBoxDebugMessage("change",5,
505 "=> BlackBox::bbSignalOutputModification("
509 OutputConnectorMapType::iterator i;
510 for ( i = bbGetOutputConnectorMap().begin();
511 i != bbGetOutputConnectorMap().end(); ++i)
513 // std::cout << "Stat = "
514 //<<GetIOStatusString(i->second->GetStatus())
516 // LG : CANNOT SIGNAL ONLY WHEN UPTODATE
517 // See bbtkSampleOutputObserver
518 // if (i->second->GetStatus()==UPTODATE)
520 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
524 if (reaction) bbGlobalProcessExecutionList();
526 bbtkBlackBoxDebugMessage("change",5,
527 "<= BlackBox::bbSignalOutputModification()"
530 //=========================================================================
533 //=========================================================================
534 void BlackBox::bbSignalOutputModification(const std::string& output,
537 bbtkBlackBoxDebugMessage("change",5,
538 "=> BlackBox::bbSignalOutputModification("
539 <<output<<","<<reaction<<")"
542 OutputConnectorMapType::iterator i =
543 bbGetOutputConnectorMap().find(output);
546 if ( i == bbGetOutputConnectorMap().end() )
548 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
551 // if (i->second->GetStatus()==UPTODATE)
553 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
554 // Has to notify the output "BoxChange" also
555 if (output != "BoxChange")
557 i = bbGetOutputConnectorMap().find("BoxChange");
558 if ( i != bbGetOutputConnectorMap().end() )
560 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
563 if (reaction) bbGlobalProcessExecutionList();
566 bbtkBlackBoxDebugMessage("change",5,
567 "<= BlackBox::bbSignalOutputModification("
571 //=========================================================================
572 //=========================================================================
573 void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
576 bbtkBlackBoxDebugMessage("change",5,
577 "=> BlackBox::bbSignalOutputModification(vector of outputs)"
579 OutputConnectorMapType::iterator i;
580 std::vector<std::string>::const_iterator o;
581 bool changed = false;
582 for (o=output.begin();o!=output.end();++o)
584 // the output "BoxChange" must be signaled **AFTER** all others
585 if (*o == "BoxChange") continue;
586 // Look for the connector
587 i = bbGetOutputConnectorMap().find(*o);
588 if ( i == bbGetOutputConnectorMap().end() )
590 bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
593 // if (i->second->GetStatus()==UPTODATE)
595 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
599 // Has to notify the output "BoxChange" also
600 i = bbGetOutputConnectorMap().find("BoxChange");
601 if ( changed && (i != bbGetOutputConnectorMap().end()))
603 // if (i->second->GetStatus()==UPTODATE)
605 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first);
606 if (reaction) bbGlobalProcessExecutionList();
610 bbtkBlackBoxDebugMessage("change",5,
611 "<= BlackBox::bbSignalOutputModification(vector of outputs)"
614 //=========================================================================
622 //=========================================================================
623 /// Main processing method of the box.
624 void BlackBox::bbExecute(bool force)
626 bbtkBlackBoxDebugMessage("process",2,
627 "=> BlackBox::bbExecute("<<(int)force<<")"
630 // If already executing : return
632 if (bbGetExecuting())
634 bbtkBlackBoxDebugMessage("process",2,
635 " -> already executing : abort"<<std::endl);
640 // If execution frozen : return
641 if (bbGlobalGetFreezeExecution())
643 bbtkBlackBoxDebugMessage("process",2,
644 " -> FreezeExecution global flag is 'true' : abort execution"<<std::endl);
649 // If force is true then update is triggered even if the box is UPTODATE
650 // if (force) bbSetModifiedStatus();
652 if ( bbBoxProcessModeIsManual() )
654 bbLetRecursiveExecuteManualMode = true;
658 // Calls the main recursive execution method
659 bbRecursiveExecute(Connection::Pointer());
662 if ( bbBoxProcessModeIsManual() )
664 bbLetRecursiveExecuteManualMode = false;
668 bbtkBlackBoxDebugMessage("process",2,
669 "<= BlackBox::bbExecute()"
672 //=========================================================================
674 //=========================================================================
675 void BlackBox::bbInitializeProcessing()
679 bbtkBlackBoxDebugMessage("process",2,"** Initialize processing"
681 this->bbRecursiveInitializeProcessing();
682 bbmInitialized = true;
685 //=========================================================================
687 //=========================================================================
688 void BlackBox::bbFinalizeProcessing()
692 bbtkBlackBoxDebugMessage("process",2,"** Finalize processing"
694 this->bbRecursiveFinalizeProcessing();
695 bbmInitialized = false;
698 //=========================================================================
701 //=========================================================================
702 void BlackBox::bbRecursiveExecute( Connection::Pointer caller )
704 bbtkBlackBoxDebugMessage("process",3,
705 "=> BlackBox::bbRecursiveExecute("
706 <<(caller?caller->GetFullName():"0")<<")"
709 // If already executing : return
710 if (bbGetExecuting())
712 bbtkBlackBoxDebugMessage("process",3,
713 " -> already executing : abort"<<std::endl);
717 // If not initialized do it
718 bbInitializeProcessing();
720 bbSetExecuting(true);
721 bool wasExecuting = bbGlobalGetSomeBoxExecuting();
722 bbGlobalSetSomeBoxExecuting(true);
724 // Creates the window if the black box has one
725 this->bbCreateWindow();
727 // Updates its inputs
731 // IOStatus s=OUTOFDATE;
732 // IOStatus s=MODIFIED;
735 if ( ( bbBoxProcessModeIsManual()==false ) ||
736 ( (bbBoxProcessModeIsManual()==true) && (bbLetRecursiveExecuteManualMode==true) ) )
738 s = bbUpdateInputs();
741 if ( (s != UPTODATE) || bbBoxProcessModeIsAlways() )
743 // Displays the window (WxBlackbox)
744 // bbShowWindow(caller);
746 // Actual processing (virtual)
747 if ( ( bbBoxProcessModeIsManual()==false ) ||
748 ( (bbBoxProcessModeIsManual()==true)&&(bbLetRecursiveExecuteManualMode==true) )
751 //printf("EED BlackBox::bbRecursiveExecute bbProcess start %s \n", bbGetFullName().c_str() );
753 //printf("EED BlackBox::bbRecursiveExecute bbProcess end %s \n", bbGetFullName().c_str() );
756 //EED ups if ((bbBoxProcessModeIsManual()==true) && (bbLetRecursiveExecuteManualMode==false))
758 //EED ups bbSignalOutputModification(true);
762 // Update the I/O statuses
763 bbComputePostProcessStatus();
765 // Test output status...
766 OutputConnectorMapType::iterator o;
767 for ( o = bbGetOutputConnectorMap().begin();
768 o!= bbGetOutputConnectorMap().end(); ++o)
770 if (o->second->GetStatus() != UPTODATE)
772 bbtkWarning("BlackBox::bbRecursiveExecute: "
773 <<"all inputs are Up-to-date but output '"
774 <<o->first<<"' is Out-of-date ???");
778 bbtkBlackBoxDebugMessage("process",3," -> Up-to-date : nothing to do"
782 // Shows the window if the black box has one
783 this->bbShowWindow();
786 bbtkBlackBoxDebugMessage("process",3,
787 "<= BlackBox::bbRecursiveExecute()"
790 bbSetExecuting(false);
791 bbGlobalSetSomeBoxExecuting(wasExecuting);
795 //=========================================================================
801 //=========================================================================
802 IOStatus BlackBox::bbUpdateInputs()
804 bbtkBlackBoxDebugMessage("process",4,
805 "=> BlackBox::bbUpdateInputs()"
808 IOStatus s = UPTODATE;
810 InputConnectorMapType::iterator i;
811 for ( i = bbGetInputConnectorMap().begin();
812 i!= bbGetInputConnectorMap().end(); ++i)
814 // if (i->first=="WinHide") continue;
815 // If input type is Void : no recurse
816 //if ( bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo()
819 bbtkBlackBoxDebugMessage("change",2,
821 <<"': status before update = '"
822 <<GetIOStatusString(i->second->GetStatus())
824 i->second->RecursiveExecute();
825 IOStatus t = i->second->GetStatus();
827 bbtkBlackBoxDebugMessage("change",2,
829 <<"': status before process = '"
830 <<GetIOStatusString(i->second->GetStatus())
834 bbtkBlackBoxDebugMessage("process",4,
835 "<= BlackBox::bbUpdateInputs()"
839 //=========================================================================
841 //==================================================================
842 void BlackBox::bbComputePostProcessStatus()
844 bbtkBlackBoxDebugMessage("process",4,
845 "=> BlackBox::bbComputePostProcessStatus()"
848 IOStatus new_output_status = UPTODATE;
849 if (bbBoxProcessModeIsAlways()) new_output_status = OUTOFDATE;
851 // Update the input statuses
852 InputConnectorMapType::iterator i;
853 for ( i = bbGetInputConnectorMap().begin();
854 i!= bbGetInputConnectorMap().end(); ++i)
856 IOStatus t = i->second->GetStatus();
857 if (t == OUTOFDATE) new_output_status = OUTOFDATE;
858 // A previously MODIFIED status turns to UPTODATE
859 if (t==MODIFIED) i->second->SetStatus(UPTODATE);
860 bbtkBlackBoxDebugMessage("change",2,
861 "Input '"<<i->first<<"' : "
862 << GetIOStatusString(t) << " -> "
863 << GetIOStatusString(i->second->GetStatus())
866 bbtkBlackBoxDebugMessage("change",2,
867 "New output status : "
868 << GetIOStatusString(new_output_status)
870 // Update the output statuses
871 OutputConnectorMapType::iterator o;
872 for ( o = bbGetOutputConnectorMap().begin();
873 o!= bbGetOutputConnectorMap().end(); ++o)
876 //EED if ( ( bbBoxProcessModeIsManual()==false ) ||
877 //EED ( (bbBoxProcessModeIsManual()==true) && (bbLetRecursiveExecuteManualMode==true) )
880 o->second->SetStatus(new_output_status);
882 //EED if (( (bbBoxProcessModeIsManual()==true) && (bbLetRecursiveExecuteManualMode==false) ) )
884 //EED o->second->SetStatus(UPTODATE);
886 //EED } // Manual analysis
890 bbtkBlackBoxDebugMessage("process",4,
891 "<= BlackBox::bbComputePostProcessStatus()"
894 //==================================================================
896 //=========================================================================
897 void BlackBox::bbConnectInput( const std::string& name, Connection* c)
899 bbtkBlackBoxDebugMessage("connection",2,
900 "==> BlackBox::bbConnectInput(\""
901 <<name<<"\","<<c->GetFullName()<<")"
904 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
905 if (i==bbGetInputConnectorMap().end())
907 bbtkError("no input called '"<<name<<"'");
909 i->second->SetConnection(c);
910 // The input *MUST* be set OUTOFDATE to update its input on next execution
911 bbSetStatusAndPropagate(i->second,OUTOFDATE);
913 bbtkBlackBoxDebugMessage("connection",2,
914 "<== BlackBox::bbConnectInput(\""
915 <<name<<"\","<<c->GetFullName()<<")"
918 //=========================================================================
921 //=========================================================================
922 void BlackBox::bbConnectOutput( const std::string& name, Connection* c)
924 bbtkBlackBoxDebugMessage("connection",2,
925 "==> BlackBox::bbConnectOutput(\""<<name<<"\","
926 <<c->GetFullName()<<")"
929 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
930 if (i==bbGetOutputConnectorMap().end())
932 bbtkError("no output called '"<<name<<"'");
934 i->second->SetConnection(c);
936 bbtkBlackBoxDebugMessage("connection",2,
937 "<== BlackBox::bbConnectOutput(\""<<name<<"\","
938 <<c->GetFullName()<<")"
941 //=========================================================================
944 //=========================================================================
945 void BlackBox::bbDisconnectInput( const std::string& name, Connection* c)
948 bbtkBlackBoxDebugMessage("connection",2,
949 "==> BlackBox::bbDisconnectInput(\""<<name
950 <<"\","<<c->GetFullName()<<")"
955 bbtkBlackBoxDebugMessage("connection",2,"c==0"<<std::endl);
959 InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
960 if (i==bbGetInputConnectorMap().end())
962 bbtkError("no input called '"<<name<<"'");
964 i->second->UnsetConnection(c);
966 bbtkBlackBoxDebugMessage("connection",2,
967 "<== BlackBox::bbDisconnectInput(\""<<name
968 <<"\","<<c->GetFullName()<<")"
972 //=========================================================================
975 //=========================================================================
976 void BlackBox::bbDisconnectOutput( const std::string& name, Connection* c)
978 bbtkBlackBoxDebugMessage("connection",2,
979 "==> BlackBox::bbDisconnectOutput(\""<<name
980 <<"\","<<c->GetFullName()<<")"
985 bbtkBlackBoxDebugMessage("connection",2,"c==0"<<std::endl);
989 OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
990 if (i==bbGetOutputConnectorMap().end())
992 bbtkError("no output called '"<<name<<"'");
994 i->second->UnsetConnection(c);
996 bbtkBlackBoxDebugMessage("connection",2,
997 "<== BlackBox::bbDisconnectOutput(\""<<name
998 <<"\","<<c->GetFullName()<<")"
1001 //=========================================================================
1025 //=========================================================================
1026 void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
1028 fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
1030 //=========================================================================
1033 //=========================================================================
1034 std::string BlackBox::bbGetOutputAsString( const std::string &output )
1037 // Looks for the adaptor
1038 if (bbGetOutputType(output).name() != typeid(std::string).name() )
1041 Package::Pointer p = bbGetDescriptor()->GetPackage();
1042 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
1044 Factory::Pointer f = p->GetFactorySet().begin()->lock();
1045 BlackBox::Pointer a;
1049 bbGetOutputType(output),
1050 typeid(std::string),
1052 } catch (bbtk::Exception e)
1057 a->bbSetInput("In",bbGetOutput(output));
1059 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
1061 v="? (no adaptor found)";
1066 v="? (no factory found)";
1072 v = bbGetOutput(output).unsafe_get<std::string>() ;
1076 //=========================================================================
1078 //=========================================================================
1079 std::string BlackBox::bbGetInputAsString( const std::string &input )
1082 // Looks for the adaptor
1083 if (bbGetInputType(input) != typeid(std::string))
1086 Package::Pointer p = bbGetDescriptor()->GetPackage();
1087 if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
1089 Factory::Pointer f = p->GetFactorySet().begin()->lock();
1090 BlackBox::Pointer a;
1094 bbGetInputType(input),
1095 typeid(std::string),
1097 }catch (bbtk::Exception e)
1103 a->bbSetInput("In",bbGetInput(input));
1105 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
1109 v="? (no adaptor found)";
1114 v="? (no factory found)";
1119 v = bbGetInput(input).unsafe_get<std::string>() ;
1123 //=======================================================================
1125 //=======================================================================
1126 // Replaces substrings "<" by "["
1127 void SubsBrackets ( std::string& s )
1129 // std::cout << "BEFORE=["<<s<<"]"<<std::endl;
1130 std::string ss("<");
1131 std::string::size_type pos = 0;
1133 std::string cr("[");
1134 while ( pos != std::string::npos )
1136 // std::cout << "*** find one "<<std::endl;
1137 s.replace(pos,1,cr.c_str(),1);
1138 pos = s.find(ss, pos);
1144 while ( pos != std::string::npos )
1146 // std::cout << "*** find one "<<std::endl;
1147 s.replace(pos,1,cr.c_str(),1);
1148 pos = s.find(ss, pos);
1154 while ( pos != std::string::npos )
1156 // std::cout << "*** find one "<<std::endl;
1157 s.replace(pos,1,cr.c_str(),1);
1158 pos = s.find(ss, pos);
1159 } // std::cout << "AFTER=["<<s<<"]"<<std::endl;
1161 //=======================================================================
1163 //=========================================================================
1164 /// Write Graphviz-dot description in file
1165 void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
1166 BlackBox::Pointer parentblackbox,
1167 int detail, int level,
1168 bool instanceOrtype,
1169 bool relative_link )
1172 InputConnectorMapType::iterator i;
1174 std::string labelStr;
1175 std::string valueStr("");
1178 labelStr = bbGetName() ;
1180 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
1182 labelStr = bbGetName();
1183 labelStr = labelStr + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "] ";
1186 SubsBrackets(labelStr);
1189 labelStr = labelStr + " | {{ ";
1190 std::string tempStrTypeName;
1193 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1197 labelStr=labelStr+" | ";
1200 if (instanceOrtype==true)
1202 valueStr = this->bbGetInputAsString(i->first) + " = ";
1204 const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
1205 tempStrTypeName=id->GetTypeName();
1206 SubsBrackets(tempStrTypeName);
1207 std::string Name(i->first);
1209 labelStr=labelStr + "<"+i->first.c_str()+"> " + valueStr + Name.c_str() + " [" + tempStrTypeName.c_str() + "]";
1211 labelStr=labelStr+ " } | {";
1213 OutputConnectorMapType::iterator ii;
1214 for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii )
1218 labelStr=labelStr+" | ";
1221 if (instanceOrtype==true)
1223 valueStr = this->bbGetOutputAsString(ii->first) + " = ";
1225 const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first);
1226 tempStrTypeName=id->GetTypeName();
1227 SubsBrackets(tempStrTypeName);
1228 std::string Name(ii->first);
1230 labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + " ["+tempStrTypeName+"]";
1232 labelStr = labelStr+ " } }" ;
1236 bbWriteDotInputOutputName(ff,true,detail,level);
1237 std::string tmp ( bbGetTypeName() );
1241 url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
1243 url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
1245 fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
1246 // std::cout << labelStr << std::endl;
1249 if (GetThisPointer<BlackBox>()!=parentblackbox){
1250 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1254 Connection* con = i->second->GetConnection();
1256 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
1257 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
1259 a->bbWriteDotInputOutputName(ff,false,detail,level);
1262 fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
1265 b->bbWriteDotInputOutputName(ff,true,detail,level);
1268 fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
1270 fprintf(ff,"%s\n",";");
1274 } // if parentblackbox
1276 //=========================================================================
1281 //=========================================================================
1282 void BlackBox::bbPrintHelp(BlackBox::Pointer parentblackbox,
1283 int detail, int level
1284 /*,Factory *factory*/ )
1287 if (this->bbGetDescriptor()->GetPackage())
1289 bbtkBlackBoxMessage("help",1,"Black Box '"<<bbGetName()<<"' <"<<
1290 this->bbGetDescriptor()->GetPackage()->GetName()
1291 <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1295 bbtkBlackBoxMessage("help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1300 bbtkBlackBoxMessage("help",1,"Up-to-date ["<<mMaxInputChangeTime<<","
1301 <<mMinOutputChangeTime<<"]"<<std::endl);
1305 bbtkBlackBoxMessage("help",1,"Out-of-date ["<<mMaxInputChangeTime<<","
1306 <<mMinOutputChangeTime<<"]"<<std::endl);
1309 // bbtkBlackBoxMessage("help",1," "<<GetDescription()<<std::endl);
1310 // bbtkBlackBoxMessage("help",1," By : "<<GetAuthor()<<std::endl);
1312 std::vector<std::string> iname;
1313 std::vector<std::string> ivalue;
1314 std::vector<std::string> iconn;
1315 std::vector<std::string> istatus;
1317 InputConnectorMapType::iterator i;
1318 unsigned int namelmax = 0;
1319 unsigned int valuelmax = 0;
1320 // unsigned int connlmax = 0;
1321 for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i )
1323 iname.push_back(i->first);
1324 if (iname.back().size()>namelmax) namelmax = iname.back().size();
1325 ivalue.push_back(bbGetInputAsString(i->first));
1326 if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
1328 Connection* con = i->second->GetConnection();
1330 s = con->GetOriginalBlackBoxFrom()->bbGetName();
1332 s += con->GetOriginalBlackBoxFromOutput();
1335 istatus.push_back(GetIOStatusString(i->second->GetStatus()));
1337 OutputConnectorMapType::iterator o;
1338 std::vector<std::string> oname;
1339 std::vector<std::string> ovalue;
1340 std::vector<std::vector<std::string> > oconn;
1341 std::vector<std::string> ostatus;
1342 for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o )
1344 oname.push_back(o->first);
1345 if (oname.back().size()>namelmax)
1346 namelmax = oname.back().size();
1347 ovalue.push_back(bbGetOutputAsString(o->first));
1348 if (ovalue.back().size()>valuelmax)
1349 valuelmax = ovalue.back().size();
1350 std::vector<std::string> ss;
1351 const std::vector<Connection*>& con
1352 = o->second->GetConnectionVector();
1353 std::vector<Connection*>::const_iterator c;
1354 for (c=con.begin();c!=con.end();++c)
1357 s = (*c)->GetOriginalBlackBoxTo()->bbGetName();
1359 s += (*c)->GetOriginalBlackBoxToInput();
1362 oconn.push_back(ss);
1363 ostatus.push_back(GetIOStatusString(o->second->GetStatus()));
1367 bbtkBlackBoxMessage("help",1," * Inputs : "<<std::endl);
1369 bbtkBlackBoxMessage("help",1," * No inputs"<<std::endl);
1371 std::vector<std::string>::iterator i1,i2,i3,i4;
1372 for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin(),i4=istatus.begin();
1373 i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end(),i4!=istatus.end();
1374 ++i1,++i2,++i3,++i4)
1376 std::string name(*i1);
1378 name.append(1+namelmax-name.size(),' ');
1379 std::string value(*i2);
1381 value.append(1+valuelmax-value.size(),' ');
1383 bbtkBlackBoxMessage("help",1," '"<<name<<" = '"<<value<<" <-- '" <<*i3<<"'");
1385 bbtkBlackBoxMessage("help",1," '"<<name<<" = '"<<value);
1386 bbtkBlackBoxMessage("help",1," ["<<*i4<<"]"<<std::endl);
1390 bbtkBlackBoxMessage("help",1," * Outputs : "<<std::endl);
1392 bbtkBlackBoxMessage("help",1," * No outputs"<<std::endl);
1394 std::vector<std::vector<std::string> >::iterator i5;
1396 for (i1=oname.begin(),i2=ovalue.begin(),i5=oconn.begin(),i4=ostatus.begin();
1397 i1!=oname.end(),i2!=ovalue.end(),i5!=oconn.end(),i4!=ostatus.end();
1398 ++i1,++i2,++i4,++i5)
1400 std::string name(*i1);
1402 name.append(1+namelmax-name.size(),' ');
1403 std::string value(*i2);
1405 value.append(1+valuelmax-value.size(),' ');
1407 bbtkBlackBoxMessage("help",1," '"<<name<<" = '"<<value);
1410 std::string pref = " '"+name+" = '"+value;
1411 for (i3=i5->begin();i3!=i5->end();++i3)
1413 bbtkBlackBoxMessage("help",1,pref<<" --> '"<<*i3<<"'");
1414 pref.replace(0,pref.size(),pref.size(),' ');
1417 bbtkBlackBoxMessage("help",1," ["<<*i4<<"]"<<std::endl);
1421 //=========================================================================
1440 static bool bbmgGlobalProcessingExecutionList = false;
1442 //=========================================================================
1443 void BlackBox::bbGlobalProcessExecutionList()
1445 bbtkDebugMessage("process",3,
1446 "=> BlackBox::bbGlobalProcessExecutionList()"
1448 if (bbmgGlobalProcessingExecutionList)
1450 bbtkDebugMessage("process",3,"BlackBox::bbGlobalProcessExecutionList() reentered !");
1453 bbmgGlobalProcessingExecutionList = true;
1455 std::set<BlackBox::WeakPointer>::iterator i;
1456 while (bbmgExecutionList.size()>0)
1458 i = bbmgExecutionList.begin();
1459 BlackBox::WeakPointer p = *i;
1460 bbmgExecutionList.erase(i);
1463 bbtkDebugMessage("process",4,
1465 p.lock()->bbGetName()<<"'"<<std::endl);
1466 p.lock()->bbExecute(true);
1470 bbtkGlobalError("Strange error in BlackBox::bbGlobalProcessExecutionList() : Weak bb pointer in bbmgExecutionList is no more valid...");
1474 bbmgExecutionList.clear();
1475 bbtkDebugMessage("process",3,
1476 "<= BlackBox::bbGlobalProcessExecutionList()"
1479 bbmgGlobalProcessingExecutionList = false;
1483 //=========================================================================
1485 bool BlackBox::bbGlobalGetSomeBoxExecuting()
1487 return bbmgSomeBoxExecuting;
1490 void BlackBox::bbGlobalSetSomeBoxExecuting(bool b)
1492 bbmgSomeBoxExecuting = b;
1495 void BlackBox::bbGlobalSetFreezeExecution(bool b)
1497 bbmgFreezeExecution = b;
1500 bool BlackBox::bbGlobalGetFreezeExecution()
1502 return bbmgFreezeExecution;
1505 void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
1507 bbtkDebugMessage("process",3,"* bbGlobalAddToExecutionList("<<b->bbGetName()<<")"<<std::endl);
1508 if (bbmgGlobalProcessingExecutionList)
1510 bbtkDebugMessage("process",3,"bbGlobalAddToExecutionList called inside bbGlobalProcessExecutionList !");
1512 bbmgExecutionList.insert(b);
1516 //=========================================================================
1518 //=========================================================================
1519 void BlackBox::Check(bool recursive)
1521 bbtkBlackBoxMessage("debug",1,"*** Checking"
1522 <<" ... OK"<<std::endl);
1524 //=========================================================================
1529 } // EO namespace bbtk