1 /*=========================================================================
4 Module: $RCSfile: bbtkExecuter.cxx,v $ $
6 Date: $Date: 2008/01/28 15:34:37 $
7 Version: $Revision: 1.3 $
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 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("Core",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("Core",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("Core",9,"Executer::Define(\""<<name<<
156 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
157 b->SetScriptFileName(scriptfilename);
158 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
160 bbtkDebugDecTab("Core",9);
163 void Executer::EndDefine ()
165 bbtkDebugMessageInc("Core",9,"Executer::EndDefine(\""
166 <<Current()->GetTypeName()<<"\")"
168 // Does current package exist ?
171 std::string pname(mOpenDefinition.back().package);
176 p = GetGlobalFactory()->GetPackage(pname);
180 p = new Package(pname,
184 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
190 p = mOpenPackage.back();
192 p->RegisterBlackBox(Current());
194 mOpenDefinition.pop_back();
198 void Executer::Create ( const std::string& nodeType,
199 const std::string& nodeName)
201 Current()->Add(nodeType,nodeName);
206 void Executer::Remove (const std::string &nodeName)
208 // Current()->RemoveBlackBox(nodeName);
215 void Executer::Connect (const std::string &nodeFrom,
216 const std::string &outputLabel,
217 const std::string &nodeTo,
218 const std::string &inputLabel)
220 Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
226 void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
229 if (Current()==mRoot)
233 Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
238 Current()->AddToExecutionList(nodeName) ;
245 void Executer::DefineInput ( const std::string &name,
246 const std::string &box,
247 const std::string &input,
248 const std::string& help)
250 // If the input is defined in the Root box
251 if (Current()==mRoot)
253 // If the dialog mode is set to NoDialog
254 // and the user passed the name in the Inputs map
255 // then the associated value is set to the box.input
256 // This is the way command line parameters are passed to the Root box
257 if (mDialogMode == NoDialog)
259 // find if name is in mInputs
260 std::map<std::string,std::string>::iterator i;
261 i = mInputs.find(name);
262 if (i!=mInputs.end()) {
263 Set(box,input,(*i).second);
266 // If the dialog mode is set to TextDialog
267 // The user is prompted for the value
268 else if (mDialogMode == TextDialog)
270 std::cout << name << "=";
275 #ifdef _USE_WXWIDGETS_
276 // If the dialog mode is set to GraphicalDialog
277 // A dialog box is pop up
278 else if (mDialogMode == GraphicalDialog)
280 std::string mess("Enter the value of '");
285 std::string title(name);
288 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
294 Current()->DefineInput(name,box,input,help);
301 void Executer::DefineOutput ( const std::string &name,
302 const std::string &box,
303 const std::string &output,
304 const std::string& help)
306 Current()->DefineOutput(name,box,output,help);
312 void Executer::Set (const std::string &box,
313 const std::string &input,
314 const std::string &value)
316 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
317 // Looks for the adaptor
319 if ( b->bbGetInputType(input) != typeid(std::string) )
321 BlackBox* a = /*mFactory->*/
322 NewAdaptor(typeid(std::string),
323 b->bbGetInputType(input),
328 TypeName(b->bbGetInputType(input))
329 <<"> to <std::string> found");
331 std::string v(value);
332 a->bbSetInput("In",v);
334 b->bbSetInput(input,a->bbGetOutput("Out"));
339 std::string v(value);
340 b->bbSetInput(input,v);
348 std::string Executer::Get(const std::string &box,
349 const std::string &output)
351 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
352 // Looks for the adaptor
353 if (b->bbGetOutputType(output) != typeid(std::string))
355 BlackBox* a = /*mFactory->*/
357 b->bbGetOutputType(output),
363 TypeName(b->bbGetOutputType(output))
364 <<"> to <std::string> found");
368 a->bbSetInput("In",b->bbGetOutput(output));
370 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
371 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
372 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
374 //std::string v(value);
375 //b->bbSetInput(input,a->bbGetOutput("Out"));
382 return b->bbGetOutput(output).unsafe_get<std::string>();
383 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
384 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
386 // b->bbSetInput(input,&v);
391 void Executer::Author(const std::string &authorName)
393 Current()->AddToAuthor(authorName,Current()==mRoot);
398 void Executer::Description(const std::string &d)
400 Current()->AddToDescription(d,Current()==mRoot);
404 /// prints the list of the boxes of the current descriptor
405 void Executer::PrintBoxes()
407 bbtkMessageInc("Help",1,"The black box descriptor \""
408 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
409 Current()->PrintBlackBoxes();
410 bbtkDecTab("Help",1);
414 std::string Executer::ShowGraph(const std::string &nameblackbox,
415 const std::string &detailStr,
416 const std::string &levelStr,
417 const std::string &output_html,
418 const std::string &custom_header,
419 const std::string &custom_title,
420 bool system_display )
423 int detail = atoi(detailStr.c_str());
424 int level = atoi(levelStr.c_str());
426 std::string filename_rootHtml (output_html) ;
427 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
429 bool relative_link = true;
431 // No output provided : automatic generation
432 if (output_html.length() == 0)
434 // Don't pollute the file store with "doc_tmp" directories ...
435 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
437 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
439 std::string directory = default_doc_dir;
440 if (c != '/' && c !='\\') directory = directory + "/";
441 directory = directory + "doc_tmp";
443 filename_rootHtml = directory + "/" + "User.html";
444 simplefilename_rootHtml = "User.html" ;
446 // Creating directory
447 std::string command0("mkdir \"" +directory + "\"");
448 system( command0.c_str() );
450 relative_link = false;
457 p = GetGlobalFactory()->GetPackage(nameblackbox);
463 // Generating documentation-help of workspace
464 p->SetDocURL(filename_rootHtml);
465 p->SetDocRelativeURL(simplefilename_rootHtml);
467 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
469 std::string page = filename_rootHtml;
473 ShowGraphTypes(nameblackbox);
475 catch (bbtk::Exception a)
477 std::cout <<"EXC"<<std::endl;
478 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
484 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
485 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
489 BlackBox* blackbox=NULL;
490 if (nameblackbox==".")
492 blackbox=Current()->GetPrototype();
496 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
503 // Don't pollute the file store with "doc_tmp" directories ...
504 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
505 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
507 std::string directory = default_doc_dir;
508 if (c != '/' && c !='\\') directory = directory + "/";
510 directory = directory + "doc_tmp";
512 //std::string directory("doc_tmp");
513 std::string filename(directory + "/" + "bbtk_graph_pipeline");
514 std::string filename_html(filename+".html");
515 std::string command0("mkdir \""+directory + "\"");
518 std::string command2("start ");
520 std::string command2("gnome-open ");
523 command2=command2+filename_html;
524 page = filename_html;
525 // 1. Generate Html Diagram
527 s.open(filename_html.c_str());
530 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
531 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
532 if ( blackbox->bbGetName()=="workspacePrototype" )
534 s << "<center>Current workspace</center>";
536 s << "<center>" << blackbox->bbGetName()<< "</center>";
539 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
540 s << "</body></html>\n";
544 // 2. Starting Browser
545 if (system_display) system( command2.c_str() );
549 bbtkMessageInc("Help",1,"No black box: \""
550 <<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 &category,
586 const std::string& level)
589 sscanf(level.c_str(),"%d",&l);
590 bbtk::MessageManager::SetMessageLevel(category,l);