]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkExecuter.cxx
BUGs INSTALL , External Packages, CPACK windows
[bbtk.git] / kernel / src / bbtkExecuter.cxx
index 891b6aa6b016ff41b83880d135726d3bc1619346..5244a04a60d26bd355ed841d66040a5ca40e03a0 100644 (file)
@@ -1,30 +1,29 @@
 /*=========================================================================
-                                                                                
+         
   Program:   bbtk
   Module:    $RCSfile: bbtkExecuter.cxx,v $ $
   Language:  C++
-  Date:      $Date: 2008/01/28 15:08:53 $
-  Version:   $Revision: 1.2 $
-                                                                                
+  Date:      $Date: 2008/03/07 11:15:15 $
+  Version:   $Revision: 1.14 $
+
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
-                                                                                
+
      This software is distributed WITHOUT ANY WARRANTY; without even
      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
      PURPOSE.  See the above copyright notices for more information.
-                                                                                
+
 =========================================================================*/
 /**
  *  \file 
  *  \brief class Executer: level 0 of script execution (code)
  */
 
-#include "bbtkExecuter.h" 
+#include "bbtkExecuter.h"
 #include "bbtkMessageManager.h"
 #include "bbtkFactory.h"
 #include "bbtkUtilities.h"
-
 #include <fstream>
 
 #ifdef _USE_WXWIDGETS_
 #include "bbtkConfigurationFile.h"
 
 namespace bbtk
