]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkInterpreter.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkInterpreter.cxx
index d112dba1115b4a16ee23b9b0e056b90aa26b0b42..c925050a17fd99e20bf257351e8fcd94a2d11f9f 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkInterpreter.cxx,v $ $
   Language:  C++
-  Date:      $Date: 2008/03/26 14:47:36 $
-  Version:   $Revision: 1.57 $
+  Date:      $Date: 2008/04/08 06:59:30 $
+  Version:   $Revision: 1.58 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -26,6 +26,7 @@
 #include "bbtkMessageManager.h"
 #include "bbtkConfigurationFile.h"
 #include "bbtkUtilities.h"
+#include "bbtkWxBlackBox.h"
 #include <sys/stat.h>
 #include <algorithm>
 #ifdef CMAKE_HAVE_TERMIOS_H
@@ -73,215 +74,231 @@ bufferNb =0;
     // Builds the commands dict
     CommandInfoType info;
    
-    info.category = "new";
+    info.keyword = "new";
     info.argmin = 2;
     info.argmax = 2;
     info.code = cNew;
     info.syntax = "new <type> <name>";
     info.help = "Creates a new black box of type <type> with name <name>";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
     
-    info.category = "delete";
+    info.keyword = "delete";
     info.argmin = 1;
     info.argmax = 1;
     info.code = cDelete;
     info.syntax = "delete <box>";
     info.help = "Deletes the black box of name <box>";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "connect";
+    info.keyword = "newgui";
+    info.argmin = 2;
+    info.argmax = 2;
+    info.code = cNewGUI;
+    info.syntax = "newgui <box> <name>";
+    info.help = "Automatically creates a graphical user interface with name <name> for the black box <box> and connects it to the box inputs";
+    mCommandDict[info.keyword] = info;
+
+    info.keyword = "connect";
     info.argmin = 2;
     info.argmax = 2;
     info.code = cConnect;
     info.syntax = "connect <box1.output> <box2.input>";
     info.help = "Connects the ouput <output> of black box <box1> to the input <input> of black box <box2>";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "print";
+    info.keyword = "print";
     info.argmin = 1;
     info.argmax = 1;
     info.code = cPrint;
     info.syntax = "print <string>";
     info.help = "Prints the string. Substitutes any token of the form '$box.output$' by the string adaptation of the output of the box (requires the right adaptor). No carriage return is issued at the end, use '\\n' to add carriage returns. The level of 'Echo' messages must be greater than 1 (see the command 'message').";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "exec";
+    info.keyword = "exec";
     info.argmin = 1;
     info.argmax = 2;
     info.code = cExec;
     info.syntax = "exec <box | 'freeze' | 'unfreeze' >";
-    info.help = "Executes the black box of name <box> (and connected boxes if needed). If the special category 'freeze' is given then freezes any further execution command. 'unfreeze' reverts to normal execution mode.";
-    mCommandDict[info.category] = info;
+    info.help = "Executes the black box of name <box> (and connected boxes if needed). If the special keyword 'freeze' is given then freezes any further execution command. 'unfreeze' reverts to normal execution mode.";
+    mCommandDict[info.keyword] = info;
 
-    info.category = "package";
+    info.keyword = "package";
     info.argmin = 1;
     info.argmax = 1;
     info.code = cPackage;
     info.syntax = "package <name>";
     info.help = "Begins the definition of a package.";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
     
-    info.category = "endpackage";
+    info.keyword = "endpackage";
     info.argmin = 0;
     info.argmax = 0;
     info.code = cEndPackage;
     info.syntax = "endpackage";
     info.help = "Ends the definition of a package.";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "define";
+    info.keyword = "define";
     info.argmin = 1;
     info.argmax = 2;
     info.code = cDefine;
     info.syntax = "define <type> [<package>]";
     info.help = "Begins the definition of a new type of complex black box called <type>. If <package> if provided will create it in the given package.";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
     
-    info.category = "endefine";
+    info.keyword = "endefine";
     info.argmin = 0;
     info.argmax = 0;
     info.code = cEndDefine;
     info.syntax = "endefine";
     info.help = "Ends the definition of a new type of complex black box";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "input";
+    info.keyword = "kind";
+    info.argmin = 1;
+    info.argmax = 1;
+    info.code = cKind;
+    info.syntax = "kind <ADAPTOR|DEFAULT_ADAPTOR|WIDGET_ADAPTOR|DEFAULT_WIDGET_ADAPTOR>";
+    info.help = "Sets the kind of the currently defined complex black box";
+    mCommandDict[info.keyword] = info;
+
+    info.keyword = "input";
     info.argmin = 3;
     info.argmax = 3;
     info.code = cInput;
     info.syntax = "input <name> <box.input> <help>";
     info.help = "Defines the input <name> of the current working black box as being an alias for the input <input> of the black box <box>. <help> defines the help string for the newly created input";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "output";
