]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkBlackBox.cxx
* Major changes on IOStatus update / propagation
[bbtk.git] / kernel / src / bbtkBlackBox.cxx
index f8ad055c388726fdc01ea57d9618ccbff060c30b..b7a7ec78f4ee6d3d71be5de580d38085f24954c3 100644 (file)
@@ -2,8 +2,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkBlackBox.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/12/03 13:35:22 $
-  Version:   $Revision: 1.31 $
+  Date:      $Date: 2008/12/08 12:53:45 $
+  Version:   $Revision: 1.32 $
 =========================================================================*/
 
 /* ---------------------------------------------------------------------
@@ -36,6 +36,7 @@
 #include "bbtkPackage.h"
 #include "bbtkMessageManager.h"
 #include "bbtkFactory.h"
+#include "bbtkBlackBoxOutputConnector.h"
 
 #include "bbtkConfigurationFile.h"
 #include "bbtkWxBlackBox.h"
@@ -102,7 +103,7 @@ namespace bbtk
   //=========================================================================
   BlackBox::BlackBox(const std::string &name) 
     : 
-    bbmStatus(MODIFIED), 
+    //    bbmStatus(MODIFIED), 
     bbmExecuting(false),
     bbmName(name),
     bbmBoxProcessMode("Pipeline"),
@@ -123,7 +124,7 @@ namespace bbtk
   //=========================================================================
   BlackBox::BlackBox(BlackBox& from, const std::string &name) 
     :
-      bbmStatus(from.bbmStatus), 
+    //    bbmStatus(from.bbmStatus), 
       bbmExecuting(false),
       bbmName(name), 
       bbmBoxProcessMode(from.bbmBoxProcessMode),
@@ -178,7 +179,7 @@ namespace bbtk
     BBTK_BUSY_CURSOR;
 
     // If force is true then update is triggered even if the box is UPTODATE
-    if (force) bbSetModifiedStatus();
+    //    if (force) bbSetModifiedStatus();
 
     // Calls the main recursive update method 
     bbBackwardUpdate(Connection::Pointer());
@@ -439,8 +440,181 @@ namespace bbtk
   }
   //=========================================================================
 
+
+
+
+  //=========================================================================
+  void BlackBox::AddChangeObserver(const std::string& output_name, 
+                                  OutputChangeCallbackType f)
+  {
+  }  
+  //=========================================================================
+
+  //=========================================================================
+  void BlackBox::RemoveChangeObserver(const std::string& output_name, 
+                                     OutputChangeCallbackType f)
+  {
+  }
+  //=========================================================================
+
+
+  /*
+  //=========================================================================
+  ///  Sets the ChangeTime of input 
+  void BlackBox::bbSetInputChangeTime(BlackBoxInputConnector* c, 
+                                     const ChangeTime& t)
+  {
+    bbtkDebugMessage("change",1,
+                    "==> BlackBox::bbSetInputChangeTime("<<c<<","<<t<<") ["
+                    <<bbGetFullName()<<"]"<<std::endl);
+    
+    // If new time is greater than old one
+    if ( c->SetChangeTime(t) ) 
+      {
+       bool was_up_to_date = bbIsUpToDate();
+       // If new time is greater than the old max time of inputs
+       if ( mMaxInputChangeTime.Set(t) ) 
+         {
+           // If the box turned out-of-date
+           if ( was_up_to_date && bbIsOutOfDate() )
+             {
+               // 
+               if ( ( bbBoxProcessModeIsReactive()  ||
+                      (c==bbGetInputConnectorMap().find("BoxExecute")->second))
+                    && (bbCanReact() ) )
+                 {
+                   bbtkDebugMessage("change",2,
+                                    "an input of "
+                                    <<bbGetFullName()
+                                    <<" changed and box is in Reactive mode or BoxExecute input changed : adding it to the global execution list"
+                                    <<std::endl);
+                   bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
+                 }
+               // Have to propagate the modification to aval boxes
+               OutputConnectorMapType::iterator i;
+               for (i = bbGetOutputConnectorMap().begin();
+                    i != bbGetOutputConnectorMap().end();
+                    ++i)
+                 {
+                   i->second->SetChangeTime(t);
+                 }
+               // update the MinOutputChangeTime
+               mMinOutputChangeTime.Set(t);
+             }
+         }
+      }
+  }
+  //=========================================================================
+
+ //=========================================================================
+  ///  Sets the ChangeTime of output 
+  void BlackBox::bbSetOutputChangeTime(BlackBoxOutputConnector* c, 
+                                      const ChangeTime& t)
+  {
+    bbtkDebugMessage("change",1,
+                    "==> BlackBox::bbSetOutputChangeTime("<<c<<","<<t<<") ["
+                    <<bbGetFullName()<<"]"<<std::endl);
+    
+    //ChangeTime old = 
+    c->SetChangeTime(t);
+    // c->GetChangeTime() = t;
+    //    bbUpdateMinOutputChangeTime(t);
+    // propagate
+    
+  }
+  //=========================================================================
+  */
+
+  /*
+  //=========================================================================
+  void BlackBox::bbUpdateMaxInputChangeTime(const ChangeTime& t)
+  {    
+
+    
+    if ( t > mMaxInputChangeTime ) 
+      {
+       mMaxInputChangeTime = t;
+       if ( mMinOutputChangeTime > mMaxInputChangeTime )
+         {
+           
+         }
+      }
+    
+  }
+  //=========================================================================
+
+  //=========================================================================
+  void bbUpdateMinOutputChangeTime(const ChangeTime& t)
+  {
+    ChangeTime old = mMinOutputChangeTime;
+    mMinOutputChangeTime = MAXLONG;
+    OutputConnectorMapType::iterator i;
+    for (i = bbGetOutputConnectorMap.begin();
+        i != bbGetOutputConnectorMap.end();
+        ++i)
+      {
+       if (i->second->GetChangeTime() < mMinOutputChangeTime)
+         mMinOutputChangeTime = i->second->GetChangeTime();
+      }
+    if ( mMinOutputChangeTime < old )
+      {
+      }
+   
+  }
+  //=========================================================================
+  */
+
+  //=========================================================================
+  /// Signals that the BlackBox has been modified through 
+  /// the input connector c
+  /// and propagates it downward
+  /// ** NOT USER INTENDED **
+  void BlackBox::bbSetStatusAndPropagate(BlackBoxInputConnector* c,
+                                        IOStatus s)
+  {
+    if (s==UPTODATE) bbtkError("bbSetStatusAndPropagate with status UPTODATE!");
+    c->SetStatus(s);
+    OutputConnectorMapType::const_iterator o;  
+    for ( o = bbGetOutputConnectorMap().begin(); 
+         o != bbGetOutputConnectorMap().end(); ++o )                   
+      {                                                                        
+       //      bbSetStatusAndPropagate(o->second, OUTOFDATE);
+       if (o->second->GetStatus()==UPTODATE) 
+         {
+           o->second->SetStatus(OUTOFDATE);
+           o->second->SignalChange(GetThisPointer<BlackBox>(),o->first); 
+         }
+      }                                                                        
+    
+    if ( ( bbBoxProcessModeIsReactive()  ||
+          (c==bbGetInputConnectorMap().find("BoxExecute")->second))
+        && (bbCanReact() ) )
+      {
+       bbtkDebugMessage("modified",2,
+                        "-> Execution triggered by Reactive mode or BoxExecute input change"<<std::endl);
+        bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
+      }    
+  }
   //=========================================================================
