]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkComplexBlackBox.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkComplexBlackBox.cxx
index dbd76efd197b83a0a697152a58ce75743c09934e..f47e401248fdf3c0c5bd7fec30479eec353721a1 100644 (file)
@@ -3,8 +3,8 @@
 Program:   bbtk
 Module:    $RCSfile: bbtkComplexBlackBox.cxx,v $
 Language:  C++
-Date:      $Date: 2008/04/09 11:16:57 $
-Version:   $Revision: 1.9 $
+Date:      $Date: 2008/07/24 14:37:05 $
+Version:   $Revision: 1.20 $
                                                                                 
 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
 l'Image). All rights reserved. See Doc/License.txt or
@@ -22,6 +22,7 @@ PURPOSE.  See the above copyright notices for more information.
  *  \brief class bbtk::ComplexBlackBox : user defined complex black boxes
  */
 #include "bbtkComplexBlackBox.h"
+#include "bbtkWx.h"
 #include "bbtkBlackBoxDescriptor.h"
 #include "bbtkFactory.h"
 #include "bbtkConfigurationFile.h"
@@ -29,19 +30,37 @@ PURPOSE.  See the above copyright notices for more information.
 namespace bbtk
 {
 
+  //==========================================================================
+  /// Creates a new complex black box
+  ComplexBlackBox::Pointer ComplexBlackBox::New(const std::string& name,
+                                               ComplexBlackBoxDescriptor::Pointer desc)
+  {
+    bbtkDebugMessage("object",1,"##> ComplexBlackBox::New('"<<name<<"','"<<
+                    desc->GetTypeName()<<"')" <<bbtkendl);
+    ComplexBlackBox::Pointer p = 
+      MakeBlackBoxPointer(new ComplexBlackBox(name,desc));
+    bbtkDebugMessage("object",2,"<## ComplexBlackBox::New('"<<name<<"','"<<
+                    desc->GetTypeName()<<"')" <<bbtkendl);
+    return p;
+  }
+  //==========================================================================
 
   //=======================================================================
   /// Usefull constructor 
   ComplexBlackBox::ComplexBlackBox(const std::string &name,
-                                  ComplexBlackBoxDescriptor* desc)
-    : BlackBox(name),
-      mDescriptor(desc)
+                                  ComplexBlackBoxDescriptor::Pointer desc)
+    : 
+    BlackBox(name),
+    mLockedDescriptor(desc),
+    mDescriptor(desc)
   {
-    bbtkDebugMessageInc("Kernel",9,
-                       "ComplexBlackBox::ComplexBlackBox(\""
-                       <<name<<"\")"<<std::endl);
+    bbtkDebugMessage("object",3,
+                    "##> ComplexBlackBox::ComplexBlackBox(\""
+                    <<name<<"\")"<<std::endl);
     bbAllocateConnectors();
-    bbtkDebugDecTab("Kernel",9);
+    bbtkDebugMessage("object",3,
+                    "<## ComplexBlackBox::ComplexBlackBox(\""
+                    <<name<<"\")"<<std::endl);
   }
   //=======================================================================
 
@@ -50,50 +69,56 @@ namespace bbtk
   ComplexBlackBox::ComplexBlackBox(ComplexBlackBox& from, 
                                   const std::string &name)
     : BlackBox(from,name),
