1 /*=========================================================================
4 Module: $RCSfile: bbtkExecuter.cxx,v $ $
6 Date: $Date: 2008/01/22 15:02:00 $
7 Version: $Revision: 1.1 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 * \brief class Executer: level 0 of script execution (code)
23 #include "bbtkExecuter.h"
24 #include "bbtkMessageManager.h"
25 #include "bbtkFactory.h"
26 #include "bbtkUtilities.h"
30 #ifdef _USE_WXWIDGETS_
31 #include <wx/textdlg.h>
34 #include "bbtkWxBlackBox.h"
36 #include "bbtkConfigurationFile.h"
51 bbtkDebugMessageInc("Core",9,"Executer::Executer()" <<std::endl);
53 bbtkDebugDecTab("Core",9);
61 bbtkDebugMessageInc("Core",9,"Executer::~Executer()" <<std::endl);
64 mPackage->UnRegisterBlackBox("workspace");
69 GetGlobalFactory()->UnLoadPackage("user");
71 bbtkDebugDecTab("Core",9);
78 void Executer::Reset()
80 bbtkDebugMessageInc("Core",9,"Executer::Reset()" <<std::endl);
82 // The 'user' package must be closed before all other
83 // because box destructors must not be unloaded when bb are deleted!
84 // Similarly, the 'workspace' CBB must be destroyed before
85 // all user defined CBB otherwise any instance
86 // of a user CBB that is in the 'workspace' would try to
87 // access a user CBB descriptor which has been previously freed
90 mPackage->UnRegisterBlackBox(mRoot->GetTypeName());
95 GetGlobalFactory()->UnLoadPackage("user");
97 GetGlobalFactory()->Reset();
98 // Create user package
99 mPackage = new Package("user","internal to bbi",
100 "User defined black boxes",
102 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
103 // Create user workspace
104 mRoot = new ComplexBlackBoxDescriptor("workspace"); //,f);
105 mRoot->AddToAuthor("bbi (internal)");
106 mRoot->AddToDescription("User's workspace");
107 mCurrent.push_back(CBBDefinition(mRoot,"user"));
108 // Register it into the user package
109 mPackage->RegisterBlackBox(mRoot);
110 // Insert the user package in the factory
111 InsertPackage(mPackage);
113 bbtkDebugDecTab("Core",9);
116 /// changes the workspace name
117 void Executer::SetWorkspaceName( const std::string& n )
119 mPackage->ChangeBlackBoxName( mRoot->GetTypeName(), n );
123 void Executer::Define (const std::string &name,
124 const std::string &pack,
125 const std::string &scriptfilename)
127 bbtkDebugMessageInc("Core",9,"Executer::Define(\""<<name<<
131 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
132 b->SetScriptFileName(scriptfilename);
133 mCurrent.push_back( CBBDefinition( b, pack ) );
135 bbtkDebugDecTab("Core",9);
138 void Executer::EndDefine ()
140 bbtkDebugMessageInc("Core",9,"Executer::EndDefine(\""
141 <<Current()->GetTypeName()<<"\")"
143 // Does current package exist ?
144 std::string pname(mCurrent.back().package);
148 p = GetGlobalFactory()->GetPackage(pname);
152 p = new Package(pname,
156 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
160 p->RegisterBlackBox(Current());
166 void Executer::Create ( const std::string& nodeType,
167 const std::string& nodeName)
169 Current()->Add(nodeType,nodeName);
174 void Executer::Remove (const std::string &nodeName)
176 // Current()->RemoveBlackBox(nodeName);
183 void Executer::Connect (const std::string &nodeFrom,
184 const std::string &outputLabel,
185 const std::string &nodeTo,
186 const std::string &inputLabel)
188 Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
194 void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
197 if (Current()==mRoot)
201 Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
206 Current()->AddToExecutionList(nodeName) ;
213 void Executer::DefineInput ( const std::string &name,
214 const std::string &box,
215 const std::string &input,
216 const std::string& help)
218 // If the input is defined in the Root box
219 if (Current()==mRoot)
221 // If the dialog mode is set to NoDialog
222 // and the user passed the name in the Inputs map
223 // then the associated value is set to the box.input
224 // This is the way command line parameters are passed to the Root box
225 if (mDialogMode == NoDialog)
227 // find if name is in mInputs
228 std::map<std::string,std::string>::iterator i;
229 i = mInputs.find(name);
230 if (i!=mInputs.end()) {
231 Set(box,input,(*i).second);
234 // If the dialog mode is set to TextDialog
235 // The user is prompted for the value
236 else if (mDialogMode == TextDialog)
238 std::cout << name << "=";
243 #ifdef _USE_WXWIDGETS_
244 // If the dialog mode is set to GraphicalDialog
245 // A dialog box is pop up
246 else if (mDialogMode == GraphicalDialog)
248 std::string mess("Enter the value of '");
253 std::string title(name);
256 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
262 Current()->DefineInput(name,box,input,help);
269 void Executer::DefineOutput ( const std::string &name,
270 const std::string &box,
271 const std::string &output,
272 const std::string& help)
274 Current()->DefineOutput(name,box,output,help);
280 void Executer::Set (const std::string &box,
281 const std::string &input,
282 const std::string &value)
284 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
285 // Looks for the adaptor
287 if ( b->bbGetInputType(input) != typeid(std::string) )
289 BlackBox* a = /*mFactory->*/
290 NewAdaptor(typeid(std::string),
291 b->bbGetInputType(input),
296 TypeName(b->bbGetInputType(input))
297 <<"> to <std::string> found");
299 std::string v(value);
300 a->bbSetInput("In",v);
302 b->bbSetInput(input,a->bbGetOutput("Out"));
307 std::string v(value);
308 b->bbSetInput(input,v);
316 std::string Executer::Get(const std::string &box,
317 const std::string &output)
319 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
320 // Looks for the adaptor
321 if (b->bbGetOutputType(output) != typeid(std::string))
323 BlackBox* a = /*mFactory->*/
325 b->bbGetOutputType(output),
331 TypeName(b->bbGetOutputType(output))
332 <<"> to <std::string> found");
336 a->bbSetInput("In",b->bbGetOutput(output));
338 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
339 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
340 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
342 //std::string v(value);
343 //b->bbSetInput(input,a->bbGetOutput("Out"));
350 return b->bbGetOutput(output).unsafe_get<std::string>();
351 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
352 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
354 // b->bbSetInput(input,&v);
359 void Executer::Author(const std::string &authorName)
361 Current()->AddToAuthor(authorName,Current()==mRoot);
366 void Executer::Description(const std::string &d)
368 Current()->AddToDescription(d,Current()==mRoot);
372 /// prints the list of the boxes of the current descriptor
373 void Executer::PrintBoxes()
375 bbtkMessageInc("Help",1,"The black box descriptor \""
376 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
377 Current()->PrintBlackBoxes();
378 bbtkDecTab("Help",1);
382 std::string Executer::ShowGraph(const std::string &nameblackbox,
383 const std::string &detailStr,
384 const std::string &levelStr,
385 const std::string &output_html,
386 const std::string &custom_header,
387 const std::string &custom_title,
388 bool system_display )
391 int detail = atoi(detailStr.c_str());
392 int level = atoi(levelStr.c_str());
394 std::string filename_rootHtml (output_html) ;
395 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
397 bool relative_link = true;
399 // No output provided : automatic generation
400 if (output_html.length() == 0)
402 // Don't pollute the file store with "doc_tmp" directories ...
403 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
405 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
407 std::string directory = default_doc_dir;
408 if (c != '/' && c !='\\') directory = directory + "/";
409 directory = directory + "doc_tmp";
411 filename_rootHtml = directory + "/" + "User.html";
412 simplefilename_rootHtml = "User.html" ;
414 // Creating directory
415 std::string command0("mkdir \"" +directory + "\"");
416 system( command0.c_str() );
418 relative_link = false;
421 // Generating documentation-help of workspace
422 mPackage->SetDocURL(filename_rootHtml);
423 mPackage->SetDocRelativeURL(simplefilename_rootHtml);
425 mPackage->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
427 std::string page = filename_rootHtml;
431 ShowGraphTypes(nameblackbox);
433 catch (bbtk::Exception a)
435 std::cout <<"EXC"<<std::endl;
436 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
442 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
443 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
447 BlackBox* blackbox=NULL;
448 if (nameblackbox==".")
450 blackbox=Current()->GetPrototype();
454 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
461 // Don't pollute the file store with "doc_tmp" directories ...
462 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
463 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
465 std::string directory = default_doc_dir;
466 if (c != '/' && c !='\\') directory = directory + "/";
468 directory = directory + "doc_tmp";
470 //std::string directory("doc_tmp");
471 std::string filename(directory + "/" + "bbtk_graph_pipeline");
472 std::string filename_html(filename+".html");
473 std::string command0("mkdir \""+directory + "\"");
476 std::string command2("start ");
478 std::string command2("gnome-open ");
481 command2=command2+filename_html;
482 page = filename_html;
483 // 1. Generate Html Diagram
485 s.open(filename_html.c_str());
488 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
489 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
490 if ( blackbox->bbGetName()=="workspacePrototype" )
492 s << "<center>Current workspace</center>";
494 s << "<center>" << blackbox->bbGetName()<< "</center>";
497 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
498 s << "</body></html>\n";
502 // 2. Starting Browser
503 if (system_display) system( command2.c_str() );
507 bbtkMessageInc("Help",1,"No black box: \""
508 <<nameblackbox<<"\" " <<std::endl);
515 void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
519 int detail = atoi(detailStr.c_str());
520 int level = atoi(levelStr.c_str());
521 BlackBox* blackbox=NULL;
522 if (nameblackbox.compare(".")==0)
524 blackbox=Current()->GetPrototype();
526 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
532 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
537 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
542 /// sets the level of message
543 void Executer::Message(const std::string &category,
544 const std::string& level)
547 sscanf(level.c_str(),"%d",&l);
548 bbtk::MessageManager::SetMessageLevel(category,l);