1 /*=========================================================================
4 Module: $RCSfile: bbtkExecuter.cxx,v $ $
6 Date: $Date: 2008/02/06 10:53:02 $
7 Version: $Revision: 1.9 $
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 bbtkDebugMessageInc("Kernel",9,"Executer::~Executer()" <<std::endl);
63 mPackage->UnRegisterBlackBox("workspace");
68 GetGlobalFactory()->UnLoadPackage("user");
70 bbtkDebugDecTab("Kernel",9);
77 void Executer::Reset()
79 bbtkDebugMessageInc("Kernel",9,"Executer::Reset()" <<std::endl);
81 // The 'user' package must be closed before all other
82 // because box destructors must not be unloaded when bb are deleted!
83 // Similarly, the 'workspace' CBB must be destroyed before
84 // all user defined CBB otherwise any instance
85 // of a user CBB that is in the 'workspace' would try to
86 // access a user CBB descriptor which has been previously freed
89 mPackage->UnRegisterBlackBox(mRoot->GetTypeName());
94 GetGlobalFactory()->UnLoadPackage("user");
96 GetGlobalFactory()->Reset();
97 // Create user package
98 mPackage = new Package("user","internal to bbi",
99 "User defined black boxes",
101 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
102 // Create user workspace
103 mRoot = new ComplexBlackBoxDescriptor("workspace"); //,f);
104 mRoot->AddToAuthor("bbi (internal)");
105 mRoot->AddToDescription("User's workspace");
106 mOpenDefinition.push_back(CBBDefinition(mRoot,"user"));
107 // Register it into the user package
108 mPackage->RegisterBlackBox(mRoot);
109 // Insert the user package in the factory
110 InsertPackage(mPackage);
111 mOpenPackage.push_back(mPackage);
112 bbtkDebugDecTab("Kernel",9);
115 /// changes the workspace name
116 void Executer::SetWorkspaceName( const std::string& n )
118 mPackage->ChangeBlackBoxName( mRoot->GetTypeName(), n );
121 void Executer::BeginPackage (const std::string &name)
123 bbtkDebugMessageInc("Kernel",9,"Executer::BeginPackage(\""<<name<<"\")"
128 p = GetGlobalFactory()->GetPackage(name);
132 p = new Package(name,
136 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
139 mOpenPackage.push_back(p);
142 void Executer::EndPackage()
144 if (mOpenPackage.size()>1) mOpenPackage.pop_back();
147 void Executer::Define (const std::string &name,
148 const std::string &pack,
149 const std::string &scriptfilename)
151 bbtkDebugMessageInc("Kernel",9,"Executer::Define(\""<<name<<
155 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
156 b->SetScriptFileName(scriptfilename);
157 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
159 bbtkDebugDecTab("Kernel",9);
162 /// Sets the file name to use for the current definition
163 /// (Used to set it after the Define command)
164 void Executer::SetCurrentFileName (const std::string &name )
166 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);
212 void Executer::Remove (const std::string &nodeName)
214 // Current()->RemoveBlackBox(nodeName);
221 void Executer::Connect (const std::string &nodeFrom,
222 const std::string &outputLabel,
223 const std::string &nodeTo,
224 const std::string &inputLabel)
226 Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
232 void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
235 if (Current()==mRoot)
239 Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
244 Current()->AddToExecutionList(nodeName) ;
251 void Executer::DefineInput ( const std::string &name,
252 const std::string &box,
253 const std::string &input,
254 const std::string& help)
256 // If the input is defined in the Root box
257 if (Current()==mRoot)
259 // If the dialog mode is set to NoDialog
260 // and the user passed the name in the Inputs map
261 // then the associated value is set to the box.input
262 // This is the way command line parameters are passed to the Root box
263 if (mDialogMode == NoDialog)
265 // find if name is in mInputs
266 std::map<std::string,std::string>::iterator i;
267 i = mInputs.find(name);
268 if (i!=mInputs.end()) {
269 Set(box,input,(*i).second);
272 // If the dialog mode is set to TextDialog
273 // The user is prompted for the value
274 else if (mDialogMode == TextDialog)
276 std::cout << name << "=";
281 #ifdef _USE_WXWIDGETS_
282 // If the dialog mode is set to GraphicalDialog
283 // A dialog box is pop up
284 else if (mDialogMode == GraphicalDialog)
286 std::string mess("Enter the value of '");
291 std::string title(name);
294 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
300 Current()->DefineInput(name,box,input,help);
307 void Executer::DefineOutput ( const std::string &name,
308 const std::string &box,
309 const std::string &output,
310 const std::string& help)
312 Current()->DefineOutput(name,box,output,help);
318 void Executer::Set (const std::string &box,
319 const std::string &input,
320 const std::string &value)
322 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
323 // Looks for the adaptor
325 if ( b->bbGetInputType(input) != typeid(std::string) )
327 BlackBox* a = /*mFactory->*/
328 NewAdaptor(typeid(std::string),
329 b->bbGetInputType(input),
334 TypeName(b->bbGetInputType(input))
335 <<"> to <std::string> found");
337 std::string v(value);
338 a->bbSetInput("In",v);
340 b->bbSetInput(input,a->bbGetOutput("Out"));
345 std::string v(value);
346 b->bbSetInput(input,v);
353 std::string Executer::Get(const std::string &box,
354 const std::string &output)
356 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
357 // Looks for the adaptor
358 if (b->bbGetOutputType(output) != typeid(std::string))
360 BlackBox* a = /*mFactory->*/
362 b->bbGetOutputType(output),
368 TypeName(b->bbGetOutputType(output))
369 <<"> to <std::string> found");
373 a->bbSetInput("In",b->bbGetOutput(output));
375 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
376 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
377 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
379 //std::string v(value);
380 //b->bbSetInput(input,a->bbGetOutput("Out"));
387 return b->bbGetOutput(output).unsafe_get<std::string>();
388 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
389 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
391 // b->bbSetInput(input,&v);
396 void Executer::Author(const std::string &authorName)
398 Current()->AddToAuthor(authorName,Current()==mRoot);
401 void Executer::Category(const std::string &category)
403 Current()->AddToCategory(category,Current()==mRoot);
406 void Executer::Description(const std::string &d)
408 Current()->AddToDescription(d,Current()==mRoot);
412 /// prints the list of the boxes of the current descriptor
413 void Executer::PrintBoxes()
415 bbtkMessageInc("Help",1,"The black box descriptor \""
416 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
417 Current()->PrintBlackBoxes();
418 bbtkDecTab("Help",1);
421 std::string Executer::ShowGraph(const std::string &nameblackbox,
422 const std::string &detailStr,
423 const std::string &levelStr,
424 const std::string &output_html,
425 const std::string &custom_header,
426 const std::string &custom_title,
427 bool system_display )
429 int detail = atoi(detailStr.c_str());
430 int level = atoi(levelStr.c_str());
432 std::string filename_rootHtml (output_html) ;
433 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
435 bool relative_link = true;
437 // No output provided : automatic generation
438 if (output_html.length() == 0)
440 // Don't pollute the file store with "temp_dir" directories ...
441 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
443 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
445 std::string directory = default_doc_dir;
446 if (c != '/' && c !='\\') directory = directory + "/";
447 directory = directory + "temp_dir";
449 filename_rootHtml = directory + "/" + "User.html";
450 simplefilename_rootHtml = "User.html" ;
452 // Creating directory
453 std::string command0("mkdir \"" +directory + "\"");
454 system( command0.c_str() );
456 relative_link = false;
463 p = GetGlobalFactory()->GetPackage(nameblackbox);
469 // Generating documentation-help of workspace
470 p->SetDocURL(filename_rootHtml);
471 p->SetDocRelativeURL(simplefilename_rootHtml);
473 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
475 std::string page = filename_rootHtml;
479 ShowGraphTypes(nameblackbox);
481 catch (bbtk::Exception a)
483 std::cout <<"EXC"<<std::endl;
484 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
490 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
491 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
495 BlackBox* blackbox=NULL;
496 if (nameblackbox==".")
498 blackbox=Current()->GetPrototype();
502 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
509 // Don't pollute the file store with "temp_dir" directories ...
510 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
511 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
513 std::string directory = default_doc_dir;
514 if (c != '/' && c !='\\') directory = directory + "/";
516 directory = directory + "temp_dir";
518 //std::string directory("temp_dir");
519 std::string filename(directory + "/" + "bbtk_graph_pipeline");
520 std::string filename_html(filename+".html");
521 std::string command0("mkdir \""+directory + "\"");
524 std::string command2("start ");
526 std::string command2("gnome-open ");
529 command2=command2+filename_html;
530 page = filename_html;
531 // 1. Generate Html Diagram
533 s.open(filename_html.c_str());
536 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
537 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
538 if ( blackbox->bbGetName()=="workspacePrototype" )
540 s << "<center>Current workspace</center>";
542 s << "<center>" << blackbox->bbGetName()<< "</center>";
545 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
546 s << "</body></html>\n";
550 // 2. Starting Browser
551 if (system_display) system( command2.c_str() );
555 bbtkMessageInc("Help",1,"No black box: \""
556 <<nameblackbox<<"\" " <<std::endl);
562 void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
566 int detail = atoi(detailStr.c_str());
567 int level = atoi(levelStr.c_str());
568 BlackBox* blackbox=NULL;
569 if (nameblackbox.compare(".")==0)
571 blackbox=Current()->GetPrototype();
573 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
579 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
584 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
589 /// sets the level of message
590 void Executer::Message(const std::string &kind,
591 const std::string& level)
594 sscanf(level.c_str(),"%d",&l);
595 bbtk::MessageManager::SetMessageLevel(kind,l);