+      // The locked descriptor is copied from the unlocked one 
+      // to make the box a non-prototype !!
+      mLockedDescriptor(from.mDescriptor),
       mDescriptor(from.mDescriptor),
      mExecutionList(from.mExecutionList)    
   {
-    bbtkDebugMessageInc("Kernel",9,
-                       "ComplexBlackBox::ComplexBlackBox(\""
+    bbtkDebugMessage("object",3,
+                       "##> ComplexBlackBox::ComplexBlackBox(\""
                        <<from.bbGetName()<<"\",\""
                        <<name<<"\")"<<std::endl);
+    bbtkDebugMessage("object",4,"  * Cloning Black Boxes"<<std::endl);
+
+    // We have to make the shared_ptr on this because it is used 
+    // in bbUnsafeAddBlackBox !
+    MakeBlackBoxPointer(this,true);
 
-    bbtkDebugMessageInc("Kernel",9,"* Cloning Black Boxes"<<std::endl);
     BlackBoxMapType::const_iterator i;
     for ( i = from.mBlackBoxMap.begin(); i != from.mBlackBoxMap.end(); ++i ) 
       {
-       bbtkDebugMessageInc("Kernel",9,"* Cloning \""<<i->first<<"\""<<std::endl);
-       BlackBox* B = i->second->bbClone(i->second->bbGetName());
+       bbtkDebugMessageInc("object",5,"    * Cloning \""<<i->first<<"\""<<std::endl);
+       BlackBox::Pointer B = i->second->bbClone(i->second->bbGetName());
        bbUnsafeAddBlackBox(B);
-       
-       bbtkDebugDecTab("Kernel",9);
       }
-    bbtkDebugDecTab("Kernel",9);
    
-    bbtkDebugMessageInc("Kernel",9,"* Cloning Connections"<<std::endl);
+    bbtkDebugMessage("object",4,"  * Cloning Connections"<<std::endl);
     ConnectionListType::const_iterator j;
     for ( j = from.mConnectionList.begin(); j != from.mConnectionList.end(); ++j ) 
       {
-       bbtkDebugMessageInc("Kernel",9,"* Cloning \""<<
-                           (*j)->GetFullName()<<"\""<<std::endl);
+       bbtkDebugMessage("object",5,"    * Cloning \""<<
+                        (*j)->GetFullName()<<"\""<<std::endl);
+
+       BlackBox::Pointer bbfrom = bbGetBlackBox( (*j)->GetOriginalBlackBoxFrom()->bbGetName() );
+       BlackBox::Pointer bbto = bbGetBlackBox( (*j)->GetOriginalBlackBoxTo()->bbGetName() );
 
-       BlackBox* bbfrom = bbGetBlackBox( (*j)->GetOriginalBlackBoxFrom()->bbGetName() );
-       BlackBox* bbto = bbGetBlackBox( (*j)->GetOriginalBlackBoxTo()->bbGetName() );
-       Connection* c = mDescriptor->GetFactory()-> 
+       Connection::Pointer c = mDescriptor.lock()->GetFactory()-> 
          NewConnection( bbfrom, 
                         (*j)->GetOriginalBlackBoxFromOutput(), 
                         bbto, 
                         (*j)->GetOriginalBlackBoxToInput() );
        
-
+       //c->Check();
        bbAddConnection(c);
 
-       bbtkDebugDecTab("Kernel",9);
       }
-    bbtkDebugDecTab("Kernel",9);
 
     bbAllocateConnectors();
-    bbtkDebugDecTab("Kernel",9);    
+    bbtkDebugMessage("object",3,
+                       "<## ComplexBlackBox::ComplexBlackBox(\""
+                       <<from.bbGetName()<<"\",\""
+                       <<name<<"\")"<<std::endl);
   }
   //=======================================================================
 
@@ -101,39 +126,26 @@ namespace bbtk
   ///  Destructor
   ComplexBlackBox::~ComplexBlackBox()
   {
-    bbtkDebugMessageInc("Kernel",1,
-                       "ComplexBlackBox::~ComplexBlackBox() ["
-                       <<bbGetFullName()<<"]"<<std::endl);
-    
-    bbtkDebugMessageInc("Kernel",1,"* Delete Connections"<<std::endl);
-    ConnectionListType::iterator j;
-    for ( j = mConnectionList.begin(); j != mConnectionList.end(); ++j ) 
-      {
-       bbtkDebugMessageInc("Kernel",1,"* Delete \""<<
-                           (*j)->GetFullName()<<"\""<<std::endl);
-       delete *j;
-       bbtkDebugDecTab("Kernel",9);
-      }
-    bbtkDebugDecTab("Kernel",9);
-
-    
-    bbtkDebugMessageInc("Kernel",1,"* Delete Black Boxes"<<std::endl);
-    BlackBoxMapType::iterator i;
-    for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i ) 
-      {
-       bbtkDebugMessageInc("Kernel",1,"* Delete \""<<i->first<<"\""<<std::endl);
-       i->second->bbDelete();
-       bbtkDebugDecTab("Kernel",1);
-      }
-    bbtkDebugDecTab("Kernel",1);
-    
-    bbtkDebugMessage("Kernel",1,"EO ComplexBlackBox::~ComplexBlackBox  ["
-                    <<bbGetFullName()<<"]"<<std::endl);
-    
+    bbtkDebugMessage("object",3,
+                    "==> ComplexBlackBox::~ComplexBlackBox() ["
+                    <<bbGetName()<<"]"<<std::endl);
+
+    bbtkDebugMessage("object",4,
+                    " -> Releasing connections"<<std::endl);
+    mConnectionList.clear();
+    bbtkDebugMessage("object",4,
+                    " -> Releasing boxes"<<std::endl);
+    mBlackBoxMap.clear();
+
+    //    Clear();
     this->bbDesallocateConnectors();
-    bbtkDebugDecTab("Kernel",1);   
+
+    bbtkDebugMessage("object",3,
+                    "<== ComplexBlackBox::~ComplexBlackBox() ["
+                    <<bbGetName()<<"]"<<std::endl);
   } 
   //=======================================================================
 
   //=========================================================================
   /// Allocates the i/o connectors of the black box