+    info.keyword = "output";
     info.argmin = 3;
     info.argmax = 3;
     info.code = cOutput;
     info.syntax = "output <name> <box.output> <help>";
     info.help = "Defines the output <name> of the current working black box as being an alias for the output <output> of the black box <box>. <help> defines the help string for the newly created output";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "set";
+    info.keyword = "set";
     info.argmin = 2;
     info.argmax = 2;
     info.code = cSet;
     info.syntax = "set <box.input> <value>";
     info.help = "Sets the value of the input <input> of the black box <box> to <value>. There must exist a string to the value type adaptor";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
    
-    info.category = "config";  // JPR
+    info.keyword = "config";  // JPR
     info.argmin = 0;
     info.argmax = 0;
     info.code = cConfig;
     info.syntax = "config";
     info.help = "Prints the value of all configuration parameters";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "index";  // LG
+    info.keyword = "index";  // LG
     info.argmin = 0;
     info.argmax = 2;
     info.code = cIndex;
 
     info.syntax = "index [<filename> ['Initials'(default)|'Packages'|'Categories'|'Adaptors']]";
     info.help = "Creates an html index of known boxes. If filename is provided then save it to the file 'filename'. The default index entries are the initial letters of the names of the boxes. If 'Packages' or 'Categories' is provided then the entries are either the packages names or the categories. If 'Adaptors' is provided then an alphabetical index of all adaptors is created.";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "reset";  
+    info.keyword = "reset";  
     info.argmin = 0;
     info.argmax = 0;
     info.code = cReset;
     info.syntax = "reset";
     info.help = "Deletes all boxes and unloads all packages (bbi is reset to its start state)";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "author";
+    info.keyword = "author";
     info.argmin = 1;
     info.argmax = 1;
     info.code = cAuthor;
     info.syntax = "author <string>";
     info.help = "Adds the string <string> to the author information of the black box being defined";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
     
-    info.category = "category"; //JP
+    info.keyword = "category"; //JP
     info.argmin = 1;
     info.argmax = 1;
     info.code = cCategory;
     info.syntax = "category <list of items, separated by ;>";
     info.help = "Adds the string <string> to the category information of the black box being defined";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "description";
+    info.keyword = "description";
     info.argmin = 1;
     info.argmax = 1;
     info.code = cDescription;
     info.syntax = "description <string>";
     info.help = "Adds the string <string> to the descriptive information of the black box being defined";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "help";
+    info.keyword = "help";
     info.argmin = 0;
     info.argmax = 2;
     info.code = cHelp;
     info.syntax = "help";
     info.syntax = "\n         (1) help \n         (2) help <command name> \n         (3) help packages [all]\n         (4) help <package name> [all]\n         (5) help <black box type> \n         (6) help <black box name>";
     info.help = "Effect :\n         (1) Lists all available commands;\n         (2) Prints help on a particular command; \n         (3) Lists the packages loaded and their black boxes.\n             Add 'all' to list adaptors; \n         (4) Prints short help on the black boxes of a package.\n             Add 'all' to include adaptors; \n         (5) Prints full help on a black box type; \n         (6) Prints information on the inputs, outputs and connections of a black box instance.";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "message";
+    info.keyword = "message";
     info.argmin = 0;
     info.argmax = 2;
     info.code = cMessage;
     info.syntax = "message <kind> <level>";
-    info.help = "Sets the level of the kind of messages <kind> to <level>.\n  If kind='All' then sets the level for all kinds. If no kind nor level is passed then prints info on available kinds of messages and their current level.";  mCommandDict[info.category] = info;
+    info.help = "Sets the level of the kind of messages <kind> to <level>.\n  If kind='All' then sets the level for all kinds. If no kind nor level is passed then prints info on available kinds of messages and their current level.";  mCommandDict[info.keyword] = info;
 
-    info.category = "include";
+    info.keyword = "include";
     info.argmin = 1;
     info.argmax = 2;
     info.code = cInclude;
     info.syntax = "include <filename> [source]";
     info.help = "Includes the file <filename>.\n  'source' : If the keyword 'source' is provided then informs bbi that the included file is the source of the current box definition (Advanced; used to get the right 'Include' field in html doc of packages 'appli' scripts).";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "quit";