-  ///  Signals that the BlackBox has been modified
+
+  //=========================================================================
+  void BlackBox::bbSetStatusAndPropagate(BlackBoxOutputConnector* c,
+                                        IOStatus s)
+  {
+    bbtkError("bbSetStatusAndPropagate(BlackBoxOutputConnector* c,IOStatus s)");
+    /*
+    if (i->GetStatus()==UPTODATE) 
+      {
+       i->second->SetStatus(s);
+       i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
+      }
+    */
+  }
+  //=========================================================================
+
+ ///  Signals that the BlackBox has been modified
+  /*
   void BlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c)
   {
     bbtkDebugMessage("modified",1,
@@ -477,13 +651,13 @@ namespace bbtk
        this->bbSetStatus(MODIFIED); 
         bbGlobalAddToExecutionList( GetThisPointer<BlackBox>() );
       }
-    /*
-    else if ( bbGetStatus() == MODIFIED ) //! this->bbIsUptodate()) 
-      { 
-       bbtkDebugMessage("modified",2,"-> Already modified"<<std::endl);
-       return;
-      }
-    */
+    
+    //else if ( bbGetStatus() == MODIFIED ) //! this->bbIsUptodate()) 
+     // { 
+//     bbtkDebugMessage("modified",2,"-> Already modified"<<std::endl);
+//     return;
//     }
+   
     else 
       {
        bbtkDebugMessage("modified",2,"-> Status set to modified"<<std::endl);
@@ -492,12 +666,11 @@ namespace bbtk
  
     this->bbSignalOutputModification(false);
 
-    /* 
   bbtkDebugMessageDec("process",5,
                        "<= BlackBox::bbSetModifiedStatus("<<c<<") ["
                        <<bbGetFullName()<<"]"<<std::endl);
-    */
   }  