@@ -143,7 +155,7 @@ namespace bbtk
                        "ComplexBlackBox::bbAllocateConnectors() ["
                        <<bbGetFullName()<<"]"
                        <<std::endl);   
-
+    
     // Input connectors
     const BlackBoxDescriptor::InputDescriptorMapType& imap 
       = bbGetDescriptor()->GetInputDescriptorMap(); 
@@ -161,7 +173,6 @@ namespace bbtk
          ->bbGetInputConnectorMap()[ d->GetInput() ];
        
        bbGetInputConnectorMap()[i->second->GetName()] = c;
-//new BlackBoxInputConnector();
       }                                                                        
 
     // Output connectors
@@ -181,8 +192,8 @@ namespace bbtk
          ->bbGetOutputConnectorMap()[ d->GetOutput() ];
 
        bbGetOutputConnectorMap()[o->second->GetName()] = c;
-       //new BlackBoxOutputConnector();
       }
+    
     bbtkDebugDecTab("Kernel",8);  
   }
   //=========================================================================
@@ -209,18 +220,14 @@ namespace bbtk
   //=========================================================================
 
   //=======================================================================
-  BlackBox* ComplexBlackBox::bbClone(const std::string& name)
+  BlackBox::Pointer ComplexBlackBox::bbClone(const std::string& name)
   {
     bbtkDebugMessageInc("Kernel",9,
                        "ComplexBlackBox::bbClone(\""<<name<<"\") ["
                        <<bbGetFullName()<<"]"<<std::endl);
     
     ComplexBlackBox* CBB = new ComplexBlackBox(*this,name);
-    bbGetDescriptor()->Reference();           
-
-    bbtkDebugDecTab("Kernel",9);   
-
-    return CBB;
+    return MakeBlackBoxPointer(CBB);
   }
   //=======================================================================
 
