]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkExecuter.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkExecuter.cxx
index 241573ade7462e73776fdfb48f29080613897eff..ee60d2e992e98ea035c9615581be167c7e39777b 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkExecuter.cxx,v $ $
   Language:  C++
-  Date:      $Date: 2008/03/07 08:40:14 $
-  Version:   $Revision: 1.13 $
+  Date:      $Date: 2008/04/22 06:59:31 $
+  Version:   $Revision: 1.19 $
 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 
 namespace bbtk
 {
-/**
- *
- */
+  //=======================================================================
+  Executer::Pointer Executer::New()
+  {
+    bbtkDebugMessage("Kernel",9,"Executer::New()"<<std::endl);
+    return MakePointer(new Executer());
+  }
+  //=======================================================================
+
+  //=======================================================================
   Executer::Executer()
     : 
-    mFactory(0),
-    mRootPackage(0),
-    mRootCBB(0),
+    mFactory(),
+    mRootPackage(),
+    mRootCBB(),
     mNoExecMode(false),
     mDialogMode(NoDialog)
   {
     bbtkDebugMessageInc("Kernel",9,"Executer::Executer()" <<std::endl);
-    mFactory = new Factory;
-    mFactory->SetExecuter(this);
+    mFactory = Factory::New();
+    // The smart pointer on this is not made yet (is made by New) 
+    // -> create it to pass it to the factory
+    // We have to "lock" the smart pointer because the factory
+    // only keeps a weak pointer on the executer
+    // -> this would auto-destroy !!
+    mFactory->SetExecuter(MakePointer(this,true));
     Reset();
     bbtkDebugDecTab("Kernel",9);
   }
+  //=======================================================================
 
-/**
- *
- */
+  //=======================================================================
   Executer::~Executer()
   {
-    //std::cout << "===================================== delete Executer\n";  
-     bbtkDebugMessageInc("Kernel",9,"Executer::~Executer()" <<std::endl);
-     if (mRootCBB) 
-     {
-        mRootPackage->UnRegisterBlackBox("workspace");
-        delete mRootCBB;
-     }
-     if (mRootPackage)
-     {
-        GetFactory()->UnLoadPackage("user");
-     }
-     delete mFactory;
-
+     bbtkDebugMessageInc("Kernel",9,"==> Executer::~Executer()" <<std::endl);
+     mOpenDefinition.clear();
+     mOpenPackage.clear();
+     mFactory->Reset();
+     mFactory.reset();
      bbtkDebugDecTab("Kernel",9);
   }
+  //=======================================================================
+
+  //=======================================================================
+   /// Loads a package
+    void Executer::LoadPackage(const std::string &name )
+   {
+     GetFactory()->LoadPackage(name);
+   }
+  //=======================================================================
+
+  //=======================================================================
+    /// Unloads a package
+    void Executer::UnLoadPackage(const std::string &name )
+    {
+      GetFactory()->UnLoadPackage(name);
+    }
+  //=======================================================================
 
-
-/**
- *
- */
+  //=======================================================================
   void Executer::Reset()
   {
     bbtkDebugMessageInc("Kernel",9,"Executer::Reset()" <<std::endl);
 
-    // The 'user' package must be closed before all other 
-    // because box destructors must not be unloaded when bb are deleted!
-    // Similarly, the 'workspace' CBB must be destroyed before 
-    // all user defined CBB otherwise any instance 
-    // of a user CBB that is in the 'workspace' would try to 
-    // access a user CBB descriptor which has been previously freed
-    if (mRootCBB)
-    {
-       mRootPackage->UnRegisterBlackBox(mRootCBB->GetTypeName());
-       delete mRootCBB;
-    }
-    if (mRootPackage)
-    {
-       GetFactory()->UnLoadPackage("user");
-    }
+    GetFactory()->CheckPackages();
+    mOpenDefinition.clear();
+    mOpenPackage.clear();
     GetFactory()->Reset();
 
     // Create user package
-    mRootPackage = new Package("user","internal to bbi",
-                           "User defined black boxes",
-                           "",
-                           BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
-    // Create user workspace
-    mRootCBB = new ComplexBlackBoxDescriptor("workspace"); //,f);
-    mRootCBB->AddToAuthor("bbi (internal)");
-    mRootCBB->AddToDescription("User's workspace");
-    mOpenDefinition.push_back(CBBDefinition(mRootCBB,"user"));
-    // Register it into the user package
-    mRootPackage->RegisterBlackBox(mRootCBB);
+    Package::Pointer p =
+      Package::New("user","internal to bbi",
+                  "User defined black boxes",
+                  "",
+                  BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
     // Insert the user package in the factory
-    GetFactory()->InsertPackage(mRootPackage);
+    GetFactory()->InsertPackage(p);
     // And in the list of open packages
-    mOpenPackage.push_back(mRootPackage);
+    mOpenPackage.push_back(p);
+    mRootPackage = p;
+
+    // Create user workspace
+    ComplexBlackBoxDescriptor::Pointer r = 
+      ComplexBlackBoxDescriptor::New("workspace"); 
+    //    mRootCBB->Reference();
+    r->SetFactory(GetFactory());
+    r->AddToAuthor("bbi (internal)");
+    r->AddToDescription("User's workspace");
+    mOpenDefinition.push_back(CBBDefinition(r,"user"));
+    // Register it into the user package
+    p->RegisterBlackBox(r);
+    mRootCBB = r;
+
+    //    Object::PrintObjectListInfo();
+    //  GetFactory()->CheckPackages();
 
     bbtkDebugDecTab("Kernel",9);
   }
+  //=======================================================================
 
+  //=======================================================================
   /// changes the workspace name
   void Executer::SetWorkspaceName( const std::string& n )
   {
-    mRootPackage->ChangeBlackBoxName( mRootCBB->GetTypeName(), n );
+    GetUserPackage()->ChangeBlackBoxName( GetWorkspace()->GetTypeName(), n );
   }
-  
+  //=======================================================================
+
+  //=======================================================================
   void Executer::BeginPackage (const std::string &name)
   {
      bbtkDebugMessageInc("Kernel",9,"Executer::BeginPackage(\""<<name<<"\")"
                         <<std::endl);
-     Package* p;
+     Package::Pointer p;
      try 
       {
          p = GetFactory()->GetPackage(name);
       }
     catch (Exception e)
       {
-         p = new Package(name,
+       p = Package::New(name,
                          "",
                          "",
                          "",
                          BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
-         GetFactory()->InsertPackage(p);
+       GetFactory()->InsertPackage(p);
       }
      mOpenPackage.push_back(p);
   }
+  //=======================================================================
 
+  //=======================================================================
   void Executer::EndPackage()
   {
     if (mOpenPackage.size()>1) mOpenPackage.pop_back();
   }
+  //=======================================================================
 
+  //=======================================================================
   void Executer::Define (const std::string &name,
                          const std::string &pack,
                          const std::string &scriptfilename)
@@ -160,27 +183,33 @@ namespace bbtk
                         ","<<pack<<"\")"
                         <<std::endl);
 
-    ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
+    ComplexBlackBoxDescriptor::Pointer b 
+      = ComplexBlackBoxDescriptor::New(name);
+    b->SetFactory(GetFactory());
     b->SetScriptFileName(scriptfilename);
     mOpenDefinition.push_back( CBBDefinition( b, pack ) );
     
     bbtkDebugDecTab("Kernel",9);
   }
+  //=======================================================================
 
+  //=======================================================================
   /// Sets the file name to use for the current definition
   /// (Used to set it after the Define command)
   void Executer::SetCurrentFileName (const std::string &name )
   {
     mOpenDefinition.back().box->SetScriptFileName(name);
   }
+  //=======================================================================
 
+  //=======================================================================
   void Executer::EndDefine ()
   {
     bbtkDebugMessageInc("Kernel",9,"Executer::EndDefine(\""
                         <<Current()->GetTypeName()<<"\")" 
                         <<std::endl);
     // Does current package exist ?
-    Package* p;
+    Package::Pointer p;
     std::string pname(mOpenDefinition.back().package);
     if (pname.size()>0)
       {
@@ -190,40 +219,72 @@ namespace bbtk
          }
        catch (Exception e)
          {
-           p = new Package(pname,
-                           "",
-                           "",
-                           "",
-                           BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
+           p = Package::New(pname,
+                            "",
+                            "",
+                            "",
+                            BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
            GetFactory()->InsertPackage(p);
          }
       }
     else
       {
-       p = mOpenPackage.back();
+       p = mOpenPackage.back().lock();
       }
     p->RegisterBlackBox(Current());
     
     mOpenDefinition.pop_back();
   }
-  
+  //======================================================================= 
 
+  //=======================================================================  
+  void Executer::Kind(const std::string& kind)
+  {
+    if (kind=="ADAPTOR")
+      {
+       Current()->AddToCategory("adaptor");
+       Current()->SetKind(bbtk::BlackBoxDescriptor::ADAPTOR);
+      }
+    else if (kind=="DEFAULT_ADAPTOR")
+      {
+       Current()->AddToCategory("adaptor");
+       Current()->SetKind(bbtk::BlackBoxDescriptor::DEFAULT_ADAPTOR);
+      }
+    if (kind=="GUI")
+      {
+       Current()->AddToCategory("gui");
+       Current()->SetKind(bbtk::BlackBoxDescriptor::GUI);
+      }
+    else if (kind=="DEFAULT_GUI")
+      {
+       Current()->AddToCategory("gui");
+       Current()->SetKind(bbtk::BlackBoxDescriptor::DEFAULT_GUI);
+      }
+    else
+      {
+       bbtkError("Unknown box kind : '"<<kind<<"'. "
+                 <<"Valid kinds are 'ADAPTOR','DEFAULT_ADAPTOR',"
+                 <<"'GUI','DEFAULT_GUI'");
+      }
+  }
+  //=======================================================================
+
+  //=======================================================================
   void Executer::Create ( const std::string& nodeType, 
                           const std::string& nodeName)
   {
      Current()->Add(nodeType,nodeName);
   }
+  //=======================================================================
 
-  /*
-    void Executer::Remove (const std::string &nodeName)
+  //=======================================================================
+  void Executer::Destroy(const std::string &boxName)
   {
-    // Current()->RemoveBlackBox(nodeName);
+    Current()->Remove(boxName,true);
   }
-  */
+  //=======================================================================
 
-/**
- *
- */
+  //=======================================================================
   void Executer::Connect (const std::string &nodeFrom,
                           const std::string &outputLabel,
                           const std::string &nodeTo, 
@@ -231,14 +292,13 @@ namespace bbtk
   {
     Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
   }
+  //=======================================================================
 
- /**
- *
- */ 
-  void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
+  //=======================================================================
+  void Executer::Execute (const std::string &nodeName) 
   {
- // if in root
-     if (Current()==mRootCBB
   // if in root
+    if (Current()==GetWorkspace()
      {
         if (!mNoExecMode) 
         {
@@ -250,17 +310,16 @@ namespace bbtk
         Current()->AddToExecutionList(nodeName) ;
      }
   }
+  //=======================================================================
 
-/**
- *
- */
+  //=======================================================================
   void Executer::DefineInput ( const std::string &name,
                                const std::string &box,
                                const std::string &input,
                                const std::string& help)
   {
     // If the input is defined in the Root box
-    if (Current()==mRootCBB
+    if (Current()==GetWorkspace()
       {
       // If the dialog mode is set to NoDialog
       // and the user passed the name in the Inputs map 
@@ -305,10 +364,9 @@ namespace bbtk
     Current()->DefineInput(name,box,input,help);
 
   }
+  //=======================================================================
 
- /**
- *  
- */ 
+  //=======================================================================
    void Executer::DefineOutput ( const std::string &name,
                                  const std::string &box,
                                  const std::string &output,
@@ -316,20 +374,19 @@ namespace bbtk
   {
     Current()->DefineOutput(name,box,output,help);
   }
+  //=======================================================================
 
-  /**
-   *  
-   */ 
+  //=======================================================================
   void Executer::Set (const std::string &box,
                       const std::string &input,
                       const std::string &value)
   {
-    BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
+    BlackBox::Pointer b = Current()->GetPrototype()->bbGetBlackBox(box);
     // Looks for the adaptor
 
     if ( b->bbGetInputType(input) !=  typeid(std::string) ) 
       {
-         BlackBox* a =
+       BlackBox::Pointer a =
           GetFactory()->NewAdaptor(typeid(std::string),
                                    b->bbGetInputType(input),
                                    "tmp");
@@ -343,7 +400,7 @@ namespace bbtk
          a->bbSetInput("In",v);
          a->bbExecute();
          b->bbSetInput(input,a->bbGetOutput("Out"));
-         a->bbDelete();
+        //         a->Delete();
       }
     else 
       {
@@ -351,18 +408,17 @@ namespace bbtk
       b->bbSetInput(input,v);
       }
   }
+  //=======================================================================
 
-  /**
-   *
-   */
+  //=======================================================================
   std::string Executer::Get(const std::string &box,
                             const std::string &output)
   {
-    BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
+    BlackBox::Pointer b = Current()->GetPrototype()->bbGetBlackBox(box);
     // Looks for the adaptor
     if (b->bbGetOutputType(output) != typeid(std::string)) 
       {
-       BlackBox* a =
+       BlackBox::Pointer a =
           GetFactory()->NewAdaptor(
                                   b->bbGetOutputType(output),
                                   typeid(std::string),
@@ -383,7 +439,7 @@ namespace bbtk
        //             << std::endl;
        //std::string v(value);
        //b->bbSetInput(input,a->bbGetOutput("Out"));
-        a->bbDelete();
+       //        a->bbDelete();
         return r;
       }
     else
@@ -396,24 +452,30 @@ namespace bbtk
        // b->bbSetInput(input,&v);
       }
   }
+  //=======================================================================
 
-
+  //=======================================================================
   void Executer::Author(const std::string &authorName)
   {
-    Current()->AddToAuthor(authorName,Current()==mRootCBB);
+    Current()->AddToAuthor(authorName,Current()==GetWorkspace());
   }
+  //=======================================================================
 
+  //=======================================================================
   void Executer::Category(const std::string &category)
   {
-    Current()->AddToCategory(category,Current()==mRootCBB);
+    Current()->AddToCategory(category,Current()==GetWorkspace());
   }
+  //=======================================================================
 
+  //=======================================================================
   void Executer::Description(const std::string &d)
   {
-    Current()->AddToDescription(d,Current()==mRootCBB);
+    Current()->AddToDescription(d,Current()==GetWorkspace());
   }
+  //=======================================================================
 
-
+  //=======================================================================
   /// prints the list of the boxes of the current descriptor
   void Executer::PrintBoxes()
   {
@@ -422,7 +484,9 @@ namespace bbtk
     Current()->PrintBlackBoxes();
     bbtkDecTab("Help",1);
  }
+  //=======================================================================
 
+  //=======================================================================
   std::string Executer::ShowGraph(const std::string &nameblackbox, 
                                  const std::string &detailStr, 
                                  const std::string &levelStr,
@@ -461,14 +525,14 @@ namespace bbtk
        relative_link = false;
       }
 
-    Package* p;
+    Package::Pointer p;
     try
     {
        p = GetFactory()->GetPackage(nameblackbox);
     }
     catch (Exception e)
     {
-       p = mRootPackage;
+      p = GetUserPackage();
     }
     // Generating documentation-help of workspace
     p->SetDocURL(filename_rootHtml);
@@ -490,16 +554,18 @@ namespace bbtk
     */
     return page;
   }
+  //=======================================================================
 
+  //=======================================================================
   /// Generate a png file with the actual pipeline (Graphviz-dot needed)
   std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
                                            bool system_display)
   {
 
-    BlackBox* blackbox=NULL;
+    BlackBox::Pointer blackbox;
     if (nameblackbox==".")
     {
-       blackbox=Current()->GetPrototype();
+       blackbox = Current()->GetPrototype();
     }
     else
     {
@@ -560,43 +626,165 @@ namespace bbtk
       }
     return page;
   }
+  //=======================================================================
 
-void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
+  //=======================================================================
+  void Executer::ShowRelations(const std::string &nameblackbox, 
+                              const std::string &detailStr, 
+                              const std::string &levelStr)
   {
-       bool found=false;
+    bool found=false;
+    
+    int detail = atoi(detailStr.c_str());
+    int level  = atoi(levelStr.c_str());
+    BlackBox::Pointer blackbox;
+    if (nameblackbox.compare(".")==0)
+      {
+       blackbox=Current()->GetPrototype();
+      } 
+    else 
+      {
+       blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
+      }
+    
+    if (blackbox)
+      {
+       found=true;
+       blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
+      }
+    
+    if (!found) 
+      {
+       bbtkError("Blackbox Name not found.. <"  <<nameblackbox<<">");
+      }
+  }
+  //=======================================================================
 
-       int detail = atoi(detailStr.c_str());
-       int level  = atoi(levelStr.c_str());
-       BlackBox* blackbox=NULL;
-       if (nameblackbox.compare(".")==0)
-       {
-          blackbox=Current()->GetPrototype();
-       } else {
-          blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
-       }
+  //=======================================================================
+  /// sets the level of message
+  void Executer::SetMessageLevel(const std::string &kind,
+                                int level)
+  {
+    bbtk::MessageManager::SetMessageLevel(kind,level);
+  }
+  //=======================================================================
+
+  //=======================================================================
+  /// Prints help on the messages
+  void  Executer::HelpMessages()
+  {
+    bbtk::MessageManager::PrintInfo();
+  }
+  //=======================================================================
+
+  //=======================================================================
+  ///
+  void Executer::Print(const std::string &str)
+  {  
+    if (GetNoExecMode() &&  (Current()==GetWorkspace()) ) return;
+    if (Current()!=GetWorkspace()) return;
+
+    bbtkDebugMessageInc("Interpreter",9,"Interpreter::Print(\""<<str<<"\")"<<std::endl);
+
+ // TO DO :
+ // InterpretLine ("load std")
+ // InterpretLine("new ConcatStrings _C_ ") -> trouver un nom unique : # commande 
+ // InterpretLine("new Print _P_") 
+ // InterpretLine("connect _C_.Out _P_.In")
+ // int num = 1
 
-       if (blackbox)
+    std::vector<std::string> chains;
+    std::string delimiters("$");
+
+    // Skip delimiters at beginning.
+    std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
+    bool is_text = true;
+    if (lastPos>0) is_text = false;
+
+    // Find first delimiter.
+    std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
+
+    while (std::string::npos != pos || std::string::npos != lastPos)
+    {
+       if (is_text) 
        {
-          found=true;
-          blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
+          // Found a text token, add it to the vector.
+          chains.push_back(str.substr(lastPos, pos - lastPos));
+ // std::string token = str.substr(lastPos, pos - lastPos)
+ // InterpretLine("set _C_.In%num% %token%")
        }
-
-       if (!found) 
+       else 
        {
-          bbtkError("Blackbox Name not found.. <"  <<nameblackbox<<">");
+
+       // is an output (between $$) : decode 
+         std::string tok,box,output;
+         tok = str.substr(lastPos, pos - lastPos);
+         Utilities::SplitAroundFirstDot(tok,box,output);
+         chains.push_back( Get(box,output) );
+
+// InterpretLine("connect %tok% _C_.In%num%") 
+
        }
+        // Skip delimiters.  Note the "not_of"
+       lastPos = str.find_first_not_of(delimiters, pos);
+        // Find next delimiter
+       pos = str.find_first_of(delimiters, lastPos);
+    //
+       is_text = !is_text;
+// num ++;
+     }
+// InterpretLine("exec _P_")
+// if (IS_IN_WORKSPACE) InterpretLine("delete _C_; delete _P_");
+
+    std::vector<std::string>::iterator i;
+    for (i= chains.begin(); i!=chains.end(); ++i) 
+      {
+       //  bbtkMessage("Echo",1,*i);
+       Utilities::SubsBackslashN(*i);
+       bbtkMessage("Output",1,*i);
+      }
+    bbtkMessage("Output",1,std::endl);
   }
+  //==========================================================================
 
-  /*
-  /// sets the level of message
-  void Executer::Message(const std::string &kind,
-                         const std::string& level)
+  //==========================================================================
+  std::string Executer::GetObjectName() const
   {
-    int l;
-    sscanf(level.c_str(),"%d",&l);
-    bbtk::MessageManager::SetMessageLevel(kind,l);
+    return std::string("Executer");
   }
-  */
-
-
+  //==========================================================================
+  
+  //==========================================================================
+  std::string  Executer::GetObjectInfo() const 
+  {
+    std::stringstream i;
+    return i.str();
+  }
+  //==========================================================================
+  //==========================================================================
+size_t  Executer::GetObjectSize() const 
+{
+  size_t s = Superclass::GetObjectSize();
+  s += Executer::GetObjectInternalSize();
+  return s;
+  }
+  //==========================================================================
+  //==========================================================================
+size_t  Executer::GetObjectInternalSize() const 
+{
+  size_t s = sizeof(Executer);
+  return s;
+  }
+  //==========================================================================
+  //==========================================================================
+  size_t  Executer::GetObjectRecursiveSize() const 
+  {
+    size_t s = Superclass::GetObjectRecursiveSize();
+    s += Executer::GetObjectInternalSize();
+    s += mFactory->GetObjectRecursiveSize();
+    return s;
+  }
+  //==========================================================================
 }//namespace