+*/
   //=========================================================================
 
   //=========================================================================  
@@ -508,20 +681,16 @@ namespace bbtk
                        <<reaction<<") ["
                        <<bbGetFullName()<<"]"<<std::endl);
     
-    OutputConnectorMapType::iterator change = bbGetOutputConnectorMap().end();
     OutputConnectorMapType::iterator i;
     for ( i  = bbGetOutputConnectorMap().begin(); 
-         i != bbGetOutputConnectorMap().end(); ++i) {
-      /*     if ( i->first == "BoxChange" ) 
-       {
-         change = i;
-         continue;
-       }
-      */
-      i->second->SetModifiedStatus();
-    } 
-    //    if (change != bbGetOutputConnectorMap().end()) 
-    // change->second->SetModifiedStatus();
+         i != bbGetOutputConnectorMap().end(); ++i) 
+      {
+       if (i->second->GetStatus()==UPTODATE) 
+         {
+           //      i->second->SetStatus(MODIFIED);
+           i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
+         }
+      } 
 
     if (reaction) bbGlobalProcessExecutionList();
 
@@ -540,28 +709,37 @@ namespace bbtk
                        <<output<<","<<reaction<<") ["
                        <<bbGetFullName()<<"]"<<std::endl);
     
-    OutputConnectorMapType::iterator i = bbGetOutputConnectorMap().find(output);
+    OutputConnectorMapType::iterator i = 
+      bbGetOutputConnectorMap().find(output);
+
+
     if ( i == bbGetOutputConnectorMap().end() ) 
        {
          bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<output<<") : unknown output");
        }
-    i->second->SetModifiedStatus();
-    // Has to notify the output "BoxChange" also
-    if (output != "BoxChange") 
+
+    if (i->second->GetStatus()==UPTODATE) 
       {
-       i = bbGetOutputConnectorMap().find("BoxChange");
-       if ( i != bbGetOutputConnectorMap().end() ) 
+       //      i->second->SetStatus(MODIFIED);
+       i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
+       // Has to notify the output "BoxChange" also
+       if (output != "BoxChange") 
          {
-           i->second->SetModifiedStatus();
+           i = bbGetOutputConnectorMap().find("BoxChange");
+           if ( i != bbGetOutputConnectorMap().end() ) 
+             {
+               //              i->second->SetStatus(MODIFIED);
+               i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
+             }
          }
+       if (reaction) bbGlobalProcessExecutionList();
       }
-  if (reaction) bbGlobalProcessExecutionList();
-
-  bbtkDebugMessageDec("process",5,
-                      "<= BlackBox::bbSignalOutputModification("
-                      <<output<<") ["
-                      <<bbGetFullName()<<"]"<<std::endl);
 
+    bbtkDebugMessageDec("process",5,
+                       "<= BlackBox::bbSignalOutputModification("
+                       <<output<<") ["
+                       <<bbGetFullName()<<"]"<<std::endl);
+    
   }  
   //=========================================================================   
   //=========================================================================  
