bbtkComplexBlackBox.cxx

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkComplexBlackBox.cxx,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:12 $
00006   Version:   $Revision: 1.21 $
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 
00031 
00036 #include "bbtkComplexBlackBox.h"
00037 #include "bbtkWx.h"
00038 #include "bbtkBlackBoxDescriptor.h"
00039 #include "bbtkFactory.h"
00040 #include "bbtkConfigurationFile.h"
00041 
00042 namespace bbtk
00043 {
00044 
00045   //==========================================================================
00047   ComplexBlackBox::Pointer ComplexBlackBox::New(const std::string& name,
00048                                                 ComplexBlackBoxDescriptor::Pointer desc)
00049   {
00050     bbtkDebugMessage("object",1,"##> ComplexBlackBox::New('"<<name<<"','"<<
00051                      desc->GetTypeName()<<"')" <<bbtkendl);
00052     ComplexBlackBox::Pointer p = 
00053       MakeBlackBoxPointer(new ComplexBlackBox(name,desc));
00054     bbtkDebugMessage("object",2,"<## ComplexBlackBox::New('"<<name<<"','"<<
00055                      desc->GetTypeName()<<"')" <<bbtkendl);
00056     return p;
00057   }
00058   //==========================================================================
00059 
00060   //=======================================================================
00062   ComplexBlackBox::ComplexBlackBox(const std::string &name,
00063                                    ComplexBlackBoxDescriptor::Pointer desc)
00064     : 
00065     BlackBox(name),
00066     mLockedDescriptor(desc),
00067     mDescriptor(desc)
00068   {
00069     bbtkDebugMessage("object",3,
00070                      "##> ComplexBlackBox::ComplexBlackBox(\""
00071                      <<name<<"\")"<<std::endl);
00072     bbAllocateConnectors();
00073     bbtkDebugMessage("object",3,
00074                      "<## ComplexBlackBox::ComplexBlackBox(\""
00075                      <<name<<"\")"<<std::endl);
00076   }
00077   //=======================================================================
00078 
00079   //=======================================================================
00081   ComplexBlackBox::ComplexBlackBox(ComplexBlackBox& from, 
00082                                    const std::string &name)
00083     : BlackBox(from,name),
00084       // The locked descriptor is copied from the unlocked one 
00085       // to make the box a non-prototype !!
00086       mLockedDescriptor(from.mDescriptor),
00087       mDescriptor(from.mDescriptor),
00088      mExecutionList(from.mExecutionList)    
00089   {
00090     bbtkDebugMessage("object",3,
00091                         "##> ComplexBlackBox::ComplexBlackBox(\""
00092                         <<from.bbGetName()<<"\",\""
00093                         <<name<<"\")"<<std::endl);
00094     bbtkDebugMessage("object",4,"  * Cloning Black Boxes"<<std::endl);
00095 
00096     // We have to make the shared_ptr on this because it is used 
00097     // in bbUnsafeAddBlackBox !
00098     MakeBlackBoxPointer(this,true);
00099 
00100     BlackBoxMapType::const_iterator i;
00101     for ( i = from.mBlackBoxMap.begin(); i != from.mBlackBoxMap.end(); ++i ) 
00102       {
00103         bbtkDebugMessageInc("object",5,"    * Cloning \""<<i->first<<"\""<<std::endl);
00104         BlackBox::Pointer B = i->second->bbClone(i->second->bbGetName());
00105         bbUnsafeAddBlackBox(B);
00106       }
00107    
00108     bbtkDebugMessage("object",4,"  * Cloning Connections"<<std::endl);
00109     ConnectionListType::const_iterator j;
00110     for ( j = from.mConnectionList.begin(); j != from.mConnectionList.end(); ++j ) 
00111       {
00112         bbtkDebugMessage("object",5,"    * Cloning \""<<
00113                          (*j)->GetFullName()<<"\""<<std::endl);
00114 
00115         BlackBox::Pointer bbfrom = bbGetBlackBox( (*j)->GetOriginalBlackBoxFrom()->bbGetName() );
00116         BlackBox::Pointer bbto = bbGetBlackBox( (*j)->GetOriginalBlackBoxTo()->bbGetName() );
00117 
00118         Connection::Pointer c = mDescriptor.lock()->GetFactory()-> 
00119           NewConnection( bbfrom, 
00120                          (*j)->GetOriginalBlackBoxFromOutput(), 
00121                          bbto, 
00122                          (*j)->GetOriginalBlackBoxToInput() );
00123         
00124         //c->Check();
00125         bbAddConnection(c);
00126 
00127       }
00128 
00129     bbAllocateConnectors();
00130     bbtkDebugMessage("object",3,
00131                         "<## ComplexBlackBox::ComplexBlackBox(\""
00132                         <<from.bbGetName()<<"\",\""
00133                         <<name<<"\")"<<std::endl);
00134   }
00135   //=======================================================================
00136 
00137   //======================================================================= 
00139   ComplexBlackBox::~ComplexBlackBox()
00140   {
00141     bbtkDebugMessage("object",3,
00142                      "==> ComplexBlackBox::~ComplexBlackBox() ["
00143                      <<bbGetName()<<"]"<<std::endl);
00144 
00145     bbtkDebugMessage("object",4,
00146                      " -> Releasing connections"<<std::endl);
00147     mConnectionList.clear();
00148     bbtkDebugMessage("object",4,
00149                      " -> Releasing boxes"<<std::endl);
00150     mBlackBoxMap.clear();
00151 
00152     //    Clear();
00153     this->bbDesallocateConnectors();
00154 
00155     bbtkDebugMessage("object",3,
00156                      "<== ComplexBlackBox::~ComplexBlackBox() ["
00157                      <<bbGetName()<<"]"<<std::endl);
00158   } 
00159   //=======================================================================
00160  
00161 
00162   //=========================================================================
00164   void ComplexBlackBox::bbAllocateConnectors()
00165   {  
00166     bbtkDebugMessageInc("Kernel",8,
00167                         "ComplexBlackBox::bbAllocateConnectors() ["
00168                         <<bbGetFullName()<<"]"
00169                         <<std::endl);   
00170     
00171     // Input connectors
00172     const BlackBoxDescriptor::InputDescriptorMapType& imap 
00173       = bbGetDescriptor()->GetInputDescriptorMap(); 
00174     BlackBoxDescriptor::InputDescriptorMapType::const_iterator i;       
00175     for ( i = imap.begin(); i != imap.end(); ++i )                      
00176       {                                                                 
00177         bbtkDebugMessage("Kernel",8,"* Allocate \""<<i->first<<"\""<<std::endl);
00178         // Redirect the connector to the internal box connector
00179         // Cast the BBInputDescriptor into a ComplexBBInputDescriptor
00180         ComplexBlackBoxInputDescriptor* d = 
00181           (ComplexBlackBoxInputDescriptor*)i->second;
00182         // Get the internal box connector
00183         BlackBoxInputConnector* c = 
00184           bbUnsafeGetBlackBox ( d->GetTarget() )
00185           ->bbGetInputConnectorMap()[ d->GetInput() ];
00186         
00187         bbGetInputConnectorMap()[i->second->GetName()] = c;
00188       }                                                                 
00189 
00190     // Output connectors
00191     const BlackBoxDescriptor::OutputDescriptorMapType& omap 
00192       = bbGetDescriptor()->GetOutputDescriptorMap();                   
00193     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o; 
00194     for ( o = omap.begin(); o != omap.end(); ++o )
00195       {                                                 
00196         bbtkDebugMessage("Kernel",8,"* Allocate \""<<o->first<<"\""<<std::endl);
00197         // Redirect the connector to the internal box connector
00198         // Cast the BBOutputDescriptor into a ComplexBBOutputDescriptor
00199         ComplexBlackBoxOutputDescriptor* d = 
00200           (ComplexBlackBoxOutputDescriptor*)o->second;
00201         // Get the internal box connector
00202         BlackBoxOutputConnector* c = 
00203           bbUnsafeGetBlackBox ( d->GetTarget() )
00204           ->bbGetOutputConnectorMap()[ d->GetOutput() ];
00205 
00206         bbGetOutputConnectorMap()[o->second->GetName()] = c;
00207       }
00208     
00209     bbtkDebugDecTab("Kernel",8);  
00210   }
00211   //=========================================================================
00212 
00213 
00214   //=========================================================================
00216   void ComplexBlackBox::bbDesallocateConnectors()
00217   {
00218     bbtkDebugMessageInc("Kernel",8,
00219                         "ComplexBlackBox::DesallocateConnectors()"
00220                         <<std::endl);                                   
00221 
00222     // The connectors have not been allocated by the complex box 
00223     // but by the internal boxes. Hence **DO NOT** desallocate !
00224     // just clear the maps to avoid that 
00225     // BlackBox::bbDesallocateConnectors delete the connectors
00226     bbGetInputConnectorMap().clear();
00227     bbGetOutputConnectorMap().clear();
00228 
00229     bbtkDebugDecTab("Kernel",8);  
00230 
00231   }
00232   //=========================================================================
00233 
00234   //=======================================================================
00235   BlackBox::Pointer ComplexBlackBox::bbClone(const std::string& name)
00236   {
00237     bbtkDebugMessageInc("Kernel",9,
00238                         "ComplexBlackBox::bbClone(\""<<name<<"\") ["
00239                         <<bbGetFullName()<<"]"<<std::endl);
00240     
00241     ComplexBlackBox* CBB = new ComplexBlackBox(*this,name);
00242     return MakeBlackBoxPointer(CBB);
00243   }
00244   //=======================================================================
00245 
00246   //=======================================================================
00249   void ComplexBlackBox::bbExecute(bool force)
00250   {
00251     bbtkDebugMessageInc("process",2,
00252                         "=> ComplexBlackBox::bbExecute() ["
00253                         <<bbGetFullName()<<"]"<<std::endl);
00254 
00255 
00256     Wx::BusyCursor wait;
00257     
00258     if (mExecutionList.size() != 0) 
00259       {
00260         
00261         std::vector<std::string>::const_iterator i;
00262         for (i=mExecutionList.begin(); 
00263              i!=mExecutionList.end();
00264              ++i) 
00265           {
00266             bbtkDebugMessage("process",3," -> Executing '"<<*i<<"'"<<std::endl);
00267             mBlackBoxMap[*i]->bbExecute(force);
00268           }
00269       }
00270     else 
00271       {
00272         std::map<std::string, BlackBox::Pointer>::iterator i;
00273         for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
00274           {
00275             i->second->bbExecute(force);
00276           }
00277       } 
00278 
00279   }
00280   //==================================================================
00281 
00282   //==================================================================
00283   void ComplexBlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
00284   {
00285     bbtkDebugMessage("modified",1,
00286                         "==> ComplexBlackBox::bbSetModifiedStatus("
00287                      <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
00288 
00289     c->GetBlackBox()->bbSetModifiedStatus(c);
00290 
00291     /*
00292     bbtkDebugMessage("modified",1,
00293                      "<== ComplexBlackBox::bbSetModifiedStatus("
00294                      <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
00295     */
00296   }
00297   //==================================================================
00298 
00299   //==================================================================
00300   void ComplexBlackBox::bbAddToExecutionList( const std::string& name )
00301   {
00302         bbtkDebugMessageInc("Kernel",9,
00303                         "ComplexBlackBox::bbAddToExecutionList(\""
00304                         <<name<<"\") ["
00305                         <<bbGetFullName()<<"]"<<std::endl);
00306 
00307         mExecutionList.push_back( name );
00308 
00309      bbtkDebugDecTab("Kernel",9);   
00310 
00311   }
00312   //==================================================================
00313 
00314   //==================================================================
00315   IOStatus ComplexBlackBox::bbBackwardUpdate(Connection::Pointer caller)
00316   {
00317     bbtkDebugMessageInc("process",3,
00318                         "==> ComplexBlackBox::bbBackwardUpdate("
00319                         <<(caller?caller->GetFullName():"0")<<") ["
00320                         <<bbGetFullName()<<"]"<<std::endl);
00321     //    bbtkInternalError("ComplexBlackBox::bbBackwardUpdate should never be called !");
00322     
00323     if (caller==0)
00324       {
00325         bbtkInternalError("ComplexBlackBox::bbBackwardUpdate called with caller=0");
00326       }
00327 
00328     /*
00329     std::cout << "CBB BUP : "<<caller->GetBlackBoxFrom()->bbGetFullName()
00330               <<"."<<caller->GetBlackBoxFromOutput()<<"----"
00331               <<caller->GetOriginalBlackBoxFrom()->bbGetFullName()
00332               <<"."<<caller->GetOriginalBlackBoxFromOutput()<<std::endl;
00333     */
00334       
00335 
00336 
00337     IOStatus s = UPTODATE;
00338     const BlackBoxDescriptor::OutputDescriptorMapType& omap 
00339       = bbGetDescriptor()->GetOutputDescriptorMap(); 
00340     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator i 
00341       = omap.find(caller->GetBlackBoxFromOutput());
00342     if (i!=omap.end())
00343       { 
00344         // Cast the BBOutputDescriptor into a ComplexBBOutputDescriptor
00345         ComplexBlackBoxOutputDescriptor* d = 
00346           (ComplexBlackBoxOutputDescriptor*)i->second;
00347         // Get the internal box 
00348         BlackBox::Pointer b = bbUnsafeGetBlackBox ( d->GetTarget() );
00349         // Calls BackwardUpdate on it
00350         bbtkDebugMessageInc("process",4,"Internal box connected to output : "<<d->GetTarget()<<std::endl);
00351         // Because internal box can also be a complex box we have to 
00352         // temporarily change the connection BlackBoxFromOutput to the 
00353         // mapped one
00354         
00355         //      std::string oldout = caller->GetBlackBoxFromOutput();
00356         //      std::cout << "oldout = "<<oldout<<std::endl;
00357         //      std::cout << "tmpout = "<<d->GetOutput()<<std::endl;
00358         //      caller->SetBlackBoxFromOutput(d->GetOutput());
00359         //
00360         //Connection newcaller(*caller);
00361         //newcaller.SetBlackBoxFromOutput(d->GetOutput());
00362         //IOStatus s1 = b->bbBackwardUpdate(&newcaller);
00363         IOStatus s1 = b->bbBackwardUpdate(caller);
00364         //newcaller.Clear();
00365         // restore old output
00366         //      caller->SetBlackBoxFromOutput(oldout);
00367 
00368         // ??? STATUS OF CBBs ???
00369         // ??? Here it is only the final status of the boxes connected to the output 
00370         if (s1==MODIFIED) s=MODIFIED;
00371       }
00372     else 
00373       {
00374         bbtkError("Connection '"<<caller->GetFullName()<<"' does not point to a valid output of the complex box !");
00375       }
00376 
00377     return s;
00378     
00379   }
00380   //==================================================================
00381 
00382 
00383   //==================================================================
00384   Data ComplexBlackBox::bbGetOutput( const std::string &name )
00385   {
00386     bbtkDebugMessageInc("Data",7,
00387                         "ComplexBlackBox::bbGetOutput(\""<<name<<"\") ["
00388                         <<bbGetFullName()<<"]"<<std::endl);
00389 
00390     ComplexBlackBoxOutputDescriptor* d = 
00391       (ComplexBlackBoxOutputDescriptor*)
00392       bbGetDescriptor()->GetOutputDescriptor(name);
00393     
00394     Data p = bbGetBlackBox(d->GetTarget())->bbGetOutput(d->GetOutput());
00395 
00396 
00397     bbtkDebugDecTab("Data",7);
00398     return p;
00399   }
00400   //==================================================================
00401 
00402   //==================================================================
00404   Data ComplexBlackBox::bbGetInput( const std::string &name ) 
00405   {
00406     bbtkDebugMessageInc("Data",7,
00407                         "ComplexBlackBox::bbGetInput(\""<<name<<"\") ["
00408                         <<bbGetFullName()<<"]"<<std::endl);  
00409 
00410     ComplexBlackBoxInputDescriptor* d = 
00411       (ComplexBlackBoxInputDescriptor*)
00412       bbGetDescriptor()->GetInputDescriptor(name);
00413 
00414     Data p = bbGetBlackBox(d->GetTarget())->bbGetInput(d->GetInput());
00415 
00416     bbtkDebugDecTab("Data",7);
00417     return p;
00418   }
00419   //==================================================================
00420 
00421   //==================================================================
00423   void ComplexBlackBox::bbSetOutput( const std::string &name, Data data)
00424   {
00425     bbtkDebugMessageInc("Data",7,
00426                         "ComplexBlackBox::bbSetOutput(\""<<name<<"\",data) ["
00427                         <<bbGetFullName()<<"]"<<std::endl); 
00428 
00429     ComplexBlackBoxOutputDescriptor* d = 
00430       (ComplexBlackBoxOutputDescriptor*)
00431       bbGetDescriptor()->GetOutputDescriptor(name);
00432     
00433     bbGetBlackBox(d->GetTarget())->bbSetOutput(d->GetOutput(),data);
00434 
00435     bbtkDebugDecTab("Data",7);
00436   }
00437   //==================================================================
00438   
00439   //==================================================================
00441   void ComplexBlackBox::bbSetInput( const std::string &name, Data data,
00442                                     bool setModified)
00443   {
00444     bbtkDebugMessageInc("Data",7,
00445                         "ComplexBlackBox::bbSetInput(\""<<name<<"\",data) ["
00446                         <<bbGetFullName()<<"]"<<std::endl);  
00447 
00448     ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
00449       bbGetDescriptor()->GetInputDescriptor(name);
00450 
00451     bbGetBlackBox(d->GetTarget())->bbSetInput(d->GetInput(),data,setModified);
00452 
00453     bbtkDebugDecTab("Data",7);
00454   }
00455   //==================================================================
00456 
00457  
00458   //==================================================================
00460   void ComplexBlackBox::bbBruteForceSetInputPointer( const std::string &name, 
00461                                                      void* data,
00462                                                      bool setModified)
00463   {
00464     bbtkDebugMessageInc("Data",7,
00465                         "ComplexBlackBox::bbBruteForceSetInputPointer('"
00466                         <<name<<"',"<<data<<") ["
00467                         <<bbGetFullName()<<"]"<<std::endl);  
00468 
00469     ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
00470       bbGetDescriptor()->GetInputDescriptor(name);
00471 
00472     bbGetBlackBox(d->GetTarget())->bbBruteForceSetInputPointer(d->GetInput(),
00473                                                                data,
00474                                                                setModified);
00475 
00476     bbtkDebugDecTab("Data",7);
00477   }
00478   //==================================================================
00479 
00480   //=========================================================================
00482   void ComplexBlackBox::bbConnectInput( const std::string& name, Connection* c)
00483   {
00484     bbtkDebugMessage("connection",2,
00485                      "==> ComplexBlackBox::bbConnectInput(\""
00486                      <<name<<"\","<<c->GetFullName()<<") ["
00487                      <<bbGetFullName()<<"]"
00488                      <<std::endl);       
00489     
00490    ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
00491       bbGetDescriptor()->GetInputDescriptor(name);
00492 
00493 
00494    BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
00495 
00496    bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
00497 
00498    c->SetBlackBoxTo(t);
00499    c->SetBlackBoxToInput(d->GetInput());
00500 
00501    bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
00502    t->bbConnectInput(d->GetInput(),c);
00503 
00504    bbtkDebugMessage("connection",2,
00505                      "<== ComplexBlackBox::bbConnectInput(\""
00506                      <<name<<"\","<<c->GetFullName()<<") ["
00507                      <<bbGetFullName()<<"]"
00508                      <<std::endl);       
00509   }
00510   //=========================================================================
00511 
00512 
00513   //=========================================================================  
00515   void ComplexBlackBox::bbConnectOutput( const std::string& name, Connection* c)
00516   {
00517     bbtkDebugMessage("connection",2,
00518                      "==> ComplexBlackBox::bbConnectOutput(\""
00519                      <<name<<"\","<<c->GetFullName()<<") ["
00520                      <<bbGetFullName()<<"]"<<std::endl);       
00521 
00522    ComplexBlackBoxOutputDescriptor* d = (ComplexBlackBoxOutputDescriptor*)
00523       bbGetDescriptor()->GetOutputDescriptor(name);
00524 
00525    BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
00526  
00527   bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
00528 
00529    c->SetBlackBoxFrom(t);
00530    c->SetBlackBoxFromOutput(d->GetOutput());
00531 
00532    bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
00533 
00534    t->bbConnectOutput(d->GetOutput(),c);
00535   
00536     bbtkDebugMessage("connection",2,
00537                      "<== ComplexBlackBox::bbConnectOutput(\""
00538                      <<name<<"\","<<c->GetFullName()<<") ["
00539                      <<bbGetFullName()<<"]"<<std::endl);       
00540   }
00541   //=========================================================================
00542 
00543 
00544   //==================================================================
00546   void ComplexBlackBox::bbAddBlackBox( BlackBox::Pointer b)
00547   {
00548     bbtkDebugMessageInc("Kernel",7,
00549                         "ComplexBlackBox::AddBlackBox(\""<<b->bbGetName()
00550                         <<"\") ["
00551                         <<bbGetFullName()<<"]"<<std::endl);  
00552     
00553     if ( bbUnsafeGetBlackBox(b->bbGetName()) ) 
00554       {
00555         bbtkError("a black box called \""<<b->bbGetName()
00556                   <<"\" already exists");
00557       }
00558     b->bbSetParent(GetThisPointer<ComplexBlackBox>());
00559     mBlackBoxMap[b->bbGetName()] = b;
00560 
00561     bbtkDebugDecTab("Kernel",7);
00562   }
00563   //==================================================================
00564 
00565   //==================================================================
00567   void ComplexBlackBox::bbUnsafeAddBlackBox( BlackBox::Pointer b)
00568   {
00569     bbtkDebugMessageInc("Kernel",7,
00570                         "ComplexBlackBox::UnsafeAddBlackBox(\""<<b->bbGetName()
00571                         <<"\") ["
00572                         <<bbGetFullName()<<"]"<<std::endl);  
00573     
00574     b->bbSetParent(GetThisPointer<ComplexBlackBox>());
00575     mBlackBoxMap[b->bbGetName()] = b;
00576 
00577     bbtkDebugDecTab("Kernel",7);
00578   }
00579   //==================================================================
00580 
00581   //==================================================================
00583   void ComplexBlackBox::bbRemoveBlackBox( const std::string& name, 
00584                                           bool remove_connections )
00585   {
00586     bbtkDebugMessageInc("Kernel",7,
00587                         "ComplexBlackBox::RemoveBlackBox(\""<<name<<"\") ["
00588                         <<bbGetFullName()<<"]"<<std::endl);  
00589 
00590     BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
00591     if ( i == mBlackBoxMap.end() ) 
00592       {
00593         bbtkError("the black box \""<<name<<"\" does not exist");
00594       }
00595     BlackBox::WeakPointer p = i->second;
00596     
00597     if (remove_connections)
00598       {
00599         ConnectionListType::const_iterator j;
00600         for ( j = mConnectionList.begin(); 
00601               j != mConnectionList.end(); ++j ) 
00602           {
00603             (*j)->Check();
00604           }
00605       }
00606     if (p.use_count()!=1) 
00607       {
00608         bbtkError("the black box \""<<name<<"\" is still connected");
00609       }
00610     
00611     mBlackBoxMap.erase(i);
00612     
00613 
00614     // Unload orphan dl packages 
00615     Package::UnLoadReleasedDynamicallyLoadedPackages();
00616 
00617     bbtkDebugDecTab("Kernel",7);
00618   }
00619   //==================================================================
00620 
00621   //==================================================================
00623   void ComplexBlackBox::bbAddConnection( Connection::Pointer c)
00624   {
00625     bbtkDebugMessageInc("Kernel",7,
00626                         "ComplexBlackBox::AddConnection(\""<<"..."<<"\") ["
00627                         <<bbGetFullName()<<"]"<<std::endl);  
00628 
00629     mConnectionList.push_back(c);
00630 
00631     bbtkDebugDecTab("Kernel",7);
00632   }
00633   //==================================================================
00634   //    void RemoveConnection( );
00635 
00636   //==================================================================
00638   BlackBox::Pointer ComplexBlackBox::bbGetBlackBox( const std::string& name )
00639   {
00640     bbtkDebugMessageInc("Kernel",9,
00641                         "ComplexBlackBox::GetBlackBox(\""<<name<<"\") ["
00642                         <<bbGetFullName()<<"]"<<std::endl);  
00643 
00644     BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
00645     if ( i == mBlackBoxMap.end() ) 
00646       {
00647         bbtkError("the black box \""<<name<<"\" does not exist");
00648       }
00649 
00650     bbtkDebugDecTab("Kernel",9);
00651     return i->second;
00652   }
00653   //==================================================================
00654 
00655   //==================================================================
00658   BlackBox::Pointer ComplexBlackBox::bbUnsafeGetBlackBox( const std::string& name )
00659   {
00660     bbtkDebugMessageInc("Kernel",9,
00661                         "ComplexBlackBox::UnsafeGetBlackBox(\""<<name<<"\") ["
00662                         <<bbGetFullName()<<"]"
00663                         <<std::endl);  
00664 
00665     BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
00666     if ( i == mBlackBoxMap.end() ) 
00667       {
00668         bbtkDebugDecTab("Kernel",9);
00669         return BlackBox::Pointer();
00670       }
00671 
00672     bbtkDebugDecTab("Kernel",9);
00673     return i->second;
00674     
00675   }
00676   //==================================================================
00677 
00678   //==================================================================
00679   void ComplexBlackBox::bbPrintBlackBoxes()
00680   {
00681     bbtkDebugMessageInc("Kernel",9,
00682                         "ComplexBlackBox::PrintBlackBoxes() ["
00683                         <<bbGetFullName()<<"]"
00684                         <<std::endl);  
00685 
00686     BlackBoxMapType::iterator i;
00687     for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i ) 
00688       {
00689         bbtkMessage("Help",1,i->second->bbGetFullName()<<std::endl);
00690       }
00691 
00692     bbtkDebugDecTab("Kernel",9);
00693   }
00694   //==================================================================
00695 
00696 
00697  
00698   //=========================================================================
00700   void ComplexBlackBox::bbWriteDotInputOutputName(FILE *ff,bool inputoutput,int detail, int level)
00701   {
00702     if (inputoutput)
00703       {
00704         fprintf(ff,"%s_IN_%p",bbGetTypeName().c_str(),this);
00705       } else {
00706       fprintf(ff,"%s_OUT_%p",bbGetTypeName().c_str(),this);
00707     } // if inputoutput
00708   }
00709   //=========================================================================
00710 
00711 
00712   //=========================================================================
00713   BlackBox::Pointer ComplexBlackBox::bbFindBlackBox(const std::string &blackboxname)
00714   {
00715     BlackBox::Pointer blackbox;
00716     std::string subname="";
00717     std::string restname="";
00718     std::string delimiters(">");
00719     // Skip delimiters at beginning.
00720     std::string::size_type lastPos = blackboxname.find_first_not_of(delimiters, 0);
00721     // Find first "non-delimiter".
00722     std::string::size_type pos     = blackboxname.find_first_of(delimiters, lastPos);
00723             
00724     // Found a token, add it to the vector.
00725     subname = blackboxname.substr(lastPos, pos - lastPos);
00726     restname = blackboxname.substr(lastPos+pos - lastPos+1, 999);
00727 
00728     if (restname==subname)
00729     {
00730            restname="";
00731     }
00732 
00733     BlackBoxMapType::iterator i = mBlackBoxMap.find(subname);
00734     if ( i != mBlackBoxMap.end() ) 
00735     {
00736        blackbox = i->second;
00737        if (restname!="")
00738             {
00739              blackbox = blackbox->bbFindBlackBox(restname);
00740             }
00741     }
00742     return blackbox;
00743   }
00744   //=========================================================================
00745 
00746   //=========================================================================
00747   void ComplexBlackBox::bbInsertHTMLGraph(  std::ofstream& s, 
00748                                             int detail, 
00749                                             int level,
00750                                             bool instanceOrtype,
00751                                             const std::string& output_dir,
00752                                             bool relative_link )
00753   {
00754     
00755     std::string directory(output_dir);
00756 
00757     if (output_dir.length() == 0) 
00758     {
00759            // Don't pollute the file store with  "temp_dir" directories ... 
00760            std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
00761            directory =  default_temp_dir + "/" + "temp_dir";
00762     }
00763     
00764     std::string simplefilename  (this->bbGetTypeName()+"_"+this->bbGetName());
00765     std::string simplefilename_png (simplefilename+".png");
00766     std::string filename           (directory+"/"+simplefilename);
00767     std::string filename_png       (filename+".png");
00768     std::string filename_cmap      (filename+".cmap");
00769     std::string filename_dot       (filename+".dot");
00770 
00771     std::string filename_png2   ("\""   + filename_png  + "\"");
00772     std::string filename_cmap2  ("\""   + filename_cmap + "\"");
00773     std::string filename_dot2   ("\""   + filename_dot  + "\"");
00774     
00775 
00776     std::string command1 ("dot -T png -o "  
00777                           + filename_png2  + " " + filename_dot2);
00778     std::string command1a("dot -T cmap -o " 
00779                           + filename_cmap2 + " " + filename_dot2);
00780 
00781     // 1. Generating .dot file
00782     FILE *ff;
00783     ff = fopen(filename_dot.c_str(),"w");
00784     fprintf(ff,"digraph bbtk_graph{\n");
00785     fprintf(ff,"rankdir=LR%s\n",";");
00786     fprintf(ff,"node [shape=record]%s\n",";");
00787 
00788     this->bbWriteDotFileBlackBox(ff,
00789                                  GetThisPointer<ComplexBlackBox>(),
00790                                  detail,level,
00791                                  instanceOrtype,
00792                                  relative_link );
00793 
00794     fprintf(ff,"}\n");
00795     fclose(ff);
00796 
00797     // 2. Executing .dot file -> png
00798     system( command1.c_str() );   
00799     // 3. Executing .dot file -> cmap
00800     system( command1a.c_str() );  
00801     
00802     // 4. HTML code insertion
00803     // 4.1 image
00804     (s) << "<center><img src=\"" << simplefilename_png 
00805          <<   "\" border=\"0\" usemap=\"#map_"<< simplefilename 
00806          <<"\" alt=\"\"></center>\n";
00807 
00808     // 4.2 map
00809     (s) << "<map name=\"map_"<< simplefilename <<"\">\n";
00810     FILE *ff2;
00811     char c;
00812     ff2=fopen(filename_cmap.c_str(),"r");
00813     while (!feof(ff2))
00814     {
00815            c=fgetc(ff2);
00816            if (c!=-1)
00817            {
00818               (s) << c;
00819            }
00820     }
00821     (s) << "</map>\n\n";
00822     
00823     // End
00824   }
00825   //=========================================================================
00826 
00827 
00828   //=========================================================================
00830   void ComplexBlackBox::bbWriteDotFileBlackBox(FILE *ff,
00831                                                BlackBox::Pointer parentblackbox, 
00832                                                int detail, int level,
00833                                                bool instanceOrtype,
00834                                                bool relative_link )   
00835   {
00836     std::string tmp1;
00837     std::string valueStr("");
00838     Package::Pointer package = this->bbGetDescriptor()->GetPackage(); 
00839 
00840     if (package!=NULL)
00841     {
00842       if (relative_link)
00843              tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL();
00844            else 
00845              tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocURL();
00846     } 
00847     else 
00848     {
00849            tmp1 = "Caspitas";
00850     }
00851 
00852     std::string tmp2=bbGetTypeName();
00853     std::string url(tmp1 + "#" + tmp2 );        
00854     fprintf( ff , "subgraph cluster_%s_%p {\n",bbGetName().c_str(),this);
00855 
00856     if (!(   (bbGetTypeName()=="workspace") && (bbGetName()=="workspacePrototype")) )
00857     {
00858            fprintf( ff , "  URL = \"%s\" %s",url.c_str(),";");
00859     }
00860 
00861     std::string boxname="["+bbGetTypeName()+"]";
00862     if (GetThisPointer<ComplexBlackBox>()!=parentblackbox)
00863     {
00864        if (detail==0)
00865             {
00866           boxname=bbGetName();
00867        } 
00868        else 
00869        {
00870           boxname = bbGetName();
00871           boxname = boxname + "   [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]";
00872             }
00873 
00874     }
00875     fprintf( ff , "  label = \"%s\"%s\n",  boxname.c_str() ,";");
00876 
00877 
00878 
00879     //  fprintf( ff , "  style=filled%s\n",";");
00880     //  fprintf( ff , "  color=grey%s\n",";");
00881     fprintf( ff , "  node [style=filled]%s\n",";");
00882     fprintf( ff , "  fillcolor=grey%s\n",";");
00883     fprintf( ff , "  edge [color=blue]%s\n",";");
00884 
00885 
00886     // Labels Salida
00887     std::string labelStr1;
00888     std::string labelStr2;
00889     labelStr1 = boxname + "\\n(output)" ; 
00890     labelStr2 = " | {{ ";
00891     bool tmp; 
00892     tmp=false;
00893     OutputConnectorMapType::iterator i;
00894 
00895     const BlackBoxDescriptor::OutputDescriptorMapType& omap = this->bbGetDescriptor()->GetOutputDescriptorMap();
00896     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;      
00897     for ( o = omap.begin();  o != omap.end(); ++o ) 
00898       {
00899         if (tmp==true)
00900           {
00901             labelStr2=labelStr2+" | ";
00902           }
00903         tmp=true;
00904         if (instanceOrtype==true)
00905           {
00906             valueStr = this->bbGetOutputAsString(o->second->GetName()/*
00907                                                                    ,factory*/) + " = ";
00908           } 
00909         labelStr2=labelStr2+"<"+o->second->GetName().c_str()+"> " + valueStr + o->second->GetName().c_str();
00910       }
00911 
00912     labelStr2 = labelStr2+ " } }";
00913 
00914 
00915 
00916     if (detail==1)
00917       {
00918         labelStr1 = labelStr1 + labelStr2;
00919       }
00920 
00921     if (tmp){
00922       fprintf(ff,"  " );
00923       bbWriteDotInputOutputName(ff,false,detail,level);
00924       fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" );
00925     }
00926 
00927 
00928     // label Entrada
00929     labelStr1 = boxname + "\\n(input)" ; 
00930     labelStr2 = " | {{ "; 
00931     tmp=false;
00932     InputConnectorMapType::iterator ii;
00933 
00934     const BlackBoxDescriptor::InputDescriptorMapType& imap = this->bbGetDescriptor()->GetInputDescriptorMap();  
00935     BlackBoxDescriptor::InputDescriptorMapType::const_iterator iii;
00936     for ( iii = imap.begin();  iii != imap.end(); ++iii ) 
00937       {
00938         if (tmp==true)
00939           {
00940             labelStr2=labelStr2+" | ";
00941           }
00942         tmp=true;
00943         if (instanceOrtype==true)
00944           {
00945             valueStr = this->bbGetInputAsString(iii->second->GetName()/*,factory*/) + " = ";
00946           } 
00947         labelStr2=labelStr2+"<"+iii->second->GetName().c_str()+"> " + valueStr + iii->second->GetName().c_str();
00948       }
00949 
00950 
00951     labelStr2 = labelStr2+ " } }";
00952     if (detail==1)
00953       {
00954         labelStr1 = labelStr1 + labelStr2;
00955       }
00956     if (tmp){
00957       fprintf(ff,"  " );
00958       bbWriteDotInputOutputName(ff,true,detail,level);
00959       fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" );
00960     }
00961 
00962 
00963     // Body
00964     BlackBoxMapType::iterator j;
00965     for ( j = mBlackBoxMap.begin(); j != mBlackBoxMap.end(); ++j ) 
00966       {
00967         if (level>-1)
00968           {
00969             j->second->bbWriteDotFileBlackBox(ff,
00970                                               parentblackbox,
00971                                               detail,
00972                                               level-1,
00973                                               instanceOrtype,
00974                                               relative_link);
00975           }
00976       }
00977 
00978     fprintf( ff , "}\n\n");
00979 
00980     fprintf( ff , "  edge[color=blue]%s\n",";");
00981 
00982     if (level>-1)
00983       {
00984         // Relation Input  with the inside BlackBox of the this ComplexBlackbox
00985         ComplexBlackBoxDescriptor::InputDescriptorMapType::iterator xx;
00986         ComplexBlackBoxDescriptor::InputDescriptorMapType idmt=bbGetDescriptor()->GetInputDescriptorMap();
00987         for ( xx = idmt.begin(); xx != idmt.end(); ++xx ) 
00988           {
00989             ComplexBlackBoxInputDescriptor *cbbid = (ComplexBlackBoxInputDescriptor*)xx->second;
00990 
00991             fprintf(ff,"  ");
00992             bbWriteDotInputOutputName(ff,true,detail,level);
00993             if (detail==1)
00994               {
00995                 fprintf(ff,":%s",cbbid->GetName().c_str() );
00996               }
00997             fprintf(ff,"->" );
00998             BlackBox::Pointer bb = bbGetBlackBox( cbbid->GetTarget() ); 
00999             bb->bbWriteDotInputOutputName(ff,true,detail,level);
01000             if (detail==1)
01001               {
01002                 fprintf(ff,":%s \n", cbbid->GetInput().c_str() );
01003               }
01004           }// for xx
01005 
01006 
01007 
01008         fprintf(ff,"\n \n");
01009 
01010 
01011 
01012         // Relation Output ComplexBlackBox
01013         ComplexBlackBoxDescriptor::OutputDescriptorMapType::iterator yy;
01014         ComplexBlackBoxDescriptor::OutputDescriptorMapType odmt=bbGetDescriptor()->GetOutputDescriptorMap();
01015         for ( yy = odmt.begin(); yy != odmt.end(); ++yy ) 
01016           {
01017             ComplexBlackBoxOutputDescriptor *cbbod = (ComplexBlackBoxOutputDescriptor*)yy->second;
01018             fprintf(ff,"  ");
01019             BlackBox::Pointer bb = bbGetBlackBox( cbbod->GetTarget() ); 
01020             bb->bbWriteDotInputOutputName(ff,false,detail,level);
01021             if (detail==1)
01022               {
01023                 fprintf(ff,":%s", cbbod->GetOutput().c_str() );
01024               }
01025             fprintf(ff,"->" );
01026             bbWriteDotInputOutputName(ff,false,detail,level);
01027             if (detail==1)
01028               {
01029                 fprintf(ff,":%s",cbbod->GetName().c_str() );
01030               }
01031             fprintf(ff,"\n");
01032           } // for yy
01033 
01034       } // if level
01035 
01036 
01037 
01038         // Relation from the out side of this ComplexBlackBox with its Inputs
01039     if (GetThisPointer<ComplexBlackBox>()!=parentblackbox) {
01040       for ( ii = bbGetInputConnectorMap().begin(); 
01041             ii != bbGetInputConnectorMap().end(); ++ii ) 
01042         {
01043           if (ii->second)
01044             {
01045               Connection* con = ii->second->GetConnection();
01046               if (con!=NULL){
01047                 BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
01048                 BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
01049                 fprintf(ff,"  ");
01050                 a->bbWriteDotInputOutputName(ff,false,detail,level);
01051                 if (detail==1)
01052                   {
01053                     fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
01054                   }
01055                 fprintf(ff,"->");
01056                 b->bbWriteDotInputOutputName(ff,true,detail,level);
01057                 if (detail==1)
01058                   {
01059                     fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
01060                   }
01061                 fprintf(ff,"%s\n",";");
01062               } // if con
01063             } // if second
01064         } // for
01065     } // if parentblackbox
01066   }
01067   //=========================================================================
01068   
01069 
01070 
01071 
01072   /*
01073 
01074   //=======================================================================
01077   void ComplexBlackBox::GetPackagesDependencies(std::vector<Package*>& deps)
01078   {
01079     deps.clear;
01080     BlackBoxMapType::iterator i;
01081     for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i ) 
01082       {
01083         deps.push_back(i->second->bbGetDescriptor()->GetPackage());
01084       }
01085 
01086   }
01087   //=======================================================================
01088   */
01089 
01090   //=======================================================================
01091   void ComplexBlackBox::Check(bool recursive)
01092   {
01093      bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
01094                  <<" ["<<bbGetFullName()<<"]"<<std::endl);
01095      
01096      BlackBoxMapType::const_iterator i;
01097      for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i ) 
01098        {
01099          i->second->Check(recursive);
01100        }
01101      ConnectionListType::const_iterator j;
01102      for ( j = mConnectionList.begin(); 
01103            j != mConnectionList.end(); ++j ) 
01104        {
01105          (*j)->Check();
01106        }
01107      bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
01108                  <<" ["<<bbGetFullName()<<"] ... OK"<<std::endl);
01109   
01110   }
01111   //=======================================================================
01112 
01113   //=========================================================================
01115   std::string ComplexBlackBox::bbGetNameWithParent() const
01116   {
01117     if (!IsAPrototype()) return BlackBox::bbGetNameWithParent();
01118     if (bbGetDescriptor()) 
01119       {
01120         return bbGetDescriptor()->GetFullTypeName() + ":" + bbGetName();
01121       }
01122     else 
01123       {
01124         return std::string(":") + bbGetName();
01125       }
01126   } 
01127   //=========================================================================
01128 
01129   //==========================================================================
01130   std::string ComplexBlackBox::GetObjectName() const
01131   {
01132     return std::string("ComplexBlackBox '")+bbGetNameWithParent()
01133       +std::string("'");
01134   }
01135   //==========================================================================
01136   
01137   //==========================================================================
01138   std::string ComplexBlackBox::GetObjectInfo() const 
01139   {
01140     std::stringstream i;
01141     i << "  - "<<mBlackBoxMap.size() << " boxes / "
01142       <<mConnectionList.size() << " connections" << std::endl;
01143     return i.str();
01144   }
01145   //==========================================================================
01146 
01147   //==========================================================================
01148   size_t  ComplexBlackBox::GetObjectSize() const 
01149   {
01150     size_t s = Superclass::GetObjectSize();
01151     s += ComplexBlackBox::GetObjectInternalSize();
01152     return s;
01153   }
01154   //==========================================================================
01155   //==========================================================================
01156   size_t  ComplexBlackBox::GetObjectInternalSize() const 
01157   {
01158     size_t s = sizeof(ComplexBlackBox);
01159     return s;
01160   }
01161   //==========================================================================
01162   //==========================================================================
01163   size_t  ComplexBlackBox::GetObjectRecursiveSize() const 
01164   {
01165     size_t s = Superclass::GetObjectRecursiveSize();
01166     s += ComplexBlackBox::GetObjectInternalSize();
01167     BlackBoxMapType::const_iterator i;
01168     for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i ) 
01169       {
01170         s += i->second->GetObjectRecursiveSize();
01171       }
01172     ConnectionListType::const_iterator j;
01173     for ( j = mConnectionList.begin(); 
01174           j != mConnectionList.end(); ++j ) 
01175       {
01176         s += (*j)->GetObjectRecursiveSize();
01177       }
01178     return s;
01179   }
01180   //==========================================================================
01181 
01182 }

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