@@ -229,12 +236,12 @@ namespace bbtk
   /// Executes the box so that its outputs are up-to-date on exit
   void ComplexBlackBox::bbExecute(bool force)
   {
-    bbtkDebugMessageInc("Process",1,
-                       "ComplexBlackBox::bbExecute() ["
+    bbtkDebugMessageInc("process",2,
+                       "=> ComplexBlackBox::bbExecute() ["
                        <<bbGetFullName()<<"]"<<std::endl);
 
 
-    Wx::BeginBusyCursor();
+    Wx::BusyCursor wait;
     
     if (mExecutionList.size() != 0) 
       {
@@ -244,39 +251,36 @@ namespace bbtk
             i!=mExecutionList.end();
             ++i) 
          {
-           bbtkDebugMessage("Process",2," -> Executing '"<<*i<<"'"<<std::endl);
+           bbtkDebugMessage("process",3," -> Executing '"<<*i<<"'"<<std::endl);
            mBlackBoxMap[*i]->bbExecute(force);
          }
       }
     else 
       {
-       std::map<std::string, BlackBox*>::iterator i;
+       std::map<std::string, BlackBox::Pointer>::iterator i;
        for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
          {
            i->second->bbExecute(force);
          }
       } 
 
-    Wx::EndBusyCursor();
-    
-
-    bbtkDebugDecTab("Process",1);
-    
   }
   //==================================================================
 
   //==================================================================
   void ComplexBlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
   {
-    bbtkDebugMessage("Process",3,
-                       "ComplexBlackBox::bbSetModifiedStatus("
+    bbtkDebugMessage("modified",1,
+                       "==> ComplexBlackBox::bbSetModifiedStatus("
                     <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
 
     c->GetBlackBox()->bbSetModifiedStatus(c);
 
-    bbtkDebugMessage("Process",3,
-                    "EO ComplexBlackBox::bbSetModifiedStatus("
+    /*
+    bbtkDebugMessage("modified",1,
+                    "<== ComplexBlackBox::bbSetModifiedStatus("
                     <<c<<") ["<<bbGetFullName()<<"]"<<std::endl);
+    */
   }
   //==================================================================
 
@@ -296,25 +300,27 @@ namespace bbtk
   //==================================================================
 
   //==================================================================
-  IOStatus ComplexBlackBox::bbBackwardUpdate(Connection* caller)
+  IOStatus ComplexBlackBox::bbBackwardUpdate(Connection::Pointer caller)
   {
-    bbtkDebugMessageInc("Process",1,
-                       "ComplexBlackBox::bbBackwardUpdate("
+    bbtkDebugMessageInc("process",3,
+                       "==> ComplexBlackBox::bbBackwardUpdate("
                        <<(caller?caller->GetFullName():"0")<<") ["
                        <<bbGetFullName()<<"]"<<std::endl);
-    bbtkInternalError("ComplexBlackBox::bbBackwardUpdate should never be called !");
-    /*
+    //    bbtkInternalError("ComplexBlackBox::bbBackwardUpdate should never be called !");
+    
     if (caller==0)
       {
        bbtkInternalError("ComplexBlackBox::bbBackwardUpdate called with caller=0");
       }
 
-    if (bbGetExecuting()) 
-      {
-       bbtkWarning(bbGetFullName()<<" : Cyclic execution stopped");
-       return UPTODATE;
-      }
-    bbSetExecuting(true);
+    /*
+    std::cout << "CBB BUP : "<<caller->GetBlackBoxFrom()->bbGetFullName()
+             <<"."<<caller->GetBlackBoxFromOutput()<<"----"
+             <<caller->GetOriginalBlackBoxFrom()->bbGetFullName()
+             <<"."<<caller->GetOriginalBlackBoxFromOutput()<<std::endl;
+    */
+      
+
 
     IOStatus s = UPTODATE;
     const BlackBoxDescriptor::OutputDescriptorMapType& omap 
@@ -327,9 +333,9 @@ namespace bbtk
        ComplexBlackBoxOutputDescriptor* d = 
          (ComplexBlackBoxOutputDescriptor*)i->second;
        // Get the internal box 
-       BlackBox* b = bbUnsafeGetBlackBox ( d->GetTarget() );
+       BlackBox::Pointer b = bbUnsafeGetBlackBox ( d->GetTarget() );
        // Calls BackwardUpdate on it
-       bbtkDebugMessageInc("Process",2,"Internal box connected to output : "<<d->GetTarget()<<std::endl);
+       bbtkDebugMessageInc("process",4,"Internal box connected to output : "<<d->GetTarget()<<std::endl);
        // Because internal box can also be a complex box we have to 
        // temporarily change the connection BlackBoxFromOutput to the 
        // mapped one
@@ -350,19 +356,14 @@ namespace bbtk
        // ??? STATUS OF CBBs ???
        // ??? Here it is only the final status of the boxes connected to the output 
        if (s1==MODIFIED) s=MODIFIED;
-       bbtkDebugDecTab("Process",2);
       }
     else 
       {
        bbtkError("Connection '"<<caller->GetFullName()<<"' does not point to a valid output of the complex box !");
       }
-    bbtkDebugDecTab("Process",1);
-
-    bbSetExecuting(false);
 
     return s;
-    */
-    return UPTODATE;
+    
   }
   //==================================================================
 
@@ -468,24 +469,31 @@ namespace bbtk
   /// Connects the input <name> to the connection c
   void ComplexBlackBox::bbConnectInput( const std::string& name, Connection* c)
   {
-    bbtkDebugMessageInc("Kernel",7,
-                       "ComplexBlackBox::bbConnectInput(\""
-                       <<name<<"\","<<c<<") ["
-                       <<bbGetFullName()<<"]"
-                       <<std::endl);       
-
+    bbtkDebugMessage("connection",2,
+                    "==> ComplexBlackBox::bbConnectInput(\""
+                    <<name<<"\","<<c->GetFullName()<<") ["
+                    <<bbGetFullName()<<"]"
+                    <<std::endl);       
+    
    ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)
       bbGetDescriptor()->GetInputDescriptor(name);
 
-   BlackBox* t = bbGetBlackBox(d->GetTarget());
+
+   BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
+
+   bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
+
    c->SetBlackBoxTo(t);
    c->SetBlackBoxToInput(d->GetInput());
-   t->bbConnectInput(d->GetInput(),c);
 
-   bbtkMessage("Debug",1,"ComplexBlackBox["<<bbGetFullName()<<"]::bbConnectInput : "
-              <<c->GetFullName()<<std::endl);
+   bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
+   t->bbConnectInput(d->GetInput(),c);
 
-    bbtkDebugDecTab("Kernel",7);
+   bbtkDebugMessage("connection",2,
+                    "<== ComplexBlackBox::bbConnectInput(\""
+                    <<name<<"\","<<c->GetFullName()<<") ["
+                    <<bbGetFullName()<<"]"
+                    <<std::endl);       
   }
   //=========================================================================
 
@@ -494,30 +502,36 @@ namespace bbtk
   /// Connects the output <name> to the connection c
   void ComplexBlackBox::bbConnectOutput( const std::string& name, Connection* c)
   {
-    bbtkDebugMessageInc("Kernel",7,
-                       "ComplexBlackBox::bbConnectOutput(\""
-                       <<name<<"\","<<c<<") ["
-                       <<bbGetFullName()<<"]"<<std::endl);       
+    bbtkDebugMessage("connection",2,
+                    "==> ComplexBlackBox::bbConnectOutput(\""
+                    <<name<<"\","<<c->GetFullName()<<") ["
+                    <<bbGetFullName()<<"]"<<std::endl);       
 
    ComplexBlackBoxOutputDescriptor* d = (ComplexBlackBoxOutputDescriptor*)
       bbGetDescriptor()->GetOutputDescriptor(name);
 
-   BlackBox* t = bbGetBlackBox(d->GetTarget());
+   BlackBox::Pointer t = bbGetBlackBox(d->GetTarget());
+  bbtkDebugMessage("connection",2," - Target = "<<d->GetTarget()<<" = "<<t->bbGetFullName()<<std::endl);
+
    c->SetBlackBoxFrom(t);
    c->SetBlackBoxFromOutput(d->GetOutput());
+
+   bbtkDebugMessage("connection",2," - New conn = "<<c->GetFullName()<<std::endl);
+
    t->bbConnectOutput(d->GetOutput(),c);
   
-   bbtkMessage("Debug",1,"ComplexBlackBox["<<bbGetFullName()<<"]::bbConnectOutput : "
-              <<c->GetFullName()<<std::endl);
-
-    bbtkDebugDecTab("Kernel",7);
+    bbtkDebugMessage("connection",2,
+                    "<== ComplexBlackBox::bbConnectOutput(\""
+                    <<name<<"\","<<c->GetFullName()<<") ["
+                    <<bbGetFullName()<<"]"<<std::endl);       
   }
   //=========================================================================
 
 
   //==================================================================
   /// Adds the black box to the complex box
-  void ComplexBlackBox::bbAddBlackBox( BlackBox* b)
+  void ComplexBlackBox::bbAddBlackBox( BlackBox::Pointer b)
   {
     bbtkDebugMessageInc("Kernel",7,
                        "ComplexBlackBox::AddBlackBox(\""<<b->bbGetName()
@@ -529,7 +543,7 @@ namespace bbtk
        bbtkError("a black box called \""<<b->bbGetName()
                  <<"\" already exists");
       }
-    b->bbSetParent(this);
+    b->bbSetParent(GetThisPointer<ComplexBlackBox>());
     mBlackBoxMap[b->bbGetName()] = b;
 
     bbtkDebugDecTab("Kernel",7);
@@ -538,14 +552,14 @@ namespace bbtk
 
   //==================================================================
   /// Adds the black box to the complex box (unsafe)
-  void ComplexBlackBox::bbUnsafeAddBlackBox( BlackBox* b)
+  void ComplexBlackBox::bbUnsafeAddBlackBox( BlackBox::Pointer b)
   {
     bbtkDebugMessageInc("Kernel",7,
                        "ComplexBlackBox::UnsafeAddBlackBox(\""<<b->bbGetName()
                        <<"\") ["
                        <<bbGetFullName()<<"]"<<std::endl);  
     
-    b->bbSetParent(this);
+    b->bbSetParent(GetThisPointer<ComplexBlackBox>());
     mBlackBoxMap[b->bbGetName()] = b;
 
     bbtkDebugDecTab("Kernel",7);
@@ -554,13 +568,39 @@ namespace bbtk
 
   //==================================================================
   /// Removes the black box from the complex box
-  void ComplexBlackBox::bbRemoveBlackBox( const std::string& name )
+  void ComplexBlackBox::bbRemoveBlackBox( const std::string& name, 
+                                         bool remove_connections )
   {
     bbtkDebugMessageInc("Kernel",7,
                        "ComplexBlackBox::RemoveBlackBox(\""<<name<<"\") ["
                        <<bbGetFullName()<<"]"<<std::endl);  
 
-    bbtkError("ComplexBlackBox::RemoveBlackBox not implemented");
+    BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
+    if ( i == mBlackBoxMap.end() ) 
+      {
+       bbtkError("the black box \""<<name<<"\" does not exist");
+      }
+    BlackBox::WeakPointer p = i->second;
+    
+    if (remove_connections)
+      {
+       ConnectionListType::const_iterator j;
+       for ( j = mConnectionList.begin(); 
+             j != mConnectionList.end(); ++j ) 
+         {
+           (*j)->Check();
+         }
+      }
+    if (p.use_count()!=1) 
+      {
+       bbtkError("the black box \""<<name<<"\" is still connected");
+      }
+    
+    mBlackBoxMap.erase(i);
+    
+
+    // Unload orphan dl packages 
+    Package::UnLoadReleasedDynamicallyLoadedPackages();
 
     bbtkDebugDecTab("Kernel",7);
   }
@@ -568,7 +608,7 @@ namespace bbtk
 
   //==================================================================
   /// Adds the connection to the complex box
-  void ComplexBlackBox::bbAddConnection( Connection* c)
+  void ComplexBlackBox::bbAddConnection( Connection::Pointer c)
   {
     bbtkDebugMessageInc("Kernel",7,
                        "ComplexBlackBox::AddConnection(\""<<"..."<<"\") ["
@@ -583,7 +623,7 @@ namespace bbtk
 
   //==================================================================
   /// Returns the black box with name <name>
-  BlackBox* ComplexBlackBox::bbGetBlackBox( const std::string& name )
+  BlackBox::Pointer ComplexBlackBox::bbGetBlackBox( const std::string& name )
   {
     bbtkDebugMessageInc("Kernel",9,
                        "ComplexBlackBox::GetBlackBox(\""<<name<<"\") ["
@@ -603,7 +643,7 @@ namespace bbtk
   //==================================================================
   /// Returns the black box with name <name> : does not throw an exception 
   /// if it does not exist but return a null pointer
-  BlackBox* ComplexBlackBox::bbUnsafeGetBlackBox( const std::string& name )
+  BlackBox::Pointer ComplexBlackBox::bbUnsafeGetBlackBox( const std::string& name )
   {
     bbtkDebugMessageInc("Kernel",9,
                        "ComplexBlackBox::UnsafeGetBlackBox(\""<<name<<"\") ["
@@ -614,7 +654,7 @@ namespace bbtk
     if ( i == mBlackBoxMap.end() ) 
       {
        bbtkDebugDecTab("Kernel",9);
-       return 0;
+       return BlackBox::Pointer();
       }
 
     bbtkDebugDecTab("Kernel",9);
@@ -658,9 +698,9 @@ namespace bbtk
 
 
   //=========================================================================
-  BlackBox *ComplexBlackBox::bbFindBlackBox(const std::string &blackboxname)
+  BlackBox::Pointer ComplexBlackBox::bbFindBlackBox(const std::string &blackboxname)
   {
-    BlackBox *blackbox=NULL;
+    BlackBox::Pointer blackbox;
     std::string subname="";
     std::string restname="";
     std::string delimiters(">");
@@ -679,11 +719,7 @@ namespace bbtk
     }
 
     BlackBoxMapType::iterator i = mBlackBoxMap.find(subname);
-    if ( i == mBlackBoxMap.end() ) 
-    {
-          blackbox = NULL;
-    } 
-    else 
+    if ( i != mBlackBoxMap.end() ) 
     {
        blackbox = i->second;
        if (restname!="")
@@ -737,7 +773,9 @@ namespace bbtk
     fprintf(ff,"rankdir=LR%s\n",";");
     fprintf(ff,"node [shape=record]%s\n",";");
 
-    this->bbWriteDotFileBlackBox(ff,this,detail,level,
+    this->bbWriteDotFileBlackBox(ff,
+                                GetThisPointer<ComplexBlackBox>(),
+                                detail,level,
                                 instanceOrtype,
                                 relative_link );
 
@@ -778,14 +816,14 @@ namespace bbtk
   //=========================================================================
   /// Write Graphviz-dot description in file 
   void ComplexBlackBox::bbWriteDotFileBlackBox(FILE *ff,
-                                              BlackBox *parentblackbox, 
+                                              BlackBox::Pointer parentblackbox, 
                                               int detail, int level,
                                               bool instanceOrtype,
                                               bool relative_link )   
   {
     std::string tmp1;
     std::string valueStr("");
-    Package *package = this->bbGetDescriptor()->GetPackage(); 
+    Package::Pointer package = this->bbGetDescriptor()->GetPackage(); 
 
     if (package!=NULL)
     {
@@ -809,7 +847,7 @@ namespace bbtk
     }
 
     std::string boxname="["+bbGetTypeName()+"]";
-    if (this!=parentblackbox)
+    if (GetThisPointer<ComplexBlackBox>()!=parentblackbox)
     {
        if (detail==0)
            {
@@ -916,7 +954,9 @@ namespace bbtk
       {
        if (level>-1)
          {
-           j->second->bbWriteDotFileBlackBox(ff,parentblackbox,detail,
+           j->second->bbWriteDotFileBlackBox(ff,
+                                             parentblackbox,
+                                             detail,
                                              level-1,
                                              instanceOrtype,
                                              relative_link);
@@ -943,7 +983,7 @@ namespace bbtk
                fprintf(ff,":%s",cbbid->GetName().c_str() );
              }
            fprintf(ff,"->" );
-           BlackBox *bb = bbGetBlackBox( cbbid->GetTarget() ); 
+           BlackBox::Pointer bb = bbGetBlackBox( cbbid->GetTarget() ); 
            bb->bbWriteDotInputOutputName(ff,true,detail,level);
            if (detail==1)
              {
@@ -964,7 +1004,7 @@ namespace bbtk
          {
            ComplexBlackBoxOutputDescriptor *cbbod = (ComplexBlackBoxOutputDescriptor*)yy->second;
            fprintf(ff,"  ");
-           BlackBox *bb = bbGetBlackBox( cbbod->GetTarget() ); 
+           BlackBox::Pointer bb = bbGetBlackBox( cbbod->GetTarget() ); 
            bb->bbWriteDotInputOutputName(ff,false,detail,level);
            if (detail==1)
              {
@@ -984,7 +1024,7 @@ namespace bbtk
 
 
        // Relation from the out side of this ComplexBlackBox with its Inputs
-    if (this!=parentblackbox) {
+    if (GetThisPointer<ComplexBlackBox>()!=parentblackbox) {
       for ( ii = bbGetInputConnectorMap().begin(); 
            ii != bbGetInputConnectorMap().end(); ++ii ) 
        {
@@ -992,19 +1032,19 @@ namespace bbtk
            {
              Connection* con = ii->second->GetConnection();
              if (con!=NULL){
-               BlackBox *a=con->GetBlackBoxFrom();
-               BlackBox *b=con->GetBlackBoxTo();
+               BlackBox::Pointer a=con->GetOriginalBlackBoxFrom();
+               BlackBox::Pointer b=con->GetOriginalBlackBoxTo();
                fprintf(ff,"  ");
                a->bbWriteDotInputOutputName(ff,false,detail,level);
                if (detail==1)
                  {
-                   fprintf(ff,":%s",con->GetBlackBoxFromOutput().c_str());
+                   fprintf(ff,":%s",con->GetOriginalBlackBoxFromOutput().c_str());
                  }
                fprintf(ff,"->");
                b->bbWriteDotInputOutputName(ff,true,detail,level);
                if (detail==1)
                  {
-                   fprintf(ff,":%s",con->GetBlackBoxToInput().c_str());
+                   fprintf(ff,":%s",con->GetOriginalBlackBoxToInput().c_str());
                  }
                fprintf(ff,"%s\n",";");
              } // if con
@@ -1038,7 +1078,7 @@ namespace bbtk
   //=======================================================================
   void ComplexBlackBox::Check(bool recursive)
   {
-     bbtkMessage("Debug",1,"**** Checking Complex Black Box "<<(void*)this
+     bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
                 <<" ["<<bbGetFullName()<<"]"<<std::endl);
      
      BlackBoxMapType::const_iterator i;
@@ -1052,10 +1092,79 @@ namespace bbtk
        {
         (*j)->Check();
        }
-     bbtkMessage("Debug",1,"**** Checking Complex Black Box "<<(void*)this
+     bbtkMessage("debug",1,"**** Checking Complex Black Box "<<(void*)this
                 <<" ["<<bbGetFullName()<<"] ... OK"<<std::endl);
   
   }
   //=======================================================================
 
+  //=========================================================================
+  /// Returns the name with the name of the parent prepended if any
+  std::string ComplexBlackBox::bbGetNameWithParent() const
+  {
+    if (!IsAPrototype()) return BlackBox::bbGetNameWithParent();
+    if (bbGetDescriptor()) 
+      {
+       return bbGetDescriptor()->GetFullTypeName() + ":" + bbGetName();
+      }
+    else 
+      {
+       return std::string(":") + bbGetName();
+      }
+  } 
+  //=========================================================================
+
+  //==========================================================================
+  std::string ComplexBlackBox::GetObjectName() const
+  {
+    return std::string("ComplexBlackBox '")+bbGetNameWithParent()
+      +std::string("'");
+  }
+  //==========================================================================
+  
+  //==========================================================================
+  std::string ComplexBlackBox::GetObjectInfo() const 
+  {
+    std::stringstream i;
+    i << "  - "<<mBlackBoxMap.size() << " boxes / "
+      <<mConnectionList.size() << " connections" << std::endl;
+    return i.str();
+  }
+  //==========================================================================
+
+  //==========================================================================
+  size_t  ComplexBlackBox::GetObjectSize() const 
+  {
+    size_t s = Superclass::GetObjectSize();
+    s += ComplexBlackBox::GetObjectInternalSize();
+    return s;
+  }
+  //==========================================================================
+  //==========================================================================
+  size_t  ComplexBlackBox::GetObjectInternalSize() const 
+  {
+    size_t s = sizeof(ComplexBlackBox);
+    return s;
+  }
+  //==========================================================================
+  //==========================================================================
+  size_t  ComplexBlackBox::GetObjectRecursiveSize() const 
+  {
+    size_t s = Superclass::GetObjectRecursiveSize();
+    s += ComplexBlackBox::GetObjectInternalSize();
+    BlackBoxMapType::const_iterator i;
+    for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i ) 
+      {
+       s += i->second->GetObjectRecursiveSize();
+      }
+    ConnectionListType::const_iterator j;
+    for ( j = mConnectionList.begin(); 
+         j != mConnectionList.end(); ++j ) 
+      {
+       s += (*j)->GetObjectRecursiveSize();
+      }
+    return s;
+  }
+  //==========================================================================
+
 }