@@ -573,6 +751,7 @@ namespace bbtk
                        <<bbGetFullName()<<"]"<<std::endl);
     OutputConnectorMapType::iterator i;
     std::vector<std::string>::const_iterator o;
+    bool changed = false;
     for (o=output.begin();o!=output.end();++o) 
       {
        // the output "BoxChange" must be signaled **AFTER** all others
@@ -583,18 +762,29 @@ namespace bbtk
          {
            bbtkError("BlackBox["<<bbGetFullName()<<"]::bbSignalOutputModification("<<*o<<") : unknown output");
          }
-       i->second->SetModifiedStatus();
+       // Already OUTOFDATE : noting to do
+       if (i->second->GetStatus()==UPTODATE)
+         {
+           //  i->second->SetStatus(MODIFIED);
+           i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
+           changed = true;
+         }
       }
     // Has to notify the output "BoxChange" also
     i = bbGetOutputConnectorMap().find("BoxChange");
-    if ( i != bbGetOutputConnectorMap().end() 
+    if ( changed && (i != bbGetOutputConnectorMap().end())
       {
-       i->second->SetModifiedStatus();
+       // Already OUTOFDATE : noting to do
+       if (i->second->GetStatus()==UPTODATE) 
+         {
+           //  i->second->SetStatus(MODIFIED);
+           i->second->SignalChange(GetThisPointer<BlackBox>(),i->first); 
+           if (reaction) bbGlobalProcessExecutionList();
+         }
       }
-  if (reaction) bbGlobalProcessExecutionList();
 
-   bbtkDebugMessageDec("process",5,
-                      "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
+    bbtkDebugMessageDec("process",5,
+                       "<= BlackBox::bbSignalOutputModification(vector of outputs) ["
                        <<bbGetFullName()<<"]"<<std::endl);
 
   }  
@@ -602,9 +792,9 @@ namespace bbtk
 
   //=========================================================================
   /// Updates the BlackBox inputs
-  /// \returns UPTODATE if all inputs are in UPTODATE status after update
-  ///          else MODIFIED 
-  IOStatus BlackBox::bbUpdateInputs(bool excludeParent)
+  /// Calls BackwardUpdate on all BlackBoxInputConnector
+  /// \returns The maximum of final IOStatus after each input update
+  IOStatus BlackBox::bbUpdateInputs()
   {
     bbtkDebugMessageInc("process",4,
                        "=> BlackBox::bbUpdateInputs() ["
@@ -617,28 +807,54 @@ namespace bbtk
     for ( i = bbGetInputConnectorMap().begin(); 
          i!= bbGetInputConnectorMap().end(); ++i) 
       {
-       if (excludeParent && (i->first=="WinParent")) continue;
-       if (i->first=="WinHide") continue;
+       //      if (i->first=="WinHide") continue;
        // If input type is Void : no recurse
        //if (  bbGetDescriptor()->GetInputDescriptor(i->first)->GetTypeInfo() 
        //      == typeid(Void) ) 
        //  continue;
-
-       IOStatus t = i->second->BackwardUpdate();
-       if (t==MODIFIED) s = MODIFIED;
+       i->second->BackwardUpdate();
+       IOStatus t = i->second->GetStatus();
+       if (t > s) s = t;
       }
     
-   bbtkDebugMessageDec("process",4,
+    bbtkDebugMessageDec("process",4,
                        "<= BlackBox::bbUpdateInputs() ["
                        <<bbGetFullName()<<"]"
                        <<std::endl);   
-
-
+    
+    
     return s;
   }
   //=========================================================================
 
+  //==================================================================
+  /// Computes the final IOStatus of inputs and outputs after processing
+  void BlackBox::bbComputePostProcessStatus()
+  {
+    IOStatus new_output_status = UPTODATE;
+    if (bbBoxProcessModeIsAlways()) new_output_status = OUTOFDATE;
+
+    // Update the input statuses
+    InputConnectorMapType::iterator i;
+    for ( i = bbGetInputConnectorMap().begin(); 
+         i!= bbGetInputConnectorMap().end(); ++i) 
+      {
+       IOStatus t = i->second->GetStatus();
+       if (t == OUTOFDATE) new_output_status = OUTOFDATE;
+       // A previously MODIFIED status turns to UPTODATE
+       if (t==MODIFIED) i->second->SetStatus(UPTODATE);
+      }
+
+    // Update the output statuses
+    OutputConnectorMapType::iterator o;
+    for ( o = bbGetOutputConnectorMap().begin(); 
+         o!= bbGetOutputConnectorMap().end(); ++o) 
+      {
+       o->second->SetStatus(new_output_status);
+      }
+  }
+  //==================================================================
+
   //=========================================================================
   /// Connects the input <name> to the connection c
   void BlackBox::bbConnectInput( const std::string& name, Connection* c)
@@ -656,13 +872,19 @@ namespace bbtk
        bbtkError("no input called '"<<name<<"'");
       }
     i->second->SetConnection(c);
+
+    // Check the status of the from.output of c 
+    // to set the new status of the input
+    IOStatus s = MODIFIED;
+    if ( c->GetBlackBoxFrom()->bbGetOutputConnector(c->GetBlackBoxFromOutput()).GetStatus() == OUTOFDATE ) 
+      s = OUTOFDATE;
+    bbSetStatusAndPropagate(i->second,s);
     
     bbtkDebugMessage("connection",2,
                        "<== BlackBox::bbConnectInput(\""
                        <<name<<"\","<<c->GetFullName()<<") ["
                        <<bbGetFullName()<<"]"
                        <<std::endl);       
-    //  bbSetModifiedStatus();
 
   }
   //=========================================================================
@@ -869,11 +1091,11 @@ namespace bbtk
     std::string ss("<");
     std::string::size_type pos = 0;
     pos = s.find(ss,0);
-    char* cr = "[";
+    std::string cr("[");
     while ( pos != std::string::npos )
       {
        //      std::cout << "*** find one "<<std::endl;
-       s.replace(pos,1,cr,1);
+       s.replace(pos,1,cr.c_str(),1);
        pos = s.find(ss, pos);
       } 
     ss = ">";
@@ -883,7 +1105,7 @@ namespace bbtk
     while ( pos != std::string::npos )
       {
        //      std::cout << "*** find one "<<std::endl;
-       s.replace(pos,1,cr,1);
+       s.replace(pos,1,cr.c_str(),1);
        pos = s.find(ss, pos);
       } 
     ss = ",";
@@ -893,7 +1115,7 @@ namespace bbtk
     while ( pos != std::string::npos )
       {
        //      std::cout << "*** find one "<<std::endl;
-       s.replace(pos,1,cr,1);
+       s.replace(pos,1,cr.c_str(),1);
        pos = s.find(ss, pos);
       }     //    std::cout << "AFTER=["<<s<<"]"<<std::endl;
   }
@@ -1033,12 +1255,25 @@ namespace bbtk
       {
        bbtkMessage("Help",1,"Black Box <::"<<this->bbGetDescriptor()->GetTypeName()<<">"<<std::endl);
       }
+    /*
+    if (bbIsUpToDate())
+      {
+       bbtkMessage("Help",1,"Up-to-date ["<<mMaxInputChangeTime<<","
+                   <<mMinOutputChangeTime<<"]"<<std::endl);
+      }
+    else 
+      {
+       bbtkMessage("Help",1,"Out-of-date ["<<mMaxInputChangeTime<<","
+                   <<mMinOutputChangeTime<<"]"<<std::endl);
+      }
+    */
     //    bbtkMessage("Help",1," "<<GetDescription()<<std::endl);
     //    bbtkMessage("Help",1," By : "<<GetAuthor()<<std::endl);
 
     std::vector<std::string> iname;
     std::vector<std::string> ivalue;
     std::vector<std::string> iconn;
+    std::vector<std::string> istatus;
 
     InputConnectorMapType::iterator i;
     unsigned int namelmax = 0;
@@ -1058,11 +1293,13 @@ namespace bbtk
          s += con->GetOriginalBlackBoxFromOutput();
        }  // if con
        iconn.push_back(s);