+    info.keyword = "quit";
     info.argmin = 0;
     info.argmax = 0;
     info.code = cQuit;
     info.syntax = "quit";
     info.help = "Quits the program (during script execution it stops the complete execution)";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "load";
+    info.keyword = "load";
     info.argmin = 1;
     info.argmax = 1;
     info.code = cLoad;
     info.syntax = "load <packagename>";
     info.help = "Loads the black box package <packagename>";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "unload";
+    info.keyword = "unload";
     info.argmin = 1;
     info.argmax = 1;
     info.code = cUnload;
     info.syntax = "unload <packagename>";
     info.help = "Unloads the black box package <packagename>";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
-    info.category = "graph";
+    info.keyword = "graph";
     info.argmin = 0;
     info.argmax = 6;
     info.code = cGraph;
     info.syntax = "graph [ BlackBoxName [ Detail 0..1 [ Level 0..99999 [ Output html file [ Custom header [ Custom title ]]]]]] \n         graph [ BlackBoxNameType [ Detail 0..1 [ Level 0..99999 [ Output html file [ Custom header [ Custom title ]]]]]]";
     info.help = "Shows a graphical view of a bbtk pipeline.\n- BlackBoxName : name of the box to view. Default '.' : current box.\n- BlackBoxNameType : name of the type of box to view, ex : 'workspace')";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
 
     /*
-    info.category = "workspace";
+    info.keyword = "workspace";
     info.argmin = 1;
     info.argmax = 2;
     info.code = cWorkspace;
     info.syntax = "workspace < ( freeze | unfreeze ) | ( rename <newname> ) >";
     info.help = "Configures the workspace.\n        'freeze' allow to block execution commands while keeping definition commands active. 'unfreeze' turns back the worspace in 'normal' mode.\n      'rename' allow to set a new name to the workspace.";
-    mCommandDict[info.category] = info;
+    mCommandDict[info.keyword] = info;
     */
 
     bbtkDebugDecTab("Interpreter",9);
@@ -652,7 +669,7 @@ void Interpreter::InterpretLine( const std::string& line, bool &insideComment )
     InterpretCommand(words,command);
 
     bbtkDebugMessage("Interpreter",9,
-                     "Command='"<<command.category
+                     "Command='"<<command.keyword
                       <<"' code="<<command.code<<std::endl); 
     int level=0;
     std::string left,right,left2,right2;
@@ -700,6 +717,10 @@ void Interpreter::InterpretLine( const std::string& line, bool &insideComment )
         mExecuter->EndDefine();
         break;
 
+      case cKind :
+        mExecuter->Kind(words[1]);
+        break;
+
       case cPrint :
         mExecuter->Print(words[1]);
         break;
@@ -732,6 +753,10 @@ void Interpreter::InterpretLine( const std::string& line, bool &insideComment )
         mExecuter->Author(words[1]);
         break;
 
+      case cNewGUI :
+        NewGUI(words[1],words[2]);
+        break;
+
       case cCategory :
         mExecuter->Category(words[1]);
         break;