-{  
+{
 /**
- *  
+ *
  */
-  Executer::Executer() 
-    : mPackage(0),
-      mRoot(0),
-      mNoExecMode(false),
-      mDialogMode(NoDialog)
+  Executer::Executer()
+    : 
+    mFactory(0),
+    mRootPackage(0),
+    mRootCBB(0),
+    mNoExecMode(false),
+    mDialogMode(NoDialog)
   {
-    //VirtualExec();
-    
-    bbtkDebugMessageInc("Core",9,"Executer::Executer()" <<std::endl);
+    bbtkDebugMessageInc("Kernel",9,"Executer::Executer()" <<std::endl);
+    mFactory = new Factory;
+    mFactory->SetExecuter(this);
     Reset();
-    bbtkDebugDecTab("Core",9);
-  } 
-  
+    bbtkDebugDecTab("Kernel",9);
+  }
+
 /**
- *  
+ *
  */
   Executer::~Executer()
   {
-     bbtkDebugMessageInc("Core",9,"Executer::~Executer()" <<std::endl);
-     if (mRoot) 
+    //std::cout << "===================================== delete Executer\n";  
+     bbtkDebugMessageInc("Kernel",9,"Executer::~Executer()" <<std::endl);
+     if (mRootCBB) 
      {
-        mPackage->UnRegisterBlackBox("workspace");
-        delete mRoot;    
+        mRootPackage->UnRegisterBlackBox("workspace");
+        delete mRootCBB;
      }
-     if (mPackage) 
+     if (mRootPackage)
      {
-        GetGlobalFactory()->UnLoadPackage("user");
+        GetFactory()->UnLoadPackage("user");
      }
-     bbtkDebugDecTab("Core",9);
+     delete mFactory;
+
+     bbtkDebugDecTab("Kernel",9);
   }
-  
+
 
 /**
- *  
+ *
  */
   void Executer::Reset()
   {
-    bbtkDebugMessageInc("Core",9,"Executer::Reset()" <<std::endl);
+    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!
@@ -85,59 +89,63 @@ namespace bbtk
     // 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 (mRoot
+    if (mRootCBB)
     {
-       mPackage->UnRegisterBlackBox(mRoot->GetTypeName());
-       delete mRoot;    
+       mRootPackage->UnRegisterBlackBox(mRootCBB->GetTypeName());
+       delete mRootCBB;
     }
-    if (mPackage) 
+    if (mRootPackage)
     {
-       GetGlobalFactory()->UnLoadPackage("user");
+       GetFactory()->UnLoadPackage("user");
     }
-    GetGlobalFactory()->Reset();
+    GetFactory()->Reset();
+
     // Create user package
-    mPackage = new Package("user","internal to bbi",
+    mRootPackage = new Package("user","internal to bbi",
                            "User defined black boxes",
                            "",
                            BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
     // Create user workspace
-    mRoot = new ComplexBlackBoxDescriptor("workspace"); //,f);
-    mRoot->AddToAuthor("bbi (internal)");
-    mRoot->AddToDescription("User's workspace");
-    mOpenDefinition.push_back(CBBDefinition(mRoot,"user"));
+    mRootCBB = new ComplexBlackBoxDescriptor("workspace"); //,f);
+    mRootCBB->SetFactory(GetFactory());
+    mRootCBB->AddToAuthor("bbi (internal)");
+    mRootCBB->AddToDescription("User's workspace");
+    mOpenDefinition.push_back(CBBDefinition(mRootCBB,"user"));
     // Register it into the user package
-    mPackage->RegisterBlackBox(mRoot);
+    mRootPackage->RegisterBlackBox(mRootCBB);
     // Insert the user package in the factory
-    InsertPackage(mPackage);
-    mOpenPackage.push_back(mPackage);
-    bbtkDebugDecTab("Core",9);
+    GetFactory()->InsertPackage(mRootPackage);
+    // And in the list of open packages
+    mOpenPackage.push_back(mRootPackage);
+
+    bbtkDebugDecTab("Kernel",9);
   }
-  
+
   /// changes the workspace name
   void Executer::SetWorkspaceName( const std::string& n )
   {
-    mPackage->ChangeBlackBoxName( mRoot->GetTypeName(), n );
+    mRootPackage->ChangeBlackBoxName( mRootCBB->GetTypeName(), n );
   }
   
   void Executer::BeginPackage (const std::string &name)
   {
-     bbtkDebugMessageInc("Core",9,"Executer::BeginPackage(\""<<name<<"\")" 
-                        <<std::endl);  
+     bbtkDebugMessageInc("Kernel",9,"Executer::BeginPackage(\""<<name<<"\")"
+                        <<std::endl);
      Package* p;
      try 
       {
-       p = GetGlobalFactory()->GetPackage(name);
+         p = GetFactory()->GetPackage(name);
       }
     catch (Exception e)
       {
-       p = new Package(name,
-                       "",
-                       "",
-                       "",
-                       BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
-       InsertPackage(p);
+         p = new Package(name,
+                         "",
+                         "",
+                         "",
+                         BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
+         GetFactory()->InsertPackage(p);
       }
-     mOpenPackage.push_back(p); 
+     mOpenPackage.push_back(p);
   }
 
   void Executer::EndPackage()
@@ -146,46 +154,53 @@ namespace bbtk
   }
 
   void Executer::Define (const std::string &name,
-                        const std::string &pack,
-                        const std::string &scriptfilename)
+                         const std::string &pack,
+                         const std::string &scriptfilename)
   {
-    bbtkDebugMessageInc("Core",9,"Executer::Define(\""<<name<<
-                       ","<<pack<<"\")" 
+    bbtkDebugMessageInc("Kernel",9,"Executer::Define(\""<<name<<
+                        ","<<pack<<"\")"
                         <<std::endl);
 
     ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
+    b->SetFactory(GetFactory());
     b->SetScriptFileName(scriptfilename);
     mOpenDefinition.push_back( CBBDefinition( b, pack ) );
     
-    bbtkDebugDecTab("Core",9);
+    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("Core",9,"Executer::EndDefine(\""
+    bbtkDebugMessageInc("Kernel",9,"Executer::EndDefine(\""
                         <<Current()->GetTypeName()<<"\")" 
                         <<std::endl);
     // Does current package exist ?
     Package* p;
-
     std::string pname(mOpenDefinition.back().package);
     if (pname.size()>0)
       {
-    try 
-      {
-       p = GetGlobalFactory()->GetPackage(pname);
-      }
-    catch (Exception e)
-      {
-       p = new Package(pname,
-                       "",
-                       "",
-                       "",
-                       BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
-       InsertPackage(p);
-      }
+       try
+         {
+           p = GetFactory()->GetPackage(pname);
+         }
+       catch (Exception e)
+         {
+           p = new Package(pname,
+                           "",
+                           "",
+                           "",
+                           BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
+           GetFactory()->InsertPackage(p);
+         }
       }
-    else 
+    else
       {
        p = mOpenPackage.back();
       }
@@ -193,40 +208,39 @@ namespace bbtk
     
     mOpenDefinition.pop_back();
   }
-
+  
 
   void Executer::Create ( const std::string& nodeType, 
                           const std::string& nodeName)
   {
      Current()->Add(nodeType,nodeName);
   }
-  
 
   /*
     void Executer::Remove (const std::string &nodeName)
-  { 
+  {
     // Current()->RemoveBlackBox(nodeName);
   }
-  */    
+  */
 
 /**
- *  
+ *
  */
-  void Executer::Connect (const std::string &nodeFrom, 
-                          const std::string &outputLabel, 
+  void Executer::Connect (const std::string &nodeFrom,
+                          const std::string &outputLabel,
                           const std::string &nodeTo, 
                           const std::string &inputLabel)
   {
-    Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel); 
+    Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
   }
-   
+
  /**
- *  
+ *
  */ 
   void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
   {
  // if in root
-     if (Current()==mRoot) 
+     if (Current()==mRootCBB
      {
         if (!mNoExecMode) 
         {
@@ -238,9 +252,9 @@ namespace bbtk
         Current()->AddToExecutionList(nodeName) ;
      }
   }
-    
+
 /**
- *  
+ *
  */
   void Executer::DefineInput ( const std::string &name,
                                const std::string &box,
@@ -248,7 +262,7 @@ namespace bbtk
                                const std::string& help)
   {
     // If the input is defined in the Root box
-    if (Current()==mRoot) 
+    if (Current()==mRootCBB
       {
       // If the dialog mode is set to NoDialog
       // and the user passed the name in the Inputs map 
@@ -272,7 +286,7 @@ namespace bbtk
            std::cin >> ans;
            Set(box,input,ans);
         }
-       #ifdef _USE_WXWIDGETS_
+#ifdef _USE_WXWIDGETS_
        // If the dialog mode is set to GraphicalDialog
        // A dialog box is pop up
        else if (mDialogMode == GraphicalDialog) 
@@ -284,17 +298,16 @@ namespace bbtk
           mess += ")";
           std::string title(name);
           title += " ?";
-    
           std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
           Set(box,input,ans); 
        }
 #endif
     }
-    
+
     Current()->DefineInput(name,box,input,help);
-        
+
   }
-  
+
  /**
  *  
  */ 
@@ -305,7 +318,7 @@ namespace bbtk
   {
     Current()->DefineOutput(name,box,output,help);
   }
-  
+
   /**
    *  
    */ 
@@ -318,16 +331,16 @@ namespace bbtk
 
     if ( b->bbGetInputType(input) !=  typeid(std::string) ) 
       {
-         BlackBox* a = /*mFactory->*/
-         NewAdaptor(typeid(std::string),
-                    b->bbGetInputType(input),
-                    "tmp");
+         BlackBox* a =
+          GetFactory()->NewAdaptor(typeid(std::string),
+                                   b->bbGetInputType(input),
+                                   "tmp");
          if (!a) 
-         {
-            bbtkError("No <"<<
-                      TypeName(b->bbGetInputType(input))
-                      <<"> to <std::string> found");
-         }
+          {
+            bbtkError("No <"<<
+                      TypeName(b->bbGetInputType(input))
+                      <<"> to <std::string> found");
+          }
          std::string v(value);
          a->bbSetInput("In",v);
          a->bbExecute();
@@ -341,10 +354,9 @@ namespace bbtk
       }
   }
 
-  
   /**
-   *  
-   */ 
+   *
+   */
   std::string Executer::Get(const std::string &box,
                             const std::string &output)
   {
@@ -352,65 +364,67 @@ namespace bbtk
     // Looks for the adaptor
     if (b->bbGetOutputType(output) != typeid(std::string)) 
       {
-      BlackBox* a = /*mFactory->*/
-          NewAdaptor(
-             b->bbGetOutputType(output),
-             typeid(std::string),
-             "tmp");
-      if (!a) 
-        {
-        bbtkError("No <"<<
-                   TypeName(b->bbGetOutputType(output))
-                   <<"> to <std::string> found");
-        }
+       BlackBox* a =
+          GetFactory()->NewAdaptor(
+                                  b->bbGetOutputType(output),
+                                  typeid(std::string),
+                                  "tmp");
+       if (!a) 
+         {
+           bbtkError("No <"<<
+                     TypeName(b->bbGetOutputType(output))
+                     <<"> to <std::string> found");
+         }
         b->bbExecute();
-
+       
         a->bbSetInput("In",b->bbGetOutput(output));
         a->bbExecute();
         std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
-       //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
-       //   std::cout << a->bbGetOutput("Out").unsafe_get<std::string>() 
-       //             << std::endl;
-       //std::string v(value);
-       //b->bbSetInput(input,a->bbGetOutput("Out"));
+       //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
+       //   std::cout << a->bbGetOutput("Out").unsafe_get<std::string>() 
+       //             << std::endl;
+       //std::string v(value);
+       //b->bbSetInput(input,a->bbGetOutput("Out"));
         a->bbDelete();
         return r;
       }
-    else 
+    else
       {
-       b->bbExecute();
-       return b->bbGetOutput(output).unsafe_get<std::string>();
-       // std::string v = *((std::string*)b->bbGetOutput(output)) ;
-       // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>() 
-       //   << std::endl;
-       // b->bbSetInput(input,&v);
+       b->bbExecute();
+       return b->bbGetOutput(output).unsafe_get<std::string>();
+       // std::string v = *((std::string*)b->bbGetOutput(output)) ;
+       // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>() 
+       //   << std::endl;
+       // b->bbSetInput(input,&v);
       }
   }
 
 
   void Executer::Author(const std::string &authorName)
   {
-    Current()->AddToAuthor(authorName,Current()==mRoot);
+    Current()->AddToAuthor(authorName,Current()==mRootCBB);
   }
 
-
+  void Executer::Category(const std::string &category)
+  {
+    Current()->AddToCategory(category,Current()==mRootCBB);
+  }
 
   void Executer::Description(const std::string &d)
   {
-     Current()->AddToDescription(d,Current()==mRoot);
+    Current()->AddToDescription(d,Current()==mRootCBB);
   }
 
 
   /// prints the list of the boxes of the current descriptor
   void Executer::PrintBoxes()
-  {  
+  {
     bbtkMessageInc("Help",1,"The black box descriptor \""
                    <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
     Current()->PrintBlackBoxes();
     bbtkDecTab("Help",1);
  }
 
-
   std::string Executer::ShowGraph(const std::string &nameblackbox, 
                                  const std::string &detailStr, 
                                  const std::string &levelStr,
@@ -419,7 +433,6 @@ namespace bbtk
                                  const std::string &custom_title,
                                  bool system_display )
   {
-
     int detail =       atoi(detailStr.c_str());
     int level  =       atoi(levelStr.c_str());
 
@@ -431,80 +444,88 @@ namespace bbtk
     // No output provided : automatic generation
     if (output_html.length() == 0)
       {
-       // Don't pollute the file store with  "doc_tmp" directories ...    
-       std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
+       // Don't pollute the file store with  "temp_dir" directories ...    
+       std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
        
        char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
        
        std::string directory = default_doc_dir; 
        if (c != '/' && c !='\\') directory = directory + "/";
-       directory = directory +  "doc_tmp";    
+       directory = directory +  "temp_dir";    
        
        filename_rootHtml = directory + "/" + "User.html";
        simplefilename_rootHtml = "User.html" ;
 
        // Creating directory
        std::string command0("mkdir \"" +directory + "\"");
-       system( command0.c_str() );      
+       system( command0.c_str() );
 
        relative_link = false;
       }
 
+    Package* p;
+    try
+    {
+       p = GetFactory()->GetPackage(nameblackbox);
+    }
+    catch (Exception e)
+    {
+       p = mRootPackage;
+    }
     // Generating documentation-help of workspace
-    mPackage->SetDocURL(filename_rootHtml);
-    mPackage->SetDocRelativeURL(simplefilename_rootHtml);
+    p->SetDocURL(filename_rootHtml);
+    p->SetDocRelativeURL(simplefilename_rootHtml);
+
+    p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
 
-    mPackage->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
-    
     std::string page = filename_rootHtml;
     /*
     try 
-      {
-       ShowGraphTypes(nameblackbox);
-      }
-    catch (bbtk::Exception a) 
-      {
-       std::cout <<"EXC"<<std::endl;
-       page = ShowGraphInstances(nameblackbox,detail,level,system_display);
-      }
+    {
+       ShowGraphTypes(nameblackbox);
+    }
+    catch (bbtk::Exception a)
+    {
+       std::cout <<"EXC"<<std::endl;
+       page = ShowGraphInstances(nameblackbox,detail,level,system_display);
+    }
     */
     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)
+                                           bool system_display)
   {
 
     BlackBox* blackbox=NULL;
     if (nameblackbox==".")
-      {
-       blackbox=Current()->GetPrototype();
-      } 
-    else 
-      {
-       blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
-      }
+    {
+       blackbox=Current()->GetPrototype();
+    }
+    else
+    {
+       blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
+    }
     
     std::string page;
 
     if (blackbox)
       {      
-       // Don't pollute the file store with  "doc_tmp" directories ...    
-       std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
+       // Don't pollute the file store with  "temp_dir" directories ...
+       std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
        char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
-       
+
        std::string directory = default_doc_dir; 
        if (c != '/' && c !='\\') directory = directory + "/";
 
-       directory = directory +  "doc_tmp"; 
-       
-       //std::string directory("doc_tmp");
+       directory = directory +  "temp_dir";
+       //std::string directory("temp_dir");
        std::string filename(directory + "/" + "bbtk_graph_pipeline");
        std::string filename_html(filename+".html");
        std::string command0("mkdir \""+directory + "\"");
 
-#if defined(_WIN32)  
+#if defined(_WIN32)
        std::string command2("start ");
 #else 
        std::string command2("gnome-open ");
@@ -527,7 +548,7 @@ namespace bbtk
            } 
 
            blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
-           s << "</body></html>\n";      
+           s << "</body></html>\n";
          }
        s.close();
        
@@ -538,11 +559,9 @@ namespace bbtk
       {
        bbtkMessageInc("Help",1,"No black box: \""
                       <<nameblackbox<<"\" " <<std::endl);
-       
       }
     return page;
   }
-  
 
 void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
   {
@@ -572,12 +591,12 @@ void Executer::ShowRelations(const std::string &nameblackbox, const std::string
 
   /*
   /// sets the level of message
-  void Executer::Message(const std::string &category, 
+  void Executer::Message(const std::string &kind,
                          const std::string& level)
   {
     int l;
     sscanf(level.c_str(),"%d",&l);
-    bbtk::MessageManager::SetMessageLevel(category,l);
+    bbtk::MessageManager::SetMessageLevel(kind,l);
   }
   */