]> Creatis software - bbtk.git/blob - kernel/src/bbtkBlackBox.cxx
*** MAJOR CHANGE *** NOT WORKING YET !!!
[bbtk.git] / kernel / src / bbtkBlackBox.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkBlackBox.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/05/14 14:43:33 $
6   Version:   $Revision: 1.44 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
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.
20 *
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
25 *  liability. 
26 *
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 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief Class bbtk::BlackBox : abstract black-box interface. 
34  */
35 #include "bbtkBlackBox.h"
36 #include "bbtkPackage.h"
37 #include "bbtkMessageManager.h"
38 #include "bbtkFactory.h"
39 #include "bbtkBlackBoxOutputConnector.h"
40
41 #include "bbtkConfigurationFile.h"
42 #include "bbtkWxBlackBox.h"
43 #include "bbtkWx.h"
44
45 #include <fstream>
46 //#include <vector>
47
48
49 namespace bbtk
50 {
51
52
53   static bool bbmgSomeBoxExecuting = false;
54   static bool bbmgFreezeExecution = false;
55   static std::set<BlackBox::WeakPointer> bbmgExecutionList;
56
57   //=========================================================================
58
59   BlackBox::Deleter::Deleter()
60   {
61   }
62   //=========================================================================
63   
64   //=========================================================================
65   int BlackBox::Deleter::Delete(Object* p)
66   {
67     BlackBox* b = dynamic_cast<BlackBox*>(p);
68     if (!b)
69       {
70         bbtkInternalError("BlackBox::Deleter::Delete("<<p->GetObjectName()
71                           <<"["<<p<<"]) : "
72                           <<"dynamic cast to BlackBox* failed !");
73       }
74     std::string name = p->GetObjectName();//b->bbGetNameWithParent();
75     bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
76
77
78     BlackBoxDescriptor::WeakPointer desc = b->bbGetDescriptor();
79     bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : deleting black box"<<std::endl);
80     
81     int refs = b->bbDelete();
82
83     bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : releasing descriptor"<<std::endl);
84     
85     if (!desc.expired()) 
86       {
87         Package::WeakPointer pack = desc.lock()->GetPackage();
88         if (!pack.expired()) 
89           {
90             Package::ReleaseBlackBoxDescriptor(pack,desc);
91           }
92         else 
93           {
94             bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : descriptor package expired (was not held by a package and the box was the last instance)"<<std::endl);
95           }
96       }
97     else
98       {
99         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);
100       }
101     bbtkDebugMessage("object",2,"<## BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
102     return refs;
103   }
104   //=========================================================================
105
106   //=========================================================================
107   BlackBox::BlackBox(const std::string &name) 
108     : 
109     //    bbmStatus(MODIFIED), 
110     bbmInitialized(false),
111     bbmExecuting(false),
112     bbmName(name),
113     bbmBoxProcessMode("Pipeline"),
114     bbmParent()
115     
116   {
117     bbtkDebugMessage("object",4,"==> BlackBox::BlackBox(\""
118                      <<name<<"\")"<<std::endl);
119     bbtkDebugMessage("object",4,"<== BlackBox::BlackBox(\""
120                      <<name<<"\")"<<std::endl);
121   }
122   //=========================================================================
123
124   //=========================================================================
125   BlackBox::BlackBox(const BlackBox&)
126   {}
127
128   //=========================================================================
129   BlackBox::BlackBox(BlackBox& from, const std::string &name) 
130     :
131     //    bbmStatus(from.bbmStatus), 
132     bbmInitialized(false),
133     bbmExecuting(false),
134     bbmName(name), 
135     bbmBoxProcessMode(from.bbmBoxProcessMode),
136     bbmParent()
137     
138   {
139     bbtkDebugMessage("object",4,"==> BlackBox::BlackBox("
140                      <<from.bbGetFullName()<<",\""
141                      <<name<<"\")"<<std::endl);
142     bbtkDebugMessage("object",4,"<== BlackBox::BlackBox("
143                      <<from.bbGetFullName()<<",\""
144                      <<name<<"\")"<<std::endl);
145   }
146   //=========================================================================
147
148
149   //=========================================================================
150   BlackBox::~BlackBox()
151   {
152     bbtkDebugMessage("object",4,"==> BlackBox::~BlackBox() ["<<bbmName
153                      <<"]"<<std::endl);
154     this->bbDesallocateConnectors();
155     bbtkDebugMessage("object",4,"<== BlackBox::~BlackBox() ["<<bbmName
156                      <<"]"<<std::endl);
157   }
158   //=========================================================================
159
160
161
162   //=========================================================================
163   std::string BlackBox::bbGetFullName() const
164   { 
165     return this->bbGetNameWithParent()+"<"+this->bbGetDescriptor()->GetTypeName()+">";
166   }
167   //=========================================================================
168      
169
170
171   //=========================================================================
172   /// Returns the name with the name of the parent prepended if any
173   std::string BlackBox::bbGetNameWithParent() const
174   {
175     if (bbmParent.lock()) 
176       {
177         return bbmParent.lock()->bbGetNameWithParent() + ":" + bbmName;
178       }
179     else 
180       {
181         return bbmName;
182       }
183   } 
184   //=========================================================================
185
186   //=========================================================================
187   /// Prints the Help on the BlackBox type 
188   void BlackBox::bbGetHelp(bool full) const
189   {
190     bbGetDescriptor()->GetHelp(full); 
191   }
192   //=========================================================================
193
194
195   //=========================================================================
196   /// Returns true if the UserBlackBox has an input of name name
197   bool BlackBox::bbHasInput(const std::string& name) const
198   {
199     bbtkDebugMessageInc("Kernel",8,
200                         "BlackBox::bbHasInput(\""
201                         <<name<<"\") ["<<bbGetFullName()<<"]"
202                         <<std::endl);
203     bool r = ( bbGetDescriptor()->GetInputDescriptorMap().find(name)
204                != bbGetDescriptor()->GetInputDescriptorMap().end());
205     bbtkDebugDecTab("Kernel",8);
206     return r;
207   }
208   //=========================================================================
209
210
211   //=========================================================================  
212   /// Returns true if the UserBlackBox has an output of name name
213   bool BlackBox::bbHasOutput(const std::string& name) const
214   {
215     bbtkDebugMessageInc("Kernel",8,"BlackBox::bbHasOutput(\""
216                         <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
217     bool r = ( bbGetDescriptor()->GetOutputDescriptorMap().find(name)
218                != bbGetDescriptor()->GetOutputDescriptorMap().end());
219     bbtkDebugDecTab("Kernel",8);
220     return r;
221   }
222   //=========================================================================
223
224
225   //=========================================================================  
226   ///  Gets the output type of a given name
227   TypeInfo BlackBox::bbGetOutputType( const std::string &name ) const 
228   {
229     bbtkDebugMessageInc("Kernel",8,
230                         "BlackBox::bbGetOutputType(\""
231                         <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
232     TypeInfo r = bbGetDescriptor()->GetOutputDescriptor(name)->GetTypeInfo();
233     bbtkDebugDecTab("Kernel",8); 
234     return r;
235   }
236   //=========================================================================
237
238   //=========================================================================
239   ///  Gets the input type of a given name
240   TypeInfo BlackBox::bbGetInputType( const std::string &name ) const
241   {
242     bbtkDebugMessageInc("Kernel",8,
243                         "BlackBox::bbGetInputType(\""
244                         <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
245     TypeInfo r = bbGetDescriptor()->GetInputDescriptor(name)->GetTypeInfo();
246     bbtkDebugDecTab("Kernel",8);
247     return r;
248   }
249   //=========================================================================
250
251
252   //=========================================================================
253   /// Allocates the i/o connectors of the black box
254   void BlackBox::bbAllocateConnectors()
255   {  
256     bbtkDebugMessageInc("Kernel",8,
257                         "BlackBox::bbAllocateConnectors() ["
258                         <<bbGetFullName()<<"]"
259                         <<std::endl);                                   
260     const BlackBoxDescriptor::InputDescriptorMapType& imap 
261       = bbGetDescriptor()->GetInputDescriptorMap(); 
262     BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;       
263     for ( i = imap.begin(); i != imap.end(); ++i )                      
264       {                                                                 
265         bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
266         bbGetInputConnectorMap()[i->second->GetName()] 
267           = new BlackBoxInputConnector(GetThisPointer<BlackBox>());
268       }                                                                 
269     const BlackBoxDescriptor::OutputDescriptorMapType& omap 
270       = bbGetDescriptor()->GetOutputDescriptorMap();                   
271     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o; 
272     for ( o = omap.begin(); o != omap.end(); ++o )
273       {                                                 
274         bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
275         bbGetOutputConnectorMap()[o->second->GetName()] 
276           = new BlackBoxOutputConnector();
277       }
278     bbtkDebugDecTab("Kernel",8);  
279   }
280   //=========================================================================
281
282
283   //=========================================================================
284   /// Desallocates the i/o connectors of the black box
285   void BlackBox::bbDesallocateConnectors()
286   {
287     bbtkDebugMessageInc("Kernel",8,
288                         "BlackBox::bbDesallocateConnectors()"
289                         <<std::endl);                                   
290
291     InputConnectorMapType::const_iterator i;
292     for ( i = bbGetInputConnectorMap().begin();
293           i != bbGetInputConnectorMap().end(); ++i )                   
294       {                                                                 
295         bbtkDebugMessage("Kernel",8,"* Delete \""<<i->first<<"\""<<std::endl);
296         delete (i->second);
297       }                                                                 
298     OutputConnectorMapType::const_iterator o;   
299     for ( o = bbGetOutputConnectorMap().begin(); 
300           o != bbGetOutputConnectorMap().end(); ++o )                   
301       {                                                                 
302         bbtkDebugMessage("Kernel",8,"* Delete \""<<o->first<<"\""<<std::endl);         
303         delete (o->second);
304       }                                                                 
305    
306     bbtkDebugDecTab("Kernel",8);  
307
308   }
309   //=========================================================================
310
311
312   //=========================================================================
313   /// Copies the input / output values from another box
314   void BlackBox::bbCopyIOValues(BlackBox& from)
315   {
316     bbtkDebugMessageInc("Kernel",1,
317                         "BlackBox::bbCopyIOValues("
318                         <<from.bbGetFullName()<<") ["
319                         <<bbGetFullName()<<"]"<<std::endl);
320     // copies the input values
321     const BlackBoxDescriptor::InputDescriptorMapType& imap 
322       = bbGetDescriptor()->GetInputDescriptorMap(); 
323     BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;       
324     for ( i = imap.begin(); i != imap.end(); ++i )                      
325       {         
326         if (! i->second->GetCopyConstruct() ) continue;
327         std::string input = i->second->GetName();
328         bbtkDebugMessage("Kernel",2,"* Copying input "<<input<<std::endl);
329         this->bbSetInput(input, from.bbGetInput(input) );
330       }                                                                 
331     // copies the output values
332     const BlackBoxDescriptor::OutputDescriptorMapType& omap 
333       = bbGetDescriptor()->GetOutputDescriptorMap();                   
334     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o; 
335     for ( o = omap.begin(); o != omap.end(); ++o )
336       {                                                 
337         if (! o->second->GetCopyConstruct() ) continue;
338         std::string output = o->second->GetName();
339         bbtkDebugMessage("Kernel",2,"* Copying output "<<output<<std::endl);
340         this->bbSetOutput(output, from.bbGetOutput(output) );
341       }
342
343     bbtkDebugDecTab("Kernel",9);
344
345   }
346   //=========================================================================
347
348
349
350   //=========================================================================
351   bool BlackBox::bbCanReact() const 
352   { 
353     return ( bbGlobalGetSomeBoxExecuting() 
354 #ifdef USE_WXWIDGETS
355              || Wx::IsSomeWindowAlive() 
356 #endif
357              ); 
358   }
359   //=========================================================================
360
361
362
363   //=========================================================================
364   /// User overloadable destruction method of a black box
365   void BlackBox::bbUserDelete() 
366   {   
367     bbtkDebugMessage("process",5,
368                      "=> BlackBox::bbUserDelete() ["
369                      <<bbGetFullName()<<"]"
370                      <<" : not overloaded; using standard deletion"
371                      <<std::endl);
372     delete this;
373   }
374   //=========================================================================
375
376
377   //=========================================================================
378   BlackBox::BoxProcessModeValue BlackBox::bbGetBoxProcessModeValue() const
379   {
380     const std::string& p = bbmBoxProcessMode;
381     if ( (p == "0") ||
382          (p == "P") || (p == "p") ||
383          (p == "Pipeline") || (p == "pipeline") ) return Pipeline;
384     if ( (p == "1") ||
385          (p == "A") || (p == "a") ||
386          (p == "Always") || (p == "always") ) return Always;
387     if ( (p == "2") ||
388          (p == "R") || (p == "r") ||
389          (p == "Reactive") || (p == "reactive") ) return Reactive;
390     /*
391     if ( (p == "3") ||
392          (p == "F") || (p == "f") ||
393          (p == "Flash") || (p == "flash") ) return Flash;
394     */
395     bbtkError(bbGetFullName()<<" : BoxProcessMode value '"<<p
396               <<"' unknown. Possible values : "
397               <<"'0'/'P'/'p'/'Pipeline'/'pipeline' | "
398               <<"'1'/'A'/'a'/'Always'/'always' | "
399               <<"'2'/'R'/'r'/'Reactive'/'reactive'"
400               //      <<"'3'/'F'/'f'/'Flash'/'flash'"
401               <<std::endl);
402   }
403   //=========================================================================
404   
405   //=========================================================================
406   bool  BlackBox::bbBoxProcessModeIsReactive() const
407   {
408     return (bbGetBoxProcessModeValue() == Reactive);
409   }
410   //=========================================================================
411
412   //=========================================================================
413   bool  BlackBox::bbBoxProcessModeIsAlways() const
414   {
415     return (bbGetBoxProcessModeValue() == Always);
416   }
417   //=========================================================================
418
419
420
421
422   //=========================================================================
423   void BlackBox::bbAddOutputObserver(const std::string& output, 
424                                    OutputChangeCallbackType f)
425   {
426     bbGetOutputConnector(output).AddChangeObserver(f);
427   }  
428   //=========================================================================
429
430   //=========================================================================
431   void BlackBox::bbRemoveOutputObserver(const std::string& output_name, 
432                                       OutputChangeCallbackType f)
433   {
434     bbtkError("BlackBox::RemoveChangeObserver NOT IMPLEMENTED");
435   }
436   //=========================================================================
437
438
439   /*
440   //=========================================================================
441   ///  Sets the ChangeTime of input 
442   void BlackBox::bbSetInputChangeTime(BlackBoxInputConnector* c, 
443                                       const ChangeTime& t)
444   {
445     bbtkDebugMessage("change",1,
446                      "==> BlackBox::bbSetInputChangeTime("<<c<<","<<t<<") ["
447                      <<bbGetFullName()<<"]"<<std::endl);
448     
449     // If new time is greater than old one
450     if ( c->SetChangeTime(t) ) 
451       {
452         bool was_up_to_date = bbIsUpToDate();
453         // If new time is greater than the old max time of inputs
454         if ( mMaxInputChangeTime.Set(t) ) 
455           {
456             // If the box turned out-of-date
457             if ( was_up_to_date && bbIsOutOfDate() )
458               {
459                 // 
460                 if ( ( bbBoxProcessModeIsReactive()  ||
461                        (c==bbGetInputConnectorMap().find("BoxExecute")->second))
462                      && (bbCanReact() ) )
463                   {
464                     bbtkDebugMessage("change",2,
465                                      "an input of "
466                                      <<bbGetFullName()
467                                      <<" changed and box is in Reactive mode or BoxExecute input changed : adding it to the global execution list"
468                                      <<std::endl);
469                     bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
470                   }
471                 // Have to propagate the modification to aval boxes
472                 OutputConnectorMapType::iterator i;
473                 for (i = bbGetOutputConnectorMap().begin();
474                      i != bbGetOutputConnectorMap().end();
475                      ++i)
476                   {
477                     i->second->SetChangeTime(t);
478                   }
479                 // update the MinOutputChangeTime
480                 mMinOutputChangeTime.Set(t);
481               }
482           }
483       }
484   }
485   //=========================================================================
486
487  //=========================================================================
488   ///  Sets the ChangeTime of output 
489   void BlackBox::bbSetOutputChangeTime(BlackBoxOutputConnector* c, 
490                                        const ChangeTime& t)
491   {
492     bbtkDebugMessage("change",1,
493                      "==> BlackBox::bbSetOutputChangeTime("<<c<<","<<t<<") ["
494                      <<bbGetFullName()<<"]"<<std::endl);
495     
496     //ChangeTime old = 
497     c->SetChangeTime(t);
498     // c->GetChangeTime() = t;
499     //    bbUpdateMinOutputChangeTime(t);
500     // propagate
501     
502   }
503   //=========================================================================
504   */
505
506   /*
507   //=========================================================================
508   void BlackBox::bbUpdateMaxInputChangeTime(const ChangeTime& t)
509   {    
510
511     
512     if ( t > mMaxInputChangeTime ) 
513       {
514         mMaxInputChangeTime = t;
515         if ( mMinOutputChangeTime > mMaxInputChangeTime )
516           {
517             
518           }
519       }
520     
521   }
522   //=========================================================================
523
524   //=========================================================================
525   void bbUpdateMinOutputChangeTime(const ChangeTime& t)
526   {
527     ChangeTime old = mMinOutputChangeTime;
528     mMinOutputChangeTime = MAXLONG;
529     OutputConnectorMapType::iterator i;
530     for (i = bbGetOutputConnectorMap.begin();
531          i != bbGetOutputConnectorMap.end();
532          ++i)
533       {
534         if (i->second->GetChangeTime() < mMinOutputChangeTime)
535           mMinOutputChangeTime = i->second->GetChangeTime();
536       }
537     if ( mMinOutputChangeTime < old )
538       {
539       }
540    
541   }
542   //=========================================================================
543   */
544
545   //=========================================================================
546   /// Signals that the BlackBox has been modified through 
547   /// the input connector c
548   /// and propagates it downward
549   /// ** NOT USER INTENDED **
550   void BlackBox::bbSetStatusAndPropagate(BlackBoxInputConnector* c,
551                                          IOStatus s)
552   {
553     bbtkDebugMessageInc("change",5,
554                         "=> BlackBox::bbSetStatusAndPropagate(input,"
555                         <<GetIOStatusString(s)<<") ["
556                         <<bbGetFullName()<<"]"<<std::endl);
557
558     if (s==UPTODATE) bbtkError("bbSetStatusAndPropagate with status UPTODATE!");
559     c->SetStatus(s);
560
561     // Flash reaction
562     /*
563     if (bbGetBoxProcessModeValue() == Flash)
564       {
565         this->bbExecute();
566       }
567     */
568
569     OutputConnectorMapType::const_iterator o;   
570     for ( o = bbGetOutputConnectorMap().begin(); 
571           o != bbGetOutputConnectorMap().end(); ++o )                   
572       {                                                                 
573         if (o->second->GetStatus()==UPTODATE) 
574           {
575             o->second->SetStatus(OUTOFDATE);
576             o->second->SignalChange(GetThisPointer<BlackBox>(),o->first); 
577           }
578       }                                                                 
579     
580     if ( ( bbBoxProcessModeIsReactive()  ||
581            (c==bbGetInputConnectorMap().find("BoxExecute")->second))
582          && (bbCanReact() ) )
583       {
584         bbtkDebugMessage("change",2,
585                          "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
586         bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
587       }    
588     bbtkDebugMessageInc("change",5,
589                         "<= BlackBox::bbSetStatusAndPropagate(input) ["
590                         <<bbGetFullName()<<"]"<<std::endl);
591   }
592   //=========================================================================
593
594
595   //=========================================================================  
596   void BlackBox::bbSignalOutputModification(bool reaction)
597   {
598     bbtkDebugMessageInc("change",5,
599                         "=> BlackBox::bbSignalOutputModification("
600                         <<reaction<<") ["
601                         <<bbGetFullName()<<"]"<<std::endl);
602     
603     OutputConnectorMapType::iterator i;
604     for ( i  = bbGetOutputConnectorMap().begin(); 
605           i != bbGetOutputConnectorMap().end(); ++i) 
606       {
607         //      std::cout << "Stat = "
608         //<<GetIOStatusString(i->second->GetStatus())
609         //                <<std::endl;
610         // LG : CANNOT SIGNAL ONLY WHEN UPTODATE 
611         // See bbtkSampleOutputObserver
612         //      if (i->second->GetStatus()==UPTODATE) 
613         //        {
614             i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
615             //    }
616       } 
617
618     if (reaction) bbGlobalProcessExecutionList();
619
620     bbtkDebugMessageDec("change",5,
621                         "<= BlackBox::bbSignalOutputModification() ["
622                         <<bbGetFullName()<<"]"<<std::endl);
623     
624   }  
625   //=========================================================================   
626   //=========================================================================  
627   void BlackBox::bbSignalOutputModification(const std::string& output,
628                                             bool reaction)
629   {
630     bbtkDebugMessageInc("change",5,
631                         "=> BlackBox::bbSignalOutputModification("
632                         <<output<<","<<reaction<<") ["
633                         <<bbGetFullName()<<"]"<<std::endl);
634     
635     OutputConnectorMapType::iterator i = 
636       bbGetOutputConnectorMap().find(output);
637
638
639     if ( i == bbGetOutputConnectorMap().end() ) 
640         {
641           bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
642         }
643
644     //    if (i->second->GetStatus()==UPTODATE) 
645     //      {
646         i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
647         // Has to notify the output "BoxChange" also
648         if (output != "BoxChange") 
649           {
650             i = bbGetOutputConnectorMap().find("BoxChange");
651             if ( i != bbGetOutputConnectorMap().end() ) 
652               {
653                 i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
654               }
655           }
656         if (reaction) bbGlobalProcessExecutionList();
657         //      }
658
659     bbtkDebugMessageDec("change",5,
660                         "<= BlackBox::bbSignalOutputModification("
661                         <<output<<") ["
662                         <<bbGetFullName()<<"]"<<std::endl);
663     
664   }  
665   //=========================================================================   
666   //=========================================================================  
667   void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
668         bool reaction)
669   {
670     bbtkDebugMessageInc("change",5,
671                         "=> BlackBox::bbSignalOutputModification(vector of outputs) ["
672                         <<bbGetFullName()<<"]"<<std::endl);
673     OutputConnectorMapType::iterator i;
674     std::vector<std::string>::const_iterator o;
675     bool changed = false;
676     for (o=output.begin();o!=output.end();++o) 
677       {
678         // the output "BoxChange" must be signaled **AFTER** all others
679         if (*o == "BoxChange") continue;
680         // Look for the connector
681         i = bbGetOutputConnectorMap().find(*o);
682         if ( i == bbGetOutputConnectorMap().end() ) 
683           {
684             bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
685           }
686
687         //      if (i->second->GetStatus()==UPTODATE)
688         //        {
689             i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
690             changed = true;
691             //  }
692       }
693     // Has to notify the output "BoxChange" also
694     i = bbGetOutputConnectorMap().find("BoxChange");
695     if ( changed && (i != bbGetOutputConnectorMap().end())) 
696       {
697         // if (i->second->GetStatus()==UPTODATE) 
698         //        {
699             i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
700             if (reaction) bbGlobalProcessExecutionList();
701             //  }
702       }
703
704     bbtkDebugMessageDec("change",5,
705                         "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
706                         <<bbGetFullName()<<"]"<<std::endl);
707
708   }  
709   //=========================================================================   
710
711
712
713
714
715
716
717   //=========================================================================
718   /// Main processing method of the box.
719   void BlackBox::bbExecute(bool force)
720   {
721     bbtkDebugMessageInc("process",2,
722                         "=> BlackBox::bbExecute("<<(int)force<<") ["
723                         <<bbGetFullName()<<"]"<<std::endl);
724  
725     // If already executing : return
726     /*
727     if (bbGetExecuting()) 
728       {
729         bbtkDebugMessage("process",2,
730                          " -> already executing : abort"<<std::endl);
731         return;
732       }
733     */
734
735     // If execution frozen : return
736     if (bbGlobalGetFreezeExecution()) 
737       {
738         bbtkDebugMessage("process",2,
739                          " -> FreezeExecution global flag is 'true' : abort execution"<<std::endl);
740       }
741
742     BBTK_BUSY_CURSOR;
743
744     // If force is true then update is triggered even if the box is UPTODATE
745     //    if (force) bbSetModifiedStatus();
746
747     // Calls the main recursive execution method 
748     bbRecursiveExecute(Connection::Pointer());
749
750     bbtkDebugMessageDec("process",2,
751                         "<= BlackBox::bbExecute() ["
752                         <<bbGetFullName()<<"]"<<std::endl);
753   }
754   //=========================================================================
755
756   //=========================================================================
757   void BlackBox::bbInitializeProcessing()
758   {
759     if (!bbmInitialized) 
760       {
761         std::cout << "INIT "<<bbGetFullName()<<std::endl;
762         this->bbRecursiveInitializeProcessing();
763         bbmInitialized = true;
764       }
765   }
766   //=========================================================================
767
768   //=========================================================================
769   void BlackBox::bbFinalizeProcessing()
770   {
771     if (bbmInitialized) 
772       {
773         std::cout << "FINI "<<bbGetFullName()<<std::endl;
774         this->bbRecursiveFinalizeProcessing();
775         bbmInitialized = false;
776       }
777   }
778   //=========================================================================
779
780   
781   //=========================================================================
782   /// Main recursive processing method of the box.
783   void BlackBox::bbRecursiveExecute( Connection::Pointer caller )
784   {
785     bbtkDebugMessageInc("process",3,
786                         "=> BlackBox::bbRecursiveExecute("
787                         <<(caller?caller->GetFullName():"0")<<") ["
788                         <<bbGetFullName()<<"]"<<std::endl);
789
790     // If already executing : return
791     if (bbGetExecuting()) 
792       {
793         bbtkDebugMessage("process",3,
794                          " -> already executing : abort"<<std::endl);
795         return; 
796       }
797     
798     // If not initialized do it
799     bbInitializeProcessing();
800
801     bbSetExecuting(true);
802     bool wasExecuting = bbGlobalGetSomeBoxExecuting();
803     bbGlobalSetSomeBoxExecuting(true);
804     
805     // Creates the window if the black box has one
806     this->bbCreateWindow();
807     
808     // Updates its inputs
809     IOStatus s = bbUpdateInputs();
810     
811     if ( (s != UPTODATE) ||
812          bbBoxProcessModeIsAlways() )
813       {
814           // Displays the window (WxBlackbox)
815           //    bbShowWindow(caller);
816
817           // Actual processing (virtual)
818           this->bbProcess();
819         
820         
821           // Update the I/O statuses
822           bbComputePostProcessStatus();
823       }
824     else 
825       {
826         // Test output status...
827         OutputConnectorMapType::iterator o;
828         for ( o = bbGetOutputConnectorMap().begin(); 
829               o!= bbGetOutputConnectorMap().end(); ++o) 
830           {
831             if (o->second->GetStatus() != UPTODATE)
832               {
833                 bbtkWarning("BlackBox::bbRecursiveExecute ["
834                             <<bbGetFullName()
835                             <<"] : all inputs are Up-to-date but output '"
836                             <<o->first<<"' is Out-of-date ???");
837               }
838           }
839         
840         bbtkDebugMessage("process",3," -> Up-to-date : nothing to do"
841                          <<std::endl);
842       }
843
844     // Shows the window if the black box has one
845     this->bbShowWindow(); 
846
847           
848     bbtkDebugMessage("process",3,
849             "<= BlackBox::bbRecursiveExecute() ["
850             <<bbGetFullName()<<"]"<<std::endl);
851
852     bbSetExecuting(false);
853     bbGlobalSetSomeBoxExecuting(wasExecuting);
854
855     return; 
856
857   }
858   //=========================================================================
859   
860    
861
862
863
864   //=========================================================================
865   /// Updates the BlackBox inputs
866   /// Calls RecursiveExecute on all BlackBoxInputConnector
867   /// \returns The maximum of final IOStatus after each input update
868   IOStatus BlackBox::bbUpdateInputs()
869   {
870     bbtkDebugMessageInc("process",4,
871                         "=> BlackBox::bbUpdateInputs() ["
872                         <<bbGetFullName()<<"]"
873                         <<std::endl);   
874
875     IOStatus s = UPTODATE;
876
877     InputConnectorMapType::iterator i;
878     for ( i = bbGetInputConnectorMap().begin(); 
879           i!= bbGetInputConnectorMap().end(); ++i) 
880       {
881         //      if (i->first=="WinHide") continue;
882         // If input type is Void : no recurse
883         //if (  bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo() 
884         //      == typeid(Void) ) 
885         //  continue;
886         bbtkDebugMessageDec("change",2,
887                             bbGetName()<<"."<<i->first
888                             <<" ["<<i->second<<"] "
889                             <<" status before update = '"
890                             <<GetIOStatusString(i->second->GetStatus())
891                             <<"'"<<std::endl);
892         i->second->RecursiveExecute();
893         IOStatus t = i->second->GetStatus();
894         if (t > s) s = t;
895         bbtkDebugMessageDec("change",2,
896                             bbGetName()<<"."<<i->first
897                             <<" ["<<i->second<<"] "
898                             <<" status before process = '"
899                             <<GetIOStatusString(i->second->GetStatus())
900                             <<"'"<<std::endl);
901       }
902     
903     bbtkDebugMessageDec("process",4,
904                         "<= BlackBox::bbUpdateInputs() ["
905                         <<bbGetFullName()<<"]"
906                         <<std::endl);   
907     
908     
909     return s;
910   }
911   //=========================================================================
912
913   //==================================================================
914   /// Computes the final IOStatus of inputs and outputs after processing
915   void BlackBox::bbComputePostProcessStatus()
916   {
917     bbtkDebugMessageInc("process",4,
918                         "=> BlackBox::bbComputePostProcessStatus() ["
919                         <<bbGetFullName()<<"]"
920                         <<std::endl);   
921
922     IOStatus new_output_status = UPTODATE;
923     if (bbBoxProcessModeIsAlways()) new_output_status = OUTOFDATE;
924
925     // Update the input statuses
926     InputConnectorMapType::iterator i;
927     for ( i = bbGetInputConnectorMap().begin(); 
928           i!= bbGetInputConnectorMap().end(); ++i) 
929       {
930         IOStatus t = i->second->GetStatus();
931         if (t == OUTOFDATE) new_output_status = OUTOFDATE;
932         // A previously MODIFIED status turns to UPTODATE
933         if (t==MODIFIED) i->second->SetStatus(UPTODATE);
934         bbtkDebugMessage("change",2,
935                          bbGetName()<<"."<<i->first<<" : "
936                          << GetIOStatusString(t) << " -> "
937                          << GetIOStatusString(i->second->GetStatus())
938                          << std::endl);
939       }
940         bbtkDebugMessage("change",2,
941                          bbGetName()<<" new output status : "
942                          << GetIOStatusString(new_output_status)
943                          <<std::endl);
944     // Update the output statuses
945     OutputConnectorMapType::iterator o;
946     for ( o = bbGetOutputConnectorMap().begin(); 
947           o!= bbGetOutputConnectorMap().end(); ++o) 
948       {
949         o->second->SetStatus(new_output_status);
950       }
951
952     bbtkDebugMessageInc("process",4,
953                         "<= BlackBox::bbComputePostProcessStatus() ["
954                         <<bbGetFullName()<<"]"
955                         <<std::endl);   
956
957   }
958   //==================================================================
959
960   //=========================================================================
961   /// Connects the input <name> to the connection c
962   void BlackBox::bbConnectInput( const std::string& name, Connection* c)
963   {
964     bbtkDebugMessage("connection",2,
965                         "==> BlackBox::bbConnectInput(\""
966                         <<name<<"\","<<c->GetFullName()<<") ["
967                         <<bbGetFullName()<<"]"
968                         <<std::endl);       
969
970
971     InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
972     if (i==bbGetInputConnectorMap().end())
973       {
974         bbtkError("no input called '"<<name<<"'");
975       }
976     i->second->SetConnection(c);
977     // The input *MUST* be set OUTOFDATE to update its input on next execution
978     bbSetStatusAndPropagate(i->second,OUTOFDATE);
979     
980     bbtkDebugMessage("connection",2,
981                         "<== BlackBox::bbConnectInput(\""
982                         <<name<<"\","<<c->GetFullName()<<") ["
983                         <<bbGetFullName()<<"]"
984                         <<std::endl);       
985
986   }
987   //=========================================================================
988
989
990   //=========================================================================  
991   /// Connects the output <name> to the connection c
992   void BlackBox::bbConnectOutput( const std::string& name, Connection* c)
993   {
994     bbtkDebugMessage("connection",2,
995                      "==> BlackBox::bbConnectOutput(\""<<name<<"\","
996                      <<c->GetFullName()<<") ["
997                      <<bbGetFullName()<<"]"<<std::endl);       
998     
999     OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
1000     if (i==bbGetOutputConnectorMap().end())
1001       {
1002         bbtkError("no output called '"<<name<<"'");
1003       }
1004     i->second->SetConnection(c);
1005
1006     bbtkDebugMessage("connection",2,
1007                      "<== BlackBox::bbConnectOutput(\""<<name<<"\","
1008                      <<c->GetFullName()<<") ["
1009                      <<bbGetFullName()<<"]"<<std::endl);       
1010
1011   }
1012   //=========================================================================
1013
1014
1015   //=========================================================================
1016   /// Disconnects the input <name> from the connection c
1017   void BlackBox::bbDisconnectInput( const std::string& name, Connection* c)
1018   {
1019
1020     bbtkDebugMessage("connection",2,
1021                      "==> BlackBox::bbDisconnectInput(\""<<name
1022                      <<"\","<<c->GetFullName()<<") ["
1023                      <<bbGetFullName()<<"]"
1024                      <<std::endl);      
1025
1026     if (!c) 
1027       {
1028
1029         bbtkDebugMessage("connection",2,"c==0"<<std::endl);     
1030         return;
1031       }
1032
1033     InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
1034     if (i==bbGetInputConnectorMap().end())
1035       {
1036         bbtkError("no input called '"<<name<<"'");
1037       }
1038     i->second->UnsetConnection(c);
1039
1040     bbtkDebugMessage("connection",2,
1041                      "<== BlackBox::bbDisconnectInput(\""<<name
1042                      <<"\","<<c->GetFullName()<<") ["
1043                      <<bbGetFullName()<<"]"
1044                      <<std::endl);      
1045
1046   }
1047   //=========================================================================
1048
1049
1050   //=========================================================================
1051   /// Disconnects the output <name> from the connection c
1052   void BlackBox::bbDisconnectOutput( const std::string& name, Connection* c)
1053   {
1054     bbtkDebugMessage("connection",2,
1055                      "==> BlackBox::bbDisconnectOutput(\""<<name
1056                      <<"\","<<c->GetFullName()<<") ["
1057                      <<bbGetFullName()<<"]"
1058                      <<std::endl);       
1059     if (!c) 
1060       {
1061
1062         bbtkDebugMessage("connection",2,"c==0"<<std::endl);     
1063         return;
1064       }
1065
1066     OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
1067     if (i==bbGetOutputConnectorMap().end())
1068       {
1069         bbtkError("no output called '"<<name<<"'");
1070       }
1071     i->second->UnsetConnection(c);
1072
1073     bbtkDebugMessage("connection",2,
1074                      "<== BlackBox::bbDisconnectOutput(\""<<name
1075                      <<"\","<<c->GetFullName()<<") ["
1076                      <<bbGetFullName()<<"]"
1077                      <<std::endl);       
1078   } 
1079   //=========================================================================
1080  
1081
1082   //=========================================================================
1083   /// Virtual
1084   void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
1085   {
1086     fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
1087   }
1088   //=========================================================================
1089
1090
1091   //=========================================================================
1092   std::string BlackBox::bbGetOutputAsString( const std::string &output ) 
1093   {
1094     std::string v;
1095     // Looks for the adaptor
1096     if (bbGetOutputType(output).name() != typeid(std::string).name() ) 
1097       {
1098         // Look for factory 
1099         Package::Pointer p = bbGetDescriptor()->GetPackage();
1100         if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
1101           {
1102             Factory::Pointer f = p->GetFactorySet().begin()->lock();
1103             BlackBox::Pointer a;
1104             try
1105               {
1106                 a = f->NewAdaptor(  
1107                                   bbGetOutputType(output),
1108                                   typeid(std::string),
1109                                   "");
1110               } catch (bbtk::Exception e) 
1111               {
1112               }
1113             if (a){
1114               //                        bbUpdate();
1115               a->bbSetInput("In",bbGetOutput(output));
1116               a->bbExecute();
1117               v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
1118             } else {
1119               v="? (no adaptor found)";
1120             }
1121           }
1122         else 
1123           {
1124             v="? (no factory found)";
1125           }
1126       } 
1127     else 
1128       {
1129         //         bbUpdate();
1130         v = bbGetOutput(output).unsafe_get<std::string>() ;
1131       }
1132     return v;
1133   }
1134   //=========================================================================
1135
1136   //=========================================================================
1137   std::string BlackBox::bbGetInputAsString( const std::string &input ) 
1138   {
1139     std::string v;
1140     // Looks for the adaptor
1141     if (bbGetInputType(input) != typeid(std::string)) 
1142       {
1143         // Look for factory 
1144         Package::Pointer p = bbGetDescriptor()->GetPackage();
1145         if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
1146           {
1147             Factory::Pointer f = p->GetFactorySet().begin()->lock();
1148             BlackBox::Pointer a;
1149             try
1150               {
1151                 a = f->NewAdaptor(  
1152                                bbGetInputType(input),
1153                                typeid(std::string),
1154                                "");
1155               }catch (bbtk::Exception e) 
1156               {
1157               }
1158             if (a)
1159               {
1160                 //                      bbUpdate();
1161                 a->bbSetInput("In",bbGetInput(input));
1162                 a->bbExecute();
1163                 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
1164               } 
1165             else 
1166               {
1167                 v="? (no adaptor found)";
1168               }
1169           } 
1170         else 
1171           {
1172             v="? (no factory found)";
1173           }
1174       }
1175     else 
1176       {
1177         v = bbGetInput(input).unsafe_get<std::string>() ;
1178       }
1179     return v;
1180   }
1181   //=======================================================================
1182
1183   //=======================================================================
1184   // Replaces substrings "<" by "["
1185   void SubsBrackets ( std::string& s )
1186   {
1187     //   std::cout << "BEFORE=["<<s<<"]"<<std::endl;
1188     std::string ss("<");
1189     std::string::size_type pos = 0;
1190     pos = s.find(ss,0);
1191     std::string cr("[");
1192     while ( pos != std::string::npos )
1193       {
1194         //      std::cout << "*** find one "<<std::endl;
1195         s.replace(pos,1,cr.c_str(),1);
1196         pos = s.find(ss, pos);
1197       } 
1198     ss = ">";
1199     pos = 0;
1200     pos = s.find(ss,0);
1201     cr = "]";
1202     while ( pos != std::string::npos )
1203       {
1204         //      std::cout << "*** find one "<<std::endl;
1205         s.replace(pos,1,cr.c_str(),1);
1206         pos = s.find(ss, pos);
1207       } 
1208     ss = ",";
1209     pos = 0;
1210     pos = s.find(ss,0);
1211     cr = "-";
1212     while ( pos != std::string::npos )
1213       {
1214         //      std::cout << "*** find one "<<std::endl;
1215         s.replace(pos,1,cr.c_str(),1);
1216         pos = s.find(ss, pos);
1217       }     //    std::cout << "AFTER=["<<s<<"]"<<std::endl;
1218   }
1219   //=======================================================================
1220
1221   //=========================================================================
1222   /// Write Graphviz-dot description in file
1223   void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
1224                                         BlackBox::Pointer parentblackbox, 
1225                                         int detail, int level,
1226                                         bool instanceOrtype,
1227                                         bool relative_link )
1228
1229   { 
1230     InputConnectorMapType::iterator i;
1231     // label
1232     std::string labelStr;
1233     std::string valueStr("");
1234
1235         if (detail==0) {
1236                 labelStr = bbGetName() ; 
1237 //EED 18 Fev 2008
1238                 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
1239         } else {
1240                 labelStr = bbGetName();
1241                 labelStr = labelStr + "   [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]  ";
1242     }
1243
1244     SubsBrackets(labelStr);
1245     if (detail==1)
1246       {
1247         labelStr = labelStr + " | {{ "; 
1248         std::string tempStrTypeName;
1249         bool tmp; 
1250         tmp=false;
1251         for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i ) 
1252           {
1253             if (tmp==true)
1254               {
1255                 labelStr=labelStr+" | ";
1256               }
1257             tmp=true;
1258             if (instanceOrtype==true)
1259               {
1260                 valueStr = this->bbGetInputAsString(i->first) + " = ";
1261               } 
1262             const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
1263             tempStrTypeName=id->GetTypeName();
1264             SubsBrackets(tempStrTypeName);
1265             std::string Name(i->first);
1266             SubsBrackets(Name);
1267             labelStr=labelStr + "<"+i->first.c_str()+"> "  + valueStr +  Name.c_str() + "  [" + tempStrTypeName.c_str() + "]";
1268           }
1269         labelStr=labelStr+ " } | {";
1270         tmp = false;
1271         OutputConnectorMapType::iterator ii;
1272         for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii ) 
1273         {
1274            if (tmp==true)
1275            {
1276                    labelStr=labelStr+" | ";
1277            }
1278            tmp = true;
1279            if (instanceOrtype==true)
1280            {
1281                    valueStr = this->bbGetOutputAsString(ii->first) + " = ";
1282            }
1283            const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first); 
1284            tempStrTypeName=id->GetTypeName();
1285            SubsBrackets(tempStrTypeName);
1286             std::string Name(ii->first);
1287             SubsBrackets(Name);
1288            labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + "  ["+tempStrTypeName+"]";
1289         }
1290         labelStr = labelStr+ "      } }" ;
1291 } // detail
1292
1293     fprintf(ff,"  " );
1294     bbWriteDotInputOutputName(ff,true,detail,level);
1295     std::string tmp ( bbGetTypeName() );
1296     SubsBrackets(tmp);
1297     std::string url;
1298     if (relative_link) 
1299       url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
1300     else 
1301       url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
1302   
1303     fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
1304     //    std::cout  << labelStr << std::endl;
1305
1306     // Relation Input
1307     if (GetThisPointer<BlackBox>()!=parentblackbox){
1308       for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i ) 
1309         {
1310           if (i->second)
1311             {
1312               Connection* con = i->second->GetConnection();
1313               if (con!=NULL){
1314                 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
1315                 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
1316                 fprintf(ff,"  ");
1317                 a->bbWriteDotInputOutputName(ff,false,detail,level);
1318                 if (detail==1)
1319                   {
1320                     fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
1321                   }
1322                 fprintf(ff,"->");
1323                 b->bbWriteDotInputOutputName(ff,true,detail,level);
1324                 if (detail==1)
1325                   {
1326                     fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
1327                   }
1328                 fprintf(ff,"%s\n",";");
1329               }  // if con
1330             } // if second
1331         } // for
1332     } // if parentblackbox
1333   }
1334   //=========================================================================
1335
1336
1337
1338
1339   //=========================================================================
1340   void BlackBox::bbShowRelations(BlackBox::Pointer parentblackbox, 
1341                                  int detail, int level
1342                                  /*,Factory *factory*/ )
1343   {
1344      
1345     if (this->bbGetDescriptor()->GetPackage()) 
1346       {
1347         bbtkMessage("Help",1,"Black Box '"<<bbGetName()<<"' <"<<
1348                     this->bbGetDescriptor()->GetPackage()->GetName()
1349                     <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1350       }
1351     else 
1352       {
1353         bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
1354       }
1355     /*
1356     if (bbIsUpToDate())
1357       {
1358         bbtkMessage("Help",1,"Up-to-date ["<<mMaxInputChangeTime<<","
1359                     <<mMinOutputChangeTime<<"]"<<std::endl);
1360       }
1361     else 
1362       {
1363         bbtkMessage("Help",1,"Out-of-date ["<<mMaxInputChangeTime<<","
1364                     <<mMinOutputChangeTime<<"]"<<std::endl);
1365       }
1366     */
1367     //    bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
1368     //    bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
1369
1370     std::vector<std::string> iname;
1371     std::vector<std::string> ivalue;
1372     std::vector<std::string> iconn;
1373     std::vector<std::string> istatus;
1374
1375     InputConnectorMapType::iterator i;
1376     unsigned int namelmax = 0;
1377     unsigned int valuelmax = 0;
1378     //   unsigned int connlmax = 0;
1379     for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i ) 
1380       {
1381         iname.push_back(i->first);
1382         if (iname.back().size()>namelmax) namelmax = iname.back().size();
1383         ivalue.push_back(bbGetInputAsString(i->first));
1384         if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
1385         std::string s("");
1386         Connection* con = i->second->GetConnection();
1387         if (con!=0){
1388           s = con->GetOriginalBlackBoxFrom()->bbGetName();
1389           s += ".";
1390           s += con->GetOriginalBlackBoxFromOutput();
1391         }  // if con
1392         iconn.push_back(s);
1393         istatus.push_back(GetIOStatusString(i->second->GetStatus()));
1394       }
1395     OutputConnectorMapType::iterator o;
1396     std::vector<std::string> oname;
1397     std::vector<std::string> ovalue;
1398     std::vector<std::vector<std::string> > oconn;
1399     std::vector<std::string> ostatus;
1400     for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o ) 
1401       {
1402         oname.push_back(o->first);
1403         if (oname.back().size()>namelmax) namelmax = oname.back().size();
1404         ovalue.push_back(bbGetOutputAsString(o->first));
1405         if (ovalue.back().size()>valuelmax) valuelmax = ovalue.back().size();
1406         std::vector<std::string> ss;
1407         const std::vector<Connection*>& con 
1408           = o->second->GetConnectionVector();
1409         std::vector<Connection*>::const_iterator c;
1410         for (c=con.begin();c!=con.end();++c) 
1411           {
1412             std::string s;
1413             s = (*c)->GetOriginalBlackBoxTo()->bbGetName();
1414             s += ".";
1415             s += (*c)->GetOriginalBlackBoxToInput();
1416             ss.push_back(s);
1417         }  // if con
1418         oconn.push_back(ss);
1419         ostatus.push_back(GetIOStatusString(o->second->GetStatus()));
1420       }
1421
1422     if (iname.size()) 
1423       bbtkMessage("Help",1," * Inputs : "<<std::endl);
1424     else 
1425       bbtkMessage("Help",1," * No inputs"<<std::endl);
1426
1427     std::vector<std::string>::iterator i1,i2,i3,i4;
1428     for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin(),i4=istatus.begin();
1429          i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end(),i4!=istatus.end();
1430          ++i1,++i2,++i3,++i4)
1431       {
1432         std::string name(*i1);
1433         name += "'";
1434         name.append(1+namelmax-name.size(),' ');
1435         std::string value(*i2);
1436         value += "'";
1437         value.append(1+valuelmax-value.size(),' ');
1438         if (i3->size()) 
1439           bbtkMessage("Help",1,"    '"<<name<<" = '"<<value<<" <-- '"
1440                       <<*i3<<"'");
1441         else 
1442           bbtkMessage("Help",1,"    '"<<name<<" = '"<<value);
1443         bbtkMessage("Help",1," ["<<*i4<<"]"<<std::endl);
1444       }
1445
1446     if (oname.size()) 
1447       bbtkMessage("Help",1," * Outputs : "<<std::endl);
1448     else 
1449       bbtkMessage("Help",1," * No outputs"<<std::endl);
1450
1451     std::vector<std::vector<std::string> >::iterator i5;
1452
1453     for (i1=oname.begin(),i2=ovalue.begin(),i5=oconn.begin(),i4=ostatus.begin();
1454          i1!=oname.end(),i2!=ovalue.end(),i5!=oconn.end(),i4!=ostatus.end();
1455          ++i1,++i2,++i4,++i5)
1456       {
1457         std::string name(*i1);
1458         name += "'";
1459         name.append(1+namelmax-name.size(),' ');
1460         std::string value(*i2);
1461         value += "'";
1462         value.append(1+valuelmax-value.size(),' ');
1463         if (!(*i5).size())
1464           bbtkMessage("Help",1,"    '"<<name<<" = '"<<value);
1465         else 
1466           {
1467             std::string pref = "    '"+name+" = '"+value;
1468             for (i3=i5->begin();i3!=i5->end();++i3)
1469               {
1470                 bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'");
1471                 pref.replace(0,pref.size(),pref.size(),' ');
1472               }
1473           }
1474         bbtkMessage("Help",1," ["<<*i4<<"]"<<std::endl);
1475       }
1476
1477    }
1478   //=========================================================================
1479
1480   static bool bbmgGlobalProcessingExecutionList = false;
1481
1482   //=========================================================================
1483    void BlackBox::bbGlobalProcessExecutionList()
1484    {   
1485      bbtkDebugMessageInc("process",3,
1486                          "=> BlackBox::bbGlobalProcessExecutionList()"
1487                          <<std::endl);    
1488      if (bbmgGlobalProcessingExecutionList) 
1489        {
1490          bbtkDebugMessage("process",3,"BlackBox::bbGlobalProcessExecutionList() reentered !");
1491          return;
1492        }
1493      bbmgGlobalProcessingExecutionList = true;
1494
1495      std::set<BlackBox::WeakPointer>::iterator i; 
1496      while (bbmgExecutionList.size()>0)
1497        {
1498          i = bbmgExecutionList.begin();
1499          BlackBox::WeakPointer p = *i;
1500          bbmgExecutionList.erase(i);
1501          if (p.lock())
1502            {
1503              bbtkDebugMessage("process",4,
1504                               " -> Executing "<<
1505                               p.lock()->bbGetFullName()<<std::endl);
1506              p.lock()->bbExecute(true);
1507            }
1508          else 
1509            {
1510              bbtkGlobalError("Strange error in BlackBox::bbGlobalProcessExecutionList() : Weak bb pointer in bbmgExecutionList is no more valid...");
1511            }
1512        }
1513      
1514      bbmgExecutionList.clear();
1515      bbtkDebugMessageDec("process",3,
1516                          "<= BlackBox::bbGlobalProcessExecutionList()"
1517                          <<std::endl);     
1518      
1519      bbmgGlobalProcessingExecutionList = false;
1520      
1521    }
1522   //=========================================================================
1523
1524     bool BlackBox::bbGlobalGetSomeBoxExecuting()
1525         { 
1526                 return bbmgSomeBoxExecuting; 
1527         }
1528
1529     void BlackBox::bbGlobalSetSomeBoxExecuting(bool b) 
1530         { 
1531                 bbmgSomeBoxExecuting = b; 
1532         }
1533
1534     void BlackBox::bbGlobalSetFreezeExecution(bool b) 
1535         { 
1536                 bbmgFreezeExecution = b;
1537         }
1538
1539     bool BlackBox::bbGlobalGetFreezeExecution() 
1540         { 
1541                 return bbmgFreezeExecution; 
1542         }
1543
1544   void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
1545   {  
1546     bbtkDebugMessage("process",3,"* bbGlobalAddToExecutionList("<<b->bbGetFullName()<<")"<<std::endl);
1547     if (bbmgGlobalProcessingExecutionList) 
1548       {
1549         bbtkDebugMessage("process",3,"bbGlobalAddToExecutionList called inside bbGlobalProcessExecutionList !");
1550       }
1551     bbmgExecutionList.insert(b); 
1552   } 
1553
1554
1555    //=========================================================================
1556
1557   //=========================================================================
1558   void BlackBox::Check(bool recursive)
1559   {
1560     bbtkMessage("debug",1,"*** Checking Black Box "<<(void*)this<<" ["<<bbGetFullName()
1561                 <<"] ... OK"<<std::endl);
1562   }
1563   //=========================================================================
1564
1565   void BlackBox::bbUserOnShowWidget(std::string nameInput)
1566   {
1567           bbtk::BlackBoxInputConnector *cc;
1568           cc = this->bbGetInputConnectorMap().find( nameInput.c_str() )->second;
1569           if (cc->GetConnection()!=NULL) 
1570           {
1571                   cc->GetConnection()->GetBlackBoxFrom()->bbUserOnShow();
1572           }
1573   }
1574
1575
1576
1577 }  // EO namespace bbtk
1578
1579 // EOF
1580