+       istatus.push_back(GetIOStatusString(i->second->GetStatus()));
       }
     OutputConnectorMapType::iterator o;
     std::vector<std::string> oname;
     std::vector<std::string> ovalue;
     std::vector<std::vector<std::string> > oconn;
+    std::vector<std::string> ostatus;
     for ( o = mOutputConnectorMap.begin(); o != mOutputConnectorMap.end(); ++o ) 
       {
        oname.push_back(o->first);
@@ -1082,6 +1319,7 @@ namespace bbtk
            ss.push_back(s);
        }  // if con
        oconn.push_back(ss);
+       ostatus.push_back(GetIOStatusString(o->second->GetStatus()));
       }
 
     if (iname.size()) 
@@ -1089,10 +1327,10 @@ namespace bbtk
     else 
       bbtkMessage("Help",1," * No inputs"<<std::endl);
 
-    std::vector<std::string>::iterator i1,i2,i3;
-    for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin();
-        i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end();
-        ++i1,++i2,++i3)
+    std::vector<std::string>::iterator i1,i2,i3,i4;
+    for (i1=iname.begin(),i2=ivalue.begin(),i3=iconn.begin(),i4=istatus.begin();
+        i1!=iname.end(),i2!=ivalue.end(),i3!=iconn.end(),i4!=istatus.end();
+        ++i1,++i2,++i3,++i4)
       {
        std::string name(*i1);
        name += "'";
@@ -1101,9 +1339,11 @@ namespace bbtk
        value += "'";
        value.append(1+valuelmax-value.size(),' ');
        if (i3->size()) 
-         bbtkMessage("Help",1,"    '"<<name<<" = '"<<value<<" <-- '"<<*i3<<"'"<<std::endl);
+         bbtkMessage("Help",1,"    '"<<name<<" = '"<<value<<" <-- '"
+                     <<*i3<<"'");
        else 
-         bbtkMessage("Help",1,"    '"<<name<<" = '"<<value<<std::endl);
+         bbtkMessage("Help",1,"    '"<<name<<" = '"<<value);
+       bbtkMessage("Help",1," ["<<*i4<<"]"<<std::endl);
       }
 
     if (oname.size()) 
