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

Generated on Thu May 31 14:12:03 2012 for BBTK by  doxygen 1.5.7.1