@@ -1328,7 +1353,7 @@ void Interpreter::SwitchToStream( std::stringstream* stream )
   {
     bbtkDebugMessageInc("Interpreter",9,"Interpreter::InterpretCommand(...)"<<std::endl);
 
-    // searches the command category
+    // searches the command keyword
     CommandDictType::iterator c;
     c = mCommandDict.find(words[0]);
     if ( c == mCommandDict.end() ) {
@@ -1850,6 +1875,127 @@ void  Interpreter::Index(const std::string& filename,
 }
 //=======================================================================
 
+
+//=======================================================================
+void  Interpreter::NewGUI(const std::string& boxname,
+                            const std::string& instanceName)
+{
+  std::string typeName = instanceName+"Type";
+  std::stringstream* s = new std::stringstream;
+  // create the complex box
+  (*s) << "define "<<typeName<<std::endl;
+  //  (*s) << "  description 'Automatically generated user interface for the box "
+  //       << boxname << "'" <<std::endl;
+  // create the Layout box
+  (*s) << "  load wx"<<std::endl;
+  (*s) << "  new LayoutLine layout"<<std::endl;
+  // create the output 'Widget'
+  (*s) << "  output Widget layout.Widget Widget"<<std::endl;
+  // the box change output 
+  (*s) << "  new MultipleInputs change"<<std::endl;
+  (*s) << "  output BoxChange change.Out BoxChange"<<std::endl;
+
+  // Browse the inputs of the box in order to find which ones are not 
+  // connected and can be adapted from a widget adaptor
+  // vector which stores the list of inputs of the box which must be connected
+  std::vector<std::string> in;
+  Factory* F = mExecuter->GetFactory();
+  if (F==0)
+    {
+      delete s;
+      bbtkError("Interpreter::CreateGUI : could not access the executer factory");
+    }
+  Package* user = F->GetPackage("user");
+  ComplexBlackBoxDescriptor* workspace 
+    = (ComplexBlackBoxDescriptor*)user->GetBlackBoxMap().find("workspace")->second;
+  
+  BlackBox* box = workspace->GetPrototype()->bbGetBlackBox(boxname);
+  //  BlackBox::InputConnectorMapType incm = box->bbGetInputConnectorMap();
+  int nb = 0;
+  BlackBox::InputConnectorMapType::iterator i;
+  for (i=box->bbGetInputConnectorMap().begin();
+       i!=box->bbGetInputConnectorMap().end();
+       ++i)
+    {
+      // If the input is connected : continue
+      if (i->second->IsConnected()) continue;
+      // Get the input descriptor 
+      const BlackBoxInputDescriptor* d = box->bbGetDescriptor()->GetInputDescriptor(i->first);
+      // If it is a "system" input : skip it
+      if ( ( d->GetCreatorTypeInfo() == typeid(AtomicBlackBoxDescriptor)) ||
+          ( d->GetCreatorTypeInfo() == typeid(WxBlackBoxDescriptor)) )
+       continue;
+      // try to find a widget adaptor
+      std::string adaptor;
+      if (F->FindWidgetAdaptor(DataInfo(typeid(Void),""),
+                              d->GetDataInfo(),
+                              adaptor))
+       {
+         // store the input name
+         in.push_back(i->first);
+         // command to create the adaptor
+         (*s) << "  new "<<adaptor<<" "<<i->first<<std::endl;
+         // command to create the output
+         (*s) << "  output "<<i->first<<" "
+              <<i->first<<".Out "<<i->first<<std::endl;
+           //         <<" Output of the widget which allows to set "
+           //<i->first<<"'"<<std::endl;
+         // Sets the label of the widget adaptor to the name of the input
+         (*s) << "  set "<<i->first<<".Label "<<i->first<<std::endl;
+         (*s) << "  connect "<<i->first<<".Widget layout.Widget"<<in.size()<<std::endl;
+         (*s) << "  connect "<<i->first<<".BoxChange change.In"<<in.size()<<std::endl;
+       }
+      // try to find an adaptor from string 
+      // If found then can create a text input which 
+      // will be automatically adapted 
+      else if (F->FindAdaptor(DataInfo(typeid(std::string),""),
+                              d->GetDataInfo(),
+                              adaptor))
+       {
+         // store the input name
+         in.push_back(i->first);
+         // command to create the adaptor
+         (*s) << "  new InputText "<<i->first<<std::endl;
+         // command to create the output
+         (*s) << "  output "<<i->first<<" "
+              <<i->first<<".Out "<<i->first<<std::endl;
+           //         <<" Output of the widget which allows to set "
+           //<i->first<<"'"<<std::endl;
+         // Sets the label of the widget adaptor to the name of the input
+         (*s) << "  set "<<i->first<<".Title "<<i->first<<std::endl;
+         (*s) << "  connect "<<i->first<<".Widget layout.Widget"<<in.size()<<std::endl;
+         (*s) << "  connect "<<i->first<<".BoxChange change.In"<<in.size()<<std::endl;
+       }
+
+    }   
+  // Inputs for window properties
+  (*s) << "  input WinTitle layout.WinTitle Title"<<std::endl;
+  (*s) << "  input WinWidth layout.WinWidth Width"<<std::endl;
+  (*s) << "  input WinHeight layout.WinHeight Height"<<std::endl;
+  (*s) << "  input WinDialog layout.WinDialog Dialog"<<std::endl;
+  (*s) << "  input WinHide layout.WinHide Hide"<<std::endl;
+
+  
+  
+  // Execute the box executes the layout
+  (*s) << "  exec layout" << std::endl;
+  (*s) << "endefine" << std::endl;
+  // (*s) << "help "<< typeName<< std::endl;
+  // instanciate the box and connect it
+  (*s) << "new "<<typeName<<" "<<instanceName<<std::endl;
+  // connections
+  std::vector<std::string>::iterator j;
+  for (j=in.begin();j!=in.end();++j)
+    {
+      // connect
+      (*s) << "connect "<<instanceName<<"."<<*j<<" "
+          << boxname<<"."<<*j<<std::endl;
+    }
+  // That's all folks ! now execute the commands :
+  SwitchToStream(s);
+}
+//=======================================================================
+
 }//namespace