@@ -1111,11 +1351,11 @@ namespace bbtk
     else 
       bbtkMessage("Help",1," * No outputs"<<std::endl);
 
-    std::vector<std::vector<std::string> >::iterator i4;
+    std::vector<std::vector<std::string> >::iterator i5;
 
-    for (i1=oname.begin(),i2=ovalue.begin(),i4=oconn.begin();
-        i1!=oname.end(),i2!=ovalue.end(),i4!=oconn.end();
-        ++i1,++i2,++i4)
+    for (i1=oname.begin(),i2=ovalue.begin(),i5=oconn.begin(),i4=ostatus.begin();
+        i1!=oname.end(),i2!=ovalue.end(),i5!=oconn.end(),i4!=ostatus.end();
+        ++i1,++i2,++i4,++i5)
       {
        std::string name(*i1);
        name += "'";
@@ -1123,17 +1363,18 @@ namespace bbtk
        std::string value(*i2);
        value += "'";
        value.append(1+valuelmax-value.size(),' ');
-       if (!(*i4).size())
-         bbtkMessage("Help",1,"    '"<<name<<" = '"<<value<<std::endl);
+       if (!(*i5).size())
+         bbtkMessage("Help",1,"    '"<<name<<" = '"<<value);
        else 
          {
            std::string pref = "    '"+name+" = '"+value;
-           for (i3=i4->begin();i3!=i4->end();++i3)
+           for (i3=i5->begin();i3!=i5->end();++i3)
              {
-               bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'"<<std::endl);
+               bbtkMessage("Help",1,pref<<" --> '"<<*i3<<"'");
                pref.replace(0,pref.size(),pref.size(),' ');
              }
          }
+       bbtkMessage("Help",1," ["<<*i4<<"]"<<std::endl);
       }
 
    }