1 /*=========================================================================
4 Module: $RCSfile: bbtkExecuter.cxx,v $ $
6 Date: $Date: 2008/02/14 20:57:27 $
7 Version: $Revision: 1.12 $
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"
29 #ifdef _USE_WXWIDGETS_
30 #include <wx/textdlg.h>
33 #include "bbtkWxBlackBox.h"
35 #include "bbtkConfigurationFile.h"
50 bbtkDebugMessageInc("Kernel",9,"Executer::Executer()" <<std::endl);
52 bbtkDebugDecTab("Kernel",9);
60 std::cout << "====================================================== delete Executer\n";
61 bbtkDebugMessageInc("Kernel",9,"Executer::~Executer()" <<std::endl);
64 mPackage->UnRegisterBlackBox("workspace");
69 GetGlobalFactory()->UnLoadPackage("user");
71 bbtkDebugDecTab("Kernel",9);
78 void Executer::Reset()
80 bbtkDebugMessageInc("Kernel",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 mOpenDefinition.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);
112 mOpenPackage.push_back(mPackage);
113 bbtkDebugDecTab("Kernel",9);
116 /// changes the workspace name
117 void Executer::SetWorkspaceName( const std::string& n )
119 mPackage->ChangeBlackBoxName( mRoot->GetTypeName(), n );
122 void Executer::BeginPackage (const std::string &name)
124 bbtkDebugMessageInc("Kernel",9,"Executer::BeginPackage(\""<<name<<"\")"
129 p = GetGlobalFactory()->GetPackage(name);
133 p = new Package(name,
137 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
140 mOpenPackage.push_back(p);
143 void Executer::EndPackage()
145 if (mOpenPackage.size()>1) mOpenPackage.pop_back();
148 void Executer::Define (const std::string &name,
149 const std::string &pack,
150 const std::string &scriptfilename)
152 bbtkDebugMessageInc("Kernel",9,"Executer::Define(\""<<name<<
156 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
157 b->SetScriptFileName(scriptfilename);
158 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
160 bbtkDebugDecTab("Kernel",9);
163 /// Sets the file name to use for the current definition
164 /// (Used to set it after the Define command)
165 void Executer::SetCurrentFileName (const std::string &name )
167 mOpenDefinition.back().box->SetScriptFileName(name);
170 void Executer::EndDefine ()
172 bbtkDebugMessageInc("Kernel",9,"Executer::EndDefine(\""
173 <<Current()->GetTypeName()<<"\")"
175 // Does current package exist ?
177 std::string pname(mOpenDefinition.back().package);
182 p = GetGlobalFactory()->GetPackage(pname);
186 p = new Package(pname,
190 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
196 p = mOpenPackage.back();
198 p->RegisterBlackBox(Current());
200 mOpenDefinition.pop_back();
204 void Executer::Create ( const std::string& nodeType,
205 const std::string& nodeName)
207 Current()->Add(nodeType,nodeName);
211 void Executer::Remove (const std::string &nodeName)
213 // Current()->RemoveBlackBox(nodeName);
220 void Executer::Connect (const std::string &nodeFrom,
221 const std::string &outputLabel,
222 const std::string &nodeTo,
223 const std::string &inputLabel)
225 Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
231 void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
234 if (Current()==mRoot)
238 Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
243 Current()->AddToExecutionList(nodeName) ;
250 void Executer::DefineInput ( const std::string &name,
251 const std::string &box,
252 const std::string &input,
253 const std::string& help)
255 // If the input is defined in the Root box
256 if (Current()==mRoot)
258 // If the dialog mode is set to NoDialog
259 // and the user passed the name in the Inputs map
260 // then the associated value is set to the box.input
261 // This is the way command line parameters are passed to the Root box
262 if (mDialogMode == NoDialog)
264 // find if name is in mInputs
265 std::map<std::string,std::string>::iterator i;
266 i = mInputs.find(name);
267 if (i!=mInputs.end()) {
268 Set(box,input,(*i).second);
271 // If the dialog mode is set to TextDialog
272 // The user is prompted for the value
273 else if (mDialogMode == TextDialog)
275 std::cout << name << "=";
280 #ifdef _USE_WXWIDGETS_
281 // If the dialog mode is set to GraphicalDialog
282 // A dialog box is pop up
283 else if (mDialogMode == GraphicalDialog)
285 std::string mess("Enter the value of '");
290 std::string title(name);
292 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
298 Current()->DefineInput(name,box,input,help);
305 void Executer::DefineOutput ( const std::string &name,
306 const std::string &box,
307 const std::string &output,
308 const std::string& help)
310 Current()->DefineOutput(name,box,output,help);
316 void Executer::Set (const std::string &box,
317 const std::string &input,
318 const std::string &value)
320 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
321 // Looks for the adaptor
323 if ( b->bbGetInputType(input) != typeid(std::string) )
326 NewAdaptor(typeid(std::string),
327 b->bbGetInputType(input),
332 TypeName(b->bbGetInputType(input))
333 <<"> to <std::string> found");
335 std::string v(value);
336 a->bbSetInput("In",v);
338 b->bbSetInput(input,a->bbGetOutput("Out"));
343 std::string v(value);
344 b->bbSetInput(input,v);
351 std::string Executer::Get(const std::string &box,
352 const std::string &output)
354 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
355 // Looks for the adaptor
356 if (b->bbGetOutputType(output) != typeid(std::string))
360 b->bbGetOutputType(output),
366 TypeName(b->bbGetOutputType(output))
367 <<"> to <std::string> found");
371 a->bbSetInput("In",b->bbGetOutput(output));
373 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
374 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
375 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
377 //std::string v(value);
378 //b->bbSetInput(input,a->bbGetOutput("Out"));
385 return b->bbGetOutput(output).unsafe_get<std::string>();
386 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
387 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
389 // b->bbSetInput(input,&v);
394 void Executer::Author(const std::string &authorName)
396 Current()->AddToAuthor(authorName,Current()==mRoot);
399 void Executer::Category(const std::string &category)
401 Current()->AddToCategory(category,Current()==mRoot);
404 void Executer::Description(const std::string &d)
406 Current()->AddToDescription(d,Current()==mRoot);
410 /// prints the list of the boxes of the current descriptor
411 void Executer::PrintBoxes()
413 bbtkMessageInc("Help",1,"The black box descriptor \""
414 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
415 Current()->PrintBlackBoxes();
416 bbtkDecTab("Help",1);
419 std::string Executer::ShowGraph(const std::string &nameblackbox,
420 const std::string &detailStr,
421 const std::string &levelStr,
422 const std::string &output_html,
423 const std::string &custom_header,
424 const std::string &custom_title,
425 bool system_display )
427 int detail = atoi(detailStr.c_str());
428 int level = atoi(levelStr.c_str());
430 std::string filename_rootHtml (output_html) ;
431 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
433 bool relative_link = true;
435 // No output provided : automatic generation
436 if (output_html.length() == 0)
438 // Don't pollute the file store with "temp_dir" directories ...
439 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
441 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
443 std::string directory = default_doc_dir;
444 if (c != '/' && c !='\\') directory = directory + "/";
445 directory = directory + "temp_dir";
447 filename_rootHtml = directory + "/" + "User.html";
448 simplefilename_rootHtml = "User.html" ;
450 // Creating directory
451 std::string command0("mkdir \"" +directory + "\"");
452 system( command0.c_str() );
454 relative_link = false;
460 p = GetGlobalFactory()->GetPackage(nameblackbox);
466 // Generating documentation-help of workspace
467 p->SetDocURL(filename_rootHtml);
468 p->SetDocRelativeURL(simplefilename_rootHtml);
470 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
472 std::string page = filename_rootHtml;
476 ShowGraphTypes(nameblackbox);
478 catch (bbtk::Exception a)
480 std::cout <<"EXC"<<std::endl;
481 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
487 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
488 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
492 BlackBox* blackbox=NULL;
493 if (nameblackbox==".")
495 blackbox=Current()->GetPrototype();
499 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
506 // Don't pollute the file store with "temp_dir" directories ...
507 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
508 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
510 std::string directory = default_doc_dir;
511 if (c != '/' && c !='\\') directory = directory + "/";
513 directory = directory + "temp_dir";
514 //std::string directory("temp_dir");
515 std::string filename(directory + "/" + "bbtk_graph_pipeline");
516 std::string filename_html(filename+".html");
517 std::string command0("mkdir \""+directory + "\"");
520 std::string command2("start ");
522 std::string command2("gnome-open ");
525 command2=command2+filename_html;
526 page = filename_html;
527 // 1. Generate Html Diagram
529 s.open(filename_html.c_str());
532 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
533 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
534 if ( blackbox->bbGetName()=="workspacePrototype" )
536 s << "<center>Current workspace</center>";
538 s << "<center>" << blackbox->bbGetName()<< "</center>";
541 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
542 s << "</body></html>\n";
546 // 2. Starting Browser
547 if (system_display) system( command2.c_str() );
551 bbtkMessageInc("Help",1,"No black box: \""
552 <<nameblackbox<<"\" " <<std::endl);
557 void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
561 int detail = atoi(detailStr.c_str());
562 int level = atoi(levelStr.c_str());
563 BlackBox* blackbox=NULL;
564 if (nameblackbox.compare(".")==0)
566 blackbox=Current()->GetPrototype();
568 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
574 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
579 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
584 /// sets the level of message
585 void Executer::Message(const std::string &kind,
586 const std::string& level)
589 sscanf(level.c_str(),"%d",&l);
590 bbtk::MessageManager::SetMessageLevel(kind,l);