bbtkBlackBox.cxx

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkBlackBox.cxx,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:12 $
00006   Version:   $Revision: 1.25 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00035 #include "bbtkBlackBox.h"
00036 #include "bbtkPackage.h"
00037 #include "bbtkMessageManager.h"
00038 #include "bbtkFactory.h"
00039 
00040 #include "bbtkConfigurationFile.h"
00041 #include "bbtkWxBlackBox.h"
00042 
00043 #include <fstream>
00044 //#include <vector>
00045 
00046 
00047 namespace bbtk
00048 {
00049 
00050 
00051   static bool bbmgSomeBoxExecuting = false;
00052   static bool bbmgFreezeExecution = false;
00053   static std::set<BlackBox::Pointer> bbmgExecutionList;
00054 
00055   //=========================================================================
00056   BlackBox::Deleter::Deleter()
00057   {
00058   }
00059   //=========================================================================
00060   
00061   //=========================================================================
00062   void BlackBox::Deleter::Delete(Object* p)
00063   {
00064     BlackBox* b = dynamic_cast<BlackBox*>(p);
00065     if (!b)
00066       {
00067         bbtkInternalError("BlackBox::Deleter::Delete("<<p->GetObjectName()
00068                           <<"["<<p<<"]) : "
00069                           <<"dynamic cast to BlackBox* failed !");
00070       }
00071     std::string name = p->GetObjectName();//b->bbGetNameWithParent();
00072     bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
00073 
00074 
00075     BlackBoxDescriptor::WeakPointer desc = b->bbGetDescriptor();
00076     bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : deleting black box"<<std::endl);
00077     
00078     b->bbDelete();
00079 
00080     bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : releasing descriptor ["<<desc.lock()<<"]"<<std::endl);
00081     
00082     if (!desc.expired()) 
00083       {
00084         Package::WeakPointer pack = desc.lock()->GetPackage();
00085         if (!pack.expired()) 
00086           {
00087             Package::ReleaseBlackBoxDescriptor(pack,desc);
00088           }
00089         else 
00090           {
00091             bbtkDebugMessage("object",2,"##> BlackBox::Deleter(\""<<name<<"\") : descriptor package expired (was not held by a package and the box was the last instance)"<<std::endl);
00092           }
00093       }
00094     else
00095       {
00096         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);
00097       }
00098     bbtkDebugMessage("object",2,"<## BlackBox::Deleter(\""<<name<<"\")"<<std::endl);
00099   }
00100   //=========================================================================
00101 
00102   //=========================================================================
00103   BlackBox::BlackBox(const std::string &name) 
00104     : 
00105     bbmStatus(MODIFIED), 
00106     bbmName(name),
00107     bbmBoxProcessMode("Pipeline"),
00108     bbmParent()
00109     
00110   {
00111     bbtkDebugMessage("object",4,"==> BlackBox::BlackBox(\""
00112                      <<name<<"\")"<<std::endl);
00113     bbtkDebugMessage("object",4,"<== BlackBox::BlackBox(\""
00114                      <<name<<"\")"<<std::endl);
00115   }
00116   //=========================================================================
00117 
00118   //=========================================================================
00119   BlackBox::BlackBox(const BlackBox&)
00120   {}
00121 
00122   //=========================================================================
00123   BlackBox::BlackBox(BlackBox& from, const std::string &name) 
00124     :
00125       bbmStatus(from.bbmStatus), 
00126       bbmName(name), 
00127       bbmBoxProcessMode(from.bbmBoxProcessMode),
00128       bbmParent()
00129 
00130   {
00131     bbtkDebugMessage("object",4,"==> BlackBox::BlackBox("
00132                      <<from.bbGetFullName()<<",\""
00133                      <<name<<"\")"<<std::endl);
00134     bbtkDebugMessage("object",4,"<== BlackBox::BlackBox("
00135                      <<from.bbGetFullName()<<",\""
00136                      <<name<<"\")"<<std::endl);
00137   }
00138   //=========================================================================
00139 
00140 
00141   //=========================================================================
00142   BlackBox::~BlackBox()
00143   {
00144     bbtkDebugMessage("object",4,"==> BlackBox::~BlackBox() ["<<bbmName
00145                      <<"]"<<std::endl);
00146     this->bbDesallocateConnectors();
00147     bbtkDebugMessage("object",4,"<== BlackBox::~BlackBox() ["<<bbmName
00148                      <<"]"<<std::endl);
00149   }
00150   //=========================================================================
00151 
00152 
00153   //=========================================================================
00155   void BlackBox::bbExecute(bool force)
00156   {
00157     bbtkDebugMessageInc("process",2,
00158                         "=> BlackBox::bbExecute() ["
00159                         <<bbGetFullName()<<"]"<<std::endl);
00160  
00161 
00162     // If execution frozen : return
00163     if (bbGlobalGetFreezeExecution()) 
00164       {
00165         bbtkDebugMessage("process",2,
00166                          " -> FreezeExecution global flag is 'true' : abort execution"<<std::endl);
00167       }
00168 
00169     BBTK_BUSY_CURSOR;
00170 
00171     // If force is true then update is triggered even if the box is UPTODATE
00172     if (force) bbSetModifiedStatus();
00173 
00174     // Calls the main recursive update method 
00175     bbBackwardUpdate(Connection::Pointer());
00176 
00177     bbtkDebugMessageDec("process",2,
00178                         "<= BlackBox::bbExecute() ["
00179                         <<bbGetFullName()<<"]"<<std::endl);
00180   }
00181   //=========================================================================
00182 
00183   //=========================================================================
00184   std::string BlackBox::bbGetFullName() const
00185   { 
00186     return this->bbGetNameWithParent()+"<"+this->bbGetDescriptor()->GetTypeName()+">";
00187   }
00188   //=========================================================================
00189      
00190 
00191 
00192   //=========================================================================
00194   std::string BlackBox::bbGetNameWithParent() const
00195   {
00196     if (bbmParent.lock()) 
00197       {
00198         return bbmParent.lock()->bbGetNameWithParent() + ":" + bbmName;
00199       }
00200     else 
00201       {
00202         return bbmName;
00203       }
00204   } 
00205   //=========================================================================
00206 
00207   //=========================================================================
00209   void BlackBox::bbGetHelp(bool full) const
00210   {
00211     bbGetDescriptor()->GetHelp(full); 
00212   }
00213   //=========================================================================
00214 
00215 
00216   //=========================================================================
00218   bool BlackBox::bbHasInput(const std::string& name) const
00219   {
00220     bbtkDebugMessageInc("Kernel",8,
00221                         "BlackBox::bbHasInput(\""
00222                         <<name<<"\") ["<<bbGetFullName()<<"]"
00223                         <<std::endl);
00224     bool r = ( bbGetDescriptor()->GetInputDescriptorMap().find(name)
00225                != bbGetDescriptor()->GetInputDescriptorMap().end());
00226     bbtkDebugDecTab("Kernel",8);
00227     return r;
00228   }
00229   //=========================================================================
00230 
00231 
00232   //=========================================================================  
00234   bool BlackBox::bbHasOutput(const std::string& name) const
00235   {
00236     bbtkDebugMessageInc("Kernel",8,"BlackBox::bbHasOutput(\""
00237                         <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
00238     bool r = ( bbGetDescriptor()->GetOutputDescriptorMap().find(name)
00239                != bbGetDescriptor()->GetOutputDescriptorMap().end());
00240     bbtkDebugDecTab("Kernel",8);
00241     return r;
00242   }
00243   //=========================================================================
00244 
00245 
00246   //=========================================================================  
00248   TypeInfo BlackBox::bbGetOutputType( const std::string &name ) const 
00249   {
00250     bbtkDebugMessageInc("Kernel",8,
00251                         "BlackBox::bbGetOutputType(\""
00252                         <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
00253     TypeInfo r = bbGetDescriptor()->GetOutputDescriptor(name)->GetTypeInfo();
00254     bbtkDebugDecTab("Kernel",8); 
00255     return r;
00256   }
00257   //=========================================================================
00258 
00259   //=========================================================================
00261   TypeInfo BlackBox::bbGetInputType( const std::string &name ) const
00262   {
00263     bbtkDebugMessageInc("Kernel",8,
00264                         "BlackBox::bbGetInputType(\""
00265                         <<name<<"\") ["<<bbGetFullName()<<"]"<<std::endl);
00266     TypeInfo r = bbGetDescriptor()->GetInputDescriptor(name)->GetTypeInfo();
00267     bbtkDebugDecTab("Kernel",8);
00268     return r;
00269   }
00270   //=========================================================================
00271 
00272 
00273   //=========================================================================
00275   void BlackBox::bbAllocateConnectors()
00276   {  
00277     bbtkDebugMessageInc("Kernel",8,
00278                         "BlackBox::bbAllocateConnectors() ["
00279                         <<bbGetFullName()<<"]"
00280                         <<std::endl);                                   
00281     const BlackBoxDescriptor::InputDescriptorMapType& imap 
00282       = bbGetDescriptor()->GetInputDescriptorMap(); 
00283     BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;       
00284     for ( i = imap.begin(); i != imap.end(); ++i )                      
00285       {                                                                 
00286         bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
00287         bbGetInputConnectorMap()[i->second->GetName()] 
00288           = new BlackBoxInputConnector(GetThisPointer<BlackBox>());
00289       }                                                                 
00290     const BlackBoxDescriptor::OutputDescriptorMapType& omap 
00291       = bbGetDescriptor()->GetOutputDescriptorMap();                   
00292     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o; 
00293     for ( o = omap.begin(); o != omap.end(); ++o )
00294       {                                                 
00295         bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
00296         bbGetOutputConnectorMap()[o->second->GetName()] 
00297           = new BlackBoxOutputConnector();
00298       }
00299     bbtkDebugDecTab("Kernel",8);  
00300   }
00301   //=========================================================================
00302 
00303 
00304   //=========================================================================
00306   void BlackBox::bbDesallocateConnectors()
00307   {
00308     bbtkDebugMessageInc("Kernel",8,
00309                         "BlackBox::bbDesallocateConnectors()"
00310                         <<std::endl);                                   
00311 
00312     InputConnectorMapType::const_iterator i;
00313     for ( i = bbGetInputConnectorMap().begin();
00314           i != bbGetInputConnectorMap().end(); ++i )                   
00315       {                                                                 
00316         bbtkDebugMessage("Kernel",8,"* Delete \""<<i->first<<"\""<<std::endl);
00317         delete (i->second);
00318       }                                                                 
00319     OutputConnectorMapType::const_iterator o;   
00320     for ( o = bbGetOutputConnectorMap().begin(); 
00321           o != bbGetOutputConnectorMap().end(); ++o )                   
00322       {                                                                 
00323         bbtkDebugMessage("Kernel",8,"* Delete \""<<o->first<<"\""<<std::endl);         
00324         delete (o->second);
00325       }                                                                 
00326    
00327     bbtkDebugDecTab("Kernel",8);  
00328 
00329   }
00330   //=========================================================================
00331 
00332 
00333   //=========================================================================
00335   void BlackBox::bbCopyIOValues(BlackBox& from)
00336   {
00337     bbtkDebugMessageInc("Kernel",9,
00338                         "BlackBox::bbCopyIOValues("
00339                         <<from.bbGetFullName()<<") ["
00340                         <<bbGetFullName()<<"]"<<std::endl);
00341     // copies the input values
00342     const BlackBoxDescriptor::InputDescriptorMapType& imap 
00343       = bbGetDescriptor()->GetInputDescriptorMap(); 
00344     BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;       
00345     for ( i = imap.begin(); i != imap.end(); ++i )                      
00346       {         
00347         if (! i->second->GetCopyConstruct() ) continue;
00348         std::string input = i->second->GetName();
00349         this->bbSetInput(input, from.bbGetInput(input) );
00350       }                                                                 
00351     // copies the output values
00352     const BlackBoxDescriptor::OutputDescriptorMapType& omap 
00353       = bbGetDescriptor()->GetOutputDescriptorMap();                   
00354     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o; 
00355     for ( o = omap.begin(); o != omap.end(); ++o )
00356       {                                                 
00357         if (! o->second->GetCopyConstruct() ) continue;
00358         std::string output = o->second->GetName();
00359         this->bbSetOutput(output, from.bbGetOutput(output) );
00360       }
00361 
00362     bbtkDebugDecTab("Kernel",9);
00363 
00364   }
00365   //=========================================================================
00366 
00367 
00368 
00369   //=========================================================================
00370   bool BlackBox::bbCanReact() const 
00371   { 
00372     return ( bbGlobalGetSomeBoxExecuting() 
00373 #ifdef _USE_WXWIDGETS_
00374              || Wx::IsSomeWindowAlive() 
00375 #endif
00376              ); 
00377   }
00378   //=========================================================================
00379 
00380 
00381 
00382   //=========================================================================
00384   void BlackBox::bbUserDelete() 
00385   {   
00386     bbtkDebugMessage("process",5,
00387                      "=> BlackBox::bbUserDelete() ["
00388                      <<bbGetFullName()<<"]"
00389                      <<" : not overloaded; using standard deletion"
00390                      <<std::endl);
00391     delete this;
00392   }
00393   //=========================================================================
00394 
00395 
00396   //=========================================================================
00397   BlackBox::BoxProcessModeValue BlackBox::bbGetBoxProcessModeValue() const
00398   {
00399     const std::string& p = bbmBoxProcessMode;
00400     if ( (p == "0") ||
00401          (p == "P") || (p == "p") ||
00402          (p == "Pipeline") || (p == "pipeline") ) return Pipeline;
00403     if ( (p == "1") ||
00404          (p == "A") || (p == "a") ||
00405          (p == "Always") || (p == "always") ) return Always;
00406     if ( (p == "2") ||
00407          (p == "R") || (p == "r") ||
00408          (p == "Reactive") || (p == "reactive") ) return Reactive;
00409     bbtkError(bbGetFullName()<<" : BoxProcessMode value '"<<p
00410               <<"' unknown. Possible values : "
00411               <<"'0'/'P'/'p'/'Pipeline'/'pipeline' | "
00412               <<"'1'/'A'/'a'/'Always'/'always' | "
00413               <<"'2'/'R'/'r'/'Reactive'/'reactive'"<<std::endl);
00414   }
00415   //=========================================================================
00416   
00417   //=========================================================================
00418   bool  BlackBox::bbBoxProcessModeIsReactive() const
00419   {
00420     return (bbGetBoxProcessModeValue() == Reactive);
00421   }
00422   //=========================================================================
00423 
00424   //=========================================================================
00425   bool  BlackBox::bbBoxProcessModeIsAlways() const
00426   {
00427     return (bbGetBoxProcessModeValue() == Always);
00428   }
00429   //=========================================================================
00430 
00431   //=========================================================================
00433   void BlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
00434   {
00435     bbtkDebugMessage("modified",1,
00436                      "==> BlackBox::bbSetModifiedStatus("<<c<<") ["
00437                      <<bbGetFullName()<<"]"<<std::endl);
00438     
00439     if ( (c==bbGetInputConnectorMap().find("WinHide")->second) )
00440       //         && (bbCanReact()))
00441       {
00442         bbtkDebugMessage("modified",2,
00443                          "-> Hide triggered by WinHide input change"
00444                          <<std::endl);
00445         this->bbHideWindow();
00446         this->bbSetStatus(MODIFIED); 
00447         return;
00448       }
00449     if ( (c==bbGetInputConnectorMap().find("WinClose")->second) )
00450       //         && (bbCanReact()))
00451       {
00452         bbtkDebugMessage("modified",2,
00453                          "-> Close triggered by WinClose input change"
00454                          <<std::endl);
00455         this->bbHideWindow();
00456         this->bbSetStatus(MODIFIED); 
00457         return;
00458       }
00459     
00460     if ( ( bbBoxProcessModeIsReactive()  ||
00461            (c==bbGetInputConnectorMap().find("BoxExecute")->second))
00462          && (bbCanReact() ) )
00463       {
00464         bbtkDebugMessage("modified",2,
00465                          "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
00466         this->bbSetStatus(MODIFIED); 
00467         bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
00468       }
00469     /*
00470     else if ( bbGetStatus() == MODIFIED ) //! this->bbIsUptodate()) 
00471       { 
00472         bbtkDebugMessage("modified",2,"-> Already modified"<<std::endl);
00473         return;
00474       }
00475     */
00476     else 
00477       {
00478         bbtkDebugMessage("modified",2,"-> Status set to modified"<<std::endl);
00479         this->bbSetStatus(MODIFIED); 
00480       }
00481  
00482     this->bbSignalOutputModification(false);
00483 
00484     /* 
00485   bbtkDebugMessageDec("process",5,
00486                         "<= BlackBox::bbSetModifiedStatus("<<c<<") ["
00487                         <<bbGetFullName()<<"]"<<std::endl);
00488     */
00489   }  
00490   //=========================================================================
00491 
00492   //=========================================================================  
00493   void BlackBox::bbSignalOutputModification(bool reaction)
00494   {
00495     bbtkDebugMessageInc("process",5,
00496                         "=> BlackBox::bbSignalOutputModification() ["
00497                         <<bbGetFullName()<<"]"<<std::endl);
00498     
00499     OutputConnectorMapType::iterator change = bbGetOutputConnectorMap().end();
00500     OutputConnectorMapType::iterator i;
00501     for ( i  = bbGetOutputConnectorMap().begin(); 
00502           i != bbGetOutputConnectorMap().end(); ++i) {
00503       /*     if ( i->first == "BoxChange" ) 
00504         {
00505           change = i;
00506           continue;
00507         }
00508       */
00509       i->second->SetModifiedStatus();
00510     } 
00511     //    if (change != bbGetOutputConnectorMap().end()) 
00512     // change->second->SetModifiedStatus();
00513 
00514     if (reaction) bbGlobalProcessExecutionList();
00515 
00516     bbtkDebugMessageDec("process",5,
00517                         "<= BlackBox::bbSignalOutputModification() ["
00518                         <<bbGetFullName()<<"]"<<std::endl);
00519     
00520   }  
00521   //=========================================================================   
00522   //=========================================================================  
00523   void BlackBox::bbSignalOutputModification(const std::string& output,
00524         bool reaction)
00525   {
00526     bbtkDebugMessageInc("process",5,
00527                         "=> BlackBox::bbSignalOutputModification("
00528                         <<output<<") ["
00529                         <<bbGetFullName()<<"]"<<std::endl);
00530     
00531     OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(output);
00532     if ( i == bbGetOutputConnectorMap().end() ) 
00533         {
00534           bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
00535         }
00536     i->second->SetModifiedStatus();
00537     // Has to notify the output "BoxChange" also
00538     if (output != "BoxChange") 
00539       {
00540         i = bbGetOutputConnectorMap().find("BoxChange");
00541         if ( i != bbGetOutputConnectorMap().end() ) 
00542           {
00543             i->second->SetModifiedStatus();
00544           }
00545       }
00546   if (reaction) bbGlobalProcessExecutionList();
00547 
00548   bbtkDebugMessageDec("process",5,
00549                       "<= BlackBox::bbSignalOutputModification("
00550                       <<output<<") ["
00551                       <<bbGetFullName()<<"]"<<std::endl);
00552 
00553   }  
00554   //=========================================================================   
00555   //=========================================================================  
00556   void BlackBox::bbSignalOutputModification(const std::vector<std::string>& output,
00557         bool reaction)
00558   {
00559     bbtkDebugMessageInc("process",5,
00560                         "=> BlackBox::bbSignalOutputModification(vector of outputs) ["
00561                         <<bbGetFullName()<<"]"<<std::endl);
00562     OutputConnectorMapType::iterator i;
00563     std::vector<std::string>::const_iterator o;
00564     for (o=output.begin();o!=output.end();++o) 
00565       {
00566         // the output "BoxChange" must be signaled **AFTER** all others
00567         if (*o == "BoxChange") continue;
00568         // Look for the connector
00569         i = bbGetOutputConnectorMap().find(*o);
00570         if ( i == bbGetOutputConnectorMap().end() ) 
00571           {
00572             bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
00573           }
00574         i->second->SetModifiedStatus();
00575       }
00576     // Has to notify the output "BoxChange" also
00577     i = bbGetOutputConnectorMap().find("BoxChange");
00578     if ( i != bbGetOutputConnectorMap().end() ) 
00579       {
00580         i->second->SetModifiedStatus();
00581       }
00582   if (reaction) bbGlobalProcessExecutionList();
00583 
00584    bbtkDebugMessageDec("process",5,
00585                        "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
00586                         <<bbGetFullName()<<"]"<<std::endl);
00587 
00588   }  
00589   //=========================================================================   
00590 
00591   //=========================================================================
00595   IOStatus BlackBox::bbUpdateInputs(bool excludeParent)
00596   {
00597     bbtkDebugMessageInc("process",4,
00598                         "=> BlackBox::bbUpdateInputs() ["
00599                         <<bbGetFullName()<<"]"
00600                         <<std::endl);   
00601 
00602     IOStatus s = UPTODATE;
00603 
00604     InputConnectorMapType::iterator i;
00605     for ( i = bbGetInputConnectorMap().begin(); 
00606           i!= bbGetInputConnectorMap().end(); ++i) 
00607       {
00608         if (excludeParent && (i->first=="WinParent")) continue;
00609         if (i->first=="WinHide") continue;
00610         // If input type is Void : no recurse
00611         //if (  bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo() 
00612         //      == typeid(Void) ) 
00613         //  continue;
00614 
00615         IOStatus t = i->second->BackwardUpdate();
00616         if (t==MODIFIED) s = MODIFIED;
00617       }
00618     
00619    bbtkDebugMessageDec("process",4,
00620                         "<= BlackBox::bbUpdateInputs() ["
00621                         <<bbGetFullName()<<"]"
00622                         <<std::endl);   
00623 
00624 
00625     return s;
00626   }
00627   //=========================================================================
00628 
00629  
00630   //=========================================================================
00632   void BlackBox::bbConnectInput( const std::string& name, Connection* c)
00633   {
00634     bbtkDebugMessage("connection",2,
00635                         "==> BlackBox::bbConnectInput(\""
00636                         <<name<<"\","<<c->GetFullName()<<") ["
00637                         <<bbGetFullName()<<"]"
00638                         <<std::endl);       
00639 
00640 
00641     InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
00642     if (i==bbGetInputConnectorMap().end())
00643       {
00644         bbtkError("no input called '"<<name<<"'");
00645       }
00646     i->second->SetConnection(c);
00647     
00648     bbtkDebugMessage("connection",2,
00649                         "<== BlackBox::bbConnectInput(\""
00650                         <<name<<"\","<<c->GetFullName()<<") ["
00651                         <<bbGetFullName()<<"]"
00652                         <<std::endl);       
00653     //  bbSetModifiedStatus();
00654 
00655   }
00656   //=========================================================================
00657 
00658 
00659   //=========================================================================  
00661   void BlackBox::bbConnectOutput( const std::string& name, Connection* c)
00662   {
00663     bbtkDebugMessage("connection",2,
00664                      "==> BlackBox::bbConnectOutput(\""<<name<<"\","
00665                      <<c->GetFullName()<<") ["
00666                      <<bbGetFullName()<<"]"<<std::endl);       
00667     
00668     OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
00669     if (i==bbGetOutputConnectorMap().end())
00670       {
00671         bbtkError("no output called '"<<name<<"'");
00672       }
00673     i->second->SetConnection(c);
00674 
00675     bbtkDebugMessage("connection",2,
00676                      "<== BlackBox::bbConnectOutput(\""<<name<<"\","
00677                      <<c->GetFullName()<<") ["
00678                      <<bbGetFullName()<<"]"<<std::endl);       
00679 
00680   }
00681   //=========================================================================
00682 
00683 
00684   //=========================================================================
00686   void BlackBox::bbDisconnectInput( const std::string& name, Connection* c)
00687   {
00688 
00689     bbtkDebugMessage("connection",2,
00690                      "==> BlackBox::bbDisconnectInput(\""<<name
00691                      <<"\","<<c->GetFullName()<<") ["
00692                      <<bbGetFullName()<<"]"
00693                      <<std::endl);      
00694 
00695     if (!c) 
00696       {
00697 
00698         bbtkDebugMessage("connection",2,"c==0"<<std::endl);     
00699         return;
00700       }
00701 
00702     InputConnectorMapType::iterator i = bbGetInputConnectorMap().find(name);
00703     if (i==bbGetInputConnectorMap().end())
00704       {
00705         bbtkError("no input called '"<<name<<"'");
00706       }
00707     i->second->UnsetConnection(c);
00708 
00709     bbtkDebugMessage("connection",2,
00710                      "<== BlackBox::bbDisconnectInput(\""<<name
00711                      <<"\","<<c->GetFullName()<<") ["
00712                      <<bbGetFullName()<<"]"
00713                      <<std::endl);      
00714 
00715   }
00716   //=========================================================================
00717 
00718 
00719   //=========================================================================
00721   void BlackBox::bbDisconnectOutput( const std::string& name, Connection* c)
00722   {
00723     bbtkDebugMessage("connection",2,
00724                      "==> BlackBox::bbDisconnectOutput(\""<<name
00725                      <<"\","<<c->GetFullName()<<") ["
00726                      <<bbGetFullName()<<"]"
00727                      <<std::endl);       
00728     if (!c) 
00729       {
00730 
00731         bbtkDebugMessage("connection",2,"c==0"<<std::endl);     
00732         return;
00733       }
00734 
00735     OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(name);
00736     if (i==bbGetOutputConnectorMap().end())
00737       {
00738         bbtkError("no output called '"<<name<<"'");
00739       }
00740     i->second->UnsetConnection(c);
00741 
00742     bbtkDebugMessage("connection",2,
00743                      "<== BlackBox::bbDisconnectOutput(\""<<name
00744                      <<"\","<<c->GetFullName()<<") ["
00745                      <<bbGetFullName()<<"]"
00746                      <<std::endl);       
00747   } 
00748   //=========================================================================
00749  
00750 
00751   //=========================================================================
00753   void BlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
00754   {
00755     fprintf(ff,"%s%p",bbGetTypeName().c_str(),this);
00756   }
00757   //=========================================================================
00758 
00759 
00760   //=========================================================================
00761   std::string BlackBox::bbGetOutputAsString( const std::string &output ) 
00762   {
00763     std::string v;
00764     // Looks for the adaptor
00765     if (bbGetOutputType(output).name() != typeid(std::string).name() ) 
00766       {
00767         // Look for factory 
00768         Package::Pointer p = bbGetDescriptor()->GetPackage();
00769         if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
00770           {
00771             Factory::Pointer f = p->GetFactorySet().begin()->lock();
00772             BlackBox::Pointer a;
00773             try
00774               {
00775                 a = f->NewAdaptor(  
00776                                   bbGetOutputType(output),
00777                                   typeid(std::string),
00778                                   "");
00779               } catch (bbtk::Exception e) 
00780               {
00781               }
00782             if (a){
00783               //                        bbUpdate();
00784               a->bbSetInput("In",bbGetOutput(output));
00785               a->bbExecute();
00786               v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
00787             } else {
00788               v="? (no adaptor found)";
00789             }
00790           }
00791         else 
00792           {
00793             v="? (no factory found)";
00794           }
00795       } 
00796     else 
00797       {
00798         //         bbUpdate();
00799         v = bbGetOutput(output).unsafe_get<std::string>() ;
00800       }
00801     return v;
00802   }
00803   //=========================================================================
00804 
00805   //=========================================================================
00806   std::string BlackBox::bbGetInputAsString( const std::string &input ) 
00807   {
00808     std::string v;
00809     // Looks for the adaptor
00810     if (bbGetInputType(input) != typeid(std::string)) 
00811       {
00812         // Look for factory 
00813         Package::Pointer p = bbGetDescriptor()->GetPackage();
00814         if ((p != 0) && ( ! p->GetFactorySet().empty() ) )
00815           {
00816             Factory::Pointer f = p->GetFactorySet().begin()->lock();
00817             BlackBox::Pointer a;
00818             try
00819               {
00820                 a = f->NewAdaptor(  
00821                                bbGetInputType(input),
00822                                typeid(std::string),
00823                                "");
00824               }catch (bbtk::Exception e) 
00825               {
00826               }
00827             if (a)
00828               {
00829                 //                      bbUpdate();
00830                 a->bbSetInput("In",bbGetInput(input));
00831                 a->bbExecute();
00832                 v = a->bbGetOutput("Out").unsafe_get<std::string>() ;
00833               } 
00834             else 
00835               {
00836                 v="? (no adaptor found)";
00837               }
00838           } 
00839         else 
00840           {
00841             v="? (no factory found)";
00842           }
00843       }
00844     else 
00845       {
00846         v = bbGetInput(input).unsafe_get<std::string>() ;
00847       }
00848     return v;
00849   }
00850   //=======================================================================
00851 
00852   //=======================================================================
00853   // Replaces substrings "<" by "["
00854   void SubsBrackets ( std::string& s )
00855   {
00856     //   std::cout << "BEFORE=["<<s<<"]"<<std::endl;
00857     std::string ss("<");
00858     std::string::size_type pos = 0;
00859     pos = s.find(ss,0);
00860     char* cr = "[";
00861     while ( pos != std::string::npos )
00862       {
00863         //      std::cout << "*** find one "<<std::endl;
00864         s.replace(pos,1,cr,1);
00865         pos = s.find(ss, pos);
00866       } 
00867     ss = ">";
00868     pos = 0;
00869     pos = s.find(ss,0);
00870     cr = "]";
00871     while ( pos != std::string::npos )
00872       {
00873         //      std::cout << "*** find one "<<std::endl;
00874         s.replace(pos,1,cr,1);
00875         pos = s.find(ss, pos);
00876       } 
00877     ss = ",";
00878     pos = 0;
00879     pos = s.find(ss,0);
00880     cr = "-";
00881     while ( pos != std::string::npos )
00882       {
00883         //      std::cout << "*** find one "<<std::endl;
00884         s.replace(pos,1,cr,1);
00885         pos = s.find(ss, pos);
00886       }     //    std::cout << "AFTER=["<<s<<"]"<<std::endl;
00887   }
00888   //=======================================================================
00889 
00890   //=========================================================================
00892   void BlackBox::bbWriteDotFileBlackBox(FILE *ff,
00893                                         BlackBox::Pointer parentblackbox, 
00894                                         int detail, int level,
00895                                         bool instanceOrtype,
00896                                         bool relative_link )
00897 
00898   { 
00899     InputConnectorMapType::iterator i;
00900     // label
00901     std::string labelStr;
00902     std::string valueStr("");
00903 
00904         if (detail==0) {
00905                 labelStr = bbGetName() ; 
00906 //EED 18 Fev 2008
00907                 labelStr = labelStr + "\\n[" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
00908         } else {
00909                 labelStr = bbGetName();
00910                 labelStr = labelStr + "   [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]  ";
00911     }
00912 
00913     SubsBrackets(labelStr);
00914     if (detail==1)
00915       {
00916         labelStr = labelStr + " | {{ "; 
00917         std::string tempStrTypeName;
00918         bool tmp; 
00919         tmp=false;
00920         for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i ) 
00921           {
00922             if (tmp==true)
00923               {
00924                 labelStr=labelStr+" | ";
00925               }
00926             tmp=true;
00927             if (instanceOrtype==true)
00928               {
00929                 valueStr = this->bbGetInputAsString(i->first) + " = ";
00930               } 
00931             const BlackBoxInputDescriptor* id = bbGetDescriptor()->GetInputDescriptor(i->first);
00932             tempStrTypeName=id->GetTypeName();
00933             SubsBrackets(tempStrTypeName);
00934             std::string Name(i->first);
00935             SubsBrackets(Name);
00936             labelStr=labelStr + "<"+i->first.c_str()+"> "  + valueStr +  Name.c_str() + "  [" + tempStrTypeName.c_str() + "]";
00937           }
00938         labelStr=labelStr+ " } | {";
00939         tmp = false;
00940         OutputConnectorMapType::iterator ii;
00941         for ( ii = mOutputConnectorMap.begin(); ii != mOutputConnectorMap.end(); ++ii ) 
00942         {
00943            if (tmp==true)
00944            {
00945                    labelStr=labelStr+" | ";
00946            }
00947            tmp = true;
00948            if (instanceOrtype==true)
00949            {
00950                    valueStr = this->bbGetOutputAsString(ii->first) + " = ";
00951            }
00952            const BlackBoxOutputDescriptor* id = bbGetDescriptor()->GetOutputDescriptor(ii->first); 
00953            tempStrTypeName=id->GetTypeName();
00954            SubsBrackets(tempStrTypeName);
00955             std::string Name(ii->first);
00956             SubsBrackets(Name);
00957            labelStr=labelStr+"<"+ii->first.c_str()+"> " + valueStr + Name.c_str() + "  ["+tempStrTypeName+"]";
00958         }
00959         labelStr = labelStr+ "      } }" ;
00960 } // detail
00961 
00962     fprintf(ff,"  " );
00963     bbWriteDotInputOutputName(ff,true,detail,level);
00964     std::string tmp ( bbGetTypeName() );
00965     SubsBrackets(tmp);
00966     std::string url;
00967     if (relative_link) 
00968       url = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL() + "#" + tmp;
00969     else 
00970       url = this->bbGetDescriptor()->GetPackage()->GetDocURL() + "#" + tmp;
00971   
00972     fprintf( ff , " [shape=record, URL=\"%s\",label=\"%s\"]%s\n",url.c_str(),labelStr.c_str(),";" );
00973     //    std::cout  << labelStr << std::endl;
00974 
00975     // Relation Input
00976     if (GetThisPointer<BlackBox>()!=parentblackbox){
00977       for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i ) 
00978         {
00979           if (i->second)
00980             {
00981               Connection* con = i->second->GetConnection();
00982               if (con!=NULL){
00983                 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
00984                 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
00985                 fprintf(ff,"  ");
00986                 a->bbWriteDotInputOutputName(ff,false,detail,level);
00987                 if (detail==1)
00988                   {
00989                     fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
00990                   }
00991                 fprintf(ff,"->");
00992                 b->bbWriteDotInputOutputName(ff,true,detail,level);
00993                 if (detail==1)
00994                   {
00995                     fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
00996                   }
00997                 fprintf(ff,"%s\n",";");
00998               }  // if con
00999             } // if second
01000         } // for
01001     } // if parentblackbox
01002   }
01003   //=========================================================================
01004 
01005 
01006 
01007 
01008   //=========================================================================
01009   void BlackBox::bbShowRelations(BlackBox::Pointer parentblackbox, 
01010                                  int detail, int level
01011                                  /*,Factory *factory*/ )
01012   {
01013      
01014     if (this->bbGetDescriptor()->GetPackage()) 
01015       {
01016         bbtkMessage("Help",1,"Black Box '"<<bbGetName()<<"' <"<<
01017                     this->bbGetDescriptor()->GetPackage()->GetName()
01018                     <<"::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
01019       }
01020     else 
01021       {
01022         bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
01023       }
01024     //    bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
01025     //    bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
01026 
01027     std::vector<std::string> iname;
01028     std::vector<std::string> ivalue;
01029     std::vector<std::string> iconn;
01030 
01031     InputConnectorMapType::iterator i;
01032     unsigned int namelmax = 0;
01033     unsigned int valuelmax = 0;
01034     //   unsigned int connlmax = 0;
01035     for ( i = mInputConnectorMap.begin(); i != mInputConnectorMap.end(); ++i ) 
01036       {
01037         iname.push_back(i->first);
01038         if (iname.back().size()>namelmax) namelmax = iname.back().size();
01039         ivalue.push_back(bbGetInputAsString(i->first));
01040         if (ivalue.back().size()>valuelmax) valuelmax = ivalue.back().size();
01041         std::string s("");
01042         Connection* con = i->second->GetConnection();
01043         if (con!=0){
01044           s = con->GetOriginalBlackBoxFrom()->bbGetName();
01045           s += ".";
01046           s += con->GetOriginalBlackBoxFromOutput();
01047         }  // if con
01048         iconn.push_back(s);
01049       }
01050     OutputConnectorMapType::iterator o;
01051     std::vector<std::string> oname;
01052     std::vector<std::string> ovalue;
01053     std::vector<std::vector<std::string> > oconn;
01054     for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o ) 
01055       {
01056         oname.push_back(o->first);
01057         if (oname.back().size()>namelmax) namelmax = oname.back().size();
01058         ovalue.push_back(bbGetOutputAsString(o->first));
01059         if (ovalue.back().size()>valuelmax) valuelmax = ovalue.back().size();
01060         std::vector<std::string> ss;
01061         const std::vector<Connection*>& con 
01062           = o->second->GetConnectionVector();
01063         std::vector<Connection*>::const_iterator c;
01064         for (c=con.begin();c!=con.end();++c) 
01065           {
01066             std::string s;
01067             s = (*c)->GetOriginalBlackBoxTo()->bbGetName();
01068             s += ".";
01069             s += (*c)->GetOriginalBlackBoxToInput();
01070             ss.push_back(s);
01071         }  // if con
01072         oconn.push_back(ss);
01073       }
01074 
01075     if (iname.size()) 
01076       bbtkMessage("Help",1," * Inputs : "<<std::endl);
01077     else 
01078       bbtkMessage("Help",1," * No inputs"<<std::endl);
01079 
01080     std::vector<std::string>::iterator i1,i2,i3;
01081     for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin();
01082          i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end();
01083          ++i1,++i2,++i3)
01084       {
01085         std::string name(*i1);
01086         name += "'";
01087         name.append(1+namelmax-name.size(),' ');
01088         std::string value(*i2);
01089         value += "'";
01090         value.append(1+valuelmax-value.size(),' ');
01091         if (i3->size()) 
01092           bbtkMessage("Help",1,"    '"<<name<<" = '"<<value<<" <-- '"<<*i3<<"'"<<std::endl);
01093         else 
01094           bbtkMessage("Help",1,"    '"<<name<<" = '"<<value<<std::endl);
01095       }
01096 
01097     if (oname.size()) 
01098       bbtkMessage("Help",1," * Outputs : "<<std::endl);
01099     else 
01100       bbtkMessage("Help",1," * No outputs"<<std::endl);
01101 
01102     std::vector<std::vector<std::string> >::iterator i4;
01103 
01104     for (i1=oname.begin(),i2=ovalue.begin(),i4=oconn.begin();
01105          i1!=oname.end(),i2!=ovalue.end(),i4!=oconn.end();
01106          ++i1,++i2,++i4)
01107       {
01108         std::string name(*i1);
01109         name += "'";
01110         name.append(1+namelmax-name.size(),' ');
01111         std::string value(*i2);
01112         value += "'";
01113         value.append(1+valuelmax-value.size(),' ');
01114         if (!(*i4).size())
01115           bbtkMessage("Help",1,"    '"<<name<<" = '"<<value<<std::endl);
01116         else 
01117           {
01118             std::string pref = "    '"+name+" = '"+value;
01119             for (i3=i4->begin();i3!=i4->end();++i3)
01120               {
01121                 bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'"<<std::endl);
01122                 pref.replace(0,pref.size(),pref.size(),' ');
01123               }
01124           }
01125       }
01126 
01127    }
01128   //=========================================================================
01129 
01130 
01131   //=========================================================================
01132    void BlackBox::bbGlobalProcessExecutionList()
01133    {   
01134      bbtkDebugMessageInc("process",3,
01135                          "=> BlackBox::bbGlobalProcessExecutionList()"
01136                          <<std::endl);     
01137      
01138      std::set<BlackBox::Pointer>::iterator i;
01139      for (i=bbmgExecutionList.begin();
01140           i!=bbmgExecutionList.end();
01141           ++i)
01142        {
01143          bbtkDebugMessage("process",4,
01144                           " -> Executing "<<(*i)->bbGetFullName()<<std::endl);
01145          (*i)->bbExecute(true);
01146        }
01147      
01148      bbmgExecutionList.clear();
01149      bbtkDebugMessageDec("process",3,
01150                          "<= BlackBox::bbGlobalProcessExecutionList()"
01151                          <<std::endl);     
01152      
01153      
01154    }
01155   //=========================================================================
01156 
01157     bool BlackBox::bbGlobalGetSomeBoxExecuting()
01158         { 
01159                 return bbmgSomeBoxExecuting; 
01160         }
01161 
01162     void BlackBox::bbGlobalSetSomeBoxExecuting(bool b) 
01163         { 
01164                 bbmgSomeBoxExecuting = b; 
01165         }
01166 
01167     void BlackBox::bbGlobalSetFreezeExecution(bool b) 
01168         { 
01169                 bbmgFreezeExecution = b;
01170         }
01171 
01172     bool BlackBox::bbGlobalGetFreezeExecution() 
01173         { 
01174                 return bbmgFreezeExecution; 
01175         }
01176 
01177   void BlackBox::bbGlobalAddToExecutionList( BlackBox::Pointer b )
01178         {  
01179                 bbmgExecutionList.insert(b); 
01180         } 
01181 
01182 
01183    //=========================================================================
01184 
01185   //=========================================================================
01186   void BlackBox::Check(bool recursive)
01187   {
01188     bbtkMessage("debug",1,"*** Checking Black Box "<<(void*)this<<" ["<<bbGetFullName()
01189                 <<"] ... OK"<<std::endl);
01190   }
01191   //=========================================================================
01192 
01193   void BlackBox::bbUserOnShowWidget(std::string nameInput)
01194   {
01195           bbtk::BlackBoxInputConnector *cc;
01196           cc = this->bbGetInputConnectorMap().find( nameInput.c_str() )->second;
01197           if (cc->GetConnection()!=NULL) 
01198           {
01199                   cc->GetConnection()->GetBlackBoxFrom()->bbUserOnShow();
01200           }
01201   }
01202 
01203 
01204 
01205 }  // EO namespace bbtk
01206 
01207 // EOF
01208 

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6